cancel
Showing results for 
Search instead for 
Did you mean: 

How to control one STM32 using another STM32 via UART/USART?

MArvi.1
Associate II

I am trying to do control one STM32-L476RG using another STM32-L476RG via UART/USART. For now, I would like to turn on the LED of the "slave" STM32 using UART from the "master" STM32. Is that possible? If so, how? Thanks.

19 REPLIES 19
Danish1
Lead II

It is possible.

The "Master" would have to send "commands" down the UART which the "Slave" would receive, decode and act upon.

So you need to decide on a protocol (what form the commands take).

You need to write a program that the master runs, which generates and sends the commands as and when appropriate.

And another program that receives the commands.

The two programs would need to be downloaded into their respective stm32l476 processors.

When thinking of the protocol, you should allow for all the extra features you might add later - you might not yet know what they are, but might they involve sending large amounts of data?

Also consider what happens if the Master and Slave get out-of-sync (e.g. the master starts sending commands before the slave is ready).

Maybe the slave should have replies that say something along the lines of "command xxy received, understood and acted upon".

Should the slave also be able to say "that button has just been pressed"?

I have seen discussions where ST employees like the idea of a "break" in transmission indicating that one command is complete. I personally don't like that because it complicates the case where you want to send two commands in quick succession.

I've only talked about this in vague terms. You're welcome to come back and expand on what you want to do so we might help more. But if this is an assignment for class, we would be doing you a disservice by giving you a complete solution.

Piranha
Chief II

It's possible for those, who actually learn programming and develop software. Just implement sending and receiving commands. Not possible by just clicking CubeMX.

ST's employees have not been able implementing a decent USART driver API even for a simple stream reception since the beginning in 2007...

https://community.st.com/s/question/0D53W00001RiIo1SAF/stm32f407-haluartexreceivetoidle-function-missing

Andrew Neil
Evangelist III

@MArvi.1​ "I would like to turn on the LED of the 'slave'"

Maybe look at Firmata: http://firmata.org/wiki/Main_Page ?

Or there are plenty of other protocols that could be used.

Or, as @Danish​ said, create a protocol of your own...

Andrew Neil
Evangelist III

An all-too-common mistake made by people doing this kind of project is to try to implement both ends of the link at once; eg,

0693W00000QLcNJQA1.pnghttps://community.st.com/s/question/0D53W00001dwYIASA2/i2c-master-transmit-and-slave-receive-functions-work-master-receive-and-slave-transmit-dont

An advantage of using an existing, standard protocol is that there is likely to be ready-made test utilities available.

If you invent your own protocol, then you are stuck with having to do both ends of the link!

In this case, I would strongly suggest that your protocol be something that can be tested & debugged using a terminal.

On debugging serial comms:

https://www.avrfreaks.net/comment/2336161#comment-2336161

S.Ma
Principal

I used the android bluetooth electronics protocol. It is generic, frugal, and versatile. HC05 bluetooth enables you to build a debug dashboard on your phone. Save debug time and no crude printf or need text lcd.... one way to enlarge the debug wiggle room.

You mean this: https://www.keuwl.com/apps/bluetoothelectronics/ ?

Sounds a bit beyond the capabilities of the OP ... ? :flushed_face:

S.Ma
Principal

Yes. The app is in the playstorr, the web gives details of protocol and widgets.

Well codesize is small, header is just '*' and bidirectional to get data or control data including receivr and transmit console strings. So to communicatr between mcus or between mcu anx phone app is reusable.Works well with low baud rate. Very well designed because the MCU can push the dashboard layout to the phone, so nothing needs to be preloaded on the phone side. Anuway, this is way beyond the original question....

Thanks for the reply. I asked it a bit vaguely because the topic is a bit of a combination between STM32, Simulink, and Arduino IDE. And I wasn't sure where to ask exactly.

I am trying to control or for now send data/value from one STM32-L476RG board with another using UART/USART. I have attached the Rx pin of the "master" to the Tx of the "slave" and vice versa. The "master" board will be controlled from Simulink similar to this first model of this example. While the "slave" board will be flashed from Arduino IDE, because of compatibility issues with another library that will be added if this is a success. I tried running the code below, but there seems to be no data received by the "slave" board, which I checked through the serial monitor of Arduino IDE. If you have any suggestions on how to fix this, I would greatly appreciate it. Thanks.

0693W00000QM4xGQAT.png

typedef union{
  uint16_t number;
  uint8_t bytes[2];
} INTUNION_t;
 
INTUNION_t myValue_int;
void setup() {
  Serial.begin(115200);
 
}
void loop(){
 
  if(Serial.available()){
    myValue_int.number = getINT();
    int a = myValue_int.bytes[0] | myValue_int.bytes[1] << 8;
    Serial.println();
  }
  else {
    Serial.print("No data received\n");
  }  
}
 
uint16_t getINT(){
  int cont = 0;
  INTUNION_t i;
  while (cont < 2 ){
      i.bytes[cont] = Serial.read();
      cont = cont +1;
  }
  return i.number;
}