默认情况下,微控制器(MCU)在系统复位或电源复位后处于运行模式。在这种模式下,微控制器会执行程序代码,并提供时钟给CPU,使其能够正常工作。然而,当系统不需要CPU持续运行时,例如在等待外部事件或处于空闲状态时,为了节省功耗,可以选择进入低功耗模式。微控制器通常提供多种低功耗模式供用户选择,这些模式在功耗、启动时间和唤醒源方面各有特点。
进入停止模式,所有的时钟都关闭,于是所有的外设也停止了工作。但是内核域的 VCORE电源是没有关闭的,所以内核的寄存器和内存信息都保留下来,等待重新开启时钟就可以从上停止的地方继续执行程序。停止模式可以被任何一个外部中断(EXTI)或事件唤醒。在停止模式中可以选择稳压器的工作方式为主模式或者低功耗模式。
API: void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry); 这个函数只能进入 STOP ModeRegulator : 电压域的选择。可选择的有两个,PWR_MAINREGULATOR_ON 和 PWR_LOWPOWERREGULATOR_ON。
SLEEPEntry : 睡眠模式进入的方式。两个选项,PWR_SLEEPENTRY_WFI 和 PWR_SLEEPENTRY_WFE
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* STM32H5xx HAL library initialization:
- - Systick timer is configured by default as source of time base, but user
- can eventually implement his proper time base source (a general purpose
- timer for example or other time source), keeping in mind that Time base
- duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
- handled in milliseconds basis.
- - Set NVIC Group Priority to 4
- - Low Level Initialization
- */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* Configure LED1, and LED2 */
- BSP_LED_Init(LED1);
- BSP_LED_Init(LED2);
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_ICACHE_Init();
- /* USER CODE BEGIN 2 */
- /* Enable Systick interruption for HAL */
- LL_SYSTICK_EnableIT();
- /* USER push-button (External line 13) will be used to wakeup the system from STOP mode */
- BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
- /* Ensure that HSI is wake-up system clock */
- __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
-
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- /* Insert 5 second delay */
- HAL_Delay(5000);
- /* Turn OFF LED's */
- BSP_LED_Off(LED1);
- BSP_LED_Off(LED2);
- /* Enter STOP mode */
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
-
- /* ... STOP mode ... */
- /* At Stop mode exit, enable and select PLL as system clock source
- (PLL is disabled in STOP mode) */
- SYSCLKConfig_FromSTOP();
- }
- /* USER CODE END 3 */
- }
待机模式可实现最低功耗。
- 待机模式在保持欠压复位(BOR)功能的同时,实现了最低的功耗。
- 内部调节器被关闭,从而关闭了核心域的电源。
- 所有振荡器(PLL、HSI、HSI48、CSI、HSE)都被关闭。
- RTC可以保持活动,允许基于警报、周期性唤醒或时间戳的唤醒。
- 待机模式下,I/O引脚的状态(除了那些被待机模式使用的引脚)可以被保留。
- 进入待机模式后,除了备份域和待机电路中的寄存器和备份SRAM之外,SRAM和寄存器的内容将丢失。
- 设备可以通过外部复位(NRST引脚)、独立看门狗(IWDG)复位、WKUP引脚事件(可配置上升沿或下降沿)、RTC事件或篡改检测来退出待机模式。
API: void HAL_PWR_EnterSTANDBYMode(void) 立刻进入待机模式;
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- HAL_PWR_EnableBkUpAccess();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_ICACHE_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /* Initialize leds */
- BSP_LED_Init(LED_GREEN);
- /* Initialize USER push-button, will be used to trigger an interrupt each time it's pressed.*/
- BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
- // /* Initialize COM1 port (115200, 8 bits (7-bit data + 1 stop bit), no parity */
- // BspCOMInit.BaudRate = 115200;
- // BspCOMInit.WordLength = COM_WORDLENGTH_8B;
- // BspCOMInit.StopBits = COM_STOPBITS_1;
- // BspCOMInit.Parity = COM_PARITY_NONE;
- // BspCOMInit.HwFlowCtl = COM_HWCONTROL_NONE;
- // if (BSP_COM_Init(COM1, &BspCOMInit) != BSP_ERROR_NONE)
- // {
- // Error_Handler();
- // }
- // /* USER CODE BEGIN BSP */
- // /* -- Sample board code to send message over COM1 port ---- */
- // printf("Welcome to STM32 world !\n");
- //
- //
- // led_gpio_init();
- // sw_gpio_init();
- BSP_LED_On(LED_GREEN);
- HAL_Delay(5000);
-
-
- while (1)
- {
- /* -- Sample board code for User push-button in interrupt mode ---- */
- // if (BspButtonState == BUTTON_PRESSED)
- // {
- // /* Update button state */
- // BspButtonState = BUTTON_RELEASED;
- // BSP_LED_On(LED_GREEN);
- //
- //
- // }
- BSP_LED_On(LED_GREEN);
- HAL_Delay(500);
- /*Suspend Tick increment to prevent wakeup by Systick interrupt.
- Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
- HAL_SuspendTick();
- BSP_LED_Off(LED_GREEN);
- /* Enter Sleep Mode , wake up is done once USER push-button is pressed */
- // HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
- HAL_PWR_EnterSTANDBYMode();
- /* Resume Tick interrupt if disabled prior to SLEEP mode entry */
- HAL_ResumeTick();
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
|