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


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.

Mon May 13 08:42:21 2019 +0000

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 8:114dbc93d9f7 1 #include "stm32f103c8t6.h"
hudakz 0:0279e8c1f111 2 #include "mbed.h"
hudakz 0:0279e8c1f111 3 #include "USBSerial.h"
hudakz 0:0279e8c1f111 4
hudakz 0:0279e8c1f111 5 DigitalOut myled(LED1);
hudakz 0:0279e8c1f111 6
hudakz 0:0279e8c1f111 7 int main() {
hudakz 8:114dbc93d9f7 8 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
hudakz 4:00ccc80cbeb8 9
hudakz 4:00ccc80cbeb8 10 Serial pc(PA_2, PA_3);
hudakz 8:114dbc93d9f7 11 // USBSerial usbSerial; // connection is blocked when USB is not plugged in
hudakz 5:0be85f1bdb35 12 USBSerial usbSerial(0x1f00, 0x2012, 0x0001, false); // connection is not blocked when USB is not plugged in
hudakz 0:0279e8c1f111 13
hudakz 0:0279e8c1f111 14 while(1) {
hudakz 0:0279e8c1f111 15 myled = !myled;
hudakz 2:2c819b99d216 16 pc.printf("I am a serial port\r\n"); // 9600 bit/s
hudakz 3:dd01afd4f893 17 usbSerial.printf("I am a USB serial port\r\n"); // 12 Mbit/s (USB full-speed)
hudakz 0:0279e8c1f111 18 wait_ms(1000);
hudakz 0:0279e8c1f111 19 }
hudakz 0:0279e8c1f111 20 }
hudakz 0:0279e8c1f111 21
hudakz 7:bd396db062ea 22