PlatformIO Community

Cannot upload to STM32 blue pill fake after pio update

Hi,
I was developing some projects with pio and an stm32 f103c8t6 a few months ago. Unfortunately now all serial communication seems to be broken. Also previously I was able to use multiple serial ports before. Now compiling a program using another port than Serial1 does lead to a ld compilation error. Any ideas what happened?

When I set board_build.core=maple at least in the ide all three serial ports are detected correctly. Also debugging works. However still no data arrives on the serial monitor, there is jot even a device detected :frowning:

What exact platformio.ini and code are you running? You’re trying to use the USB serial (CDC)?

Thanks for your answer!
The solution in this post actually fixed the serial usb issue for me: Difficulty with getting USB serial [USB CDC] working

However, when trying to compile while using Serial2 or Serial3, I get the following error:

main.cpp:(.text.setup+0x20): undefined reference to `Serial2'
collect2: error: ld returned 1 exit status
*** [.pio/build/genericSTM32F103C8/firmware.elf] Error 1

Does anyone have an idea why all this is necessary to only get the sub serial port running? Is there a way to fix this or to roll back to the old version which worked seamlessly? I also realized that the IDE does no more recognize that my board only has 3 serial ports and not 10. This is fixed by setting board_build.core = maple, then I can also compile when using Serial2, however the USB Serial does not work even with the solution from the link posted above.

Which core do you intend to use? Maple or STM32 one? They have different ways of enabling the serial ports.

currently stm32. But when using maple I get the strange error when trying to compile with serial2.

There is really weird issue that you have to set the (virtual) DTR line now for your to be able to receive data: https://github.com/stm32duino/Arduino_Core_STM32/issues/1193

I was able to do this using http://der-hammer.info/pages/terminal.html and clicking on the “DTR” button in the “input control” section and in PlatformIO per monitor_dtr.

I was just really struggling with this, too.

Also, some of the flags above are not necessary anymore. Please try the following platformio.ini:

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
build_flags = 
    ; enable Serial2 and 3
    -D HAVE_HWSERIAL2
    -D HAVE_HWSERIAL3
    ; enable USB serial
    -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
    -D USBCON
; QUIRK: without setting this, no  
; data will be received on the serial USB port
; https://github.com/stm32duino/Arduino_Core_STM32/issues/1193
monitor_dtr = 1
; optional: set COM port to monitor here if there are multiple
;monitor_port= COM27

with the following src\main.cpp

#include <Arduino.h>

void setup() { 
    Serial1.begin(115200);
    Serial2.begin(115200);
    Serial3.begin(115200);
    SerialUSB.begin(115200);
    pinMode(PC13, OUTPUT); //blinky
    //while(!Serial); //wait for USB connection
}

void loop() { 
    Serial1.println("Hello from Serial1");
    Serial2.println("Hello from Serial2");
    Serial3.println("Hello from Serial3");
    SerialUSB.println("Hello from SerialUSB");
    SerialUSB.flush();
    digitalWrite(PC13, digitalRead(PC13) ^ 1); //blinky
    delay(500);
}

Then a “upload” and then a “monitor” gives me

> Executing task in folder bluepill_f103c8_128k_repro: C:\Users\Max\AppData\Local\Programs\Python\Python38\Scripts\platformio.exe device monitor --environment genericSTM32F103C8 <

--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at http://bit.ly/pio-monitor-filters
--- forcing DTR active
--- Miniterm on COM27  9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
Hello from SerialUSB
Hello from SerialUSB
Hello from SerialUSB
Hello from SerialUSB
Hello from SerialUSB
Hello from SerialUSB

--- exit ---

Yes, this works now! Thank you so much!
If you know a way to make this work out of the box or at least to document it properly, I am willing to put work into it. PIO is such a cool project!

Mhm I was about to open an issue for the fact that PIO_FRAMEWORK_ARDUINO_ENABLE_CDC isn’t enough to get the USB-CDC working (due to mising USBCON) but it’s already been fixed at https://github.com/stm32duino/Arduino_Core_STM32/pull/1164 so it will be included in the next release. So the already existing PlatformIO documentation will be right for that.

As for having to do HAVE_HWSERIAL2 etc., those things are documented in the STM32 core as they’re specific for that, and they’ve done so in their wiki page

By default, only one Serialx instance is available mapped to the generic Serial name.
To use a second serial port, a HardwareSerial object should be declared in the sketch before the setup() function:
[or]
Another solution is to add a build_opt.h file alongside your main .ino file with: -DENABLE_HWSERIALx. This will define the Serialx instance using the first USARTx instance found in the PeripheralPins.c of your variant.

And then PlatformIO has docs for adding these -D commands to the compiler invocation.

It’s a bit scattered but the right documentation is at the right places.

But the having to set the DTR of the serial adapter to a specific value for output to appear also caught me by suprise and this should be addressed in the documentation of the core though. I’ve already commented in the linked issue.

Still, one can create a PR for the documentation files, e.g. at https://github.com/platformio/platformio-docs/blob/develop/platforms/ststm32_extra.rst, for extra commentary / clarifications / links to other Wikis.

I am sorry to disturb you again, but after the recent update I do have another issue related to the serial port: After starting the ide, I can once open the serial monitor and everything works fine. However if I terminate the terminal process and try to restart it again, I get the following error:

platformio device monitor <

--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at http://bit.ly/pio-monitor-filters
could not open port '/dev/ttyACM0': [Errno 5] could not open port /dev/ttyACM0: [Errno 5] Input/output error: '/dev/ttyACM0'
The terminal process "platformio 'device', 'monitor'" terminated with exit code: 1.

This does not happen if I attach other devices like my arduino nano.

Interesting, the same occurs in BluePill stm32f103c8t6 USBSerial issues. However there I could not reproduce the error with any linux (or Windows) operating system I’ve tried.

Is the code and paltformio.ini you’re testing this with the exact same as posted above?

This is my current platformio.ini:

platform = ststm32
board = genericSTM32F103CB
framework = arduino
upload_protocol = stlink
debug_tool = stlink
build_flags = 
	-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
	-D USBCON
monitor_dtr = 1

What does monitor_dtr do by the way?