Getting started with PWR

This article explains low-power modes, and provides code examples.

1. Introduction to the low-power modes

By default, the microcontroller is in Run mode after a system or power reset. Several low-power modes are available to save power when the CPU does not need to be kept running, for example when waiting for an external event. The ultra-low-power STM32L476xx supports six low-power modes to achieve the best compromise between low-power consumption, short startup time, available peripherals and available wake-up sources.

  • Sleep mode
  • Low-power run mode
  • Low-power sleep mode
  • Stop 0, Stop1, Stop2 modes
  • Standby mode
  • Shutdown mode

1.1. Voltage regulators

Two embedded linear voltage regulators supply most of the digital circuitries: the main regulator (MR) and the low-power regulator (LPR).

  • The MR is used in the Run and Sleep modes and in the Stop 0 mode.
  • The LPR is used in Low-power run, Low-power sleep, Stop 1 and Stop 2 modes. It is also used to supply the 32 Kbyte SRAM2 in Standby with SRAM2 retention.
  • Both regulators are in power-down in Standby and Shutdown modes: the regulator output is in high impedance, and the kernel circuitry is powered down thus inducing zero consumption.

The main regulator has two possible programmable voltage ranges:

  • Range 1 with the CPU running at up to 80 MHz.
  • Range 2 with a maximum CPU frequency of 26 MHz. All peripheral clocks are also limited to 26 MHz.

PWR voltage regulators.png

1.2. Objectives

  • Create a simple project with each low-power mode, and wake up when a button (configured in the EXTI setup) is pressed.​
  • Learn how to set up low-power modes with the HAL​.
  • Verify the correct functionality by measuring the current consumption​.

2. Sleep mode

2.1. Definition

In Sleep mode, the CPU clock is OFF and there is no effect on other clocks or analog clock sources. All peripherals continue to operate and can wake up the CPU when an interrupt/event occurs.

PWR sleep mode.png

2.2. Configure the sleep mode

2.2.1. STM32CubeMX configuration

The system clock is set to 80 MHz.
clock config sleep mode.png

  • An EXTI line is connected to the user button through PC13.
  • LED2 connected to PA5 pin.
2.2.2. Code configuration
  • Open the project from Getting started with EXTI [1].
  • Open main.c
  • Add a function to enter Sleep mode.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

    HAL_Delay(1000);
    HAL_SuspendTick();
    HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI);
    HAL_ResumeTick();

}
/* USER CODE END 3 */
  • Set all GPIO to analog state to reduce power consumption as shown in the following picture:

Aanalog pins.png

Warning white.png Warning
In debug mode, make sure you are working in Range 1 by checking VOS bit in PWR_CR1 in SFR registers panel

2.3. Compile and flash

  • Click the Build button Built.png
  • Click the Run button to execute Run.png

2.4. Measure the current consumption

  • Stop any debug session and do a full-power cycle.
  • Use an amperemeter on the IDD connector (JP6 on NUCLEO-L476RG. For other boards, refer to their user manual).

PWR capture.png

  • Check the current consumption while in Sleep mode.
  • Press the configured user button on the Nucleo board to see the consumption increase as the MCU is waken up from sleep mode.
Info white.png Information
It is possible to measure the current consumption with the STM32 Power shield.

To do so, you need the X-NUCLEO-LPM01A [2] expansion board for power consumption measurement and its user manual [3]

  • Referring to the datasheet [4], in Sleep mode at 25°C, VDD = 3V and fHCLK= 80MHz, the current consumption should be 2.96mA

datasheet sleep mode.png

Info white.png Information
Note that you should always check that the measured value is bounded between the TYP and Max value in the datasheet.
  • Using the X-NUCLEO-LPM01A [2] and the STM32CubeMonPwr [5] we measured:

PWR final sleep.png

3. Low-power run mode

3.1. Definition

This mode is achieved when the system clock frequency is reduced below 2 MHz. The code is executed from the SRAM or the flash memory. The regulator is in low-power mode to minimize its operating current.

PWR lprun.png

3.2. Configure the Low-power run mode

3.2.1. STM32CubeMX configuration

