Question
Unable to dynamically change baudrate on STM32F405
I am using STM32CubeIde, No RTOS, USART1, RX DMA (Circular), TX DMA in normal mode with DMA_IT_HT disabled.
I’ve got main loop periodically outputting a simple message on the USART. System appears to hang when commanded to change baud.
I’ve tried perhaps dozens of variations that have been recommended by “STM32 Sidekick” and Google AI. Most work with the debugger connected. None work without the debugger which is making this difficult to troubleshoot. Sidekick example shown below.
// 1. Wait for any ongoing transmissions to finish safely
// This prevents splitting a character mid-air.
while (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) == RESET)
{
// Optional: Implement a timeout mechanism here
}
// 2. Temporarily de-initialize the UART peripheral
// This turns off the UART but keeps your GPIO and DMA settings intact.
if (HAL_UART_DeInit(huart) != HAL_OK)
{
return; //HAL_ERROR;
}
// 3. Update the baud rate in the configuration structure
huart->Init.BaudRate = new_baudrate;
// 4. Re-initialize the peripheral with the new configuration
// HAL automatically calculates the correct BRR value using PCLK1/PCLK2 internal maps.
if (HAL_UART_Init(huart) != HAL_OK)
{
return; // HAL_ERROR;
}
// apparently not necessary: SET_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
// Restart DMA reception (using Receive-to-Idle for variable length message)
HAL_UARTEx_ReceiveToIdle_DMA(saUartControl[id].pHuart,
saUartControl[id].rx_dma_buffer,
sizeof(saUartControl[id].rx_dma_buffer));
return; // HAL_OK;
