Skip to main content
Yves Bmnt
Associate III
February 19, 2025
Solved

Change UART baudrate on the go STM32G4 (DMA)

  • February 19, 2025
  • 3 replies
  • 709 views

Hi all,

I'm having problem implementing a function to change the baudrate during runtime. I've read all the other topics about it and tried the proposed solutions but couldn't get it to work, maybe I missed something.

I use DMA for the UART which might also change the way things have to be handled.

Here's my code, basically a copy of the MX_USART1_UART_Init function:

 

 

void MX_USART1_ChangeBaudRate(uint32_t baudrate)
{
 while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) == SET)
 {
 // wait for UART to be idle
 }

 huart1.Init.BaudRate = baudrate;

	if (HAL_UART_Init(&huart1) != HAL_OK)
	{
		Error_Handler();
	}
 if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
 {
 Error_Handler();
 }
}

 

 

I've also tried calling DeInit / MspDeInit before the Init but without success too ( + few other things...). The init function seems to call the UART disable function as recommended before changing the settings.

Kind regards

Best answer by Yves Bmnt

Thanks for asking me the right questions :)

It was with DMA and I didn't Rx any more messages (no callback of my Rx function).

I found that I needed to call the HAL_UARTEx_ReceiveToIdle_DMA after calling my change baud rate function, then it works just fine :)

Kind regards

3 replies

Andrew Neil
Super User
February 19, 2025

@Yves Bmnt wrote:

I'm having problem implementing a function to change the baudrate during runtime.

Kind regards


What problem(s), exactly?

  • Baudrate doesn't change?
  • Baudrate changes to wrong value?
  • System crashes? (if so, give details)
  • other ??  (if so, give details)

 


@Yves Bmnt wrote:

I use DMA for the UART which might also change the way things have to be handled.


It might - so start getting it working first without DMA.

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Yves Bmnt
Yves BmntAuthor
Associate III
February 19, 2025

The system doesn't crash, however, it ignores any message I send after changing, even at the right baudrate.

The value in the BRR changes when I change the baudrate, and it is correct (16667 for 9600 baud with clk = 160MHz & oversampling 16, 2778 for 57600 baud)

One interesting thing, if I use the function to set the baudrate to the same value as the default, my messages are ignored during a few seconds, then answered again.

Andrew Neil
Super User
February 19, 2025

@Yves Bmnt wrote:

The system doesn't crash, however, it ignores any message I send after changing, even at the right baudrate.


So RX stops receiving ?

Does TX work ?

Is this with or without DMA ?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Yves Bmnt
Yves BmntAuthorBest answer
Associate III
February 19, 2025

Thanks for asking me the right questions :)

It was with DMA and I didn't Rx any more messages (no callback of my Rx function).

I found that I needed to call the HAL_UARTEx_ReceiveToIdle_DMA after calling my change baud rate function, then it works just fine :)

Kind regards