This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Part Number: MSP432P401R
Tool/software: TI C/C++ Compiler
I created a program where I set up the UART with MSP432, however I can not make the UART work well, can someone help me to configure the UART. Or show me an example where it works with interrupts and no interrupts
uart.c
/*UART CONFIGURATION*/ void uart_init(void){ /*Configure UART*/ EUSCI_A2->CTLW0 |= EUSCI_A_CTLW0_SWRST; /*PUT EUSCI IN RESET*/ EUSCI_A2->CTLW0 = EUSCI_A_CTLW0_SWRST | /*REMAIN EUSCI IN RESET*/ EUSCI_B_CTLW0_SSEL__SMCLK; /*CONFIGURE EUSCI CLOCK SMCLK*/ /*Baud Rate calculation *12000000/(16*9600) = 78.125 *Fractional portion = 0.125 *User's Guide Table 21-4: UCBRSx = 0x10 *UCBRFx = int ( (78.125-78)*16) = 2 */ EUSCI_A2->BRW = 78; /*12000000/16/9600*/ EUSCI_A2->MCTLW = (2 << EUSCI_A_MCTLW_BRF_OFS) | EUSCI_A_MCTLW_OS16; EUSCI_A2->CTLW0 &= ~(EUSCI_A_CTLW0_SWRST); /*INITIALIZE EUSCI*/ EUSCI_A2->IFG &= ~(EUSCI_A_IFG_RXIFG); EUSCI_A2->IE |= EUSCI_A_IE_RXIE; /*ENABLE THE UART RX INTERRUPT*/ /*ENABLE GLOBAL INTERRUPT*/ __enable_irq(); /*ENABLE NVIC IN EUSCI_A2 INTERRUPT*/ NVIC->ISER[0] = 1 << ((EUSCIA2_IRQn) & 31); } /*UART PORTS*/ void confg_uart_wired(void){ P3->SEL0 |= UART_TX | UART_RX; /*SET 2 UART PIN AS SECOND FUNCTION*/ } /*UART CLOCK SYSTEM*/ void uart_cs(void){ CS->KEY = CS_KEY_VAL; /*UNLOCK CS MODULE REGISTER ACESS*/ CS->CTL0 = 0; /*RESET TUNING PARAMETERS*/ CS->CTL0 = CS_CTL0_DCORSEL_3; /*SET DCO TO 12MHz (NOMINAL, CENTER OF 8-16MHz RANGE)*/ CS->CTL1 = CS_CTL1_SELA_2 | /*SELECT ACLK = REFO*/ CS_CTL1_SELS_3 | /*SMCLK = DCO*/ CS_CTL1_SELM_3; /*MCLK = DCO*/ CS->KEY = 0; } void EUSCIA2_IRQHandler(void){ if(EUSCI_A2->IFG & EUSCI_A_IFG_RXIFG){ LED_PyOUT(G_LED); } if(EUSCI_A2->IFG & EUSCI_A_IFG_TXIFG){ LED_PyOUT(R_LED); /*LIGHT UP P2.x LED ON RX*/ } }
main.c
void main(void){ /*CLEAR ALL LED RGB*/ LED_PxCLR(R_LED|G_LED|B_LED); LED_PyDIR(R_LED|G_LED|B_LED); WatchDogHold(); uart_cs(); confg_uart_wired(); uart_init(); while(1){ } } void WatchDogHold(void){ WDTCTL = WDTPW | WDTHOLD; /*STOP WATCHDOG TIMER*/ }
Bruce McKenney first of all Thank you for your help.
I am trying to mount a program that sends data (text) through the UART to a bluetooth module. R_LED and G_LED are assigned to the MSP432 card, and are only used for debugging. Verify that this transmit or receive, however I did not understand the operation of RXIFG and TXIFG.if I have understood well the part of the interruption can be done as follow:
void EUSCIA2_IRQHandler(void){ if(EUSCI_A2->IFG & EUSCI_A_IFG_RXIFG){ LED_PyOUT(G_LED); /*LIGHT UP P2.1 LED ON RX*/ EUSCI_A2->IFG &= ~(EUSCI_A_IFG_RXIFG); } if(EUSCI_A2->IFG & EUSCI_A_IFG_TXIFG){ LED_PyOUT(R_LED); /*LIGHT UP P2.0 LED ON TX*/ while(!(EUSCI_A2->IFG & EUSCI_A_IFG_TXIFG)); } }
This may be the doubt of many other users, after resolving this problem I will make available the resources to be able to help everyone who has the same problem.
ok, I made some modifications and some implementations based on what you told me
uart.c
char RX_Buffer[]; int BUFFER_COUNT = 0; /*UART CONFIGURATION*/ void uart_init(void){ /*Configure UART*/ EUSCI_A2->CTLW0 |= EUSCI_A_CTLW0_SWRST; /*PUT EUSCI IN RESET*/ EUSCI_A2->CTLW0 = EUSCI_A_CTLW0_SWRST | /*REMAIN EUSCI IN RESET*/ EUSCI_B_CTLW0_SSEL__SMCLK; /*CONFIGURE EUSCI CLOCK SMCLK*/ /*Baud Rate calculation *12000000/(16*9600) = 78.125 *Fractional portion = 0.125 *User's Guide Table 21-4: UCBRSx = 0x10 *UCBRFx = int ( (78.125-78)*16) = 2 */ EUSCI_A2->BRW = 78; /*12000000/16/9600*/ EUSCI_A2->MCTLW = (2 << EUSCI_A_MCTLW_BRF_OFS) | EUSCI_A_MCTLW_OS16; EUSCI_A2->CTLW0 &= ~(EUSCI_A_CTLW0_SWRST); /*INITIALIZE EUSCI*/ EUSCI_A2->IFG &= ~(EUSCI_A_IFG_RXIFG); EUSCI_A2->IE |= EUSCI_A_IE_RXIE; /*ENABLE THE UART RX INTERRUPT*/ /*ENABLE GLOBAL INTERRUPT*/ __enable_irq(); /*ENABLE NVIC IN EUSCI_A2 INTERRUPT*/ NVIC->ISER[0] = 1 << ((EUSCIA2_IRQn) & 31); } /*UART PORTS*/ void confg_uart_wired(void){ P3->SEL0 |= UART_TX | UART_RX; /*SET 2 UART PIN AS SECOND FUNCTION*/ } /*UART CLOCK SYSTEM*/ void uart_cs(void){ CS->KEY = CS_KEY_VAL; /*UNLOCK CS MODULE REGISTER ACESS*/ CS->CTL0 = 0; /*RESET TUNING PARAMETERS*/ CS->CTL0 = CS_CTL0_DCORSEL_3; /*SET DCO TO 12MHz (NOMINAL, CENTER OF 8-16MHz RANGE)*/ CS->CTL1 = CS_CTL1_SELA_2 | /*SELECT ACLK = REFO*/ CS_CTL1_SELS_3 | /*SMCLK = DCO*/ CS_CTL1_SELM_3; /*MCLK = DCO*/ CS->KEY = 0; } /*DELAY MILLISECONDS WHEN SYSTEM CLOCK IS AT 3MHz*/ void delayMs(int n){ int i, j; for(j= 0; j < n;j++); for(i= 750;i>0;i--); } /*UART COUNT CARACTERS SENT*/ void uart_c_String(char *c){ int i; for(i=0; c[i];i++){ BUFFER_COUNT++; } uart_send(c, BUFFER_COUNT); } /*UART SEND MULTIPLE BYTES*/ void uart_send(unsigned char *pucData, unsigned char ucLength){ while(ucLength){ /*WAIT FOR TX BUFFER TO BE READY FOR NEW DATA*/ while(!(EUSCI_A2->IFG & EUSCI_A_IFG_TXIFG)); /*CHECK IF NOT SET*/ /*IF SET, TX INTERRUPT IS PENDING*/ EUSCI_A2->TXBUF = *pucData; /*PUSH DATA TO TX BUFFER*/ ucLength--; /*LENGTH OF DATA LEFT*/ pucData++; /*SHIFT POINTER*/ } /*WAIT UNTIL THE LAST BYTE IS COMPLETELY SENT*/ while(EUSCI_A2->STATW & EUSCI_A_STATW_BUSY); /*THIS INDICATE IF EUSCI IS SEND OR RECEIVE*/ } /*UART GET MULTIPLE BYTES*/ void uart_get(void){ RX_Buffer[BUFFER_COUNT] = EUSCI_A2->RXBUF; if(++BUFFER_COUNT >= 2){ BUFFER_COUNT = 0; EUSCI_A2->IFG &= ~(EUSCI_A_IFG_RXIFG); /*RESET RX flag*/ } } void EUSCIA2_IRQHandler(void){ if(EUSCI_A2->IFG & EUSCI_A_IFG_RXIFG){ uart_get(); /*LIGHT UP P2.1 LED ON RX*/ LED_PyOUT(G_LED); } if(EUSCI_A2->IFG & EUSCI_A_IFG_TXIFG){ EUSCI_A2->IFG &= ~(EUSCI_A_IFG_TXIFG); LED_PyOUT(R_LED); /*LIGHT UP P2.0 LED ON RX*/ } }
main.c
void main(void){ /*CLEAR ALL LED RGB*/ LED_PxCLR(R_LED|G_LED|B_LED); LED_PyDIR(R_LED|G_LED|B_LED); WatchDogHold(); uart_cs(); confg_uart_wired(); uart_init(); uart_c_String("Hello World!"); while(1){ } } void WatchDogHold(void){ WDTCTL = WDTPW | WDTHOLD; /*STOP WATCHDOG TIMER*/ }
Ok Bruce,
I already done that you have you told me, but I have declare char RX_Buffer[], because in main a call for a function "uart_c_String()" where i pass the entire text I want sent, so this function count the characters and pass them to "uart_send()" function where dynamically, refill the buffer. Is this a bad practice?
uart.c
#include "msp.h" #include "uart.h" #include "main.h" #include <stdint.h> #include <stdio.h> char RX_Buffer[2]; int BUFFER_COUNT = 0; /*UART CONFIGURATION*/ void uart_init(void){ /*Configure UART*/ EUSCI_A2->CTLW0 |= EUSCI_A_CTLW0_SWRST; /*PUT EUSCI IN RESET*/ EUSCI_A2->CTLW0 = EUSCI_A_CTLW0_SWRST | /*REMAIN EUSCI IN RESET*/ EUSCI_B_CTLW0_SSEL__SMCLK; /*CONFIGURE EUSCI CLOCK SMCLK*/ /*Baud Rate calculation *12000000/(16*9600) = 78.125 *Fractional portion = 0.125 *User's Guide Table 21-4: UCBRSx = 0x10 *UCBRFx = int ( (78.125-78)*16) = 2 */ EUSCI_A2->BRW = 78; /*12000000/16/9600*/ EUSCI_A2->MCTLW = (2 << EUSCI_A_MCTLW_BRF_OFS) | EUSCI_A_MCTLW_OS16; EUSCI_A2->CTLW0 &= ~(EUSCI_A_CTLW0_SWRST); /*INITIALIZE EUSCI*/ EUSCI_A2->IFG &= ~(EUSCI_A_IFG_RXIFG); EUSCI_A2->IE |= EUSCI_A_IE_RXIE; /*ENABLE THE UART RX INTERRUPT*/ /*ENABLE GLOBAL INTERRUPT*/ __enable_irq(); /*ENABLE NVIC IN EUSCI_A2 INTERRUPT*/ NVIC->ISER[0] = 1 << ((EUSCIA2_IRQn) & 31); } /*UART PORTS*/ void confg_uart_wired(void){ P3->SEL0 |= UART_TX | UART_RX; /*SET 2 UART PIN AS SECOND FUNCTION*/ } /*UART CLOCK SYSTEM*/ void uart_cs(void){ CS->KEY = CS_KEY_VAL; /*UNLOCK CS MODULE REGISTER ACESS*/ CS->CTL0 = 0; /*RESET TUNING PARAMETERS*/ CS->CTL0 = CS_CTL0_DCORSEL_3; /*SET DCO TO 12MHz (NOMINAL, CENTER OF 8-16MHz RANGE)*/ CS->CTL1 = CS_CTL1_SELA_2 | /*SELECT ACLK = REFO*/ CS_CTL1_SELS_3 | /*SMCLK = DCO*/ CS_CTL1_SELM_3; /*MCLK = DCO*/ CS->KEY = 0; } /*DELAY MILLISECONDS WHEN SYSTEM CLOCK IS AT 3MHz*/ void delayMs(int n){ int i, j; for(j= 0; j < n;j++); for(i= 750;i>0;i--); } /*UART COUNT CARACTERS SENT*/ void uart_c_String(char *c){ int i; for(i=0; c[i];i++){ BUFFER_COUNT++; } uart_send(c, BUFFER_COUNT); } /*UART SEND MULTIPLE BYTES*/ void uart_send(unsigned char *pucData, unsigned char ucLength){ while(ucLength){ /*WAIT FOR TX BUFFER TO BE READY FOR NEW DATA*/ while(!(EUSCI_A2->IFG & EUSCI_A_IFG_TXIFG)); /*CHECK IF NOT SET*/ /*IF SET, TX INTERRUPT IS PENDING*/ EUSCI_A2->TXBUF = *pucData; /*PUSH DATA TO TX BUFFER*/ ucLength--; /*LENGTH OF DATA LEFT*/ pucData++; /*SHIFT POINTER*/ } /*WAIT UNTIL THE LAST BYTE IS COMPLETELY SENT*/ while(EUSCI_A2->STATW & EUSCI_A_STATW_BUSY); /*THIS INDICATE IF EUSCI IS SEND OR RECEIVE*/ } /*UART GET MULTIPLE BYTES*/ void uart_get(void){ RX_Buffer[BUFFER_COUNT] = EUSCI_A2->RXBUF; if(++BUFFER_COUNT >= 2){ BUFFER_COUNT = 0; } } void EUSCIA2_IRQHandler(void){ if(EUSCI_A2->IFG & EUSCI_A_IFG_RXIFG){ LED_PyOUT(G_LED); uart_get(); } }
Bruce,
I have done some alterations and the characters sent by the serial are the same as those that are counted in the bluetooth terminal
uart.c
char input[100]; unsigned int RXByteCtr = 0; /*UART CONFIGURATION*/ void uart_init(void){ /*Configure UART*/ EUSCI_A2->CTLW0 |= EUSCI_A_CTLW0_SWRST; /*PUT EUSCI IN RESET*/ EUSCI_A2->CTLW0 = EUSCI_A_CTLW0_SWRST | /*REMAIN EUSCI IN RESET*/ EUSCI_A_CTLW0_MODE_0| EUSCI_A_CTLW0_SSEL__SMCLK; /*CONFIGURE EUSCI CLOCK SMCLK*/ /*Baud Rate calculation *12000000/(16*115200) = 78.125 *Fractional portion = 0.125 *User's Guide Table 21-4: UCBRSx = 0x10 *UCBRFx = int ( (78.125-78)*16) = 2 */ EUSCI_A2->BRW = 6; /*12000000/16/9600*/ EUSCI_A2->MCTLW = (8 << EUSCI_A_MCTLW_BRF_OFS) | EUSCI_A_MCTLW_OS16; EUSCI_A2->CTLW0 &= ~(EUSCI_A_CTLW0_SWRST); /*INITIALIZE EUSCI*/ EUSCI_A2->IFG &= ~(EUSCI_A_IFG_RXIFG); EUSCI_A2->IE |= EUSCI_A_IE_RXIE; /*ENABLE THE UART RX INTERRUPT*/ /*ENABLE GLOBAL INTERRUPT*/ __enable_irq(); /*ENABLE NVIC IN EUSCI_A2 INTERRUPT*/ NVIC->ISER[0] = 1 << ((EUSCIA2_IRQn) & 31); } /*UART PORTS*/ void confg_uart_wired(void){ P3->SEL0 |= UART_TX | UART_RX; /*SET 2 UART PIN AS SECOND FUNCTION*/ } /*UART CLOCK SYSTEM*/ void uart_cs(void){ CS->KEY = CS_KEY_VAL; /*UNLOCK CS MODULE REGISTER ACESS*/ CS->CTL0 = 0; /*RESET TUNING PARAMETERS*/ CS->CTL0 = CS_CTL0_DCORSEL_3; /*SET DCO TO 12MHz (NOMINAL, CENTER OF 8-16MHz RANGE)*/ CS->CTL1 = CS_CTL1_SELA_2 | /*SELECT ACLK = REFO*/ CS_CTL1_SELS_3 | /*SMCLK = DCO*/ CS_CTL1_SELM_3; /*MCLK = DCO*/ CS->KEY = 0; } /*DELAY MILLISECONDS WHEN SYSTEM CLOCK IS AT 3MHz*/ void delayMs(int n){ int i, j; for(j= 0; j < n;j++); for(i= 750;i>0;i--); } void uart_transmit(const char *str) { while (*str != 0) { //Do this during current element is not //equal to null character while (!(EUSCI_A2->IFG & EUSCI_A_IFG_TXIFG)); //Ensure that transmit interrupt flag is set EUSCI_A2->TXBUF = *str++; //Load UCA0TXBUF with current string element printf("%c",EUSCI_A2->TXBUF); } } void EUSCIA2_IRQHandler(void){ if(EUSCI_A2->IFG & EUSCI_A_IFG_RXIFG){ input[RXByteCtr++] = EUSCI_A2->RXBUF; LED_PyOUT(G_LED); } }
main.c
const char correct[] = "Your password is correct\n"; void main(void){ /*CLEAR ALL LED RGB*/ LED_PxCLR(R_LED|G_LED|B_LED); LED_PyDIR(R_LED|G_LED|B_LED); WatchDogHold(); uart_cs(); confg_uart_wired(); uart_init(); uart_transmit(correct); while(1){ } } void WatchDogHold(void){ WDTCTL = WDTPW | WDTHOLD; /*STOP WATCHDOG TIMER*/ }
result:
No Bruce, I can not get the data correctly on the device, can you give me some tips.
I'm completely lost I do not know what to do
Hello Bruce,
all definitions, and functions prototypes are defined in uart.h header file.
yes the definitions are right...
uart.h
#ifndef UART_H_ #define UART_H_ /*DEFINE*/ #define UART_TX BIT3 #define UART_RX BIT2 void uart_init(void); void confg_uart_wired(void); void uart_cs(void); #endif /* UART_H_ */
Bruce i share with you the main.h, there is where that functions are...
main.h
#ifndef MAIN_H_ #define MAIN_H_ void WatchDogHold(void); /*LED PIN CONFIGURATION*/ #define LED_PxOUT P2->OUT #define LED_PxDIR P2->DIR #define LED_PxCLR(x) LED_PxOUT &= ~(x & 0x00FF) #define LED_PxSET(x) LED_PxOUT |= (x & 0x00FF) #define LED_PyDIR(x) LED_PxDIR |= (x & 0x00FF) #define LED_PyOUT(x) LED_PxOUT ^= (x & 0x00FF) #define R_LED BIT0 /*RED LED*/ #define G_LED BIT1 /*GREEN LED*/ #define B_LED BIT2 /*BLUE LED*/ #endif /* MAIN_H_ */
Armando,
A couple of suggestions:
Hope that helps.
-Bob L.
Bruce an Bob thank you for your advices,
have realized the changes they told me but I did not get results, I think my problem is the data sending function and reception, connected interruptions, I think it is not well done,
**Attention** This is a public forum