STM32 Low-Power Modes.

First I'd like to say a couple of words about the microcontroller's supply voltages: STM32 needs 2 to 3.6V for normal operation, and the built-in regulator provides 1.8 volts to power the core. Below is a schematic of the controller's overall power supply.
STM32 Low-Power Modes.

As you can see, the power supply is divided into domains, each responsible for powering certain peripherals/parts of the MCU.

You can reduce the MCU's consumption in several ways. The simplest thing to do is lower the clock frequency or turn off the APB and AHB buses when they're not in use. Another option, which requires a bit more knowledge, is to use the low-power modes; by the way, there are only three of them:

  • sleep mode
  • stop mode
  • standby mode


Before describing the modes, I'd like to note that entering any of the low-power modes is done with the WFI and WFE assembly instructions. The difference is that if the controller was put into a low-power mode with WFI (wait for interrupt), it can only exit this mode on an interrupt; if with WFE (wait for event), it can exit this mode when an event occurs. Let me remind you that an event is a peripheral interrupt that was not enabled in the NVIC.

Sleep mode is characterized by the following: program execution stops, the core clock stops, the peripherals keep working, and all pins keep their state.

Sleep mode is entered with the WFE and WFI instructions when:

  • the SLEEPDEEP bit is cleared
  • the value of the SLEEPONEXIT bit is selected


As for SLEEPONEXIT, its state determines the controller's behavior when exiting sleep mode on an interrupt (WFI): if the bit is cleared, the controller wakes up, handles the interrupt and continues executing code as usual, but if SLEEPONEXIT is set, then after waking up and handling the interrupt, the controller goes back to sleep. This makes it possible to create devices with minimal consumption that run on interrupts.

Exiting sleep mode on an event (WFE) lets the program resume execution from where it was stopped.
The source of the event can be:

  • an interrupt enabled in the peripheral control register but not in the NVIC, or setting the SEVONPEND bit in the core control register
  • a signal on an external interrupt line configured in event mode. But the signal doesn't necessarily have to come from outside; for example, EXTI contains a line through which an interrupt request from the real-time clock (RTC) can arrive

Exiting sleep mode on an interrupt (WFI) can be triggered by any interrupt in the NVIC.


As for consumption, you need to look at the documentation for the specific chip; for example, the minimum consumption of the STM32F103VE is 0.45mA.
STM32 Low-Power Modes.


Stop mode is characterized by stopping the PLL, HSI and HSE oscillators, while all I/O pins keep their state.

Stop mode is entered with the WFE and WFI instructions when:

  • the SLEEPDEEP bit is set
  • the PDDS bit in the PWR_CR register is cleared
  • the voltage regulator mode (the one mentioned at the beginning of the article) is selected with the LPDS bit in the PWR_CR register


Exiting stop mode in both cases (WFI and WFE) is initiated by a signal on any of the EXTI lines, configured in interrupt mode in one case and in event mode in the other.

As for power consumption, in this case it's tens of microamps and depends on the voltage regulator mode.
STM32 Low-Power Modes.




Standby mode is characterized by the lowest consumption, about 2µA, and the loss of all data, i.e. when exiting this mode the controller behaves the same as when the Reset button is pressed.

Standby mode is entered with the WFE and WFI instructions when:
  • the SLEEPDEEP bit is set
  • the PDDS bit in the PWR_CR register is set
  • the WUF bit in the PWR_CSR register is cleared

Standby mode is exited:
  • on a rising edge on the WKUP (A0) pin, which must be specially configured for this
  • on a rising edge of the RTC alarm
  • on an external reset (the NRST pin)
  • on an IWDG reset


The set SBF flag indicates that the controller has exited standby mode. In standby mode, all pins are in a high-impedance state except NRST, WKUP (if configured) and Tamper (if configured).

For clarity, I'll leave this table here and explain that the entry column describes the bits that need to be configured, not set.
STM32 Low-Power Modes.


Debugging in low-power modes can be enabled by setting the corresponding bits in the DBGMCU_CR register.
 
DBGMCU->CR |= DBGMCU_CR_DBG_SLEEP;
DBGMCU->CR |= DBGMCU_CR_DBG_STOP;
DBGMCU->CR |= DBGMCU_CR_DBG_STANDBY;


Power control register (PWR_CR).
STM32 Low-Power Modes.

Bits 31:9 – reserved, filled with zeros.

DBP(Disable backup domain write protection) — a 1 in this bit enables access to the backup registers and RTC, a 0 disables it.

PLS[2:0](PVD level selection) — set the threshold level of the voltage detector.
STM32 Low-Power Modes.

PVDE(Power voltage detector enable) — a 1 in this bit enables the PVD, a 0 disables it.

CSBF(Clear standby flag) — always reads as zero; setting this bit to 1 clears SBF (Standby Flag).

CWUF(Clear wakeup flag) — always reads as zero; setting this bit to 1 clears WUF (Wakeup Flag), but this happens two system clock cycles later.

PDDS(Power down deepsleep) — when the controller is in deep sleep (Deepsleep), writing 1 to this bit puts it into standby mode, writing 0 into stop mode; the voltage regulator status depends on LPDS, which applies only to stop mode.

LPDS(Low-power deepsleep) — a 0 in this bit keeps the voltage regulator on in stop mode; a 1 puts the regulator into low-power mode in stop mode.


Power control/status register (PWR_CSR)
STM32 Low-Power Modes.

Bits 31:9 – reserved, filled with zeros.

EWUP(Enable WKUP pin) — a 1 in this bit enables the WKUP pin to wake up the microcontroller on a rising edge; a 0 disables it, in which case the WKUP pin is used as an ordinary GPIO. It is cleared automatically after a reset.

Bits 7:3 – reserved, filled with zeros.

PVDO(PVD output) — this bit is changed by hardware and is valid only when the PVDE bit is set. A one in this bit means that VDD/VDDA has dropped below the threshold selected by the PLS[2:0] bits; a 0 means VDD/VDDA is above the threshold selected by the PLS[2:0] bits.

SBF(Standby flag) — this bit is set by hardware and cleared by a POR/PDR (power on reset/power down reset) or by setting the CSBF bit in the PWR_CR register; a 1 in this bit means the controller was in standby mode.

WUF(Wakeup flag) — this bit is set by hardware and cleared by a POR/PDR (power on reset/power down reset) or by setting the CWUF bit in the PWR_CR register; a 1 in this bit means a wakeup event came from the WKUP pin or from the RTC alarm.


You can find out which pin implements the WKUP function in the STM32CUBE program.
STM32 Low-Power Modes.


And finally, code that enters standby mode and exits it on a rising edge on the WKUP pin; in my case (STM32F103VET) this is pin A0.

        RCC->APB1ENR |= RCC_APB1ENR_PWREN;//enable PWR clocking
	SCB->SCR |= SCB_SCR_SLEEPDEEP; //for M3, enable sleepdeep
	PWR->CR |= PWR_CR_PDDS;//select Power Down Deepsleep mode
	PWR->CR |= PWR_CR_CWUF ; //clear the wakeup flag
	PWR->CSR |= PWR_CSR_EWUP; //enable wakeup, i.e. waking up on a rising edge on A0
	__WFE();  //go to sleep

For more detailed information, see the AN2629 Application note