一. 认识睡眠模式
查看官方手册对睡眠模式的描述:
通过上图可以得出结论:
- 睡眠模式有 4 种电压调节器方案
- 在睡眠模式下 CPU 是停止状态
- 在睡眠模式下程序在 SRAM 执行情况下,Flash 可以被断电
- SRAM1 SRAM2 可以独立的开启或关闭
- 时钟都处于开启状态, 低功耗运行模式下 PLL 不能使用
- 根据电压调节器的选择,外设全部开启或者 USB_FS RNG 不能使用
- 所有的中断和事件都可以唤醒
- 根据选择的电压调节器,MCU 也有 4 种功耗,功耗与运行的频率成正比关系
- 唤醒时间 6 个时钟周期
SMPS (Switched-mode Power Supply)
二. 睡眠模式的进入
进入睡眠模式的方法很简单,直接调用 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
使用这个函数时,先看看 ST 的注释
/**
* @brief Enter Sleep or Low-power Sleep mode.
* @note In Sleep/Low-power Sleep mode, all I/O pins keep the same state as in Run mode.
* @param Regulator: Specifies the regulator state in Sleep/Low-power Sleep mode.
* This parameter can be one of the following values:
* @arg @ref PWR_MAINREGULATOR_ON Sleep mode (regulator in main mode)
* @arg @ref PWR_LOWPOWERREGULATOR_ON Low-power Sleep mode (regulator in low-power mode)
* @note Low-power Sleep mode is entered from Low-power Run mode. Therefore, if not yet
* in Low-power Run mode before calling HAL_PWR_EnterSLEEPMode() with Regulator set
* to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the
* Flash in power-down monde in setting the SLEEP_PD bit in FLASH_ACR register.
* Additionally, the clock frequency must be reduced below 2 MHz.
* Setting SLEEP_PD in FLASH_ACR then appropriately reducing the clock frequency must
* be done before calling HAL_PWR_EnterSLEEPMode() API.
* @note When exiting Low-power Sleep mode, the MCU is in Low-power Run mode. To move in
* Run mode, the user must resort to HAL_PWREx_DisableLowPowerRunMode() API.
* @param SLEEPEntry: Specifies if Sleep mode is entered with WFI or WFE instruction.
* This parameter can be one of the following values:
* @arg @ref PWR_SLEEPENTRY_WFI enter Sleep or Low-power Sleep mode with WFI instruction
* @arg @ref PWR_SLEEPENTRY_WFE enter Sleep or Low-power Sleep mode with WFE instruction
* @note When WFI entry is used, tick interrupt have to be disabled if not desired as
* the interrupt wake up source.
* @retval None
*/
- 这个函数时让 MCU 进入睡眠模式或者低功耗睡眠模式
- 睡眠模式和低功耗睡眠模式,所有的 I/O 的状态都和运行模式时是一样的
- Regulator 可以传入的参数有
2
个。 PWR_MAINREGULATOR_ON 调节器在主模式。PWR_LOWPOWERREGULATOR_ON 调节器在低功耗模式 - 从低功耗运行模式切换到低功耗睡眠模式。如果设置调节器在为 LPR 之前,系统时钟要降低到 2MHZ以下
- 当退出低功耗睡眠模式后,MCU 会进入低功耗运行模式,如果要切换到运行模式,则需要通过HAL_PWREx_DisableLowPowerRunMode() 来退出低功耗运行模式
- 进入睡眠模式的方式。WFI 还是 WFE
- 当使用 WFI 进入睡眠模式前,必须要关闭
tick
的中断,不然tick
的中断会唤醒 MCU
三. 睡眠模式的进入代码
进入睡眠模式的代码实现
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
HAL_Delay(500); // 上电之后延时一会再进入睡眠模式,这样可以保证复位之后可以立即下载程序
HAL_SuspendTick(); // 关闭 tick 中断,防止唤醒睡眠状态的 MCU
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
HAL_ResumeTick();
while (1)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_13);
HAL_Delay(500);
}
}
代码解析:
- 进入睡眠模式后将导致调试器无法识别到
MCU
,所以在启动的时候加了一个HAL_Delay(500)
,如果没有加,则需要按住复位键点击下载,点击下载之后立即松开复位键,就可以正常的下载程序了。 - HAL 生成的代码默认将
systick
的中断设置为 1KHZ.进入睡眠模式前需要关闭 tick 中断,防止唤醒睡眠状态的 MCU - 让
MCU
进入睡眠模式HAL_PWR_EnterSLEEPMode
- 唤醒
MCU
之后,将执行进入睡眠模式的下一个指令。所以要立刻打开tick
中断
通过这个表可以看到,睡眠模式支持WFI
和WFE
WFI: 立刻进入低功耗模式
WFE: 不是立刻进入低功耗模式,根据Event Register(一个单bit的寄存器,每个PE一个)的状态,有两种情况:如果Event Register为1,该指令会把它清零,然后执行完成(不会standby)
选择WFE
可是使用如下代码
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE);
四. 总结
- 进入睡眠模式是
MCU
停止,唤醒之后无需重新配置时钟 - 睡眠模式唤醒后,无需对外设重新进入初始化
- 睡眠模式支持 中断唤醒 和 事件唤醒
USB_FS
RNG
不能使用