The system clock is limited to 2 MHz maximum. The MSI internal RC oscillator can be selected as it supports several frequency ranges. clock config.png

Info white.png Information
In Low-power run mode, all I/O pins keep the same state as in Run mode.
3.2.2. Code configuration
  • Open the project from Getting started with EXTI [1]
  • Open main.c
  • Add a function to enter the Low-power run mode.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{ /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    HAL_Delay(2000);
    HAL_PWREx_EnableLowPowerRunMode();

}
/* USER CODE END 3 */

3.3. Compile and flash

  • Click the Build button Built.png
  • Click the Run button to execute Run.png

3.4. Measure the current consumption

  • Referring to the datasheet [4] , in Low-power run mode at 25°C, Fhclk = 100kHz and VDD = 3V, the current consumption should be 42μA.

PWR datasheet LPRUN.png

  • Using the STM32Cube Monitor-Power [5], we measured:

PWR final lprun.png


4. Low Power Sleep mode

4.1. Definition

This mode is entered from the Low-power run mode. Only the CPU clock is stopped. When wake-up is triggered by an event or an interrupt, the system reverts to the Low-power run mode.

4.2. Configure the Low-power sleep mode

  • Open the project from Getting started with EXTI [1]
  • Open main.c
  • Add a function to enter Low-power sleep mode
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{ /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    HAL_Delay(2000);
    HAL_SuspendTick();
    HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
    HAL_ResumeTick();

}
/* USER CODE END 3 */
Info white.png Information
Unlike Sleep mode, in Low-power sleep mode the main regulator is OFF and the low-power regulator is ON.

4.3. Compile and flash

  • Click the Build button Built.png
  • Click the Run button to execute Run.png
Warning white.png Warning
This example can not be used in DEBUG mode due to the fact that the Cortex-M4 core is no longer clocked during low-power mode so debugging features are disabled.

4.4. Measure the current consumption

  • Referring to the datasheet [4], in Low-power sleep mode at 25°C and VDD = 3V, the current consumption should be 33mA

datasheet lpsleep.png

  • Using the STM32Cube Monitor-Power [5], we measured:

PWR LPSLEEP.png

5. Stop0, Stop1, and Stop2 modes

5.1. Definition

Stop mode achieves the lowest power consumption while retaining the content of SRAM and registers. All clocks in the VCORE domain are stopped, the PLL, the MSI RC, the HSI16 RC and the HSE crystal oscillators are disabled. The LSE or LSI can be kept running.
PWR Stop modes.png

5.2. Stop0 mode

  • Open the project from Getting started with EXTI [1]
  • Open main.c
  • Add a function to suspend the Systick (the SysTick is typically set to raise an interrupt every 1 ms).
  • Add a function to enter the Stop0 mode.
  • Add a function to resume the Systick on wake-up.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

    HAL_Delay(1000);
    HAL_SuspendTick();
    HAL_PWREx_EnterSTOP0Mode(PWR_SLEEPENTRY_WFI);
    HAL_ResumeTick();
}
/* USER CODE END 3 */
Info white.png Information
After using WFI by pressing user button, and exiting Stop0, Stop1 or Stop 2 mode, the code in the handler will be executed.
5.2.1. Compile and flash
  • Click the Build button Built.png
  • Click the Run button to execute Run.png
5.2.2. Measure the current consumption
  • Referring to the datasheet [4], in Stop0 mode at 25°C and VDD = 3.6V, the current consumption should be 113μA

PWR datasheet stop0.png

  • Using the STM32Cube Monitor-Power [5], we measured:

PWR final stop0.png

5.3. Stop1 mode

Stop 1 offers the largest number of active peripherals and wake-up sources, a smaller wake-up time but a higher consumption than Stop 2.

  • Change only this line in the code:
HAL_PWREx_EnterSTOP1Mode(PWR_SLEEPENTRY_WFI);
5.3.1. Compile and flash
  • Click the Build button Built.png
  • Click the Run button to execute Run.png
5.3.2. Measure the current consumption
  • Referring to the datasheet [4], in Stop1 mode at 25°C and VDD = 3.6V, the current consumption should be 6.70μA

