Stm32 Serial communication issues

Hello, I am new to the community and to the project I am working on.. I am using a STM32 blue pill board and programming it with a simple code of serial communication :

void setup() {
Serial1.begin(9600);
pinMode(PC13,OUTPUT);
}


void loop() {

Serial1.println("Working\n");
delay(1000);
digitalWrite(PC13,LOW);
for(int i=0;i<2;i++){
Serial1.println(i++);
delay(100);
}
digitalWrite(PC13,HIGH);
}

I am using stm32103c8t6 blue pill board
with generic Stm32f1 series
my doubts :

  1. I previously uploaded a code with memory size >64 kb on the blue pill and it worked and after 10-15 cycles of uploading, the physical board was not working even after i upload the code correctly even with less size.
  2. Now i am using a different stm32blue pill board , but it is having this issue : when I increase the value of i in my code to 2 and after, serial.print suddenly stops working..not even the "working" string is getting printed.
  3. I tried erasing the memory of both boards using stmcube programmer, tried changing board variant to generic stm32f1x, nothing productive has happened yet.
    can someone please direct me on this ? its very crucial for my project..

if you replace Serial1 with Serial and run your program the Serial Monitor displays


0
Working

0
Working

0
Working

0
Working

0
Working

0
Working

in an endless loop - you are resetting i to 0 on each loop()
what do you wish the code to do?

@horace Thanks for the reply !


monitor is empty when i replace Serial1 with Serial...
What i am trying to do is increase the number of print statements on my code...
But after 2 statements, the whole serial communication breaks down :frowning:

You must provide some more information. There are at least two different board-manager packages to program the stm32. Which one do you use?
And there are different possibilities to connect the blue pill to the PC. Did you use the native USB-connection?

@MicroBahner

  1. I am using Arduino STM32 core provided by STM32duino
    https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
  2. I am using FTDI to program the board

What happens if, after loading your sketch, that you disconnect the USB cable from the FTDI device and connect a USB cable between the Bluepill's USB socket and your PC ?

Under tools --> port, do you see other devices apart from COM3 ?

if I run the following program on a STM32F103C8T6

void setup() {
  while(!Serial);
  Serial.begin(115200);
  Serial.println();  Serial.println();
  delay(2000);
  for(long i=0;i<5000l;i++)Serial.print("!");  // delay
  delay(2000);
  Serial.println();  Serial.println();
  Serial.println("STM32F103C blink and serial test");
  pinMode(PC13,OUTPUT);
}


void loop() {

Serial.println("Working\n");
delay(1000);
digitalWrite(PC13,LOW);
for(int i=0;i<2;i++){
Serial.println(i++);
delay(100);
}
digitalWrite(PC13,HIGH);
}

the serial monitor displays



STM32F103C blink and serial test
Working

0
Working

0
Working

0
Working

0
Working

0
Working

0
Working

and the blue LED blinks

I used

@6v6gt My PC doesn't recognize the com port if I'm connecting usb directly...
@horace I had tried this before with the core you are using...still didn't get any results....can you please show the link in the preferences ?

This is how I configure a Bluepill to use its USB socket to attach the serial console. It may give you another com port:

This is the STM32 core I am using:

this is the one
http://dan.drown.org/stm32duino/package_STM32duino_index.json

I got it from Getting-Started-With-Stm32-Using-Arduino-IDE plus the connection to the FTDI board

@horace I tried doing exactly what you were.....I am am the point of breakdown sigh nothings working for past few days....both of my stm blue pills blink with a random delay and nothing goes on serial monitor....is stm32duino really a thing ?

@6v6gt are you getting the data on serial monitor using USB ?

Yes, using a direct cable connection between the Bluepill and the PC but:

  1. I use this option for the Bluepill type:
    image
  2. COM3 on my PC is defined but used for an Intel Service. Check that the serial monitor is looking at the right com port:
    image
    COM17 here is for the Bluepill.
  3. I am using Serial, not Serial1
void setup() {
  Serial.begin(9600);
  pinMode(PC13, OUTPUT);
}


void loop() {

  Serial.println("Working\n");
  delay(1000);
  digitalWrite(PC13, LOW);
  for (int i = 0; i < 2; i++) {
    Serial.println(i++);
    delay(100);
  }
  digitalWrite(PC13, HIGH);
}
  1. I am using ST-Link and the Cube programmer to upload code.

for the serial monitor I use the FTDI board connected to TX1 (PA9) and RX1 (PA10)
the STM32F USB is just to power the device
running the following program

void setup() {
  while(!Serial);
  Serial.begin(115200);
  Serial3.begin(115200);
  Serial.println();  Serial.println();
  delay(2000);
  for(long i=0;i<5000l;i++)Serial.print("!");  // delay
  delay(2000);
  Serial.println();  Serial.println();
  Serial.println("STM32F103C blink and serial test");
  Serial.println("Serial3 at 115200 baud");
  pinMode(PC13,OUTPUT);
}


