Skip to main content
nibbly78
Associate III
September 18, 2017
Question

Change Uart baud rate on the fly with cube?

  • September 18, 2017
  • 8 replies
  • 2257 views
Posted on September 18, 2017 at 16:07

I see that this has been asked a few times with no real resolution or workaround, so I'll ask again please?

I have USART3 configured at 9600 Baud. I test this with a terminal program also set at 9600 and it works fine.

I then execute the following 2 line to try and change the baud rate on the fly

MX_USART3_UART_DeInit();

MX_USART3_UART_Init_Custom(19200); 

and then set my terminal program to 19200 baud but uart on processor just stops receiving....

MX_USART3_UART_Init_Custom is a copy the standard MX_USART3_UART 

function with a configurable baud rate

:-

void MX_USART3_UART_Init_Custom(unsigned int baud)

{

huart3.Instance = USART3;

huart3.Init.BaudRate = baud;

huart3.Init.WordLength = UART_WORDLENGTH_8B;

huart3.Init.StopBits = UART_STOPBITS_1;

huart3.Init.Parity = UART_PARITY_NONE;

huart3.Init.Mode = UART_MODE_TX_RX;

huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart3.Init.OverSampling = UART_OVERSAMPLING_16;

huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart3) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

Any ideas?

    This topic has been closed for replies.

    8 replies

    Tesla DeLorean
    Guru
    September 18, 2017
    Posted on September 18, 2017 at 16:47

    >>I see that this has been asked a few times with no real resolution or workaround, so I'll ask again please?

    But what is the actual issue? Do the functions report an error, does the USART have some pending status? Provide some analysis.

    Does the USART have a framing error you need to clear? Does it transition baud rate mid-transmission?

    You can use the HAL, or frankly just disable the USART, change the BRR and re-enable.

    There isn't some inherent flaw in the USART or software, got several parts here where I negotiate to higher baud rates.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    nibbly78
    nibbly78Author
    Associate III
    September 18, 2017
    Posted on September 18, 2017 at 17:08

    The issue is that nothing else get received. Software just carired on running (no had faults)

    I added the following

    MX_USART3_UART_DeInit();

    MX_USART3_UART_Init_Custom(19200);

    if( HAL_UART_GetError(&huart3) )

    {

    // __HAL_UART_CLEAR_FLAG

    diag_log( _EV_VERBOSE, DIAG_FMT( 'Error' )); // stuck a break point here too

    }

    and stuck a break point on diag_log line for good measure. errors reported back, 0.

    what pending status do you refer to?

    Tesla DeLorean
    Guru
    September 18, 2017
    Posted on September 18, 2017 at 17:21

    >>what pending status do you refer to?

    USART3->SR

    USART3->ISR

    Depends on the specific STM32 we're talking about, but start with those, and a review of the relevant RM

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    nibbly78
    nibbly78Author
    Associate III
    September 19, 2017
    Posted on September 19, 2017 at 14:04

    ok, not sure why, but its now working with no changes, so this is what I have :-

    MX_USART3_UART_DeInit();

    MX_USART3_UART_Init_Custom(19200); 

    MX_USART3_UART_Init_Custom

    is a copy the standard MX_USART3_UART function with a configurable baud rate

    :-

    void

    MX_USART3_UART_Init_Custom(unsigned int baud)

    {

    huart3.Instance = USART3;

    huart3.Init.BaudRate = baud;

    huart3.Init.WordLength = UART_WORDLENGTH_8B;

    huart3.Init.StopBits = UART_STOPBITS_1;

    huart3.Init.Parity = UART_PARITY_NONE;

    huart3.Init.Mode = UART_MODE_TX_RX;

    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

    huart3.Init.OverSampling = UART_OVERSAMPLING_16;

    huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

    huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

    if (HAL_UART_Init(&huart3) != HAL_OK)

    {

    _Error_Handler(__FILE__, __LINE__);

    }

    }

    ?????

    NHend.7
    Associate II
    September 3, 2018

    Try this might be easier​

    MX_USART3_UART_Init

    if(cond)

    {Cubesets}

    else

    {custom}

    Or

    Switch​

    if (HAL_UART_Init(huarthandle) != HAL_OK)

    {

    _Error_Handler(__FILE__, __LINE__);

    }

    }

    Main

    ​MX_USARThadle_UART_Init();

    Your code....

    ​cond=false;// this moves into the else statement as defacto

    ​MX_USARThadle_UART_deInit();

    MX_USARThadle_UART_Init(); // custom baud should work.

    M​ight be cleaner and less issues as u keeping the standard cude init function and will make things easier moving forward on the fly.

    NHend.7
    Associate II
    September 3, 2018

    ​i tried this once by trying creating a new handle with the same setting. DeInit the cube settings and then tried to used my own handle Init wouldn't work. I think the method you have and the one I am suggesting might be the only way( if I remember the stm32 autobaud function gets trigged by a conditional statement aswell( something to do with the receive start byte).

    Please feel free to correct me if I am wrong.​

    Javier1
    Principal
    March 10, 2023

    i need to increase the speed of the uart for a Bluetooth-UART module, first 115200bauds for AT commands and then 1Mbps for data mode.

    In my head the code would be like this, but reading your comments i realiced it might not work

    void changeUARTbaudrate(){
    	HAL_UART_DeInit(&huart4);
    	huart4.Init.BaudRate = 1000000;
    	HAL_UART_Init(&huart4);
    }

    hit me up in https://www.linkedin.com/in/javiermuñoz/