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.

MSP432 UART interrupt behavior



Hi Everyone,

I am trying to configure my UART to reveive mutple bytes and process them once "\n" is received.

Here is my code.

The codes expected to work like this

1)Received the first array of bytes and store them into the buffer 

2)Set test.newStringReceived to true, so if blocks the upcomming arrays until it is cleared(false)

3)The loop in the main function will constantly check the test.newStringReceived, if it is true, it will copy the data from buffer and clear it.

4)The code will be able to receive the next array of bytes

The 1) and 2) works well, However, after the UART interrupt , the code does not go back to while loop in main so 3) never happens.

It seems like the while loop in MAIN stopped excuating  after the UART interrnupt. (Even i put a break point inside of while loop. It does not get tiggered).

(This happends after 1) and 2),(after a interrupt?), this does not happends if the interrupt neven get triggered).

Did i mss something? Any insights will be really appriciated.

Thansk,

Eddy

#include "driverlib.h"

/* Standard Includes */
#include <stdint.h>
//#include "printf.h"
#include <stdbool.h>
#include "msp.h"
#include <driverlib.h>
#include <HAL_I2C.h>
#include <HAL_OPT3001.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
//#include <uart_driver.h>

/* UART Configuration Parameter. These are the configuration parameters to
 * make the eUSCI A UART module to operate with a 9600 baud rate. These
 * values were calculated using the online calculator that TI provides
 * at:
 *software-dl.ti.com/.../index.html
 */


const eUSCI_UART_Config uartConfig =
{
        EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
        78,                                     // BRDIV = 78
        2,                                       // UCxBRF = 2
        0,                                       // UCxBRS = 0
        EUSCI_A_UART_NO_PARITY,                  // No Parity
        EUSCI_A_UART_LSB_FIRST,                  // LSB First
        EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
        EUSCI_A_UART_MODE,                       // UART mode
        EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
};

/* Graphic library context */


//Receive UART Variables
#define NUM_RX_CHARS 64
char rxMsgData[NUM_RX_CHARS] = "";
int numMsgsRx = 0;
int tempIndex = 5;
int numChars = 0;

#define MAX_STR_LENGTH 271
uint8_t rx_buffer[1024];
uint16_t bufferIndex = 0;


#define FALSE 0
#define TRUE  1

typedef struct{
	unsigned char newStringReceived;
	char          txString[MAX_STR_LENGTH];
	char          rxString[MAX_STR_LENGTH];
}s_test;

extern s_test test;
s_test test = {
	FALSE,
	"",
	""
};
/* Variable for storing lux value returned from OPT3001 */
float lux;

/* Timer_A Up Configuration Parameter */
const Timer_A_UpModeConfig upConfig =
{
        TIMER_A_CLOCKSOURCE_ACLK,               // ACLK Clock SOurce
        TIMER_A_CLOCKSOURCE_DIVIDER_1,          // ACLK/1 = 3MHz
        200,                                    // 200 tick period
        TIMER_A_TAIE_INTERRUPT_DISABLE,         // Disable Timer interrupt
        TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE,    // Disable CCR0 interrupt
        TIMER_A_DO_CLEAR                        // Clear value
};

/* Timer_A Compare Configuration Parameter  (PWM) */
Timer_A_CompareModeConfig compareConfig_PWM =
{
        TIMER_A_CAPTURECOMPARE_REGISTER_3,          // Use CCR3
        TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE,   // Disable CCR interrupt
        TIMER_A_OUTPUTMODE_TOGGLE_SET,              // Toggle output but
        100                                         // 50% Duty Cycle
};


bool receiveText(char* data, int maxNumChars){
	bool result = false;
    if(test.newStringReceived == TRUE){
    	result = true;
    	strncpy(data,test.rxString,maxNumChars);
    	test.newStringReceived = FALSE;
    }
    return result;
}



int main(void)
{
    /* Halting WDT  */
    MAP_WDT_A_holdTimer();
    /* Set the core voltage level to VCORE1 */
    MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);

    /* Set 2 flash wait states for Flash bank 0 and 1*/
    MAP_FlashCtl_setWaitState(FLASH_BANK0, 2);
    MAP_FlashCtl_setWaitState(FLASH_BANK1, 2);

    // the UART DOES NOT WORK FOR more than 24Mhz
    MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_24);
    MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_2 );
    MAP_CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_2 );
    MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_2 );
    MAP_CS_initClockSignal(CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_2);


    Init_I2C_GPIO();
    I2C_init();




    /* Selecting P1.2 and P1.3 in UART mode */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
            GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);

    /* Setting DCO to 12MHz */

    /* Configuring UART Module */
    MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);

    /* Enable UART module */
    MAP_UART_enableModule(EUSCI_A0_BASE);

    /* Enabling UART interrupts */
    MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
    MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
    MAP_Interrupt_enableSleepOnIsrExit();
    MAP_Interrupt_enableMaster();

    while(1)
    {

        char *s = "printf test";
        char c = '!';

        //The receiveText will check continuously whether the data is received , by checking the test.newStringReceived.
        //receiveText also clear the test.newStringReceived to FALSE, so the next array of bytes can be received.
        bool data_received = receiveText(rxMsgData,NUM_RX_CHARS);


    	if (data_received){

    		numMsgsRx++;
    		numChars = strlen(rxMsgData);


    	}



		// Configure WDT
		// For LPM3 Clock Source should be BCLK or VLOCLK


		// Start WDT

		//Go to LPM0 (Cannot use LPM3 because we won't accurately receive UART data)
		//MAP_PCM_gotoLPM0();
    }
}

/* EUSCI A0 UART ISR - Echoes data back to PC host */
/* This code works the following way
 * 1) Open a cool term serial port
 * 2) the board has backdoor uart and will shows up as comm port
 * 3)connect to that port in coolterm
 * 4)send some strings
 * 5)add a break point at below interrupt.
 * 6)the byte send should be stored in the "receiveByte" address
 * 7)Then the byte will be eched back to the cool term
 */

void uartReceive(char data){
	static char rxInProgress = FALSE;
	static char pieceOfString[MAX_STR_LENGTH] = "";           // Holds the new addition to the string
	static char charCnt = 0;

	if( !rxInProgress){
		if ((data != '\n') ){
			pieceOfString[0] = '\0';
			rxInProgress = TRUE;
			pieceOfString[0] = data;
			charCnt = 1;
		}
	}else{ // in progress
		if((data == '\n')){
			rxInProgress = FALSE;
			if (test.newStringReceived == FALSE){ // don't mess with the string while main processes it.
				pieceOfString[charCnt]='\0';
				__no_operation();
				charCnt++;
				strncpy(test.rxString,pieceOfString,charCnt);
				__no_operation();
				test.newStringReceived = TRUE;
				__no_operation();
			}
		}else{
			if (charCnt >= MAX_STR_LENGTH){
				rxInProgress = FALSE;
			}else{
				pieceOfString[charCnt++] = data;
				//charCnt++;
			}
		}
	}
}

void EUSCIA0_IRQHandler(void)
{
    uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
    //char receiveByte = UCA0RXBUF;  //This is the address where the received byte is stored
    //int receiveByte_2 = 0x10;
    //uartReceive(data);

    //MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);

    if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)
    {
        char data = UCA0RXBUF;  //RXBUF0;
        uartReceive(data);


        //rx_buffer[bufferIndex] = UCA0RXBUF;
        //bufferIndex++;
        __no_operation();

        //MAP_UART_transmitData(EUSCI_A0_BASE, receiveByte);
    }
    MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);

}

**Attention** This is a public forum