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.
| Mode | What stops | Wakes on | Typical current |
|---|---|---|---|
| Run | Nothing | Not applicable | Milliamps |
| Sleep | Core clock | Any interrupt | Less than run |
| Stop or deep sleep | High-speed clocks; RAM kept | Timer, pin, selected peripherals | Microamps |
| Standby or shutdown | Most of the chip; most RAM lost | Wake pin, real-time clock, reset | Around a microamp or less |
Entering deep sleep safely
- Finish work: Complete or pause DMA transfers
- Park pins: Idle pins to a low-leakage state
- Arm wake: Enable sources, clear pending flags
- Sleep: Set SLEEPDEEP, execute WFI
- Wake: Restore clocks, then resume
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.