2024-09-30 02:54 AM
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:
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
Solved! Go to Solution.
2024-09-30 08:13 AM - edited 2024-09-30 08:14 AM
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
2024-09-30 03:46 AM
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?
2024-09-30 04:09 AM
@hkyu wrote:I have designed my PCB with STM32F103CBT6 and a 16MHz crystal oscillator.
That looks like just a crystal - not a crystal oscillator ?
The difference:
2024-09-30 04:15 AM
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)
2024-09-30 05:01 AM
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();
}
}
2024-09-30 05:45 AM - edited 2024-09-30 05:45 AM
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:
Do you get 16MHz on PA8?
2024-09-30 06:16 AM
Thank you so much for the quick reply.
The datasheet says it's just a crystal.
2024-09-30 06:21 AM
Here is the result:
I suppose it's running properly. The clock cycle seems ~62.5ns, so it's 16MHz.
2024-09-30 06:29 AM
@hkyu wrote:
The datasheet says it's just a crystal.
Here it says 16MHz (https://www.lcsc.com/product-detail/SMD-Crystal-Resonators_Yangxing-Tech-X322516MLB4SI_C13738.html:(
2024-09-30 06:36 AM - edited 2024-09-30 06:41 AM
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().