Skip to main content
Ehsan Nayeri
Associate
September 26, 2017
Question

Bug Report: HAL_Init() function Fails to initialize systick timer correctly.

  • September 26, 2017
  • 4 replies
  • 1767 views
Posted on September 26, 2017 at 15:20

This function which is called at the entry of Main() function(Code generated by cubeMX) can not initialize systick timer to create 1ms time intervals correctly. As a result if functions which are called after HAL_Init() use HAL_GetTick() for delay or wait purposes fail to work as programmer expect.

It is true until SystemClock_Config() function  (which itself trust on HAL_GetTick()  for some initializations wait time)  modify systick timer.

The reason is that for initialization of systick timer HAL_InitTick() function use the value of �SystemCoreClock� variable which is not updated yet.

#systick-timer #hal_init
This topic has been closed for replies.

4 replies

Vangelis Fortounas
Associate II
September 26, 2017
Posted on September 26, 2017 at 15:39

Hello!

Inside system_stm32xxxx.c there is the definition of SystemCoreClock variable which takes the value of the default core clock frequency after power on (HSI , MSI ...)

This value of SystemCoreClock used to initialize the SysTick inside

HAL_Init()  function.

SystemClock_Config()  reinitializes the SysTick with the updated value of SystemCoreClock .

Regards

vf

Ehsan Nayeri
Associate
September 27, 2017
Posted on September 27, 2017 at 10:36

Hi,

Thanks for reply. Actually I meant to say that the initialization of “SystemCoreClock�? is wrong.

In the era of SPL(standard package library, which I still prefer in some aspects mainly for its comprehensive examples ) the initialization of system clock was the duty of “SystemInit()�? function which is defined in “system_stm32xxxx.c�? and called at startup before coming to main function(for example line 150 in startup_stm32f10x_hd.s). So, At the entry of main function system clock was truly initialized base on one of the following defines in “system_stm32xxxx.c�?:

#if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
/* #define SYSCLK_FREQ_HSE HSE_VALUE */
 #define SYSCLK_FREQ_24MHz 24000000
#else
/* #define SYSCLK_FREQ_HSE HSE_VALUE */
/* #define SYSCLK_FREQ_24MHz 24000000 */ 
/* #define SYSCLK_FREQ_36MHz 36000000 */
/* #define SYSCLK_FREQ_48MHz 48000000 */
/* #define SYSCLK_FREQ_56MHz 56000000 */
#define SYSCLK_FREQ_72MHz 72000000
#endif
�?�?�?�?�?�?�?�?�?�?�?

and then:

#ifdef SYSCLK_FREQ_HSE
 uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_24MHz
 uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_36MHz
 uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_48MHz
 uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_56MHz
 uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_72MHz
 uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
#else /*!< HSI Selected as System Clock source */
 uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
#endif
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

While in new version of “system_stm32xxxx.c�? (one which is used in CubeMX) the above

definitions are truncated to :

#if defined(STM32F100xB) ||defined(STM32F100xE)
 uint32_t SystemCoreClock = 24000000; /*!< System Clock Frequency (Core Clock) */
#else /*!< HSI Selected as System Clock source */
 uint32_t SystemCoreClock = 72000000; /*!< System Clock Frequency (Core Clock) */
#endif
�?�?�?�?�?

As a result at startup that system clock is HSI (8 MHz) and no setting is done inside “SystemInit()�? (absence of “SetSysClock()�? function) the clock is wrongly assumed as 72MHz.

Vangelis Fortounas
Associate II
September 27, 2017
Posted on September 27, 2017 at 14:14

Hello again.

Actually  I meant to say that the initialization of  ?SystemCoreClock? is wrong.

For sure i agree with you about this for F1 firmware used in CubeMx

Also my yesterday's answer concerned  L1 and F4 families that have the correct HSI , MSI values.

0690X00000608LHQAY.png

The conclusion is

The values inside file system_stm32f1xx.c must be revised from ST team , to have correct generated code by CubeMx.

0690X00000608OZQAY.png

Regards

vf.