This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Race condition between wake up event and WFI on Cortex-M3/M4

When I read below thread in arm forum, I still not clear which one is the safety way.

Cortex-M4: guaranteed wakeup from WFI?

There're two solutions mentioned above, using WFE instead of WFI, and swap __WFI() and __enable_irq().

I can understand WFE method but in real cases seems more common to use WFI with properly disabling interrupt. so I want to make sure if the second method is OK.

Generally, there're some logic conditions we need to check to see if we're OK to go to sleep. Assuming in a classic single thread system, somewhere within main loop, the code will be like below:

while (1) {
....
....
__disable_irq();
if  (condition == true) {
    __WFI();
}
__enable_irq();
...
}

And in the wake up event interrupt:

wake_up_isr()
{
     condition =  false;
}

1) If wake up event (assuming a pin interrupt) comes before critical section, condition variable will be set true and  system will not go to sleep.

2) if wake up event (assuming a pin interrupt) comes just before __WFI(), as interrupt is disabled, the system will go to sleep, my question is: will system be waked up if some interrupt pending bit is set?

Parents
  • WFI.png

    About picture is from Joseph's book, it says the Cortex-M3/4 will be wake-up by a IRQ higher  than current priority even PRIMASK is 1, but it doesn't say when the  IRQ comes, if the IRQ comes after PRIMASK set to 1, so in NVIC it is pending status, and before __WFI(), can the core be wake up immediately or like behavior like WFE will not goes to low power at all?

Reply
  • WFI.png

    About picture is from Joseph's book, it says the Cortex-M3/4 will be wake-up by a IRQ higher  than current priority even PRIMASK is 1, but it doesn't say when the  IRQ comes, if the IRQ comes after PRIMASK set to 1, so in NVIC it is pending status, and before __WFI(), can the core be wake up immediately or like behavior like WFE will not goes to low power at all?

Children