Skip to main content
Lukasz Przenioslo
Associate III
September 26, 2021
Question

Change baudrate via HAL

  • September 26, 2021
  • 8 replies
  • 3990 views

Hello there,

I was trying to figure out a way of how to change the UART baudrate "in runtime" via HAL API for stm32f0. I failed to find a way to do it other than manually (copy out the functionalities used in the uart init function and change it accordingly).

Did I just miss it, or is this functionality indeed not implemented in HAL API?

This topic has been closed for replies.

8 replies

TDK
September 26, 2021

You can deinitialize and reinitialize with HAL functions, changing the initialization parameters accordingly between calls. Or you can modify registers directly.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
September 26, 2021

Just reinitialize it with the new context.

At the register level you must clear the Uart Enable before changing the Baud Rate Register, then reenabling. You need to be sure the UART is idle so as not to lose or disrupt data currently on the wire.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
gbm
Super User
September 26, 2021

Without HAL it takes 3 or 4 lines of code with 3 simple assignments: disable UART by writing 0 to CR1 (no need to check for anything, as likely you don't care about data loss while changing baud rate), load new value to BRR, reenable the UART, done.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
S.Ma
Principal
September 26, 2021

With HAL deinit reinit will probably be needed and as said by others, disabling, reconfiguring and reenabling will be ok. Some uart version have autobaud feature as well.

Lukasz Przenioslo
Associate III
September 27, 2021

Thank you for the answers guys,

As doing it without HAL, I understand how it takes few lines of code, but I need to calculate the new BRR value first based on desired baudrate. For that I need the clock source etc. To not hardcode that, its feasible to use what is already available via HAL.

The issue with using hal deinit and init is that you do.not have your mxcubegenerated structure available anymore- its enclosed in the uart.c file generated by mxcube. So in the end you end up doing some workarounds to mitigate that.

As for the auto baudrate feature, it will not work for me. I change the baud of the external device and after that it expects me to talk on the new baud already.

So what I was looking for in the HAL API is a function, that preserves all its configuration while changing thr baudrate (inc calculation of the BRR) only.

Seems like there is no clean solution (besides writing your own HAL function).

TDK
September 27, 2021
All HAL functions require the peripheral handle. The handle contains the initialization structure. If that's not available, you can't use HAL to do it.
"If you feel a post has answered your question, please click ""Accept as Solution""."
gbm
Super User
September 27, 2021

Somehow it is hard to believe that you don't know the clock frequencies in your own design. There are very few cases where clock frequency is not constant. All you need to do is:

UARTx->BRR = (UART_CLK + BAUD / 2) / BAUD;

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Lukasz Przenioslo
Associate III
September 27, 2021

Hi,

Its about code encapsulation and portability really.

Will figure this out, thank you for the answers!