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

@MArvi.1​  "the topic is a bit of a combination between STM32, Simulink, and Arduino IDE"

Again, get rid of as many unknowns as possible.

Thoroughly check & test each part in isolation before trying to bring them all together.

Doesn't Simulink provide any ready-to-use comms components?

"I tried running the code below, but there seems to be no data received by the "slave" board"

Still unclear of your setup here. Where is that code running?

"I checked through the serial monitor of Arduino IDE"

How, exactly, did you "check"? What did you find?

Please provide a block diagram of your setup - including showing where you are "checking"

Again, see the link I gave earlier on testing/debugging serial comms.

"Doesn't Simulink provide any ready-to-use comms components?"

I did use the the Simulink block for STM32's UART, which is the SCI block as you can see on the image I shared previously.

"Still unclear of your setup here. Where is that code running?"

I flashed the code I shared to the "slave" STM32L476RG board using Arduino IDE. And I connected the Simulink model with the "master" board in external mode. And I run it simultaneously.

"How, exactly, did you "check"? What did you find?"

I check it by detecting, whether there are any available data from the serial port with the Serial.available() command. If there are none, then the code will print "No data received" in the built-in serial monitor of Arduino IDE, which was what happened.

It's really hard to follow text descriptions - please draw a block diagram.

"I check it by detecting, whether there are any available data from the serial port with the Serial.available() command."

You still have too many variables there - when it doesn't work, that gives you no idea about:

  • Did the transmitter not transmit at all?
  • Did the transmitter not transmit correctly?
  • Did the receiver just fail to recognise what the transmitter transmitted?
  • etc, etc...

Again, you need to take this one step at a time.

And again, it's not just a software project - your software might be fine, but the hardware connections might be wrong and/or broken

"Did the transmitter not transmit at all?"

I used a digital oscilloscope to detect it and there was voltage there but I am unable to decode what was sent.

So I decided to take a step back and decided to try to code using the HAL library through the STM32CubeIDE and ArduinoIDE. And I tried to follow this thread and this, however when I use an oscilloscope, there seem to be no data being transmitted, but when I use the serial monitor to check the "master" board, there are output being generated.

STM32CubeIDE main code:

int main(void)
{
   char buffer[16];
   int a = 15;
   while (1)
   {
       sprintf(buffer, '&d', a);
       HAL_UART_Transmit(&huart2,(uint8_t *)buffer,strlen(buffer),1000);
   }
}

Arduino IDE code:

void setup(){
  Serial.begin(115200); // Setup Baud Rate
  pinMode(PA5, OUTPUT);
}
void loop(){
  if(Serial.available())
  {
    int data = Serial.read();
    Serial.println(data);
    digitalWrite(PA5, HIGH);
  }
  else
  { 
    digitalWrite(PA5, LOW);
  }
}

@DMavr.1​ "I used a digital oscilloscope to detect it and there was voltage there"

What does that mean?

Was it a valid UART waveform? At the correct baud rate?

"I am unable to decode what was sent."

Why not?

Does a terminal recognise what is sent?

If your 'scope doesn't have decode facilities, then get your code to transmit something that's easy to verify - eg, continuously sent uppercase 'U'

Okay let me show the picture of the scope. Because I am fairly new to UART.

When I send the number 2 as bytes through UART in Simulink:

0693W00000QMAjxQAH.jpg 

When I send the number 1024 as bytes through UART in Simulink:

0693W00000QMAk2QAH.jpg 

Your oscilloscope can save the screen image to a USB stick:

https://www.manualslib.com/manual/1595629/Voltcraft-Dso-Four-Channel-Series.html?page=64#manual

This will give far better, clearer results than a photograph!

"I am fairly new to UART"

There's plenty of resources available; eg, here's a tutorial:

https://learn.sparkfun.com/tutorials/serial-communication

"When I send the number 2 as bytes through UART in Simulink"

Do you know what Simulink actually sends when you do that?

  • The binary value 2 ?
  • The character 2 ?
  • Does it add any padding ?
  • Does it add CR and/or LF ?
  • etc, etc, ... ?

What does a simple terminal show?

Again, Simulink may be giving more confusion than help.

Maybe time to go (back) to basics?

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

"Your oscilloscope can save the screen image to a USB stick:"

Yes, I know but currently I don't have one at hand.

"Do you know what Simulink actually sends when you do that?"

So the input value is set as a uint16, and then with the Byte Pack block in Simulink, it packs the input data into a single output vector type uint8. There are no indications of whether it adds padding or CR/LF.

"I know but currently I don't have one at hand."

:\

"it packs the input data into a single output vector type uint8. There are no indications of whether it adds padding or CR/LF"

So you don't know what it's actually supposed to be sending? How, then, can you possibly write code to receive it??

Again, I think you really to go back to basics.

Or, at least, use a terminal to see exactly what it's sending (may need to be one that can display binary; eg, PuTTY)

sprintf(buffer, '&d', a);

There are two flaws in this line. One of them with an unpredictable consequences.