cancel
Showing results for 
Search instead for 
Did you mean: 

HAL API - --HAL_GetTick() always returned 0 in HAL_RCC_OscConfig() during SystemClock_Config?

chai2145
Senior

Hi all, 

STM32 start executes the following during startup in main.c:

  1. HAL_Init();

  2. SystemClock_Config();

  3. Initialize all configured peripherals (e.g., MX_GPIO_Init())

  4. Enter the main while loop

However, I've observed a cold-start issue:

  • If the power supply is disconnected for 3–5 seconds or more, the system hangs and never reaches the main while loop after reboot.

  • If the supply is disconnected for a short interval or a manual reset is applied, the system boots normally.

While debugging, I suspected that the LSE/LSI/HSI oscillators might need more time to stabilize.
Looking into HAL_RCC_OscConfig(), it waits for the HSI to be ready by checking RCC_FLAG_HSIRDY with a timeout mechanism:

 /* Get Start Tick */
        tickstart = HAL_GetTick();

        /* Wait till HSI is ready */
        while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U)
        {
          if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
          {
            return HAL_TIMEOUT;
          }
        }

 

When I set a breakpoint here, I noticed:

  • tickstart is always 0

  • HAL_GetTick() always returns 0

It seems the HAL tick is not advancing at this point, causing the function to hang indefinitely.

Has anyone encountered a similar issue, or can explain why HAL_GetTick() would not increment during system clock configuration at cold start? Any suggestions on how to fix this would be greatly appreciated.

 

 

1 REPLY 1
Saket_Om
ST Employee

Hello @chai2145 

I have tested the scenario you described, and it worked well from my side.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om