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

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

13 REPLIES 13
SofLit
ST Employee

Hello @hkyu and welcome to the community,

That's weird! X322516MLB4SI is a 16MHz crystal.

According to to your CubeMx, it looks fine.

Are you sure the CubeMx screenshot you shared is the effective clock config?

I have a doubt you are using HSI and not HSE.

Could you please share the part of code generated by CubeMx of the system clock config?

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.
Andrew Neil
Evangelist III

@hkyu wrote:

I have designed my PCB with STM32F103CBT6 and a 16MHz crystal oscillator.


That looks like just a crystal - not a crystal oscillator ?

AndrewNeil_0-1727694387169.png

 

The difference:

https://community.st.com/t5/stm32-mcus-products/stm32f4-osc-problem/m-p/664136/highlight/true#M241592

He's newbie in electronics: 

"I'm a noob at electronics"

I don't think it can differentiate at this level ;) 

But he's effectively using a crystal and HSE as clock source in Crystal/Resonator mode (not in Bypass mode)

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.

Thank you so much for the quick reply.


@SofLit wrote:

Are you sure the CubeMx screenshot you shared is the effective clock config?


Well I guess. 

Here is the system clock config code in main.c:

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV2;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

 

Hello,

Hello well. For the moment I don't see an issue in this code.

At this stage it's preferable to proceed by steps.

Now try to output the HSE signal on MCO (Master Clock Output) on PA8 pin.

In CubeMx configure the following:

SofLit_0-1727700272382.png

Do you get 16MHz on PA8?

 

 

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.

Thank you so much for the quick reply.

The datasheet says it's just a crystal. 

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

Here is the result:

IMG20240930220944_BURST000_COVER.jpg

I suppose it's running properly. The clock cycle seems ~62.5ns, so it's 16MHz. 


@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 (https://www.lcsc.com/product-detail/SMD-Crystal-Resonators_Yangxing-Tech-X322516MLB4SI_C13738.html:(

SofLit_0-1727702936365.png

 

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 

Still one thing to check:

SystemCoreClock value that the system tick configuration is based on.

Get the value of SystemCoreClock variable after executing SystemClock_Config().

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.