In the USB device mode, an STM32 microcontroller communicates with a PC directly
USB CDC (Communication Device Class) is a protocol specification for USB communication. There are many other USB classes that specify various protocols over the USB physical layer to enable communication of (data, audio, video devices, HID devices, mass storage, wireless controllers, and much more).
Virtual COM Port (VCP) is a Microsoft Windows interface to access different communication channels (e.g. Serial RS232, USB, etc) as if they were connected to a physical COM port. Therefore, the baud rate value in a serial terminal is meaningless for VCP.
As a result, an STM32 board becomes a USB device and can be connected directly to a PC's USB port.
-
STM32 development board from the Black Pill or Nucleo series can be used. The WeActStudio BluePill Plus board is a good choice. It's an improved version of the popular Blue Pill board, featuring a genuine STM32F103C8T6 chip.
-
ST-Link debugger / programmer board, original or compatible. WeActStudio MiniDebugger is recommended for use with the BluePill Plus board. It is compatible with the original ST-Link and has both SWD and UART interfaces.
-
Jumper wires - 4 for the SWD connector for programming. The amount and type of wires used depend on the board you have.
Tip
For the BluePill Plus board, installed on the breadboard and connected to the MiniDebugger, no jumper wires are needed. Four SWD jumper wires have already been combined with the connector of the debugger and can be directly connected to the SWD connector on the board.
- USB Type-C cable is needed to connect the debugger to a PC. An additional cable of the same type is required for the
bluepill-plus_usb_receiver
project in order to connect the on-board USB to the PC's USB port. Choose the length of the cables according to your needs.
- Connect SWD connector of the STM32 board (4 pins) to the corresponding wires of the debugger (4 jumper wires).
- Connect the first USB Type-C cable to the connector on the STM32 board and a PC USB port for sending and receiving data.
- Connect the second USB Type-C cable to a debugger and a PC for programming and debugging.
Warning
For the Blue Pill clone board, there is a warning (see the references): "The +5V pins on this board are directly connected to the +5V pin of the USB connector. There is no protection in place. Do not power this board through USB and an external power supply at the same time.". So, when connecting the debugger to the Blue Pill board, do not connect 3.3V pin to the SWD connector on the board. Only connect GND, SWCLK, and SWDIO.
Tip
For the BluePill Plus Board don't be afraid to connect a USB cable and an SWD connector to the STM32 board at the same time. It won't damage the board due to good schematics.
STM32CubeIDE needs to be installed in order to generate, edit, build and debug the code.
Hercules SETUP utility will be handy for sending and receiving data.
Tip
When transmitting and receiving data simultaneously, two virtual COM ports are visible in the Windows Device Manager - one for debugging and the other for data exchange. If you notice that no data is being transferred using the Hercules SETUP utility, simply close the COM port and unplug the USB cable from the STM32 board, then plug it back in again. You should see your test string ("Hello, World!") printing and could try sending some test data. Also, in the debug mode, use Live Expressions to see the contents of the created buffer. While transmitting and receiving data at the same time, the STM32CubeIDE should be in debug mode with the resume button pressed.
-
bluepill-plus_usb_transmit The global
CDC_Transmit_FS
function from theusbd_cdc_if.h
library is used to print the string "Hello, World!" every second indefinitely. -
bluepill-plus_usb_receive Contrary to the
CDC_Transmit_FS
function, theCDC_Receive_FS
is a static function defined in theusb_cdc_if.c
file and therefore cannot be used outside of that file. So, we have to define a buffer for the output string in themain.c
file, and then externally define this buffer in theusbd_cdc_if.c
file to use it in theCDC_Receive_FS
function. We modify theCDC_Receive_FS
function by clearing the created buffer, copying the data from the received Buf into our buffer, and clearing the incoming Buf too. -
bluepill-plus_usb_printf This time, we will use the standard
printf
function to output a string and includestdio.h
in ourmain.c
. When called,printf
is redirected to the_write
function defined in the nearbysyscall.c
file._write
, in turn, utilizes theCDC_Transmit_FS
function that we have already used. The while loop inside the main function prints the value of the counter second by second.