This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use WFE reliably?

After reading the fine-print regarding WFE, I came to the conclusion that WFE can send the CPU to sleep even if interrupts are still pending. Such interrupts would not be able then to wake the CPU. In order to wake the CPU the interrupt has to occur while the CPU is sleeping. Is this correct? In my case I want to wake the CPU with a key press (via the GPIOTE PORT event), so I wonder if sleeping like this

if (!NVIC_GetPendingIRQ(GPIOTE_IRQn))
  __wfe();

would allow the CPU to reliably wake up via the GPIOTE_IRQn interrupt (if GPIOTE_IRQn was not pending and thus the CPU was actually sent to sleep)? In other words, could a GPIOTE_IRQn interrupt occur right after having checked the interrupt pending flag but before or while executing WPE so that the CPU would still fall asleep and not wake up from this interrupt? I know that this would be a very rare case as the time span between NVIC_GetPendingIRQ() and __wpe() is so short, but if it happened it would render my device unresponsive.

Related