2014-03-12 08:05 AM
Setup:
I've just generated code for stm32f407 using the latest STM32CubeMX. Here's the generated code for delay function in file stm32f4xx_hal.c:void HAL_Delay(__IO uint32_t Delay)
{ uint32_t timingdelay; timingdelay = HAL_GetTick() + Delay; while(HAL_GetTick() < timingdelay) { } }It's fairly simple,
HAL_GetTick
returns a value ofuwTick
- variable that is incremented in SysTick interrupt handler.Bug Description:
When the value of variableuwTick
becomes large enough (i.e. adding Delay will overflow uint32) delay function will not work correctly, because expression ''HAL_GetTick() < timingdelay
'' will immediately be false. That results in no delay at all. Example: ConsideruwTick = 0xFFFF FF00
and
Delay = 0x101Then
:timingdelay = 0xFFFF FF00 + 0x101 == 0x01
Then
while(HAL_GetTick() < timingdelay)
is
(0xFFFF FF00
< 0x01 ).
This is already false and while is not executed. No delay is performed!
Comment:
This bug can be real pain, because it will work only when overflow of
uwTick
is near (or delay is really large). If one haduwTick
incremented every millisecond (seems to me like a reasonable value), bug can accure in about 47 days from reset.Solution:
That's easy enough:uint32_t startTime = HAL_GetTick();
while( HAL_GetTick() - startTime <= Delay);If we compare the differencies between moments of time and Delay - we are okay (as long as Delay is not bigger than
0xFFFF FFFF
, but that's sort of unrealistic and can be guaranteed by assert); overflow will not affect it. #bug-stm32-timeout-hal_gettick #bug #hal #hoist-by-their-own-petard #spl2014-03-12 08:38 AM
I see this kind of code all the time, and I want to smack people..
2014-03-17 09:57 AM
It's fun watching an ST insider struggling with this crap of a forum software... ;)
Sorry, I couldn't resist. JW2014-03-17 02:18 PM
Ha! Ha!! Ha!!!
I thought exactly the same thing - only to scroll down & find you'd already said it!!LOL!2014-04-07 06:32 PM
Great catch but unfortunately wasn't fixed in the v4.1.0 just released.
ST take note.2014-06-01 01:12 PM
This error is everywhere in the code, a check reveals at least 30 files affected.
stm32f4xx_hal_adc.c: timeout = HAL_GetTick() + Timeout; stm32f4xx_hal_adc_ex.c: timeout = HAL_GetTick() + Timeout; stm32f4xx_hal_can.c: timeout = HAL_GetTick() + 10; stm32f4xx_hal_cryp.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_cryp_ex.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_cryp_ex.c: if(HAL_GetTick() >= timeout)stm32f4xx_hal_dcmi.c: timeout = HAL_GetTick() + HAL_TIMEOUT_DCMI_STOP;stm32f4xx_hal_dma2d.c: timeout = HAL_GetTick() + HAL_TIMEOUT_DMA2D_ABORT;stm32f4xx_hal_dma.c: timeout = HAL_GetTick() + HAL_TIMEOUT_DMA_ABORT;stm32f4xx_hal_flash.c: uint32_t timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_hash.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_hash_ex.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_i2c.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_i2s.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_i2s_ex.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_irda.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_nand.c: timeout = HAL_GetTick() + NAND_WRITE_TIMEOUT;stm32f4xx_hal_nor.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_pwr_ex.c: timeout = HAL_GetTick() + PWR_BKPREG_TIMEOUT_VALUE;stm32f4xx_hal_rcc.c: timeout = HAL_GetTick() + HSE_TIMEOUT_VALUE;stm32f4xx_hal_rcc_ex.c: timeout = HAL_GetTick() + PLLI2S_TIMEOUT_VALUE;stm32f4xx_hal_rng.c: timeout = HAL_GetTick() + RNG_TIMEOUT_VALUE;stm32f4xx_hal_rtc.c: timeout = HAL_GetTick() + RTC_TIMEOUT_VALUE;stm32f4xx_hal_rtc_ex.c: timeout = HAL_GetTick() + Timeout; stm32f4xx_hal_sai.c: timeout = HAL_GetTick() + Timeout; stm32f4xx_hal_smartcard.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_spi.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_hal_uart.c: timeout = HAL_GetTick() + Timeout;stm32f4xx_ll_fmc.c: timeout = HAL_GetTick() + Timeout;Someone should start fixing this.I am using the latest version of the CubeMX (on june 1st 2014) and it still generates this faulty code.2014-06-02 01:34 AM
Hi,
We have already the limitation in the STM32Cube HAL bugs list, and it will be fixed in next releases of F4 (early July).Thanks for the report.With regards.2014-08-28 11:55 AM
Still not corrected. Using STM32F207.
2014-09-03 01:40 PM
Heisenberg said ''early July'' above. Note, he did not say, which year.
JW2014-09-09 09:12 AM
Hi waclawek.jan,
This limitation is already fixed within the STM32CubeF4 firmware package V1.3.0, available , since June 26th 2014.Regards.