Skip to main content
Denis Gottardello
Associate III
January 30, 2018
Question

How to change the uart baudrate dinamically

  • January 30, 2018
  • 4 replies
  • 962 views
Posted on January 30, 2018 at 21:53

Hi. I need to change the uart baudrate dinamically.

I have tryied this but sems to not work. Have you got an example to show?

    serial->Init.BaudRate= BaudRate;

    serial->Init.WordLength = UART_WORDLENGTH_8B;

    serial->Init.StopBits = UART_STOPBITS_1;

    serial->Init.Parity = UART_PARITY_NONE;

    serial->Init.Mode = UART_MODE_TX_RX;

    serial->Init.HwFlowCtl = UART_HWCONTROL_NONE;

    serial->Init.OverSampling = UART_OVERSAMPLING_16;

    serial->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

    serial->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

    HAL_UART_Init(serial);
    This topic has been closed for replies.

    4 replies

    Amel NASRI
    Technical Moderator
    January 31, 2018
    Posted on January 31, 2018 at 10:49

    Hi

    Gottardello.Deni

    ‌,

    May I know the STM32 product you are using?

    There are some STM32 series where USART automatic baud rate detection is supported and it depends on USART instance.

    Have a look to

    http://www.st.com/content/ccc/resource/technical/document/application_note/group0/66/fa/62/a2/5c/75/48/a8/DM00327191/files/DM00327pdf/jcr:content/translations/en.DM00327pdf

    (STM32 USART automatic baud rate detection) for more details.

    -Amel

    To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
    Andrew Neil
    Super User
    January 31, 2018
    Posted on January 31, 2018 at 17:44

    What do you mean, 'seems not to work'?

    What, exactly, does happen?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    January 31, 2018
    Posted on January 31, 2018 at 18:32

    One must first make sure all data currently being transmitted is flushed.

    You can then disable the USART, change the BRR, and then reenable the USART

    There is significant opportunity for receive data loss, and you should clear framing, parity or overrun type errors that might be induced.

    The HAL structures for USART should have global and long term scope

    Changing the rate like this should be sufficient, you could DeInit or disable if it appears not to be.

    serial->Init.BaudRate= DifferentBaudRate;

    HAL_UART_Init(serial);

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Denis Gottardello
    Associate III
    February 1, 2018
    Posted on February 01, 2018 at 21:50

    Ok I will try. Regards.