STM32 低功耗停机模式(STOP)管脚中断(INTERRUPT)唤醒实现及优化

STM32 低功耗停机模式(STOP)管脚中断(INTERRUPT)唤醒实现及优化

1. 介绍

STM32具有多种低功耗模式,当前以STM32L4系列的低功耗模式最为丰富,此处基于STM32L476和STM32CUBEIDE环境介绍停机模式(STOP)管脚中断唤醒的实现(HAL库)。STOP模式只是停止代码执行,唤醒(其实是continue的作用)后继续执行后面的代码,而不是重启之后从初始代码开始执行。

2. 低功耗模式

STM32L4的低功耗模式,相比其它系列的芯片,多了几种:
在这里插入图片描述
三种STOP模式的区别,可以参考文档:RM0351 Reference manual

STM32L4系列各种低功耗模式的特性总结如下:
在这里插入图片描述

3. 管脚中断配置

STOP模式可以通过通用管脚中断(Interrupt)或事件(Event)的方式唤醒,这里介绍通用管脚中断唤醒的方式, 选择一个GPIO管脚进行配置,这里用PC13作为唤醒源。

设置PC13为GPIO_EXTI方式:
在这里插入图片描述
为PC13选择其中一种中断触发方式,包括上升沿触发,下降沿触发和电平变化触发(即上升沿下降沿都触发):
在这里插入图片描述
然后使能中断:
在这里插入图片描述
保存后生成代码:
在这里插入图片描述

4. STOP模式进入及中断唤醒

通过HAL库函数可进入STOP模式,其中库函数有一个参数,指定唤醒时的来源,有两个可选:PWR_STOPENTRY_WFI 和 PWR_STOPENTRY_WFE, 分别对应管脚中断唤醒和管脚事件唤醒,这里我们用PWR_STOPENTRY_WFI,即管脚中断唤醒。

如果要进入STOP0模式,实施下面代码:

HAL_PWREx_EnterSTOP0Mode(PWR_STOPENTRY_WFI);

如果要进入STOP1模式,实施下面代码:

HAL_PWREx_EnterSTOP1Mode(PWR_STOPENTRY_WFI);

如果要进入STOP2模式,实施下面代码:

HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);

在执行到上述代码时,MCU就进入了STOP状态,等待管脚上发生信号变化产生的中断唤醒。在接受到中断唤醒后,会先进入中断处理函数,再出来执行进入STOP模式代码后面的代码。再PC13的管脚中断处理函数里,需要重新调用初始化时钟配置函数,以使得因为进入STOP模式关闭的时钟重新打开。PC13管脚的中断处理函数:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
   if (GPIO_Pin==GPIO_PIN_13)
    {
	  SystemClock_Config();
	}
}

这样STOP模式的进入和唤醒就实现了。

5. STOP模式进入及中断唤醒的优化

当设计中存在多种中断源,包括调试器中断和其它管脚中断时,这些中断不是想用于唤醒STOP状态的中断,此时就需要增加必要的设计,实现:

  1. 当指定的中断源产生中断时,唤醒STOP状态;
  2. 当非指定的中断源产生中断唤醒STOP后,立即重新进入STOP模式。

实现方式如下,先增加一个判断变量flag_recog:

uint8_t flag_recog = 0;

在进入STOP0模式时,采用如下代码:

flag_recog = 0;
while(flag_recog == 0) HAL_PWREx_EnterSTOP0Mode(PWR_STOPENTRY_WFI);

在进入STOP1模式时,采用如下代码:

flag_recog = 0;
while(flag_recog == 0) HAL_PWREx_EnterSTOP1Mode(PWR_STOPENTRY_WFI);

在进入STOP2模式时,采用如下代码:

flag_recog = 0;
while(flag_recog == 0) HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);

这样,当异常中断引起唤醒时,因为flag_recog保持为0,所以会再次执行进入STOP模式的代码。

当指定的唤醒中断到来时,如这里的PC13,则修改中断处理函数为:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if (GPIO_Pin==GPIO_PIN_13)
	{
		flag_recog = 1;
		SystemClock_Config();
	}
}

从而在中断处理函数里将flag_recog设置为非0,在退出中断处理函数后,也会退出while(flag_recog == 0)的循环,执行后面的代码。

通过以上优化,提高了STOP低功耗模式应用的可靠性,避免异常时间的唤醒。

6. 库函数对应关系

STM32L4系列将STOP模式分为了STOP0, STOP1和STOP2模式,实际上,STOP0和STOP1和以前的STOP模式在库函数上有对应关系。譬如以前的STOP模式的库函数在STM32L4的定义:

/**
  * @brief Enter Stop mode
  * @note  This API is named HAL_PWR_EnterSTOPMode to ensure compatibility with legacy code running
  *        on devices where only "Stop mode" is mentioned with main or low power regulator ON.
  * @note  In Stop mode, all I/O pins keep the same state as in Run mode.
  * @note  All clocks in the VCORE domain are stopped; the PLL, the MSI,
  *        the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  *        (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  *        after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  *        only to the peripheral requesting it.
  *        SRAM1, SRAM2 and register contents are preserved.
  *        The BOR is available.
  *        The voltage regulator can be configured either in normal (Stop 0) or low-power mode (Stop 1).
  * @note  When exiting Stop 0 or Stop 1 mode by issuing an interrupt or a wakeup event,
  *         the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  *         is set; the MSI oscillator is selected if STOPWUCK is cleared.
  * @note  When the voltage regulator operates in low power mode (Stop 1), an additional
  *         startup delay is incurred when waking up.
  *         By keeping the internal regulator ON during Stop mode (Stop 0), the consumption
  *         is higher although the startup time is reduced.
  * @param Regulator: Specifies the regulator state in Stop mode.
  *          This parameter can be one of the following values:
  *            @arg @ref PWR_MAINREGULATOR_ON  Stop 0 mode (main regulator ON)
  *            @arg @ref PWR_LOWPOWERREGULATOR_ON  Stop 1 mode (low power regulator ON)
  * @param STOPEntry: Specifies Stop 0 or Stop 1 mode is entered with WFI or WFE instruction.
  *          This parameter can be one of the following values:
  *            @arg @ref PWR_STOPENTRY_WFI  Enter Stop 0 or Stop 1 mode with WFI instruction.
  *            @arg @ref PWR_STOPENTRY_WFE  Enter Stop 0 or Stop 1 mode with WFE instruction.
  * @retval None
  */
void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
{
  /* Check the parameters */
  assert_param(IS_PWR_REGULATOR(Regulator));

  if(Regulator == PWR_LOWPOWERREGULATOR_ON)
  {
    HAL_PWREx_EnterSTOP1Mode(STOPEntry);
  }
  else
  {
    HAL_PWREx_EnterSTOP0Mode(STOPEntry);
  }
}

因此通过调用STOP模式的库函数,设置不同的输入参数,可以等效调用STOP0和STOP1模式。对于非STM32L4系列,可以调用HAL_PWR_EnterSTOPMode库函数进入STOP模式。

–End–

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PegasusYu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值