问题
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 stop 和 out stop 也就是没有进入等待
原因
1、我尝试hal与sys共用systick中断作为时基,这个代码就立马生效了,但是不建议共用具体原因参考STM32 HAL库:FreeRTOS系统 (带推荐使用除了Systick以外的时钟源问题及解决)
2、继续往下分析,共用时基可以进stop说明是我不共用时基的操作引起无法进入stop的,继续查发现我使用tim16作为hal时基后HAL_SuspendTick和HAL_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");
}