cancel
Showing results for 
Search instead for 
Did you mean: 

Change UART baudrate on the go STM32G4 (DMA)

Yves Bmnt
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions
Yves Bmnt
Associate III

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

View solution in original post

4 REPLIES 4

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

 

Yves Bmnt
Associate III

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.


@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 ?

Yves Bmnt
Associate III

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