Skip to main content
Visitor II
February 3, 2022
Solved

wrong HAL_Delay()

  • February 3, 2022
  • 4 replies
  • 2368 views

hey there

I have a PCB which has a STM32F030C8T6 and a 11.0592MHz crystal on it.

the thing is that when I enable HSE and try to toggle a pin every 500ms it takes way longer than expected, here is the code

while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
		HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
		HAL_Delay(500);
 /* USER CODE END 3 */
}

pretty simple right? but way wrong results.

but as I set the clock to be HSI 8MHz(inner clock) everything works just fine and crisp!

I have also enabled CSS for clock security but in HSE case I dont see right results. why is that?

    This topic has been closed for replies.
    Best answer by Ala

    Many thanks to all who helped and gave me clues and hints.

    I think that I found the solution: on my PCB, the soldered crystal was SMD. not sure why but recently these SMD crystals are not working as expected. so I swapped this one with a Dip and it worked just fine.

    4 replies

    Graduate II
    February 3, 2022

    Did you tell your source code that you are using an HSE with 11.x MHz ?

    HAL_Delay() is based on the SysTick, usually set to 1 ms.

    There's a #define EXTERNAL_CLOCK_VALUE, I think in *_hal_conf.h .

    AlaAuthor
    Visitor II
    February 3, 2022

    I use CubeMX, I guess Cube should handle this... isnt it?

    I've checked file stm32f0xx_hal_conf.h. here is the definition

    #if !defined (HSE_VALUE)
     #define HSE_VALUE ((uint32_t)11059200) /*!< Value of the External oscillator in Hz */
    #endif /* HSE_VALUE */

    seems right, but I can not see good results though...

    Graduate II
    February 3, 2022

    Okay, that looks good.

    Yes, Cube should handle that, but always check the sources, every now and then Cube omits an important setting.

    I would try to "follow" all the Systick settings to find out where and how it is set. Maybe check some register settings via debugger or UART.

    Then for testing, I would toggle a GPIO with the SysTick to find out what its actual period is.

    Super User
    February 3, 2022

    output MCO to a pin to verify frequency is what you think it is.

    Not sure how well HAL works with frequencies that are not multiple of 1 MHz.

    Define "way longer", perhaps the relation could provide a clue.

    AlaAuthor
    Visitor II
    February 5, 2022

    @TDK​  by "output MCO to a pin to verify frequency is what you think it is." you mean toggle a GPIO in SysTick?

    "way longer" is actually not same and I didnt find any relation between, say different HAL_Delay(value) and the seen delay in between HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);

    Super User
    February 5, 2022

    I mean MCO, which is the direct output of the system clock source with an optional prescaler. See RCC settings in CubeMX, or reference manual.

    Super User
    February 3, 2022

    Remember to define HSE_VALUE in the project preprocessor defines. Not [only] in ... hal_conf.h.

    AlaAuthor
    Visitor II
    February 5, 2022

    I really dont think that's the case, one #define is always sufficient and works.

    AlaAuthorAnswer
    Visitor II
    February 5, 2022

    Many thanks to all who helped and gave me clues and hints.

    I think that I found the solution: on my PCB, the soldered crystal was SMD. not sure why but recently these SMD crystals are not working as expected. so I swapped this one with a Dip and it worked just fine.