Skip to main content
Ala
Senior
Feb 3, 2022 6:40 AM UTC
Solved

wrong HAL_Delay()

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.
Ala
AlaBest answer
Senior
Feb 5, 2022 5:19 AM UTC

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.

9 replies

LCE
Principal III
Feb 3, 2022 6:48 AM UTC

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 .

Ala
AlaAuthor
Senior
Feb 3, 2022 6:51 AM UTC

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 III
Feb 3, 2022 11:32 AM UTC

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
Feb 3, 2022 3:02 PM UTC

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""."
Ala
AlaAuthor
Senior
Feb 5, 2022 5:13 AM UTC

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

TDK
Feb 5, 2022 3:28 PM UTC

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""."
Pavel A.
Feb 3, 2022 5:15 PM UTC

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

Ala
AlaAuthor
Senior
Feb 5, 2022 5:14 AM UTC

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

Ala
AlaAuthorBest answer
Senior
Feb 5, 2022 5:19 AM UTC

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.