The deepest mode that still wakes
Pick the lowest-current sleep mode that keeps the wake sources and the RAM a design needs. A medium embedded C problem (Designing for low power), run and…
medium · Designing for low power · power · sleep · stm32
A chip's sleep modes are in the table MODES (written for you): each has a name, its current in µA, the wake sources it keeps (a bit mask of WAKE_... flags) and whether it keeps RAM.
Write deepest_mode(wakes, keep_ram). It returns the index in MODES of the mode with the lowest current that offers every wake source in wakes and, when keep_ram is set, keeps RAM. Return -1 when no mode does.
Starter code
int deepest_mode(unsigned wakes, int keep_ram) {
int best = 0;
for (int i = 1; i < MODE_COUNT; i++) {
if (MODES[i].ua < MODES[best].ua) best = i;
}
(void)wakes; (void)keep_ram;
return best;
}Example tests
A door sensor: a pin interrupt, RAM kept
show(WAKE_PIN, 1);Expected output:
Stop1A logger that only needs the RTC and loses nothing
show(WAKE_RTC, 0);Expected output:
StandbyLearn it first
- Designing for low power · Sleep modes and wake sources, entering them on STM32 and AVR, clocks and pins that draw, race to sleep.