PWR DATASHEET STOP1.png

  • Using the STM32Cube Monitor-Power [5], we measured:

PWR final plot stop1.png

5.4. Stop2 mode

In Stop 2 mode, most of the VCORE domain is put in a lower leakage mode.

  • Change only this line in the code:
HAL_PWREx_EnterSTOP2Mode(PWR_SLEEPENTRY_WFI);
5.4.1. Compile and flash
  • Click the Build button Built.png
  • Click the Run button to execute Run.png
5.4.2. Measure the current consumption
  • Referring to the datasheet [4], in Stop2 mode at 25°C and VDD = 3.6V, the current consumption should be 1.26μA

PWR datasheet stop2.png

  • Using the STM32Cube Monitor-Power [5], we measured:

PWR final slot stop2.png

To exit from Stop2 mode, RTC (Real-Time Clock) peripheral can be used to periodically wake it up, please check this article about How to configure the RTC to wake up the STM32 periodically from Low Power modes

6. Standby mode

6.1. Definition

The Standby mode is used to achieve the lowest power consumption with brown-out reset. The internal regulator is switched off so that the VCORE domain is powered off. The PLL, the MSI RC, the HSI16 RC and the HSE crystal oscillators are also switched off.
RTC can remain active (Standby mode with RTC, Standby mode without RTC).
Brown-out reset (BOR) always remains active in Standby mode.
The state of each I/O during standby mode can be selected by software: I/O with internal pull-up, internal pull-down or floating.

The system can be woken up from standby mode using a SYS_WKUP pin, an RTC event (alarm or timer), IWDG, or an external reset in NRST pin.
After waking up from Standby mode, program execution restarts in the same way as after a Reset (boot pin sampling, option bytes loading, reset vector is fetched, etc.).
PWR Standby.png

6.2. HAL library workflow summary

Info white.png Information
When exiting Standby or Shutdown mode, all registers in the VCORE domain are set to their reset value.

PWR StandBy HAL.png

6.3. Configure the Standby Mode

  • Open the project from Getting started with EXTI [1]
  • Open main.c
Info white.png Information
If you intend to wake the system up using a SYS_WKUP pin, add the following line of code to clear the wake-up flag. Otherwise, standby mode will only be entered once.
/* Initialize all configured peripherals */
/* USER CODE BEGIN 2 */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
/* USER CODE END 2 */
  • Add a function to enter Standby mode
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{ /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    HAL_Delay(2000);
    HAL_PWR_EnterSTANDBYMode();    
}
/* USER CODE END 3 */

6.4. Compile and flash

  • Click the Build button Built.png
  • Click the Run button to execute Run.png

6.5. Measure the current consumption

  • Referring to the datasheet [4], in Standby mode at 25°C and VDD = 3V, the current consumption should be 150nA

PWR datasheet Standby.png

  • Using the STM32Cube Monitor-Power [5], we measured:

PWR final standby.png

7. Shutdown mode

7.1. Definition

The Shutdown mode allows to achieve the lowest power consumption. The internal regulator is switched off so that the VCORE domain is powered off. The PLL, the HSI16, the MSI, the LSI and the HSE oscillators are also switched off.
The system can be woken up from shutdown mode using a SYS_WKUP pin, an RTC event (alarm or timer), or external reset in NRST pin.
After waking up from Shutdown mode, program execution restarts in the same way as after a Reset (boot pin sampling, option bytes loading, reset vector is fetched, etc.).
600

7.2. Configure the Shutdown Mode

  • Open the project from Getting started with EXTI [1]
  • Open main.c
  • Add a function to enter Shutdown mode
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{ /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    HAL_Delay(2000);
    HAL_PWREx_EnterSHUTDOWNMode();
}
/* USER CODE END 3 */

7.3. Compile and flash

  • Click the Build button Built.png
  • Click the Run button to execute Run.png

7.4. Measure the current consumption

  • Referring to the datasheet [4], in Shutdown mode at 25°C and VDD = 3V, the current consumption should be 64.1nA

PWR datasheet shutdown.png

  • Using the STM32Cube Monitor-Power [5], we measured:

PWR final shutdown.png

8. References