STM32F103C8T6 (Bluepill board) USB CDC Usage possible?

13 Feb 2019

Hi,

is it possible to connect the bluepill board via USB to the PC via CDC driver? I see the following codesnippet on the mbed website:

usbserial

#include "mbed.h"
 
Serial pc(USBTX, USBRX); // tx, rx
 
int main() {
    pc.printf("Hello World!\n");
    while(1);
}

Here, a Serial object will be constructed with USBTX and USBRX as pins. But these pins are mapped to PA_2 and PA_3 on board which is effectively a UART on the STM32F103C8T6 controller. The SERIAL_TX, and SERIAL_RX are mapping to the same pins. Do I configure here a USB CDC connection (by using the onChip USBCore) or a standard UART connection?

Thanks,

Regards Tobias

14 Feb 2019

Hi,

I got an answer from Zoltan Hudak (thanks!):

Yes, you can connect a Blue Pill to your PC over a USB cable using mbed's CDC driver. For more deatails have look at this example: https://os.mbed.com/users/hudakz/code/STM32F103C8T6_USBSerial/.

So with a standard mbedos 5 project you only have a standard UART. The naming of USBTX and USBRX is a little bit unhappy.

14 Feb 2019

In case of mbed enabled NUCLEO boards an UART/USB converter is running on the ST-LINK/V2-1 part (optionally can be detached from the MCU part). That is connected to the PA_2 and PA_3 pins (USBTX and USBRX are additional alias names of those pins defined in the PinNames.h file). To have similar HW setup when using a Blue Pill board you can connect a stand alone UART/USB converter (or the ST-LINK/V2-1 part of a NUCLEO board over the CN3 connector, but in that case the MCU part shall be disconnected by turning off the SB13 and SB14 solder briges on the NUCLEO board ) to the PA_2 and PA_3 pins and then use the USBTX and USBRX pin names in your program as well. Please notice that in both setups a UART/USB converter is used (either an ST_LINK/V2-1 or a stand alone one) and a virtual serial port appears on the PC.

Also notice that the setup using the USB connector located on the Blue Pill board is quite different. In that case no UART/USB converter is used (in fackt, UART is not involved at all). It is a true full-speed (12Mbitps) USB connection utilising mbed's CDC driver running on the PC in order to have a virtual serial port available.
To build such connection on a NUCLEO-F103RB board an external USB connector shall be connected to the PA_11 (USB_DM) and PA_12 (USB_DP) pins. The PA_12 pin (USB_DP) shall be at the same time connected to +3.3V over a 1.5k resistor.

To feel the difference, you can easily build a USB mouse/keyboard device with the Blue Pill board by modifying the USB serial device program for example as follows:

#include "stm32f103c8t6.h"
#include "mbed.h"
//#include "USBSerial.h"
#include "USBMouseKeyboard.h"
 
int main() {
    confSysClock();         //Configure system clock (72MHz HSE clock, 48MHz USB clock)
    
    Serial    pc(PA_2, PA_3);
    DigitalOut  myled(LED1);
    //USBSerial usbSerial;    // connection is blocked when USB is not plugged in
    //USBSerial usbSerial(0x1f00, 0x2012, 0x0001,  false);// connection is not blocked when USB is not plugged in
    USBMouseKeyboard key_mouse;
    
    while (1) {
        //myled = !myled;
        //pc.printf("I am a serial port\r\n");            // 9600 bit/s
        //usbSerial.printf("I am a USB serial port\r\n"); // 12 Mbit/s (USB full-speed)
        //wait_ms(1000);
 
        key_mouse.mediaControl(KEY_VOLUME_DOWN);
        key_mouse.printf("Hello World from Mbed\r\n");
        key_mouse.keyCode('s', KEY_CTRL);
        key_mouse.move(20, 0);
        key_mouse.keyCode(KEY_SCROLL_LOCK);
        wait(1);
        myled = key_mouse.lockStatus();
    }
}

After connecting the programmed Blue Pill to the PC a new HID device shall appear on the PC emulating a USB mouse/keyboard.

07 May 2019

Sorry for necroing, but going down this rabbithole again, I've found that the USB device libraries completely lock up the Bluepill just by being included. As in, if you use them, the bluepill doesn't even start. I've confirmed this across multiple devices.

I assume that's not the behavior you've experienced? Or is this just a symptom of Mbed classic's general abandonment?

15 May 2019

This example works fine for me. Please notice that an mbed_app.json file configures the Blue Pill to use external crystal for system clock, provide clock for the USB peripheral and to use the small mbed library model in order to reduce the required SRAM and flash memory. Also remember to power cycle the Blue Pill after being programmed.