PlatformIO Community

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

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 = https://github.com/platformio/platform-ststm32.git

Actually I found a typo in the previous message:

Platformio is:

[env:bluepill_f103c8]
platform = https://github.com/platformio/platform-ststm32.git
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 = https://github.com/platformio/platform-ststm32.git
board = bluepill_f103c8
framework = arduino
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

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

5 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: https://docs.platformio.org/en/latest/boards/ststm32/b96b_neonkey.html.


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” (https://github.com/stm32duino/Arduino_Core_STM32#generic-stm32f4-boards), the latest for PlatformIO is 1.8.0 however (https://github.com/platformio/platform-ststm32/releases).

You can either wait a bit until PlatformIO updates its packages or follow workarounds like https://github.com/platformio/platform-ststm32/issues/389 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
2 Likes