Difficulty with getting USB serial [USB CDC] working

I can not seem to get USB serial [USB CDC] working on either a STM 32 Bluepill or Nucleo F767ZI. I can get the USB CDC to workly using the STCUBEMX and TrueStudio tool chain on both devices but not under arduino ststm32. I have tried with latest standard STM32 package and the developmental package on GIT [changing the platform = line].

I have added build flags in Platformio.ini (for the Nucleo F767ZI):

build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
-D PIO_FRAMEWORK_ARDUINO_USB_HIGHSPEED_FULLMODE

I get the same results with/without these flags, when I plug in the USB, I get no response on the windows PC with the Nucleo board and a USB device unrecognized error on BluePill.

I am not sure what I am missing here. Can someone help me out?

Thanks,

Tom

1 Like

I will also add that using the Arduino IDE, I immediately got the Serial over USB [USB CDC] to work on the STM32 Bluepill. How do I get this to work under the PlatformIO IDE?

Tom

What exact platformio.ini are you using for the bluebill board? It might use the Maple core and not the STM32 core, which needs different flag.s

This is the contents of my platformio.ini:

[env:bluepill_f103c8]
platform = ststm
board = bluepill_f103c8
framework = arduino
build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
-D PIO_FRAMEWORK_ARDUINO_USB_FULLMODE

I also tried with:
platform = GitHub - platformio/platform-ststm32: ST STM32: development platform for PlatformIO

Actually I found a typo in the previous message:

Platformio is:

[env:bluepill_f103c8]
platform = GitHub - platformio/platform-ststm32: ST STM32: development platform for PlatformIO
board = bluepill_f103c8
framework = arduino
build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
-D PIO_FRAMEWORK_ARDUINO_USB_HIGHSPEED

I also tried without success:

build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
-D PIO_FRAMEWORK_ARDUINO_USB_HIGHSPEED_FULLMODE

PlatformIO’s build script for activating USB serial support seems really broken right now. It’s missnig needed flags like USBD_VID, USB_MANUFACTURER, USB_PRODUCT, USBCON and HAL_PCD_MODULE_ENABLED.

Regarding high-speed (HS) mode: The bluepill only supports the lesser Full-Speeed (FS) standard. Activating PIO_FRAMEWORK_ARDUINO_USB_HIGHSPEED will cause it to try HS which it doesn’t have, and PIO_FRAMEWORK_ARDUINO_USB_HIGHSPEED_FULLMODE will cause it to put its HS peripheral into FS mode, which it doesn’t even have in the first place, so both macros must be omitted.

Can you test on your bluepill if this works? Baud rate for the CDC should be 115200.

main.cpp

#include <Arduino.h>

void setup(){
	//activate USB CDC driver
	SerialUSB.begin();
}

void loop() {
	SerialUSB.println("Hello world");
	delay(1000);
}

platformio.ini

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = arduino
build_flags = 
    ; 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

EDIT October 2020: Added USBD_PID. Also: Using the dev-platform is not required anymore, you can write platform = ststm32 instead.

EDIT March 2021: Per [CDC] Serial USB without DTR ¡ Issue #1193 ¡ stm32duino/Arduino_Core_STM32 ¡ GitHub you will only get data if the DTR line is enabled. This is what the monitor_dtr = 1 instruction above is for. Also, less flags need to be activated now in the build_flags.

7 Likes

Yes, that new configuration for platformio.org fixed the problem. Thanks! I hope that the original build script will get corrected at some point.

Tom

1 Like

The same problem seem to appear when using the original “Roger Melbourne” (derived from leaflabs)-core.

When using this in platformio.ini:

board_build.core = maple

the same thing seems to happen. The bluepill’s USB does not change from “bootloader” to “serial port”.
A funny detail: if you disable the libUSB device (maple 003) in the device manager and reactivate it after that, then the board activates the virtual serial port.

1 Like

Hi,
thank you, the solution is working. However, there is still a problem.

I am programming a bluepill, and stlink for upload and debug.
If I upload the code, the usb virtual serial port is working.
If I start a debug session, no serial port in the Device Manager, but an unknown usb device instead. So the serial monitor is not working.

I suspect it’s because the debugger stops the uC before proper enumeration.

