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 buffer storing and Identification Issue



Hello All

Am using MSP432 development board, have successfully able to identify the interrupts and save them in the buffer. But when i try to read the text i fail to compare it with the predefined text. 

Another question, is it ok to use __delay_cycles() for delay ??? If not ok why and what would be preffered over __delay_cycles()?? 

Ex: From the Interrupt i would send series of characters like " Turn On" or " Turn Off" or " Status"

In the code i compare with the buffer the above mentioned predefined text. but it does not identify.

Please suggest where am going wrong. 

/* DriverLib Includes */
#include "driverlib.h"

/* Standard Includes */
#include <stdint.h>

#include <stdbool.h>

#define BUFF_SIZE  16
static volatile uint8_t buff[BUFF_SIZE];
static volatile uint32_t buff_indx_filling = 0;
static volatile uint32_t buff_indx_emptying = 0;
static volatile bool buff_is_empty = true; // true: empty, false: full

void flash_once(void)
{
	__delay_cycles(50000);
 GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
 GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
}

void flash_twice(void)
{
	__delay_cycles(50000);

 GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN2);
  GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN2);
}

/* 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
};

int main(void)
{
    /* Halting WDT  */
    MAP_WDT_A_holdTimer();

    /* 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 */
    CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);

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

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

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

    while(1)
    {
         MAP_PCM_gotoLPM0();
    }
}

void euscia0_isr(void)
{
	MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
    uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_MODULE);
    MAP_UART_clearInterruptFlag(EUSCI_A0_MODULE, status);
    if(status & EUSCI_A_UART_RECEIVE_INTERRUPT)
    {
        if (buff_is_empty == true)
        {
        	flash_once();
            buff[buff_indx_filling++] = MAP_UART_receiveData(EUSCI_A0_MODULE);
            if (buff_indx_filling == BUFF_SIZE)
            {
                MAP_UART_transmitData(EUSCI_A0_MODULE,'L');
                if (buff == "TURNON" || "TURNOFF" )
                          	{
                           if (buff == "TURNON")
                			{
                           		MAP_UART_transmitData(EUSCI_A0_MODULE,'S');
                           	}
                           if (buff == "TURNOFF")
      					   {
                           	    MAP_UART_transmitData(EUSCI_A0_MODULE,'N');
                          	}

                           if (buff[buff_indx_filling] == "STATUS")
                           	{
                          		MAP_UART_transmitData(EUSCI_A0_MODULE,'J');
                          	}
                            buff_is_empty = true;
                            buff_indx_filling = 0;
                            MAP_UART_transmitData(EUSCI_A0_MODULE,'Q');
            }
        }
    else
    {
    	MAP_UART_transmitData(EUSCI_A0_MODULE,'Z');
    }
 }
}

  • >if (buff == "TURNON")
    C is not some runtime string-aware language.
    C is a assembly compiler and can not pre calculate what strings lengths and content might look like.

    if (buff[0] == 'T') works because at compile time the ascii value for T is used. single char only

    if(strcmp(buff, "TURNON") == 0)
    though strcmp needs two pointers, the c compiler will store the ascii text in flash and insert the pointer to it for you.

    www.tutorialspoint.com/.../c_function_strcmp.htm

    Use els if,  not the redundant multi if you use.

  • Hello Tony

    Thanks for your help

    I have changed the code according to the the link you mentioned. Now i copy the string to an char array and compare with the text i receive from the interrupts. I had to typecast the values i received as it was defines as static volatile.
    I still cannot identify the text that have been received.
    Please suggest.

    /* DriverLib Includes */
    #include "driverlib.h"

    /* Standard Includes */
    #include <stdint.h>

    #include <stdbool.h>
    #include "string.h"

    #define BUFF_SIZE 8
    #define TRUE 1
    #define FALSE 0

    //static volatile uint8_t buff[BUFF_SIZE];
    static volatile uint8_t *buff;
    static volatile uint32_t buff_indx_filling = 0;
    static volatile uint32_t buff_indx_emptying = 0;
    static volatile bool buff_is_empty = true; // true: empty, false: full

    char str1[];
    char str2[];
    char str3[];

    void flash_once(void)
    {
    __delay_cycles(50000);
    GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
    GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
    }

    void flash_twice(void)
    {
    __delay_cycles(50000);

    GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN2);
    GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN2);
    }

    /* 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, // MSB First
    EUSCI_A_UART_ONE_STOP_BIT, // One stop bit
    EUSCI_A_UART_MODE, // UART mode
    EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION // Oversampling
    };

    int main(void)
    {
    /* Halting WDT */
    MAP_WDT_A_holdTimer();

    /* 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 */
    CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);

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

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

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


    while(1)
    {
    MAP_PCM_gotoLPM0();

    }
    }

    void euscia0_isr(void)
    {

    MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);

    uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_MODULE);
    MAP_UART_clearInterruptFlag(EUSCI_A0_MODULE, status);

    if(status & EUSCI_A_UART_RECEIVE_INTERRUPT)
    {
    if (buff_is_empty == true)
    {
    flash_once();
    if (buff_indx_filling == BUFF_SIZE)
    {

    MAP_UART_transmitData(EUSCI_A0_MODULE,'L');

    strcpy(str1,"TURN ON");
    strcpy(str2,"TURN OFF");
    strcpy(str3,"STATUS");

    if (strcmp((const char*)buff,str1) == 1)
    {

    MAP_UART_transmitData(EUSCI_A0_MODULE,'W');
    flash_once();
    flash_twice();

    }

    else if (strcmp((const char*)buff,str2) == 1)
    {
    MAP_UART_transmitData(EUSCI_A0_MODULE,'O');
    flash_once();
    flash_twice();

    }
    else if (strcmp((const char*)buff,str3) == 1)
    {
    MAP_UART_transmitData(EUSCI_A0_MODULE,'S');
    flash_once();
    flash_twice();
    }
    else
    {
    MAP_UART_transmitData(EUSCI_A0_MODULE,'N');
    buff_is_empty = true;
    buff_indx_filling = 0;
    MAP_UART_transmitData(EUSCI_A0_MODULE,'Q');
    }
    }
    }
    else
    {
    MAP_UART_transmitData(EUSCI_A0_MODULE,'R');
    }
    }

    }

**Attention** This is a public forum