Hi all,
This is my first post here and also I'm a noob when it comes to microcontrollers and I need some help.
Please look at the following code example:
volatile int x = 0; int main(void) { HAL_initPorts(); HAL_initClocks(); __enable_interrupt(); while(1) { if (x) { //do some work x = 0; } } } void some_isr(void) { x = 1; }
Now from my understanding I can see a potential race condition can occur during the following scenario:
1) some_isr was triggered and set x = 1
2) main continued its work and if (x) resulted in a true value
3) right before the line "x = 0" in main the ISR was triggered again setting x = 1 again
4) main continued its work and set x = 0 not "knowing" the ISR was triggered again and it will not enter the "if (x)" condition on the next iteration
So how can this be avoided? In higher level programming languages I guess I would use some kind of a mutex or auto reset event to prevent this race condition.
Would disabling interrupts from within the ISR and then re-enabling them in main be a possible solution? If so is this recommended or can it have side-effects?
Thanks