Low-Power Firmware: Sleep Modes, Clocks and Wake Sources

A modern low-power microcontroller can sleep at a few microamps or less, but only if the firmware puts it there and keeps it there. A single floating pin, a peripheral clock left running or a busy-wait delay can raise the average current several times over. This article covers the firmware techniques that turn a datasheet sleep figure into one measured on the product.

At a glance

WFI
The Cortex-M wait-for-interrupt instruction, which stops the core until an interrupt arrives.
SLEEPDEEP
A bit in the Cortex-M System Control Register that turns WFI into a deep sleep.
C x V² x f
Dynamic power scales with switched capacitance, supply voltage squared and clock frequency.
Tickless
An RTOS idle mode that stops the periodic tick so the core can sleep between events.

The problem

Datasheet sleep currents assume everything is configured for minimum consumption. On a real board, an input pin left floating can sit near mid-rail, where its input buffer draws current, and a peripheral left clocked keeps part of the chip awake. Either can exceed the sleep current of the core itself.

Firmware structure matters as much. Polling loops, counting-loop delays and an RTOS tick every millisecond all keep the core awake with nothing to do.

Wake only to work. The rest of the time belongs to the deepest sleep mode that can still respond in time.

Choosing a sleep mode

Each deeper mode turns off more of the chip and takes longer to wake. Light sleep stops only the core clock and wakes almost immediately on any interrupt. Deeper stop modes turn off the high-speed clocks but keep RAM, waking on a timer, a pin or selected peripherals. The deepest modes lose most state and restart much like a reset. Pick the deepest mode whose wake time and sources meet the requirement.

Restoring clocks is a common oversight. Many parts wake from deep sleep running on an internal oscillator, so firmware must restart the PLL or external crystal before timing-sensitive work, and allow for its start-up time.

What good looks like

Configure every unused pin to a defined, low-leakage state and disable the clock to every peripheral that is not in use.

Make the firmware event-driven: replace polling and busy-wait delays with interrupts, timers and DMA, and enable tickless idle if an RTOS is used.

Measure each mode on real hardware and compare it with the datasheet to find leaks.

A typical sleep mode ladder
ModeWhat stopsWakes onTypical current
RunNothingNot applicableMilliamps
SleepCore clockAny interruptLess than run
Stop or deep sleepHigh-speed clocks; RAM keptTimer, pin, selected peripheralsMicroamps
Standby or shutdownMost of the chip; most RAM lostWake pin, real-time clock, resetAround a microamp or less
Generic names and orders of magnitude. Vendors name and define their modes differently; use the datasheet figures for your part.

Entering deep sleep safely

  1. Finish work: Complete or pause DMA transfers
  2. Park pins: Idle pins to a low-leakage state
  3. Arm wake: Enable sources, clear pending flags
  4. Sleep: Set SLEEPDEEP, execute WFI
  5. Wake: Restore clocks, then resume
A typical sequence. Details depend on the part and any RTOS in use.

What it changes

Datasheet Currents

Measured sleep current approaches what the part can achieve.

Longer Life

Lower average current extends battery life without changing the hardware.

Fast Response

Wake sources are chosen so the device still reacts in time.

Fewer Surprises

Leaks are found on the bench rather than in returned units.

Where to go next

Compare running fast and sleeping sooner against running slowly. Dynamic power falls with clock frequency, but static current flows for longer, so the better choice depends on the part.

Check external parts too: pull-ups held low, sensors left powered or a regulator with high quiescent current can outweigh the microcontroller.

Problems this prevents

  • Floating inputs drawing current in sleep
  • Peripheral clocks left running
  • An RTOS tick waking the core every millisecond
  • Timing errors after waking on the wrong clock

Discuss your project

Email support@aavico.com or use Get in Touch when the website finishes loading.