cancel
Showing results for 
Search instead for 
Did you mean: 

How do I change the Baud rate of a UART/USART whilst running?

C.East2
Associate III

I've followed some answers from a post here:

https://stackoverflow.com/questions/57283327/how-to-change-the-uart-baud-rate-after-running-on-stm32-board

But still can't seem to get the baud rate to change. Has anyone had any luck trying to dynamically change the baud rate of the UART?

/****************************************************/

USART6 -> CR1 &= ~(USART_CR1_UE);

USART6 -> BRR = NEWVALUE;

USART6 -> CR1 |= USART_CR1_UE;

/****************************************************/

huart.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), new_baudrate);

/****************************************************/

HAL_UART_DeInit(instance[bus].handle);

instance[bus].handle->Init.BaudRate = baud;

HAL_UART_Init(instance[bus].handle);

11 REPLIES 11
C.East2
Associate III

Here is a follow up and answer to the problem.

The issue was that The UART was part way through a interrupt transmission while I was changing the Baud rate. Having a static variable that keeps track of a message being sent has solved this issue.

sonminh
Associate II

Hi,

I meet the same problem as you, Have you change baud rate successful?

 

Thanks and brgs