WFI lets a Cortex-M core suspend instead of polling in a tight loop. Execution resumes when an interrupt becomes pending, and the corresponding handler runs. section 5.5.2
Use it in an idle loop after configuring interrupts for meaningful work such as timers, GPIO activity, or DMA completion. Interrupt handlers then drive progress while the core avoids unnecessary activity. section 5.5.2
Choose the related mechanism according to the wake-up model:
- Use wfi() when an interrupt should wake the core; it is the common choice for single-core low-power idle loops. section 5.5.2
- Use wfe() when an event should wake the core, including an explicit sev() signal for lightweight producer-consumer notification. section 5.5.2
use cortex_m::asm;
// Most cortex_m::asm functions are unsafe as they
// directly affect processor state.
unsafe {
asm::nop(); // No-op: useful for timing padding
asm::wfi(); // Wait for interrupt: suspend core until interrupt pending
asm::isb(); // Instruction Synchronisation Barrier
asm::dsb(); // Data Synchronisation Barrier
asm::dmb(); // Data Memory Barrier
asm::udf(); // Trigger UsageFault: used as a hard assert
asm::bkpt(); // Breakpoint: halts core under a connected debugger
asm::wfe(); // Wait for event: suspend core until event or interrupt
asm::sev(); // Send event: wake a core waiting in wfe()
}
Issue WFI through the cortex_m::asm intrinsic inside an unsafe block, alongside related processor-control operations. section 5.5.2
