cancel
Showing results for 
Search instead for 
Did you mean: 

Human Interface to Change UART Baud Rate and Variable Values Without Modifying Source Code

fchai.1
Associate II

Hi everyone,

I'm currently working on an STM32 project using STM32CubeIDE, and I need to implement a way to modify certain parameters, specifically the UART baud rate and some variable values, without having to exit the running application or modify the source code.

The idea is to have a simple human interface (either through UART commands, a basic GUI, or even a command line tool) that can change these settings at runtime. Here's what I'm looking for:

  • Ability to modify UART baud rate dynamically from a predefined set of values.
  • Modify some specific variables in the code (e.g., configuration parameters) without recompiling.
  • No need to exit or reset the application while making these changes.
  • If possible, I would prefer to avoid adding heavy libraries that take too much memory.

I've looked into using UART commands and CLI-based interfaces but haven't found a clear solution yet. Any suggestions or examples on how to approach this would be greatly appreciated!

Thanks in advance for your help!

Best regards,
Fawez Chaieb

4 REPLIES 4
Andrew Neil
Evangelist III

Sure, you can do that.

At its simplest, it's just a matter of receiving text strings, and parsing the data from them.

eg, the user types "baud=115200<cr>", you use just the standard C string library functions to recognise "baud=" as the command to change baud rate, and "115200" as the baud rate value.

https://en.cppreference.com/w/cpp/header/cstring 

You'd want to send some sort of confirmation back to the user.

In general, "<name>=<value><cr>".

Creating a GUI on the microcontroller will be far more complex - see TouchGFX.

An alternative would be to have simple commands to the microcontroller, and build a GUI on a PC or similar.

Will you need these settings to be non-volatile (ie, retained over power loss)?

 


@fchai.1 wrote:
  • If possible, I would prefer to avoid adding heavy libraries that take too much memory.

That pretty much rules out running a GUI on the microcontroller.

Saket_Om
ST Employee

Hello @fchai.1 

You can use UART to receive commands from a terminal or another device. These commands can be used to change the baud rate and other parameters.

Please see the following snippet of code:

//received cmd
cmd = SET_BAUD 115200
void process_command(char *cmd)
{
  if (strncmp(cmd, "SET_BAUD ", 9) == 0)
  {
    int baud_rate = atoi(cmd + 9);
    set_uart_baud_rate(baud_rate);
  }
}
void set_uart_baud_rate(int baud_rate)
{
  huart2.Init.BaudRate = baud_rate;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
}

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar
Karl Yamashita
Lead III
  • You can easily send a message to the STM32 with a new baud rate and have it start using the new baud rate.

 

  • The problem after changing the baud rate is that if the sending device isn't using the updated baud rate, it won't be able to communicate with the STM32, of course. You can keep tabs on what baud rate you've requested. But if somehow your sending device gets out of sync and the device or you don't know the baud rate, you may struggle a little trying to find the correct baud rate. You may possibly end up having to reset the STM32 so it's using the default baud rate that you know about.

 

A better approach is to use a baud rate detection scheme. That way whatever baud rate the sending device is using, the STM32 will sync up after a few tries of sending messages to it.

 

  • As for changing a variable's value, it's just like changing the baud rate. 

 

  • All this can be done during run time without the need to reset the STM32.

 

  • There isn't too much code to do this. 

 

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

@Karl Yamashita wrote:
  • The problem after changing the baud rate ...

Indeed.

@fchai.1 you would have to think carefully, and define how that works in your system.

eg, for AT commands, the change doesn't happen until after the "OK" to the "change-baud" command has been sent.

So the "OK" comes at the old rate, but the next command has to be sent at the new rate.

 


@Karl Yamashita wrote:

A better approach is to use a baud rate detection scheme.


@fchai.1 eg, see: Application note AN4908, "Getting started with USART automatic baud rate detection for STM32 MCUs"

https://www.st.com/resource/en/application_note/an4908-getting-started-with-usart-automatic-baud-rater-detection-for-stm32-mcus-stmicroelectronics.pdf

https://community.st.com/t5/stm32-mcus-products/how-to-implement-software-automatic-baud-rate-detection-for/td-p/324874