laitimes

STM32 learns 13 low-power modes with wake-up

author:Programming circles

1. Introduction

1. Introduction to the STM32 low-power mode

STM32 microcontrollers offer a variety of low-power modes to minimize power consumption in applications where energy savings are required. By default, after a system reset or power-on reset, the microcontroller goes into run mode. In run mode, HCLK provides a clock for the CPU and executes the program code.

When the MCU is not in operation, it is possible to take advantage of the low-power mode to save power. The STM32 has a variety of low-power modes, allowing users to choose the best low-power mode based on power consumption, startup speed, and wake-up resources.

2. Common low-power modes

STM32 microcontrollers offer a variety of low-power modes to minimize power consumption in applications where energy savings are required. Here are some common STM32 low-power modes and their features:

(1)睡眠模式 (Sleep Mode):

  • Function: In this mode, the CPU pauses execution and most of the internal devices are shut down. Only some key peripherals (e.g. RTC, watchdog timer) may still be active.
  • Features: Extremely low power consumption, but the system can still wake up quickly when triggered by an external event.

(2)停止模式 (Stop Mode):

  • Function: In this mode, the CPU and most of the peripherals are stopped, only the RTC and some external interrupts are still active. Stop mode consumes less power than sleep mode, but takes slightly longer to wake up.
  • Features: Extremely low power consumption and long-term sleep state.

(3)待机模式 (Standby Mode):

  • Function: This is one of the lowest power modes, the core 1.8V power is off, almost all devices are turned off, and only the RTC and a small number of external wake-up sources are active.
  • Features: Extremely low power consumption in standby mode, but relatively long wake-up time.

    The following table shows a comparison of several low-power modes from the STM32F1xx Chinese Reference Manual:

2. Sleep mode

1. Enter sleep mode

When entering sleep mode, the Cortex-M3™ core stops, and all peripherals including the peripherals of the Cortex-M3 core, such as NVIC, system

Clocks (SysTick) and others are still running.

It can be put to sleep by executing WFI or WFE commands.

There are two mechanisms for entering sleep mode:

  1. SLEEP-NOW: IF THE SYSTEM CONTROL REGISTER SLEEPONEXIT BIT IS CLEARED, THE MCU IMMEDIATELY ENTERS SLEEP MODE WHEN WRI OR WFE IS EXECUTED;
  2. SLEEP-ON-EXIT: IF THE SLEEPONEXIT BIT IS SET, THE MCU IMMEDIATELY ENTERS SLEEP MODE WHEN THE SYSTEM EXITS FROM THE LOWEST-PRIORITY INTERRUPT HANDLER.

2. Wake up

  • If the WFI command is executed to enter sleep mode, any external interrupt responded by the nested vector interrupt controller can wake the system from sleep mode;
  • If the WFE command is executed to go to sleep, the MCU will wake up once a wake-up event occurs.

Comparison of entry and exit of the two modes:

mode illustrate
SLEEP-NOW enters

Perform WFI (Waiting for Interruption) or under the following conditions

WFE (Wait for Event) Instruction:

– SLEEPDEEP = 0 AND

– SLEEPONEXIT = 0

Refer to the Cortex-M3 system control register

SLEEP-NOW EXITS

If WFI is performed to enter sleep mode:

Interrupts: Refer to the Interrupt Vector Scale (Table 54)

If you go into sleep mode by performing WFE:

Wake Events: Refer to Wake Up Event Management (Section 9.2.3)

SLEEP-ON-EXIT进入

Execute WFI instructions under the following conditions:

– SLEEPDEEP = 0 AND

– SLEEPONEXIT = 1

Refer to the Cortex-M3™ system control register

SLEEP-ON_EXIT退出 Interrupts: Refer to the Interrupt Vector Table

3. Code implementation

The following uses the key interrupt to respond to the event into a low-power mode, which can be woken up with a higher level of event. The operation effect is that the digital tube displays 0-9 digits, and when the "down" of the development board is pressed, the MCU sleeps, and the digital tube no longer changes. When you press the right button of the board again, the MCU exits hibernation.

(1)exti_utils.c

