Learning STM32 USB

Official Documentation

These two official documents provide a detailed description of the protocol and hardware features, along with some code explanations.

“USB Training_Part1_Protocol.pdf”, “USB CDC Class Introduction Training.pdf”

Reference website:https://www.usbzh.com/

•Library ExamplesUser-related files

usbd_desc.c: This file provides the USBD descriptors and string formatting method.

usbd_conf.c: This file implements the USB Device library callbacks and MSP.

usbd_cdc_interface.c: Source file for USBD CDC interface.

stm32l4xx_hal_msp.c: UART configuration.

Learning STM32 USB

STM32 USB Hardware

Low-speed signals rely on voltage levels to transmit signals, and USB is no exception. However, high-speed signals such as PCIe and Serdes require special encoding methods to reduce errors. USB uses NRZI (Non-Return-to-Zero Inverted) encoding or 8b/10b encoding schemes.

The STM32L4 manual states that the USB RAM buffer size is 1024 Bytes and supports 8 endpoints. Endpoints refer to a data buffer for USB host/device used to store various data sent and received via USB.

USB Protocol

There are two diagrams online that I find quite helpful for initial understanding of the USB framework. One is protocol-related and the other is architecture-related. Both can be found online.

Learning STM32 USB

1.Transmission Format: Bit Stream -> Field -> Packet -> Transaction -> Transfer -> Class -> Application

2.Seven Types of Fields: SYNC / PID / ADDR / ENDP / FRAM / DATA / CRC

ADDR is only 7 bits, so like I2C, USB can only connect to 127 slaves.

3.There are four types of packets: Token Packet / Data Packet / Handshake Packet / Special Packet.

4.There are three types of transactions: IN / OUT / SETUP, each transaction consists of three phases: token packet, data packet, and handshake packet.

5.There are four types of transfers: Interrupt / Bulk / Isochronous / Control.

Control transfer is relatively complex, including the initial setup phase: Device insertion detection -> Device reset -> Retrieve device descriptor, configuration descriptor, interface and endpoint descriptors -> Configure device -> Prepare for interaction.

USB Classes

1. CDC (Communication Device Class)

2. MSC (Mass Storage Class)

3. Audio Class

4. Video Class (UVC)

5. Printer Class

6. Image Class (PTP/MTP)

7. Hub Class

8. CDC-ECM (Ethernet Control Model)

9. CDC-ACM (Abstract Control Model)

CDC is practical. The library examples implement a method for using USB as a virtual serial port. Serial data is converted to USB data for transmission. The driver is responsible for packaging and encapsulating the serial data according to the USB protocol. Then the driver sends it to the computer via the USB interface. The receiving end performs the reverse operation, retrieving data from the interface, unpackaging it according to the USB protocol, and restoring it to serial data.

This means that USB to UART / TCP/IP / CAN / SPI… can all be implemented.

Device Descriptors

USB descriptors include Device Descriptor,

Configuration Descriptor,

Interface Descriptor,

Endpoint Descriptor,

and String Descriptor.

HID devices have HID descriptors, report descriptors, etc.

The class files in the library examples have already completed the configuration for some classes. You can learn and review them here; I will only list the knowledge points that are easy for me to grasp initially.

Leave a Comment