2024-07-30 08:01 AM
Some parts of my application are driven by the SysTick timer, and I got into the practice of adding `HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI)` while polling a flag, knowing that the SysTick interrupt will wake the microcontroller and update the flag as needed.
This got me thinking, can I do the same in the `HAL_Delay` function? The implementation of `HAL_Delay` is this:
__weak void HAL_Delay(uint32_t Delay)
{
uint32_t tickstart = HAL_GetTick();
uint32_t wait = Delay;
/* Add a freq to guarantee minimum wait */
if (wait < HAL_MAX_DELAY)
{
wait += (uint32_t)(uwTickFreq);
}
while((HAL_GetTick() - tickstart) < wait)
{
}
}
Can I change it to this:
__weak void HAL_Delay(uint32_t Delay)
{
uint32_t tickstart = HAL_GetTick();
uint32_t wait = Delay;
/* Add a freq to guarantee minimum wait */
if (wait < HAL_MAX_DELAY)
{
wait += (uint32_t)(uwTickFreq);
}
while((HAL_GetTick() - tickstart) < wait)
{
HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
}
}
with no problem? I need to be aware of something? Is there any reason why the HAL does not do it by default?
Looking at the datasheet of the microcontroller I am using (STM32F405), the wakeup time in this situation is typically 13 us, which I think is not that bad (correct me if I am wrong).
I would even use the sleep mode (which has a typical wakeup time of 5 cycles, or ~24ns at 168MHz), but `HAL_PWR_EnterSLEEPMode`'s doc-comment states that the SysTick is stopped (although the source code of the function doesn't appear to stop the SysTick, but I am not sure I can rely on that).