2025-02-19 06:22 AM - edited 2025-02-19 06:23 AM
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
Solved! Go to Solution.
2025-02-19 07:48 AM
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
2025-02-19 06:31 AM
@Yves Bmnt wrote:I'm having problem implementing a function to change the baudrate during runtime.
Kind regards
What problem(s), exactly?
@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.
2025-02-19 07:11 AM - edited 2025-02-19 07:16 AM
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.
2025-02-19 07:37 AM
@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 ?
2025-02-19 07:48 AM
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