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
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();
}
}