/**
 * @brief  外部中断3中断服务函数
*/
void EXTI3_IRQHandler(void) {
    if (EXTI_GetITStatus(EXTI_Line3) != RESET) {
        delay_ms(10);
        if (key_down_value  == 0) {
					 __WFI();
           // led_lightn(1);
            EXTI_ClearITPendingBit(EXTI_Line3); // 清除中断标志位
            return;
        }
    }
}
/**
 * @brief  外部中断4中断服务函数
*/
void EXTI4_IRQHandler(void) {
    if (EXTI_GetITStatus(EXTI_Line4) != RESET) {
        delay_ms(20);
        if (key_right_value  == 0) {
            //led_lightn(3);
					
					  if(EXTI_GetFlagStatus(EXTI_Line4) == SET){
								printf("exit sleep\r\n");
						}
						EXTI_ClearITPendingBit(EXTI_Line4); // 清除中断标志位
            return;
        }
    }
}

           

(2)main.c

#include "gpio_utils.h"
#include "stm32f10x.h"
#include "sys_tick_utils.h"
#include "led_utils.h"
#include "usart_utils.h"
#include "stdio.h"
#include "exti_utils.h"

// 主函数
int main(void)
{
	GPIO_Configuration(); // 调用GPIO配置函数
	// tick 初始化
	sys_tick_init(72);
	led_all_off();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	USART3_Init(9600);
	printf("starting...");

	// led 初始化
	custom_led_init();
	custom_exti_init();

	int i = 0;
	
	while (1) // 无限循环
	{
		delay_ms(990);
		printf("i=%d", i);
		led_lightn(i);
		i++;
		if(i>9){
			i=0;
		}
		
	}
}

           

3. Shutdown mode

On the basis of sleep mode, the shutdown mode turns off the clock in all 1.8V areas, and the operation status information is saved before stopping, and the program can continue to run after recovery.

Downtime mode is not covered in detail in this article.

Fourth, standby mode

On the basis of the shutdown mode, the operating information is no longer saved, the system resets the program from the beginning, and the power control/status registers PWR_CSR indicate that the kernel exits from standby.

1. How to enter standby mode

  • SET THE SLEEPDEEP BIT OF THE SYSTEM CONTROL REGISTER;
  • Set the PDDS bits in the power control register PWR_CR;
  • Clear the WOF bits in the power control/status registers (PWR_CSR).

The steps are as follows:

  1. Enable the power supply clock
  2. Set the wake-up source
  3. Enter standby mode

2. How to exit standby mode

  • the rising edge of the WKUP pin;
  • the rising edge of the RTC alarm event;
  • External reset on NRST pin;
  • IWDG reset.

In standby mode, most of the IO pins are in a high-impedance state, and the reset pin, TAMPER (PC13) pin, and WKUP (PA0) pin are available.

As you can see from the schematic diagram of the experimental board, the WKUP pin has been connected to the K_UP of the button:

STM32 learns 13 low-power modes with wake-up

The following example will use KEY_UP to wake up the MCU.

3. Code implementation

The example program below is similar to the previous example, with LEDs displaying numbers 0-9 in order. When the "Down" button is pressed, it enters standby mode.

Wake up by pressing the K_UP button. Unlike the sleep mode, the digital tube is no longer displayed after entering the standby mode.

Note that to use the relevant library functions, you need to reference the stm32f10x_pwr.c file.

STM32 learns 13 low-power modes with wake-up

(1) Enter standby mode

standby_utils.c

#include "standby_utils.h"

/**
* @brief  进入待机模式
*/
void Standby_Enter(void){
	// ¿ªÆôʱÖÓ
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
	// ÉèÖû½ÐÑÔ´,ʹÓÃWakeUpPin»½ÐÑ
	PWR_WakeUpPinCmd(ENABLE);
	// Çå¿Õ±êÖ¾
	PWR_ClearFlag(PWR_FLAG_WU);
	// ½øÈë´ý»úģʽ
	PWR_EnterSTANDBYMode();
}

           

(2) Main functions

#include "gpio_utils.h"
#include "stm32f10x.h"
#include "sys_tick_utils.h"
#include "led_utils.h"
#include "usart_utils.h"
#include "stdio.h"
#include "exti_utils.h"

// 主函数
int main(void)
{
	GPIO_Configuration(); // 调用GPIO配置函数
	// tick 初始化
	sys_tick_init(72);
	led_all_off();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	USART3_Init(9600);
	printf("starting...");

	// led 初始化
	custom_led_init();
	custom_exti_init();

	int i = 0;
	
	while (1) // 无限循环
	{
		delay_ms(990);
		printf("i=%d", i);
		led_lightn(i);
		i++;
		if(i>9){
			i=0;
		}
		
	}
}

           

Read on