liveBook · Manning

Wfi

As explained in Embedded Software with Rust

WFI is a processor instruction that puts the core into a low-power wait state until an interrupt or other wake-up event occurs.

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

How Embedded Software with Rust covers it

How deep it goes
Explains wake-up behavior, interrupt-handler flow, and alternatives, without detailing the processor’s sleep-state internals.
Examples
Uses timers, GPIO activity, DMA completion, and a cortex_m::asm::wfi() listing as concrete firmware examples.
What it assumes
Assumes interrupt configuration and handler setup, while introducing WFI’s low-power waiting behavior directly.
What you can do after
Operate/configure an interrupt-driven idle loop with cortex_m::asm::wfi().
How it connects
Connects WFI with interrupts, interrupt handlers, low-power idle loops, WFE, SEV, and cortex_m::asm.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.