cancel
Showing results for 
Search instead for 
Did you mean: 

An external crystal oscillator is running without an error while 'SystemCoreClock' is wrong

hkyu
Associate II

Hello, 

I have designed my PCB with STM32F103CBT6 and a 16MHz crystal oscillator. When I generate code with CubeMX with the clock configuration like the one of the attached images, 'SystemCoreClock', a global variable shows it's 36MHz while it should be 72MHz.

Also, the led blink interval should wait 1000ms while it does only 500ms. It seems the clock is not running properly.

As you can see in the last image, I set breakpoints at Error_Handler(), but I couldn't catch any error there so I assume the config was okay.

Here is what I did:

  1. generate the code with the settings shown in the images
  2. copy all files under 'Core' folder and paste on PlatformIO project src folder
  3. add the following two lines in the while loop

 

    HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    HAL_Delay(1000);

 

I use PlatformIO to build/upload/debug on ubuntu 24.04.

I would appreciate if someone could guide me how to debug this. I'm a noob at electronics but I have a logic analyzer, 4ch 100MHz oscilloscope, and lab power supply.

MCU: STM32F103CBT6

Crystal Oscillator: X322516MLB4SI

Load capacitor: 10pF

 

hkyu_0-1727687837837.png

Screenshot from 2024-09-30 17-36-00.png

Screenshot from 2024-09-30 17-36-41.png

Screenshot from 2024-09-30 18-14-54.png

   

Screenshot from 2024-09-30 18-21-24.png

13 REPLIES 13

@SofLit wrote:

@hkyu wrote:

The datasheet says it's just a crystal. 

https://www.lcsc.com/datasheet/lcsc_datasheet_2403291504_YXC-Crystal-Oscillators-X322516MLB4SI_C13738.pdf


Here it says 16MHz


Yes, it's 16MHz. Does it mean the clock config is wrong?

Do you think the clock config is okay. the external crystal is working properly, and the schematic is okay, but something else is causing the problem? I just uploaded the same code to the other board with the same setup, the result is the same, so maybe this is not specific to this board. 

 

 

SystemCoreClock is 36MHz while it should be 72MHz after SystemClock_Config(). 

Screenshot from 2024-10-01 00-01-20.png

Screenshot from 2024-10-01 00-00-32.png

In the file stm32f1xx_hal_conf.h,

Check your HSE value.

Replace the definition of HSE value:

 #define HSE_VALUE  8000000U

by

 #define HSE_VALUE  16000000U

 

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.
hkyu
Associate II

Thank you so much! 

It was because I forgot to add the following line in platformio.ini:

board_build.stm32cube.custom_config_header = yes

 https://docs.platformio.org/en/latest/frameworks/stm32cube.html#custom-configuration-header

That is why PlatformIO overrides any custom config headers and set `HSE_VALUE` as the default 8MHz. Sorrry my bad!