void loop() {

Serial.println("Working");
Serial3.println("Working");
delay(1000);
digitalWrite(PC13,LOW);
for(int i=0;i<2;i++){
Serial.println(i++);
Serial3.println(i++);
delay(100);
}
digitalWrite(PC13,HIGH);
}

the serial monitor displays

STM32F103C blink and serial test
Serial3 at 115200 baud
Working
0
Working
0
Working
0
Working
0

I connect TX3 (PB10) to an Arduino Due Serial1 Rx (pin 19) and it displays

Working
1
Working
1
Working
1
Working
1

This is the old "Roger Clark" STM32 core.

The OP is using the official STMicroelectronics core > 2.0.0 from this URL https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json

changed to the official STMicroelectronics core
setup is now

ran code

// test of STM32F103C8T6 board LED and serial

#define SerialHW Serial1

void setup() {
  while (!Serial);
  Serial.begin(115200);
  SerialHW.begin(115200);
  Serial.println();  Serial.println();
  delay(2000);
  Serial.println();  Serial.println();
  Serial.println("STM32F103C blink and serial test 1");
  Serial.println("Serial1 at 115200 baud");
  pinMode(PC13, OUTPUT);
}

void loop() {
  Serial.println("Working");
  SerialHW.println("Working");
  delay(1000);
  digitalWrite(PC13, LOW);
  for (int i = 0; i < 2; i++) {
    Serial.println(i++);
    SerialHW.println(i++);
    delay(100);
  }
  digitalWrite(PC13, HIGH);
}

a new COM port appears with the Serial monitor output on the STM32 USB

STM32F103C blink and serial test 1
Serial1 at 115200 baud
Working
0
Working
0
Working
0
Working
0
Working
0
Working
0

and on Serial1 (A9 and A10)

Working
1
Working
1
Working
1

if I change to use Serial2

#define SerialHW Serial2

i get error

stm32.ino.cpp:(.text.setup+0x64): undefined reference to `Serial2'

Serial2 and Serial3 don't appear to be defined

With the STM32, even just the Bluepill, there are a huge number of permutations of core ("roger clark", mapleleaf, 2 versions of the STmicroelectronics core and probably more), bootloader (anyway optional), memory configurations and other configuration options.
At least the OP got plausible messages during software upload so it does not look impossible to solve. There are clones of the STM32F103C8T6 in circulation but I don't think there are many stories in circulation about major incompatibilities.
I was going to point out also that there is a dedicated forum for such issues but, apparently, the OP has already found it: STM32 Serial Communication issues - Arduino for STM32

experimented with a STM B-L072Z-LRWAN1 discovery board (STM32L072CZ micro) - even Serial1 gives undefined reference
The same text appears on the USB COM port and D1 Tx pin
Think I will continue to use STM32cubeIDE for ST boards

reading documentation STM32 hardware serial this setsup Serial2 and Serial 3

// test of STM32F103C8T6 board LED and serial

#define LED_BUILTIN PC13

// hardware serial from https://github.com/stm32duino/wiki/wiki/API#hardwareserial
//                      RX    TX
HardwareSerial Serial2(PA3, PA2);
HardwareSerial Serial3(PB11, PB10);

void setup() {
  while (!Serial);
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial2.begin(115200);
  Serial3.begin(115200);
  Serial.println();  Serial.println();
  delay(2000);
  Serial.println();  Serial.println();
  Serial.println("STM32F103C blink and serial test 1");
  Serial.println("Serial3 at 115200 baud");
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  Serial.println("Working");
  Serial1.println("Working");
  Serial2.println("Working");
  Serial3.println("Working");
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  for (int i = 0; i < 2; i++) {
    Serial.println(i++);
    Serial1.println(i++);
    Serial2.println(i++);
    Serial3.println(i++);
    delay(100);
  }
  digitalWrite(LED_BUILTIN, HIGH);
   // read from port 1, send to port 0:
  if (Serial1.available()) {
    Serial.print("Serial 1 ");
    Serial.println((char)Serial1.read()); 
  } 
  if (Serial2.available()) {
    Serial.print("Serial 2 ");
    Serial.println((char)Serial2.read());  
  }
  if (Serial3.available()) {
    Serial.print("Serial 3 ");
    Serial.println((char)Serial3.read());  
  }

}

serial2 prints

Working
2
Working
2
Working
2

Serial3 prints

Working
3
Working
3
Working
3

the USB COM port displays as text is entered on Serial1 and Serial3

Working
0
Serial 1 1
Working
0
Serial 1 2
Working
0
Working
0
Working
0
Working
0
Serial 3 5
Working
0
Serial 3 6
Working
0
Working

it is important to close the terminal emulator associated with the USB COM port when new code is being uploaded - once uploaded the USB COM port can be reopened

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.