Getting started with PWR

Revision as of 13:26, 30 November 2022 by Registered User (→‎Measure the current consumption)

This article explains Sleep, Stop, Standby and 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.

Info white.png Information
In this article, we will be using the Nucleo STM32L476 for all the demos.

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

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

File:sleep mode.png

2.1.1. Using Sleep mode with EXTI​

2.1.1.1. Objective
  • Learn how to set up Sleep mode in the HAL​
  • Create a simple project with Sleep mode and wakeup when a button (configured in the EXTI setup) is pressed.​
  • Verify the correct functionality by measuring the current consumption​.
2.1.1.2. How
2.1.1.3. Configure the code of Stop0, Stop1 and Stop2 modes

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 */
2.1.1.4. Compile and flash

Compile the above code, then load it to the board flash memory.

2.1.1.5. 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. Stop0, Stop1 and Stop2 modes

3.1. Definition

Stop mode achieves the lowest power consumption while retaining the content oSRAM 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

3.1.1. Configure the code of Stop0, Stop1 and Stop2 modes

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 */
Info white.png Information
After using the WFI and entering Stop0, Stop1 or Stop 2 mode, the code in the handler will be executed.


3.1.2. Compile and flash
  • Click on Build button Built.png
  • Or on Run button (to execute) Run.png
3.1.3. 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)

File:capture.png

  • Check the current consumption while in Stop mode
  • Press the configured button on the Nucleo board to see the consumption variation.
Info white.png Information
It is possible to measure the current consumption with STM32Cube Monitor-Power

To do so, check the STM32 Nucleo expansion board for power consumption measurement User Manual UM2243 here.

4. Standby mode

4.1. Definition

File:standby mode.png

4.1.1. Use Standby mode

4.1.1.1. Objective
  • For this lab, create an STM32CubeMX project
  • For testing purposes, enable LED PG14 on the NucleoF429ZI (for other boards check their user manual)
  • Learn how to set up Standby mode in the HAL
  • Create a simple project with Standby mode and wakeup when the associated button is pressed.
4.1.1.2. Goals
  • Learn how to set up Standby mode in the HAL, and which events can wake up you
  • Verify the correct functionality by measuring the current consumption.
4.1.1.3. HAL library workflow summary

File:StandBy HAL.png

4.1.1.4. 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 */
  • Turn on the LED
  • Wait 2 seconds
  • Enable the wakeup pin (which is PA0 and is connected to the blue button on the NucleoF429ZI)
  • Add a function to enter Standby mode.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
    HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_14);
    HAL_Delay(2000);
    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
    HAL_PWR_EnterSTANDBYMode();
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
4.1.1.5. Compile and flash

Compile the above code, then load it to the board flash memory.

4.1.1.6. 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 current consumption while in Standby mode
  • Press the blue Nucleo button to see the consumption variation.

5. Useful power monitoring tool

A dedicated tool exists for power consumption measurement : STM32CubeMonPwr