cancel
Showing results for 
Search instead for 
Did you mean: 

Change uart baudrate on the fly

Raider E
Associate III
Posted on March 22, 2017 at 11:45

Hi everyone,

i just would like to know know the sequence of reintializing UART port to a new baudarte or change parity on the go during execution.  I am using Hal library. What would be the sequence to do so and does it take long time to reintialize. If anyone can provide an example of changing baud rate would be great!. Many thanks

10 REPLIES 10
Posted on March 22, 2017 at 12:00

In the SPL we'd just send a new USART_Init with new parameters after making sure the last TX completed on the wire by checking the TC bit.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 27, 2017 at 04:08

HI Clive,

thanks for that. I've tried the following, but it does not behave as i expect.

while (__HAL_UART_GET_FLAG(&huart6, UART_FLAG_TC) == RESET){

}

MX_USART6_UART_Init();

I can see the huart6.Init.BaudRate have changed value but the re-initialization seems to cause disrupt the transmission and reception, sometimes I receive something sometimes I dont. I am reinitialize before every transmission would that be a problem in itself,? but I need to change the baud rate constantly. not sure whats the cause. without reinitialzing everything works fine.

Posted on March 27, 2017 at 04:12

void MX_USART6_UART_Init(void)
{
 huart6.Instance = USART6;
 huart6.Init.BaudRate = rs485_getBaudrate(HUART_6);//UartBaudrate[HUART_6];
 huart6.Init.WordLength = UART_WORDLENGTH_8B;
 huart6.Init.StopBits = rs485_getStopbits(HUART_6);// UART_STOPBITS_1
 huart6.Init.Parity = rs485_getParity(HUART_6); //UART_PARITY_NONE
 huart6.Init.Mode = UART_MODE_TX_RX;
 huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart6.Init.OverSampling = UART_OVERSAMPLING_16;
 huart6.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart6.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT|UART_ADVFEATURE_DMADISABLEONERROR_INIT;
 huart6.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
 huart6.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
 if (HAL_UART_Init(&huart6) != HAL_OK)
 {
 Error_Handler();
 }
}
uint32_t rs485_getBaudrate(uint8_t subIndex){
return rs485[subIndex].baudrate;
}
static struct rs485Info rs485[ MAX_NUM_UART_PORTS ]={
{9600,UART_PARITY_NONE,UART_STOPBITS_1},
{9600,UART_PARITY_NONE,UART_STOPBITS_1},
{9600,UART_PARITY_NONE,UART_STOPBITS_1},
{9600,UART_PARITY_NONE,UART_STOPBITS_1}
};�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Posted on March 27, 2017 at 04:16

Not sure how you are going to manage reception. There is no bit to indicate it is in the middle of reception, you can see RXNE at the end, but you'll need to make a determination when it is safe to change, changing the clock mid byte is going to cause loss.

For transmit you should make use TXE and TC indicate nothing is pending, and you'll need to make sure the HAL isn't holding anything. I've been able to change baud rates using the SPL, where I have to send the attached device a command/packet to request a change in baud rate, and then change it locally with the USART.

You should examine signalling with a scope or logic analyzer if you want to debug or understand your situation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 27, 2017 at 08:19

Hi Clive,

That's exactly what am trying to do change baudrate on device then change

it on my uart. I will try with different flags checking before changing the

baudrate hopefully that will solve it.

Many thanks for your help.

Regards

Esam

On Mon, 27 Mar 2017 at 3:17 PM, Clive One <st-microelectronics@jiveon.com>

Posted on March 27, 2017 at 15:32

If you use parity, ie 8E1, you need to program the USART in 9-bit to account for the parity bit.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 26, 2017 at 02:18

Were you able to resolve this? I have the same exact issue and I need to use HAL.

Thanks,

Peter

Posted on April 26, 2017 at 03:33

On what device? I have an L0 changing baud rates on a GPS receiver from 9600 to 38400 or 115200, by first waiting for TXE, and then waiting for TXE and TC to be asserted. At that point all data has traversed the wire and after a brief delay I reinitialize the USART to the new speed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 26, 2017 at 04:03

This is an STM32F469AIH on custom board, using HAL to communicate with a Telit SE873 GNSS. Everything works fine except for the baud rate change. The command gets to the GNSS successfully, I can see it in the logic analyzer changing its baud rate, but the UART for some reason seems to be stuck in its ISR thereafter.