Building a USB serial device with STM32F103C8T6 board

Dependencies:   mbed mbed-STM32F103C8T6 USBDevice_STM32F103

Building a USB serial device with STM32F103C8T6 board

A USB device stack has been developed by mbed in order to provide all the great capabilities of USB. The USBdevice class has been extended by Norimasa Okamoto to support also the NUCLEO-F103RB boards (and others). For more information concerning the stack architecture, visit the USBDevice stack architecture.
The USBSerial class uses the USB interface to emulate a serial port. The STM32F103C8T6 board is recognized by the computer as a serial port. This is a great solution to communicate easily between the microcontroller and a computer. The mbed serial port works by default on Mac, Linux and Windows 10, but earlier version of Windows needs a driver. These instructions explain how to setup the mbed Microcontroller to use the USB serial port on Windows 7 and earlier.

Zoom in /media/uploads/hudakz/stm32f103c8t6_usb_serial.jpg

NOTE:

In this application, power the STM32F103C8T6 board over a USB cable connected to the Micro-B USB connector (see in picture above) which you are going to use also for the USB serial communication with the connected PC. Once the binary has been downloaded to the board, disconnect and then reconnect the USB cable to the STM32F103C8T6 board to start running the program and to get the USB serial connection work.

main.cpp

Committer:
hudakz
Date:
7 months ago
Revision:
8:114dbc93d9f7
Parent:
7:bd396db062ea

File content as of revision 8:114dbc93d9f7:

#include "stm32f103c8t6.h"
#include "mbed.h"
#include "USBSerial.h"

DigitalOut  myled(LED1);

int main() {
    confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
    
    Serial    pc(PA_2, PA_3);
//    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
    
    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);
    }
}