AnsweredAssumed Answered

Bug in HAL_Delay() all Cube SDK

Question asked by ryabinin.vyacheslav on May 13, 2014
Latest reply on May 13, 2014 by ryabinin.vyacheslav
Hi!

I'm sure there is a bug in HAL_Delay() when current time returned by HAL_GetTick is close to overflow (0xFFFFFFFF).

Check out this code:
http://ideone.com/MAdwBN

Second version of HAL_Delay() is my suggestion, it's pass the test.

//-----------------------------------------------------------------------------
#include <stdio.h>
#include <inttypes.h>
//-----------------------------------------------------------------------------
 
#define __IO
 
static uint32_t Ticks;
 
/// Debug version of HAL_GetTick()
static uint32_t HAL_GetTick()
{
    return Ticks++;
}
 
//-----------------------------------------------------------------------------
 
#if 1
 
/// Original HAL_Delay() from CubeMX
void HAL_Delay(__IO uint32_t Delay)
{
  uint32_t timingdelay;
   
  timingdelay = HAL_GetTick() + Delay;
  while(HAL_GetTick() < timingdelay)
  {
  }
}
 
#else
 
/// Proposed HAL_Delay()
void HAL_Delay(__IO uint32_t Delay)
{
  uint32_t tt = HAL_GetTick();
   
  while((uint32_t)(HAL_GetTick() - tt) < Delay)
  {
  }
}
 
#endif
 
//-----------------------------------------------------------------------------
 
int main(int argv, char * argc[])
{
    uint32_t temp;
     
    Ticks = 0xFFFFFFE0;
     
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
     
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    temp = Ticks;
    HAL_Delay(10);
    printf("%" PRIu32 "\n", (uint32_t)(Ticks - temp));
 
    return 0;
}
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


Outcomes