cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 UART and USART clock configuration

IvanOrot
Associate II

I am working on a project that uses STM32F103 and STM32G4 as MCUs. They need to communicate between each other using UART. I am trying to configure STM32F103 UART clocks to match the STM32G4's clocks but I have not been able to figure out where to do that. I found a document that says UART clock is based on HCLK, but is there any prescaler that configures the UART clocks? If not, what is the frequency the UART clocks are based in so I can match it by configuring the STM32G4s clock, instead.

To be more specific, I am using all ports as asynchronous. USART1, USART2, USART3, UART4, and UART5.

6 REPLIES 6
Danish1
Lead II

Everything is fully described in the Reference Manuals for your stm32s. They will be different because the G4 are much more modern and so have many more capabilities than the F1. So you will have to study both of them to understand what is going on.

Because the RM is complete, it is complicated and hard to get your head around first time. So ST produce their "Cube" tools that hide the complexity and possibly limit your choices. But I find them inadequately documented and stick with RM where I can.

In general, a UART will use a fractional-n divider to generate a baud-rate-clock from the incoming clock (which may be a PCLK - either PCLK1 or PCLK2 depending which UART) or HCLK or SysClk. I expect G4 to also support clocks directly from the HSI/HSE oscillator so that the baud-rate does not need to be recalculated should you change the CPU speed on-the-fly to save power when there isn't much processing to do.

The output from the baud-rate-clock needs to be 16 times the actual baud-rate, so that the UART may sample the input three times in the centre of the bit-period to reduce noise-susceptibility while giving tolerance to baud-rate errors. (There are sometimes options to work at 8 times as well where you want high baud-rates in relation to the baud clock).

TDK
Guru

Start a project in CubeMX and look at the Clock Configuration tab to understand the various clocking options. It's in the RM as well, but is faster to get to and understand in CubeMX.

If you feel a post has answered your question, please click "Accept as Solution".

I did. I guess I am not understanding what configuration option controls the UART/USART clocks. When using STM32G4 I was able to use CubeMx to set the USART/UART clocks at different frequencies. For now, I am setting all clocks to 16MHz, but with STM32F103 I am not able locate where to adjust the USART/UART clocks to match the 16MHz. I guess I can use my oscilloscope and play around with the configurations but it would save me a lot of time to understand what is happening.

Pavel A.
Evangelist III

USARTs of STM32G4 have auto baudrate detection capability. This means that STM32G4 can tune its USART to the actual baudrate of the other side, even "non-standard". The detection can be repeated as needed.  This solves your issue.

 

On the STM32F103, the clock feeding USART is either PCLK1 or PCLK2 depending on where the peripheral is. There is no option to change it to anything else.

TDK_0-1721768580201.png

 

If you feel a post has answered your question, please click "Accept as Solution".
Andrew Neil
Evangelist III

@IvanOrot wrote:

I am trying to configure STM32F103 UART clocks to match the STM32G4's clocks 


Why?

There is absolutely no need to do that!

You don't fiddle with your PC's clock to have it communicate over UART, do you?!

All that matters is that the baud rates match.

(and, as @Pavel A. said, the STM32G4 can even do  automatic baudrate detection)