Difficulty with getting USB serial [USB CDC] working

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