Skip to main content

Subsection 16.1.2 Part 2: The Four Low-Power Modes, and How You Enter One

The STM32C031C6 contains many separate clock domains, each serving one part of the chip (Figure 16.1.9). Stopping a clock disables the corresponding domain and saves its power. The clock we have used all term is the HSI48, a 48 MHz internal oscillator whose output is divided by four at reset to give the 12 MHz system clock.
Figure 16.1.9. Clock distribution in the STM32C0, Figure 9 of the Reference Manual. The HSI48 oscillator at the left feeds the system clock through the HSIDIV divider, whose output HSISYS is the clock the CPU runs on; HSIDIV divides by four at reset, which is where our 12 MHz comes from. Other oscillators, including the 32 kHz low-speed internal oscillator (LSI) that clocks the watchdog, serve their own domains. Stopping a clock stops the domain it feeds, and saves its power. The blue path is our clock; the boxes at the right are the domains it serves.
The low-power modes stop progressively more of the chip. The device transitions from Run mode into a low-power mode and back; there is no path from one low-power mode to another (Figure 16.1.12). The Reference Manual’s Table 19, Device resources enabled in different operating modes, lists exactly what runs and what can wake the device in each mode (Figure 16.1.14); the four rows below are its summary.
Table 16.1.11. What each mode stops, and what wakes it
Mode What stops What survives What wakes it
Sleep the CPU clock all peripherals can run and wake up the CPU; peripheral clocks can be disabled before going to sleep any enabled interrupt, in a few clock cycles; a peripheral whose clock you left running can interrupt and wake the CPU, and one whose clock you disabled cannot
Stop the CPU clock and the high-speed oscillators SRAM and register contents; the 32 kHz low-speed oscillator (LSI) that clocks the watchdog, and a few wakeup-capable peripherals; GPIO pin states an interrupt from a peripheral that still has a clock
Standby the core’s supply domain, the part of the chip that powers the CPU and most peripherals SRAM and register contents are lost, except PWR control register 3; the IWDG and LSI can keep running the reset pin or one of the dedicated wakeup (WKUP) pins; Reference Manual Table 19 lists the rest
Shutdown all oscillators and the supply monitor nothing; contents are lost, and waking behaves like a reset the reset pin or a wakeup (WKUP) pin
Figure 16.1.12. The low-power mode transit diagram, Figure 7 of the Reference Manual. Every low-power mode is entered from Run mode and exits back to Run mode.
Figure 16.1.14. Reference Manual Table 19: which resources run in each operating mode, and which can wake the device. In ST’s notation Y is enabled, O optional (off by default, software can enable it), U unchanged from before entry, A automatically disabled, and a dash not available; the gray column beside Stop, Standby and Shutdown is the resource’s ability to wake the device. Reading down the columns, Sleep keeps everything but the CPU available; Stop keeps SRAM, GPIO pin states, the LSI and a few wakeup-capable peripherals; Standby and Shutdown keep almost nothing.
To put the processor into a low-power mode, you set two fields and execute one instruction. Set the LPMS (low-power mode selection) field in PWR_CR1 to the desired mode (Figure 16.1.16). Set the SLEEPDEEP bit in the Cortex-M0+ system control register, SCB->SCR (documented in the Arm Cortex-M0+ Generic User Guide, not in RM0490). Then execute __WFI() (wait for interrupt). Plain Sleep mode needs no register writes at all: with SLEEPDEEP clear, __WFI() stops the CPU clock until an interrupt arrives. For Stop mode, LPMS is 000 and SLEEPDEEP is set; Figure 16.1.20 lays out the full table. The PWR peripheral’s registers, like every peripheral’s, are reachable only while its clock is enabled, so the sequence is:
RCC->APBENR1 |= RCC_APBENR1_PWREN;    // clock the PWR peripheral
PWR->CR1 &= ~PWR_CR1_LPMS_Msk;        // LPMS = 000 selects Stop mode
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;    // deep sleep rather than Sleep
__WFI();                              // enter Stop mode; an interrupt wakes us
Figure 16.1.16. The low half of the PWR_CR1 register (Reference Manual §4.4.1), with the LPMS field circled: bits 2 to 0 select the low-power mode entered when SLEEPDEEP is set, and 000 is Stop mode.
Waking runs the entry backwards. An appropriate interrupt occurs, the HSI48 restarts, the interrupt’s own handler runs, and then execution resumes at the instruction after __WFI(). SLEEPDEEP stays set after waking, so the next __WFI() would enter Stop mode again; the program clears it if the next sleep should be a plain one. The deeper modes, Standby and Shutdown, are selected through the same LPMS field; their entry values are in Section 16.2.
Figure 16.1.20. Reference Manual Table 20, trimmed to the columns we use: the low-power mode entered for each combination of SLEEPDEEP, LPMS and entry instruction. The two entry instructions are WFI (wait for interrupt), the one we use, and WFE (wait for event), which wakes on events as well as interrupts. Sleep needs only SLEEPDEEP clear; Stop is SLEEPDEEP set with LPMS at 000.
The place where __WFI() belongs in a program is the main loop of an interrupt-driven design. Initialize the devices, enable the interrupts, and then loop: check the flags your ISRs set, take whatever action they call for, and go to sleep until the next interrupt. At least one enabled interrupt must exist, or nothing will ever wake the processor.
int main(void) {
    // Initialize devices, enable interrupts
    ...
    while (1) {
        // Check interrupt flags, take action
        ...
        // Go to sleep, await the next interrupt
        __WFI();
        // When the CPU wakes up, execution resumes here and the loop repeats
    }
    return 0;
}
// ISRs (including at least one interrupt that awakens the processor!)
...