Notes / Communication Protocols
USB basics: talking to a PC over a virtual serial port
Descriptors, endpoints and classes, up to CDC, which makes an MCU appear as a COM port when plugged into USB.
Structure
In USB the host (PC) starts all communication. A device answers only when the host asks.
- Enumeration: when a device is plugged in, the host reads its descriptors to learn what kind of device it is and what it can do, and attaches a driver.
- Endpoints: the data windows inside the device. Endpoint 0 is for control, the rest for data (IN is device → host, OUT is host → device)
Transfer types
| Type | Use |
|---|---|
| Control | Configuration, enumeration |
| Bulk | Large data, no guaranteed bandwidth (storage, CDC) |
| Interrupt | Small and periodic (keyboard, mouse) |
| Isochronous | Real-time, no retransmission (audio) |
CDC: virtual COM port
Implement the CDC class and the device shows up on the PC as a COM port, so you can talk to it straight from a serial terminal. The baud-rate setting has no meaning; data moves at USB speed. In STM32CubeMX, enable USB_DEVICE → CDC and you can send with CDC_Transmit_FS().
Hardware cautions
- USB needs an accurate 48 MHz clock (HSE, or an HSI48 with CRS on parts that support it).
- Route D+ and D- as a 90 Ω differential pair, with ESD protection next to the connector
- Self-powered devices need VBUS detection.