2024-02-22 08:21 PM
Hi ,
I am using an STM32F205 for my project. We were using HSI as the clock source before and found out that HSI will not work very well with high temperatures for UARTs, So we added an External crystal clock (16Mhz) but I am unable to get Comms out of UARTs now (There are some random values coming through uart sometimes). This is my clock config
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 14;
RCC_OscInitStruct.PLL.PLLN = 240;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
/** Enables the Clock Security System
*/
HAL_RCC_EnableCSS();
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
I also updated the HSE value in stm32f2xx_hal_conf.h to 16Mhz.
I am using platformIO as the IDE for this project.
Am I missing any configuration?
Solved! Go to Solution.
2024-02-26 06:41 PM - edited 2024-02-26 06:42 PM
Thanks tjaekal
I Have One more question,
We started manufacturing few boards with Internal Clock(HSI) and then we found out the issue with temperature and HSI clock. So I added some calibration on HSITRIM value.
But for the new boards we will be using External Clock for better accuracy and stability.
How do we identify if there is an external clock connected to the board to configure the clock correctly?
2024-02-26 07:21 PM
I think:
You cannot really query (check a GPIO pin) if an external XTAL is connected. But you can use the clock recovery feature: MCU should have something like CSE (Clock Security Engine) or CSS (Clock Security System): if you are trying to use HSE but there is not a clock - it should fall back to use HSI.
Maybe, you can "ask" which clock source is used (e.g. CSE/CSS changed back to HSI due to fact that HSE is not there).
I think, you have to enable CSE/CSS:
On my STM32U5A5 I do this:
HAL_RCC_EnableCSS();
In my Portenta H7 project, I do something like this:
if (SetSysClock_PLL_HSE(1, 0) == 0)
SYS_SetError(SYS_ERR_SYS_INIT);
There should be a way to "realize" if HSE as clock source fails (and to realize you are back on HSI).
2024-02-27 03:45 AM
Thanks for coming back with the solution. Please select your post as the solution.
---
> How do we identify if there is an external clock connected to the board to configure the clock correctly?
What do you mean by "external clock", a complete external oscillator (which generates clock by itself, even without STM32), or a crystal?
In either case, you can use the fact that HSE/N (HSE_RTC) is fed into TIM11_CH1 (see TIM11_OR description). So start with running from HSI+PLL; and after configuring HSE to run, set TIM11_OR to HSE_RTC and TIM11_CH1 to capture. If captures occur regularly at the expected rate, then you have a clock on HSE.
You can also use CSS as @tjaekel described above, but IMO that's quite complicated.
JW
2024-02-27 04:18 PM
Thanks JW,
I am using a crystal oscillator.
May be I will do a separate post for this