STM32 Low Power Run Mode (Example Code)

In this tutorial, we’ll discuss The STM32 Low Power Run Mode (LPR), how to enter the low power mode, and how to exit from it with some code examples and a full test project. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 Low Power Run Mode
  2. STM32 Enter Low Power Run Mode
  3. STM32 Exit Low Power Run Mode
  4. STM32 Low Power Run Mode Example Code Project
  5. Wrap Up

STM32 Low Power Run Mode

This mode is achieved by reducing the system clock frequency (SYSCLK) 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.

STM32 Low Power Run Mode

For the STM32L432KC target microcontroller, in Low-Power Run Mode, the current consumption is:

  • At 2MHz: 211µA
  • At 100kHz: 30µA
❕ Note

After entering this mode, the CPU has no way to check the clock frequency. This means you can “theoretically” increase it a bit more beyond the 2MHz limit. However, it’s not recommended by the datasheet nor guaranteed to work in a stable manner.


STM32 Enter Low Power Run Mode

To enter the low power mode, we must first set the SYSCLK to 2MHz or less.

This can be done by selecting another clock source than the HSI/PLL which is typically way above this limit. The MSI clock source may be a good candidate for this purpose.

Here is a code example for entering the STM32 low-power run mode:

❕ Note

Note that the HAL_PWREx_EnableLowPowerRunMode() function should only be called after switching the SYSCLK down to 2MHz or less.


STM32 Exit Low Power Run Mode

To exit from the low-power run mode, we can use any signal we want since the microcontroller is essentially running but at a much lower speed. EXTI pin interrupts are available, timer interrupts, or whatever your system needs.

This is an example code for exiting the low-power run mode upon receiving an EXTI interrupt on the GPIO1 line.


STM32 Low Power Run Mode Example Code Project

In this example project, we’ll configure our STM32L432KC microcontroller to run at full speed (80MHz) for 3 seconds. Then it’ll automatically switch to the low power run mode to run at 2MHz.

Upon receiving an EXTI interrupt on the GPIO1 pin from a push button, the system will switch back to the original full-speed run operation (80MHz). And it’ll keep toggling between both modes each time the interrupt button is pressed.

The System clock will be observed using a global variable and the HAL_RCC_GetSysClockFreq() function.

Step #1

Open STM32CubeMX, create a new project, and select the target microcontroller. For me, it’s (STM32L432KC)

Step #2

Configure a GPIO pin in EXTI input pull-down mode (button). I’ll use A1 pin (EXTI1), you can choose any EXTI-capable pin.

Enable The EXTI interrupt from the NVIC configuration tab.

STM32 Low Power Run Mode EXTI Pin Exit

Step #3

Go to the Clock configuration page and select the internal HSI+PLL as a clock source to give you the maximum SysClk of 80MHz (in my case). This is the base full-speed run mode.

When I want to switch to the low-power run mode, I’ll need to use a 2MHz clock source. For that purpose, I’ll use the internal MSI clock source. As indicated in the screenshot below. There are two paths for the clock that feeds my SysClk with 80MHz or 2MHz.

STM32 Low Power Run Mode Example Clock Configuration

Step #4

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 Low Power Run Mode Example Code

Here is The Application Code For This LAB (main.c)

STM32 Low Power Run Mode Example Testing

To test this project, you can place two break points in the locations indicated below and keep clicking on the run button in the debug menu while monitoring the SysClk value in the live expressions window.

You can see that the system starts at full speed (80MHz), then after 3s, it goes into low-power run mode, and the SysClk becomes (2MHz). By pressing the A0 (EXTI1) push button, your system will get out of the low-power run mode back to its full-speed run mode. Further button clicks will keep the system toggling between full-speed run mode and low-power run mode.

STM32 Low Power Run Mode Code Example Project

❕ Note

If you’re using a different target STM32 microcontroller, or you’re willing to use different clock settings than mine. Let’s say you’d like to run at 48MHz in full-speed mode and 100kHz in low-power run mode. Then you can do the following:

  1. Configure your clock in CubeMX to the desired full speed (i.e. 48MHz, 100MHz, 280MHz, or whatever)
  2. Generate the code
  3. Copy the contents of the SystemClock_Config() function
  4. Go back to CubeMX and select the desired low-frequency source (i.e. MSI 100kHz, 1MHz, or whatever value below 2MHz)
  5. Generate the code
  6. Copy the contents of the SystemClock_Config() function again
  7. Now you’ve got the clock configuration code for both cases of going into and out of the low-power run mode!
  8. Use the clock initialization code snippets you’ve copied so far and replace the contents of the provided example in both Enter_LowPowerRunMode() and Exit_LowPowerRunMode() functions.

Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve explored the STM32 low power run mode, what it does to reduce the current consumption, and what are the current consumption numbers expected from this mode using the (STM32L432KC) target microcontroller.

You can build on top of the provided example code project and integrate it into your system. You can also check the rest of the tutorials in this series to learn more about other low-power modes in STM32 microcontrollers.

This Tutorial is Part of The Following Multi-Part Tutorial Series:

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment