cancel
Showing results for 
Search instead for 
Did you mean: 

Call `HAL_PWR_EnterSTOPMode` in `HAL_Delay`?

RodrigoMoraes
Associate

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).

2 REPLIES 2
STOne-32
ST Employee

Dear @RodrigoMoraes ,

What is the clock source of your function using Systick interrupt ? I know it is a basic question but I’am asking because systick is off while device is in STOP mode and can not wake your system each 13us . You have most probably something else doing that such as RTC interrupt or simply your system is not entering STOP mode . Can you remove the debugger and keep the Application in standalone within ST-Link . Let us know .

STOne-32

Sorry, you are right, STOP mode does indeed stop the Systick. I am actually using `HAL_PWR_EnterSLEEPMode`, but while reading its documentation (the "doc-comment states that the SysTick is stopped") and reading this article, I confused the two. I revised the previous project where I originally used the pattern mentioned (a stm32c031), and I use `HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);` while busy waiting on a flag.

So, going back to my intended question, is there any problem adding `HAL_PWR_EnterSLEEPMode` inside `HAL_Delay`?

And looking at the doc-comment in my previous project, it does not have the note about systick being stopped, maybe the project I am currently working on have a error due to being generated in a older version of STM32CubeIDE.