Back
Loading views...USB
USB Protocol
Overview
USB (Universal Serial Bus) is a widely adopted serial bus standard for connecting peripherals to host computers and embedded systems. It supports multiple speeds from 1.5Mbps to 40Gbps, hot-plugging, power delivery, and a rich class driver ecosystem. Embedded USB stacks (device mode) are common in STM32, PIC32, and similar MCUs.
1. Theory & Fundamentals
- Solves: Universal peripheral connectivity replacing RS-232, PS/2, parallel ports
- Physical layer: Differential pair (D+ and D−), 5V VBUS power
- Speeds: Low Speed 1.5Mbps, Full Speed 12Mbps, High Speed 480Mbps, SuperSpeed 5Gbps+
- Topology: Tree — 1 host, up to 127 devices via hubs
- Connector types: Type-A, Type-B, Mini-B, Micro-B, Type-C
- Power delivery: 5V@500mA (USB 2.0), up to 100W (USB PD)
2. Frame / Packet Structure
Token Packet: SYNC | PID | ADDR(7) | ENDP(4) | CRC5
Data Packet: SYNC | PID | DATA(0–1023 bytes) | CRC16
Handshake: SYNC | PID (ACK / NAK / STALL)
PID values: OUT=0xE1, IN=0x69, SETUP=0xB4, DATA0=0xC3, DATA1=0x4B, ACK=0xD2, NAK=0x5A
- Frames: 1ms (Full Speed), 125µs microframes (High Speed)
- SOF (Start of Frame) token sent every frame to keep sync
3. Protocol Mechanics
- Host-centric: Host initiates all transfers; devices respond
- Transfer types: Control, Bulk, Interrupt, Isochronous
- Endpoint: Logical data pipe; EP0 reserved for control (enumeration)
- Enumeration: Host assigns address, reads descriptors, loads driver
- Data toggle: DATA0/DATA1 alternates to detect lost packets
- NRZI encoding: Data encoded as No-Return-to-Zero-Inverted + bit stuffing
4. Hardware Implementation
- USB transceiver: Built into most MCUs (STM32F1, STM32F4, etc.)
- Pull-up resistors: 1.5kΩ on D+ (Full Speed) or D− (Low Speed) signals device presence
- ESD protection: Essential — PRTR5V0U2X or similar on D+/D−
- Crystal: USB requires accurate clock (8MHz or 12MHz with PLL to 48MHz)
- Cable: 90Ω differential impedance; keep D+/D− matched and paired
5. Register-Level / Configuration
USB at bare-metal level is complex. Most use USB middleware:
// Using STM32 HAL USB Device library (CDC class)
extern USBD_HandleTypeDef hUsbDeviceFS;
USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);
USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);
USBD_Start(&hUsbDeviceFS);
// Send data via CDC (Virtual COM Port)
uint8_t msg[] = "Hello USB\r\n";
CDC_Transmit_FS(msg, sizeof(msg)-1);
6. Driver / Software Development
// USB CDC receive callback
static int8_t CDC_Receive_FS(uint8_t *Buf, uint32_t *Len) {
// Process received data
for (uint32_t i = 0; i < *Len; i++) {
process_byte(Buf[i]);
}
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
return USBD_OK;
}
7. Debugging & Testing
- Use USB protocol analyzer (e.g., Total Phase Beagle, Wireshark USBPcap)
- Device Manager / lsusb: Verify enumeration
- Common issues: Clock not 48MHz → enumeration fails; missing pull-up → not detected; stall on EP0 → descriptor error
- Test with USB HID class first (simplest class)
8. Real-World Applications
- USB CDC (Virtual COM Port): Replace RS-232 with USB for PC communication
- USB HID: Custom joystick, keyboard, mouse implementation
- USB MSC (Mass Storage): Expose SD card as USB drive
- USB DFU: Firmware update over USB
- USB Audio: Custom audio interface for DAW applications
9. Advanced Topics & Edge Cases
- USB OTG: Device can be both host and device (USB On-The-Go)
- USB 3.x: SuperSpeed uses additional SS TX/RX pairs alongside USB 2 D+/D−
- USB PD: Power delivery negotiation up to 100W via CC pins
- Composite device: Appear as multiple classes simultaneously
- USB bootloader: DFU mode for field firmware updates
10. Standards & Variants
| Version | Speed | Notes |
|---|---|---|
| USB 1.0/1.1 | 1.5/12 Mbps | Low/Full speed |
| USB 2.0 | 480 Mbps | High speed, most common |
| USB 3.2 Gen 1 | 5 Gbps | SuperSpeed |
| USB 3.2 Gen 2 | 10 Gbps | SuperSpeed+ |
| USB 4 | 40 Gbps | Thunderbolt compatible |
💡 Practical Examples
Example 1: Virtual COM Port (CDC)
Configure STM32 as CDC device; PC sees it as a serial port. Send debug output at full USB 2.0 speed.
Example 2: USB HID Joystick
// Define HID report: X, Y axes + 4 buttons
uint8_t report[3] = {x_axis, y_axis, buttons};
USBD_HID_SendReport(&hUsbDeviceFS, report, 3);
Example 3: USB DFU Bootloader
Implement DFU class; PC uses dfu-util to flash new firmware over USB.
🧪 Practice Questions
Beginner
- What is the maximum number of USB devices on one host?
- Name the 4 USB transfer types.
- What voltage does USB 2.0 provide on VBUS?
- What is EP0 used for?
- What does USB enumeration mean?
Intermediate
- Explain USB NRZI encoding and bit stuffing.
- How does a USB device signal its speed to the host?
- Describe the USB enumeration sequence step by step.
- What is the difference between USB HID and CDC class?
- How does USB PD negotiate charging voltage?
Advanced
- Write a USB HID descriptor for a custom 6-axis controller.
- Implement a USB composite device with CDC + MSC simultaneously.
- How would you debug enumeration failure on a custom USB PCB?
- Explain USB isochronous transfer and why it's used for audio.
- Design a USB 2.0 full speed PHY circuit for a custom MCU board.
Hands-on Projects
- USB Serial Logger: STM32 CDC device — receive UART data, forward to PC over USB.
- USB HID Macro Keyboard: Custom keyboard with programmable macro keys.
- USB MSC Reader: Expose SPI flash chip as USB mass storage device.
Checklist
- [ ] Explain USB topology and enumeration
- [ ] Implement USB CDC (Virtual COM Port) on STM32
- [ ] Implement USB HID device
- [ ] Write USB descriptors (device, configuration, interface, endpoint)
- [ ] Use USB protocol analyzer to debug enumeration
- [ ] Implement USB DFU bootloader
- [ ] Create composite USB device
- [ ] Handle USB suspend/resume power states
- [ ] Test USB at FS and HS speeds
- [ ] Implement USB OTG host mode