Search code examples
cembeddedpragmamsp430

Pragma and Interrupt Vector Table : TI MSP430


My program contains few global variables , whose values are set during the interrupt service routine (USCI_A0_ISR()) execution.

Once the execution of USCI_A0_ISR() is done , will the global variables hold the value assigned or will be set back to void/0.????

//Global variables
int ptr = 0;
char rxBuffer[16]; 
int flag = -1;
int check[2];

void __set_flag(void)
{

    if (strcmp(rxBuffer,"OK") == 0) flag = 0; 
    else if (strcmp(rxBuffer,"CONNECT") == 0) flag = 1;
    else if (strcmp(rxBuffer,"NO CARRIER") == 0) flag = 3;
    else if (strcmp(rxBuffer,"ERROR") == 0) flag = 4;

}

void __GSM_client(void)
{
    while (flag == -1);
    if (flag == 0) check[0] = buflen(rxBuffer);
}

void main(void)
{
    __Buffer_init();
    __low_level_init();         //WDT 
    __UART0_init();                 //UART 

    __bis_SR_register(GIE);       //interrupts enabled
    __delay_cycles(1000);           // wait till UART intial

    __GSM_client();

    __no_operation();             // For debugger
}

#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
    char byte;
    while (!(UCA0IFG&UCTXIFG));             
    byte=  UCA0RXBUF;
    UCA0TXBUF = byte;
    if (byte == '\r') { 
        //push_char(byte);
        ptr = 0;
        __set_flag();
        //__Buffer_init();              
    }
    else{                       
        push_char(byte);
    }
}

Here is the code snippet of what I am doing. I am setting the "flag" based on the response obtained . When I see the register view in Code Composer Studio , the "flag" value is set correctly , but if try using the value of "flag" elsewhere the value of "flag " is not reflected.

Any pointers over concepts of the interrupt service routine or when loopholes in my coding method appreciated Thanks in Advance AK


Solution

  • Within the interrupt, you are directly or indirectly changing several global variables, e.g. ptr, flag, and I'm assuming rxBuffer[?]. They are not declared "volatile" so their value may or may not change when you return from the interrupt. This is a bug because the behavior can change based on where in the execution of the code the interrupt occurs and what the level of optimization is. As a rule of thumb, any variable modified by an interrupt routine should always be declared volatile.