A workaround:

  • start running the code in the debugger
  • disconnect the board’s usb, and reconnect it, now the virtual usb port appears in the Device Manager
  • restart the serial monitor in PIO
  • stop the uC with a breakpoint
  • now both debugging and serial is working

While it’s working, not really comfortable. Is there a better way?

Great :slight_smile:
I really struggled to make the USB serial monitor work on my BLACKPILL_F411CE work.
I was able to restore the serial monitor by slightly modifying the platformio.ini as follows:

[env:blackpill_f401cc]
platform = ststm32
board = blackpill_f401cc
framework = arduino
upload_protocol = dfu
build_flags =
  -w
  -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC  
  -D USBCON
  -D USBD_VID=0x0483
  -D USB_MANUFACTURER="Unknown"
  -D USB_PRODUCT="\"BLACKPILL_F400CE\""

However, as the saying goes, “Don’t give me a fish, but rather teach me how to fish”,
How do we go about finding the solution to this problem? which documentation if any shall we refer to?

Notice that I am using F401cc board configuration for the F411ce, and that is because the Blackpill_411 board is not yet available in the platformio boards list.
How do we know whether platformio is working on the Bloackpill_F411 board?

Thanks

Can you link said Blackpill F411 board?

PlatformIO already supports a board with a STM32F411CE chip: 96Boards Neonkey — PlatformIO latest documentation.

I have the STM32F411 Core Board WeAct V2.0

1 Like

This board was added in the latest Arduino-STM32 release 16 days ago in version 1.9.0 as “STM32F411CE WeAct Black Pill” (GitHub - stm32duino/Arduino_Core_STM32: STM32 core support for Arduino), the latest for PlatformIO is 1.8.0 however (Releases · platformio/platform-ststm32 · GitHub).

You can either wait a bit until PlatformIO updates its packages or follow workarounds like using upstream stm32 arduino core · Issue #389 · platformio/platform-ststm32 · GitHub in order to get compilation working. You’d also need a new custom board. So I’d rather recommand waiting a bit or using the Arduino IDE in the meantime.

That’s true, Arduino IDE got the 1.9 update and only then the board was available.
Thanks for the help.
:+1:

Hi , i have some problem with PIO USB serial Programming . after change boot loader i can just programing with Arduino IDE , here is my board configuration . and all the time i have some error
PORT

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = arduino
upload_protocol = hid
upload_port = /dev/cu.usbmodem14C01

thx

  1. This seems different from what’s being discussed in this topic (transmitting serial data from an STM32 Arduino board via the USB serial)
  2. please open a new topic with the full error message

I have the same problem, usb serial not working with bluepill.

It compiles fine and the serial port shows on the PC but no data is sent.

It works fine with maple core.

Here’s the platformio.ini:

[env:STM32F103C8]
platform = ststm32
board = genericSTM32F103C8 ; 64K
; board = genericSTM32F103CB ; 128K
; board_build.core = maple
framework = arduino
upload_protocol = stlink
debug_tool = stlink

build_flags = 
  -ggdb
  -D USBCON
  -D USBD_USE_CDC

SOLUTION FOR CS32F103 (So that this MCU can upload sketcks and activate the serial monitor by USB):

[env:bluepill_f103c8_128k]
platform = ststm32
framework = arduino
board = bluepill_f103c8_128k
upload_protocol = stlink
upload_flags = -c set CPUTAPID 0x2ba01477
build_flags =
   -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
   -D USBCON
   -D USBD_VID=0x0483
   -D USBD_PID=0x5740
   -D USB_MANUFACTURER="Unknown"
   -D USB_PRODUCT="\"BLUEPILL_F103C8\""
   -D HAL_PCD_MODULE_ENABLED
3 Likes

I started recently to use stm32duino and platformio for fast prototyping on small projects.
This solution allowed me add USB/serial support on my devices. :+1:

IMPORTANT NOTE: To receive data on PC I needed to enable DTR signal (Data Terminal Ready). Handshaking (flow control) does not need to be activated.

1 Like

Hi there, I’m trying to get serial monitor communication with blackpill stm32f411 via the USB cable, but I don’t know how to proceed, is there a ‘how to’ guide for that?
this is my current platformio.ini:

[env:blackpill_f411ce]
platform = ststm32
board = blackpill_f411ce
framework = arduino
upload_protocol = dfu

what else do I need to add?
thanks in advance