STM32 Deep Sleep (Low Power Sleep) Mode & Wakeup

In this tutorial, we’ll discuss The STM32 Deep Sleep (Low Power Sleep) Mode, how to enter the low power sleep 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 Deep Sleep (Low Power Sleep) Mode
  2. STM32 Enter Deep Sleep (Low Power Sleep) Mode
  3. STM32 Exit Deep Sleep (Low Power Sleep) Mode
  4. STM32 Deep Sleep (Low Power Sleep) Example Code Project
  5. Wrap Up

STM32 Deep Sleep (Low Power Sleep) Mode

The STM32 Low Power Sleep Mode (Deep Sleep) is entered from the Low Power Run Mode. It’s a combination of Low Power Run (LPR) Mode and Sleep Mode to achieve even less current consumption than both modes. According to the following state diagram that shows all the possible transitions for STM32 low-power modes, we first need to go from “Run Mode” to “Low Power Run Mode”, then we go into “Low Power Sleep (Deep Sleep) Mode”.

STM32 Low Power Modes Transitions

1) Go to Low Power Run Mode

Low Power Run 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 Sleep Mode

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

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

2) Go to Low Power Sleep (Deep Sleep) Mode

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.

Unlike Sleep mode, in Low-power sleep mode (Deep Sleep): the main regulator is OFF and the low-power regulator is ON.

For the STM32L432KC target microcontroller, in Low-Power Sleep (Deep Sleep) Mode, the current consumption is:

  • At 2MHz: 72µA
  • At 100kHz: 23µA
❕ Note

Unlike the STM32 sleep mode, while the target MCU is in the deep sleep (low-power sleep) mode, it’s not possible to start a debugging session. Due to the fact that the CPU core is no longer clocked during low-power mode, so debugging features are disabled.


STM32 Enter Deep Sleep (Low Power Sleep) Mode

To enter the low-power sleep (deep sleep) mode, we must first go into low-power run (LPR) mode. Then we can move into low-power sleep (deep sleep) mode.

#1

To go from Run mode to Low Power Run Mode, 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.

#2

Then, we can move from the low-power run mode to the low-power sleep (deep sleep) mode using the HAL_PWR_EnterSLEEPMode() function.

Here is a code example for entering the STM32 Deep Sleep (Low Power Sleep) Mode:

❕ Note

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

❕ Note

You need to Stop The SysTick Timer as it will wake up the CPU every 1ms through its interrupt signal. And don’t forget to resume its operation upon waking up to make sure any systick-dependent software components will continue to operate normally.


STM32 Exit Deep Sleep (Low Power Sleep) 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.

#1

Upon receiving an interrupt signal, the CPU automatically wakes up from the low-power sleep (deep sleep) mode and gets back into the low-power run (LPR) mode. Therefore, we need to re-enable the Systick timer using the HAL_ResumeTick() function after waking up from sleep.

#2

To completely reverse the effect of going into deep sleep mode, we still need to get out of the low power run (LPR) mode back to the normal run mode at full speed (i.e. 80MHz, or whatever). To do this, we need to switch the clock source back from the low-frequency MSI to the HSI-PLL (@ 80MHz, or whatever your original system’s clock was).

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


STM32 Deep Sleep (Low Power Sleep) Example Code Project

In this example project, we’ll configure our STM32L432KC microcontroller to run at full speed (80MHz) for 1 second. Then it’ll automatically switch to the low-power sleep (deep sleep) mode.

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 operation will be observed using a GPIO output pin (PB3) going to an LED that keeps toggling inside the main while(1) loop. It’s better to use a power profiler that can measure the board’s current consumption.

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

Configure a GPIO output pin (PB3) that goes to an LED which indicates whether the CPU is running or sleeping currently.

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 Deep Sleep (Low Power Sleep) Example Code

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

STM32 Deep Sleep (Low Power Sleep) Mode Example Testing

To test this project, you’d better use a power profiler kit that can measure extremely low currents (in µA or even nA range). Development boards are not ideal for such application testing because they typically have onboard sensors, LEDs, and a bunch of extra passive/active electronics that draw a significant amount of current compared to the MCU under test.

❕ Note

If you’re using a different target STM32 microcontroller, or you’re willing to use different clock settings than mine (STM32L432KC @ 80MHz). 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 sleep mode!
  8. Use the clock initialization code snippets you’ve copied so far and replace the contents of the provided example in both Enter_LowPowerSleepMode() and Exit_LowPowerSleepMode() 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 sleep (Deep Sleep) 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