STM32超低功耗入门之睡眠模式

一. 认识睡眠模式

查看官方手册对睡眠模式的描述:
在这里插入图片描述
在这里插入图片描述

通过上图可以得出结论:

  1. 睡眠模式有 4 种电压调节器方案
  2. 在睡眠模式下 CPU 是停止状态
  3. 在睡眠模式下程序在 SRAM 执行情况下,Flash 可以被断电
  4. SRAM1 SRAM2 可以独立的开启或关闭
  5. 时钟都处于开启状态, 低功耗运行模式下 PLL 不能使用
  6. 根据电压调节器的选择,外设全部开启或者 USB_FS RNG 不能使用
  7. 所有的中断和事件都可以唤醒
  8. 根据选择的电压调节器,MCU 也有 4 种功耗,功耗与运行的频率成正比关系
  9. 唤醒时间 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
  */
  1. 这个函数时让 MCU 进入睡眠模式或者低功耗睡眠模式
  2. 睡眠模式和低功耗睡眠模式,所有的 I/O 的状态都和运行模式时是一样的
  3. Regulator 可以传入的参数有 2 个。 PWR_MAINREGULATOR_ON 调节器在主模式。PWR_LOWPOWERREGULATOR_ON 调节器在低功耗模式
  4. 从低功耗运行模式切换到低功耗睡眠模式。如果设置调节器在为 LPR 之前,系统时钟要降低到 2MHZ以下
  5. 当退出低功耗睡眠模式后,MCU 会进入低功耗运行模式,如果要切换到运行模式,则需要通过HAL_PWREx_DisableLowPowerRunMode() 来退出低功耗运行模式
  6. 进入睡眠模式的方式。WFI 还是 WFE
  7. 当使用 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);
  }
}

代码解析:

  1. 进入睡眠模式后将导致调试器无法识别到 MCU,所以在启动的时候加了一个 HAL_Delay(500) ,如果没有加,则需要按住复位键点击下载,点击下载之后立即松开复位键,就可以正常的下载程序了。
  2. HAL 生成的代码默认将 systick 的中断设置为 1KHZ.进入睡眠模式前需要关闭 tick 中断,防止唤醒睡眠状态的 MCU
  3. MCU 进入睡眠模式 HAL_PWR_EnterSLEEPMode
  4. 唤醒 MCU 之后,将执行进入睡眠模式的下一个指令。所以要立刻打开 tick 中断
    在这里插入图片描述
    通过这个表可以看到,睡眠模式支持 WFIWFE
    WFI: 立刻进入低功耗模式
    WFE: 不是立刻进入低功耗模式,根据Event Register(一个单bit的寄存器,每个PE一个)的状态,有两种情况:如果Event Register为1,该指令会把它清零,然后执行完成(不会standby)
    选择 WFE 可是使用如下代码
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE);

四. 总结

  1. 进入睡眠模式是 MCU 停止,唤醒之后无需重新配置时钟
  2. 睡眠模式唤醒后,无需对外设重新进入初始化
  3. 睡眠模式支持 中断唤醒事件唤醒
  4. USB_FS RNG 不能使用
  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值