cancel
Showing results for 
Search instead for 
Did you mean: 

wrong HAL_Delay()

Ala
Senior

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Ala
Senior

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.

View solution in original post

9 REPLIES 9
LCE
Principal II

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 .

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...

LCE
Principal II

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.

TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist III

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

@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);

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

Ala
Senior

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.

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.

If you feel a post has answered your question, please click "Accept as Solution".