stm32单片机使用RT-Thread/Freertos无法进入sleep/stop模式

问题

stm32的单片机使用ide工具cubeide或cubemx生成工程,提示共用hal和sys共用时基建议更换时基,我换了tim16,低功耗stop操作:

void stop_do(void)
{
	printf("in stop");
	HAL_SuspendTick();
	HAL_PWREx_EnterSTOP1Mode(PWR_STOPENTRY_WFI);
	SystemClock_Config();
	HAL_ResumeTick();
	printf("out stop");
}

实际跑起来就是不断打印 in stopout stop 也就是没有进入等待

原因

1、我尝试hal与sys共用systick中断作为时基,这个代码就立马生效了,但是不建议共用具体原因参考STM32 HAL库:FreeRTOS系统 (带推荐使用除了Systick以外的时钟源问题及解决)
2、继续往下分析,共用时基可以进stop说明是我不共用时基的操作引起无法进入stop的,继续查发现我使用tim16作为hal时基后HAL_SuspendTickHAL_ResumeTick被改写了操作是关闭和打开tim16定时器中断,查找改写之前的week函数内容是:

/**
  * @brief Suspend Tick increment.
  * @note In the default implementation , SysTick timer is the source of time base. It is
  *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  *       is called, the SysTick interrupt will be disabled and so Tick increment
  *       is suspended.
  * @note This function is declared as __weak to be overwritten in case of other
  *       implementations in user file.
  * @retval None
  */
__weak void HAL_SuspendTick(void)
{
  /* Disable SysTick Interrupt */
  SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
}

/**
  * @brief Resume Tick increment.
  * @note In the default implementation , SysTick timer is the source of time base. It is
  *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  *       is called, the SysTick interrupt will be enabled and so Tick increment
  *       is resumed.
  * @note This function is declared as __weak to be overwritten in case of other
  *       implementations in user file.
  * @retval None
  */
__weak void HAL_ResumeTick(void)
{
  /* Enable SysTick Interrupt */
  SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
}

内容很好理解就是把systick中断挂起,也就是修改hal时基后调用的函数操作是没法挂起systick中断的,所以这就是为什么stop模式进去就马上出来了,是systick的1ms中断导致的退出知道了原因解决方法也很简单,加两行代码就行了:

void stop_do(void)
{
	printf("in stop");
	SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
	HAL_SuspendTick();
	HAL_PWREx_EnterSTOP1Mode(PWR_STOPENTRY_WFI);
	SystemClock_Config();
	HAL_ResumeTick();
	SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
	printf("out stop");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值