This article explains Low-power modes, and provides code examples.
1. Low-power modes Introduction
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. It is up to the user to select the mode that gives the best compromise between low-power consumption, short startup time and available wakeup sources.
The ultra-low-power STM32L476xx supports 7 low-power modes to achieve the best compromise between low-power consumption, short startup time, available peripherals and available wakeup 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.
There are two power consumption 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.
1.2. Objectives
- Create a simple project with Sleep mode and wakeup 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.
1.3. How to
- During this article, we will be using the code from EXTI Overview
2. Sleep mode
2.1. Definition
In sleep mode, the CPU clocks are 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
2.1.1. Configure the sleep mode
- Open the project from EXTI overview
- 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 Stop0 mode
- Add a function to resume the Systick on wakeup.
/* 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 */
2.1.2. Compile and flash
Compile the above code, then load it to the board flash memory.
2.1.3. Measure consumption
- Stop any debug session and do a full-power cycle
- Use an ammeter on the IDD connector (JP5 on NucleoF429ZI - for other boards check their user manual)
- Check the consumption while in Sleep mode
- Press the configured button on the Nucleo board to see the current consumption variation.
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 the regulator's operating current.
3.2. Configure the low power run mode
- Clock configuration
The system clock is limited to 2 MHz maximum. The MSI internal RC oscillator can be selected as it supports several frequency ranges
- Open the project from EXTI overview
- Open main.c
- Add a function to enter low power run mode.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(2000);
HAL_SuspendTick();
HAL_PWREx_EnableLowPowerRunMode();
HAL_ResumeTick();
}
/* USER CODE END 3 */
3.3. Compile and flash
3.4. Measure the current consumption
- Referring to the datasheet, in Shutdown mode at 25°C and VDD = 3V, the current consumption should be 64.1nA
- Using the STM32Cube Monitor-Power, we measured :
4. Stop0, Stop1 and Stop2 modes
4.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. File:Stop mode.jpg
4.2. Stop0 mode
Open the project from EXTI overview
- 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 Stop0 mode
- Add a function to resume the Systick on wakeup.
/* 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 */
4.2.1. Compile and flash
4.2.2. Measure the current consumption
- Stop any debug session and do a full-power cycle
- Use an amperemeter on the IDD connector (JP6 on NucleoL476 - for other boards check their user manual)
- Check the current consumption while in Stop mode
- Press the configured button on the Nucleo board to see the consumption variation.
To do so, check here the STM32 Nucleo expansion board for power consumption measurement User Manual UM2243.
- Referring to the datasheet, in Stop0 mode at 25°C and VDD = 3.6V, the current consumption should be 113μA
- Using the STM32Cube Monitor-Power, we measured :
4.3. Stop1 mode
Stop 1 offers the largest number of active peripherals and wakeup sources, a smaller wakeup time but a higher consumption than Stop 2.
- Change only this line in the code : HAL_PWREx_EnterSTOP1Mode(PWR_SLEEPENTRY_WFI);
4.3.1. Compile and flash
4.3.2. Measure the current consumption
- Referring to the datasheet, in Stop1 mode at 25°C and VDD = 3.6V, the current consumption should be 6.70μA
- Using the STM32Cube Monitor-Power, we measured :
4.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);
4.4.1. Compile and flash
4.4.2. Measure the current consumption
- Referring to the datasheet, in Stop2 mode at 25°C and VDD = 3.6V, the current consumption should be 1.26μA
- Using the STM32Cube Monitor-Power, we measured :
5. Standby mode
5.1. Definition
The Standby mode is used to achieve the lowest power consumption with BOR. 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.
The RTC can remain active (Standby mode with RTC, Standby mode without RTC).
The 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.
5.2. HAL library workflow summary
When exiting Standby mode, all registers in the VCORE domain are set to their reset value. File:StandBy HAL.png
5.3. Configure the Standby Mode
- Open the project from EXTI overview
- Open main.c
- Add the wakeup flag clear (without this, Standby mode is only 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_SuspendTick();
HAL_PWR_EnterSTANDBYMode();
HAL_ResumeTick();
}
/* USER CODE END 3 */
5.4. Compile and flash
5.5. Measure the current consumption
- Referring to the datasheet, in Standby mode at 25°C and VDD = 3V, the current consumption should be 150nA
- Using the STM32Cube Monitor-Power, we measured :
6. Shutdown mode
6.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.
File:shutdown mode.png
6.2. Configure the Shutdown Mode
- Open the project from EXTI overview
- 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_SuspendTick();
HAL_PWREx_EnterSHUTDOWNMode();
HAL_ResumeTick();
}
/* USER CODE END 3 */
6.3. Compile and flash
6.4. Measure the current consumption
- Referring to the datasheet, in Shutdown mode at 25°C and VDD = 3V, the current consumption should be 64.1nA
- Using the STM32Cube Monitor-Power, we measured :
7. Useful power monitoring tool
A dedicated tool exists for power consumption measurement : STM32CubeMonPwr