cancel
Showing results for 
Search instead for 
Did you mean: 

[bug report] Bug in HAL (SPL) delay function

amomum
Associate III
Posted on March 12, 2014 at 16:05

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 of

uwTick

- variable that is incremented in SysTick interrupt handler.

Bug Description:

When the value of variable

uwTick

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:

Consider

uwTick = 0xFFFF FF00

and

Delay = 0x101

Then

:

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 had

uwTick

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 #spl
13 REPLIES 13
Posted on March 12, 2014 at 16:38

I see this kind of code all the time, and I want to smack people..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 17, 2014 at 17:57

It's fun watching an ST insider struggling with this crap of a forum software... 😉

Sorry, I couldn't resist.

JW

Andrew Neil
Evangelist
Posted on March 17, 2014 at 22:18

Ha! Ha!! Ha!!!

I thought exactly the same thing - only to scroll down & find you'd already said it!!

LOL!

joe
Associate II
Posted on April 08, 2014 at 03:32

Great catch but unfortunately wasn't fixed in the v4.1.0 just released.

ST take note.

edwin1
Associate
Posted on June 01, 2014 at 22:12

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.

Posted on June 02, 2014 at 10:34

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.

isaquesuzuki
Associate II
Posted on August 28, 2014 at 20:55

Still not corrected. Using STM32F207.

Posted on September 03, 2014 at 22:40

Heisenberg said ''early July'' above. Note, he did not say, which year.

JW

Posted on September 09, 2014 at 18:12

Hi waclawek.jan,

This limitation is already fixed within the STM32CubeF4 firmware package V1.3.0, available

http://www.st.com/web/en/catalog/tools/PF259243#

, since June 26th 2014.

Regards.