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-25 04:05 PM
Hi,
This is my USART1 () and RCC regsiters
with HSI
With HSE
2024-02-25 07:12 PM
My five cents:
When you change from HSI to HSE (with a 16 MHz OSN/XTAL) - you cannot have the same PPL settings:
If HSI was based on 48 MHz (a guess) and you change to HSE with 16 MHz - the PLL config should look different, esp. to get the UART peripheral core clock as before,
If you make sure the PLL config results in the same UART peripheral clock - it should work.
2024-02-25 07:22 PM
But HSI on STM32F205 is 16MHz the same frequency as the external clock that I am using
2024-02-25 08:21 PM
Also I found out that, If I comment out HAL_RCC_ClockConfig(), the UARTS work fine.
This is the order that I initialize the peripherals
HAL_Init();
SystemClock_Config();
MX_I2C2_Init();
MX_I2C1_Init();
MX_I2C3_Init();
MX_UART_Init();
MX_ADC1_Init();
MX_GPIO_Init();
MX_CAN1_Init();
MX_CAN2_Init();
2024-02-26 02:03 AM
I meant, in RCC all registers which determine APB clock (i.e. besides RCC_CR also RCC_PLLCFGR and RCC_CFGR); and in UART registers which determine its baudrate and general working setup (UART_BRR, UART_CR1, UART_CR2).
My intention was to show you a way how *you* can find out, what's wrong, at the register level (which is how the microcontroller really works), and then work back into Cube/HAL to find out, where is the root of the problem - is it RCC/PLL setup, is it some other RCC setting, is it UART baudrate setup (and within that, is it a problem with the convoluted way how Cube/HAL determines the APB frequency from which UART's clock is derived), is it other UART setting, etc.
In other words, check everything impacting the baudrate, which is probably different from the expectation. You can confirm this by observing UART_Tx pin by oscilloscope/logic analyzer.
JW
2024-02-26 03:13 PM
Thanks JW,
I found its the UART->BRR register that changes when I use the HSE Clock.
On HSI (16MHZ) the UART->BRR is 0x186a. But on HSE(16Mhz) UART->BRR changes to 0x2626.
If I manually update the BRR register (On HSE) with 0x186a then uart works fine at 9600 baudrate.
Can anyone please explain why the BRR register changes If I use HSE with same clock frequency and clock config as the HSI?
2024-02-26 04:36 PM
I was able to set the MCO from HSE and it measures 16MHZ
So I am not sure why the UART baudrate changes when I use the HSE clock source
2024-02-26 05:04 PM
printf("%u\n", HSE_VALUE);
printf("\n\nCore=%d, %d MHz\n", SystemCoreClock, SystemCoreClock / 1000000);
{
uint32_t clk;
clk = HAL_RCC_GetHCLKFreq();
printf("HCLK=%9d, %6.2lf MHz\n", clk, (double)clk*1e-6);
clk = HAL_RCC_GetPCLK1Freq();
printf("APB1=%9d, %6.2lf MHz\n", clk, (double)clk*1e-6);
clk = HAL_RCC_GetPCLK2Freq();
printf("APB2=%9d, %6.2lf MHz\n", clk, (double)clk*1e-6);
}
2024-02-26 05:52 PM
Finally I found the issue,
We Copied stm32f2xx_hal_conf.h from PlatformIO Library in to our project. And changed the HSE_VALUE in the copied file. But the code was still using the stm32f2xx_hal_conf.h from the PlatformIO library which has got HSE_VALUE 25Mhz.
Thanks all for the Help
2024-02-26 06:31 PM
Great! Well done!
So, the clock config was wrong. Obvious. Smart, that you have found the root cause.
Yes, if one clock reference is wrong - the UART BRR has to be different (compared to what is expected to set).
Clock config can be very "tricky".
Enjoy your project now...