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?