Solved
Overflow on timeouts in STM32 HAL driver
Every code in HAL driver has the below structure that doesn´t care about overflow on HAL_GetTick().
...
tickstart = HAL_GetTick();
while (Something())
{
if (Timeout != HAL_MAX_DELAY)
{
if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
{
DoAnything();
return HAL_ERROR;
}
}
...I believe it can face some issues when the tick counter is about to overflow when the HAL is called and the results will be unpredictable.
Does anyone face this kind of problem?
One option to check the elapsed time is below:
uint32_t HAL_GetElapsedTimer(uint32_t u32_Time)
{
uint32_t u32_Tick = HAL_GetTick();
uint32_t u32_ElapsedTimer = ((u32_Time > u32_Tick) ?
(uint32_t) (((uint64_t) u32_Tick + (1ULL<<32)) - u32_Time) :
(uint32_t) (u32_Tick - u32_Time));
return u32_ElapsedTimer;
}And final HAL code:
...
tickstart = HAL_GetTick();
while (Something())
{
if (Timeout != HAL_MAX_DELAY)
{
if ((HAL_GetElapsedTimer(tickstart) > Timeout) || (Timeout == 0U))
{
DoAnything();
return HAL_ERROR;
}
}
...