2024-10-03 01:58 AM - last edited on 2024-10-03 02:18 AM by Andrew Neil
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:
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
2024-10-03 02:30 AM - edited 2024-10-03 02:31 AM
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.
2024-10-04 08:30 AM
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();
}
}
2024-10-05 11:41 AM
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.
2024-10-07 01:09 AM - edited 2024-10-07 02:54 AM
@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"