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.

TMS320F28335: There is a conflict between the shared global variables between the main loop and the interrupt service function

Part Number: TMS320F28335

Hi Team,

There's an issue from the customer need your help:

How can I avoid the access conflict between the main loop and the interrupt service function to global variables?

#define OK 1 //The device status is normal#define ERROR 0 //The device status is
abnormal

#define ON 1 // Open the valve #define OFF 0 // Close the
valve

void main(void)
{
/*initialize global variable*/
volatile int status = OK; } Device status
is normal volatile int Valve = ON; Open the valve
while(1) {
if(OK == Status)
//If the
device is normal. Step.1
{
Switch = ON; Open the valve. Step.4
}
}
}

/*
* Interrupt Service Function
* Note: When an external interrupt occurs, enter the interrupt service function
*/
void ISR_Interrupt(void)
{
status = ERROR; } The device status is abnormal. Step.2
Switch = OFF; Close the valve. Step.3
}

Suppose that the CPU execution sequence is in a certain scenario: at the beginning, the value of Status is OK, and the CPU uses the if statement in the main loop to determine that the condition is true, and the "Switch = ON" statement is about to be executed. At this point, an external interrupt is triggered, and the CPU goes to the interrupt service function and executes the "Switch = OFF" statement. After all interrupted service functions are executed, the CPU returns to the place where the main loop was interrupted and continues to execute the Switch = ON statement. The final value of the switch is ON.

Best Regards,

Ben

  • Hi,

    From our code snippet, I see the status is defined as a local variable. Can you define that as a global volatile variable?

    Regards,.

    Veena

  • Hi Veena,

    Please check this code snippet:

    #define OK 1 //The device status is normal
    #define ERROR 0 //The device status is abnormal
    
    #define ON 1 // Open the valve 
    #define OFF 0 // Close the valve
    
    void main(void)
    {
    /*initialize global variable*/
     volatile int status = OK; // Device status is normal 
     volatile int Valve = ON; // Open the valve
    
      while(1) {
        if(OK == Status)
    //If the device is normal. Step.1
    {
      Switch = ON;// Open the valve. Step.4
    }
    }
    }
    
    /*
    * Interrupt Service Function
    * Note: When an external interrupt occurs, enter the interrupt service function
    */
    void ISR_Interrupt(void)
    {
    status = ERROR; // The device status is abnormal. Step.2
    Switch = OFF;// Close the valve. Step.3
    }

    Best Regards,

    Ben

  • Hi Ben,

    I still see that the status is defined inside main and hence would be a local variable

    Regards,

    Veena