cancel
Showing results for 
Search instead for 
Did you mean: 

Change baudrate via HAL

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?

8 REPLIES 8
TDK
Guru

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".

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
Up vote any posts that you find helpful, it shows what's working..
gbm
Lead III

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

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.

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).

gbm
Lead III

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

Hi,

Its about code encapsulation and portability really.

Will figure this out, thank you for the answers!

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".