2023-06-09 12:26 AM - edited 2023-11-20 03:35 AM
Hello
I just found out that the HAL_Delay() delay actually takes 1ms longer than the desired value. Why?
STM CUBE IDE 1.9.0
STM32F072RBT6
Solved! Go to Solution.
2023-06-09 01:00 AM
2023-06-09 01:00 AM
Because it's an "at-least-N-ms" delay with 1ms granularity.
JW
2023-06-09 02:36 AM
If you want something that can resolve better, use a TIM that clocks faster, and read the TIMx->CNT
Code may also be interrupted
2023-06-09 09:29 AM
To measure or wait a precise duration, you may need a dedicated stopwatch, sharing a always running wallclock won't be the same...
2023-06-11 11:08 PM
waclavek.jan,Tesla DeLorean, S.Ma :
But I'm not interested in accuracy. The time is always accurate, just always +1ms, because as I found out, the function automatically increments the variable at the beginning.
__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)// allways add uwTickFreq (1)
{
wait += (uint32_t)(uwTickFreq);
}
while((HAL_GetTick() - tickstart) < wait)
{
}
}
For values in hundreds of milliseconds it is of course not important, but for 1ms it does 100%. And that caused me a problem that I only discovered by using a logic analyzer.
Of course, it is a weak function that I can override according to my needs, but I was more interested in the reason why it is so.
Sure, the comment says that it should guarantee at least a minimal delay, but it can certainly be better:
/* Add a freq to guarantee minimum wait */
if (wait < 1)
{
wait += (uint32_t)(uwTickFreq);
}
Or am i wrong?
2023-06-12 01:31 AM
I tried to answer ecactly this in the article I linked to above.
JW
2023-06-12 01:41 AM
Depends when it is called.
A fraction of a second prior to the systick will result in a very short delay.
The solution is really to have a time base that resolves in much finer resolution, say 1 or 2 orders of magnitude.
2023-06-12 06:09 AM
Yes, that's what I wanted to know. I just didn't notice that "Because" was a link.
Thanks