cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to change the baud rate of the USART during run time in STM32 cube IDE

PB.4
Associate II

I'm using STM32 cube IDE for STM32F030CC uC, in this the baud rate of the USART is configured as part of the *.inc file, but my requirement is have to change the baud rate during the run time based on the user input, means, there are 6 USARTs sent in that USART 6 is planned to communicate with PC (this is constant baud rate) to get the user input mostly the baud rate to be set for another USARTs, and then another USARTs to be initialized with given baud from from the user, will it be possible to implement through STM32 cube IDE? If it is not possible in STM32 cube IDE means suggest me some other compilers to meet this requirement implementation.

4 REPLIES 4

Stop usart dma, deinit, MX_init with new baud rate, start dma.

For initialization purposes you can copy and paste original function code MX_USARTN_UART_Init(void)

and modify huartN.Init.BaudRate = variable_from_user_input field before initialization, other fields are filled already during start-up.

MM..1
Chief III

Isnt issue of any IDE, is your brain problem, need write or use LL code.

/**
  * @brief  Configure USART BRR register for achieving expected Baud Rate value.
  * @note   Compute and set USARTDIV value in BRR Register (full BRR content)
  *         according to used Peripheral Clock, Oversampling mode, and expected Baud Rate values
  * @note   Peripheral clock and Baud rate values provided as function parameters should be valid
  *         (Baud rate value != 0)
  * @note   In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d.
  * @rmtoll BRR          BRR           LL_USART_SetBaudRate
  * @param  USARTx USART Instance
  * @param  PeriphClk Peripheral Clock
  * @param  OverSampling This parameter can be one of the following values:
  *         @arg @ref LL_USART_OVERSAMPLING_16
  *         @arg @ref LL_USART_OVERSAMPLING_8
  * @param  BaudRate Baud Rate
  * @retval None
  */
__STATIC_INLINE void LL_USART_SetBaudRate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t OverSampling,
                                          uint32_t BaudRate)
{
  uint32_t usartdiv;
  uint32_t brrtemp;
 
  if (OverSampling == LL_USART_OVERSAMPLING_8)
  {
    usartdiv = (uint16_t)(__LL_USART_DIV_SAMPLING8(PeriphClk, BaudRate));
    brrtemp = usartdiv & 0xFFF0U;
    brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
    USARTx->BRR = brrtemp;
  }
  else
  {
    USARTx->BRR = (uint16_t)(__LL_USART_DIV_SAMPLING16(PeriphClk, BaudRate));
  }
}
 

wpenn.1
Associate

The information in this thread has saved me 4 hours of eating out brain and turning into a frightening granny. Thank you guys!

Piranha
Chief II

All configurations are done during runtime, therefore the answer depends:

  • By clicking - not possible.
  • By writing code - possible.