Introduction to USB
USB (Universal Serial Bus) was introduced to replace legacy serial and parallel ports with a unified interface. It defines:
- Physical layer (cables, connectors, signaling)
- Protocol layer (packets, transactions)
- Device classes (HID, Mass Storage, CDC, etc.)
- Power delivery rules
Unlike UART or SPI, USB is host-driven, meaning devices cannot talk unless the host initiates the communication.
Host (PC / MCU)
|
| USB Cable
|
-------------------
| HUB |
-------------------
| | |
Device Device Device
Key architecture rule:
There is always one host controlling the bus.
USB Architecture
USB uses a tiered-star topology.
Host
- Controls bus timing
- Starts all transfers
- Performs enumeration
Device
- Responds to host requests
- Cannot initiate communication
- Has endpoints
Hub
- Expands number of ports
- Routes traffic between host and devices
USB Physical Layer
USB signaling depends on version.
USB 2.0
- Uses D+ and D− differential pair
- 4 wires:
- VBUS (5V)
- GND
- D+
- D−
USB Cable (USB 2.0)
----------------------
| VBUS | D+ | D- | G |
----------------------
Differential Signaling
Instead of single-ended signals:
D+ ------
> Differential Receiver
D- ------
Advantages:
- High noise immunity
- Low EMI
- Higher speed
This is critical in embedded boards where routing quality matters.
USB Speed Modes
| Version | Speed |
|---|---|
| USB 1.1 Low Speed | 1.5 Mbps |
| USB 1.1 Full Speed | 12 Mbps |
| USB 2.0 High Speed | 480 Mbps |
| USB 3.x SuperSpeed | 5–20 Gbps |
In microcontrollers (STM32, ESP32-S3, NXP etc.), most common:
- Full Speed (12 Mbps)
- High Speed (480 Mbps)
USB Logical Architecture
USB communication is based on:
- Endpoints
- Pipes
- Transfers
- Transactions
- Packets
Let’s build it step by step.
Endpoints
An endpoint is a logical channel inside a USB device.
Think of it like a mailbox.
USB Device
-----------------
| Endpoint 0 | Control
| Endpoint 1 IN |
| Endpoint 1 OUT|
| Endpoint 2 IN |
-----------------
- Endpoint 0: Always present (Control endpoint)
- IN: Device → Host
- OUT: Host → Device
USB Transfer Types
USB defines 4 transfer types:
Control Transfer
Used for:
- Enumeration
- Configuration
- Command/Response
Example:
- Host asking device: “Who are you?”
Bulk Transfer
Used for:
- Large reliable data
- No timing guarantee
Example:
- USB flash drive
Interrupt Transfer
Used for:
- Small periodic data
- Low latency
Example:
- Keyboard
Isochronous Transfer
Used for:
- Real-time streaming
- No retransmission
Example:
- Audio streaming
USB Packet Structure
Every USB transfer is packet-based.
+--------+---------+---------+-------+
| SYNC | PID | Payload | CRC |
+--------+---------+---------+-------+
- SYNC: Synchronization
- PID: Packet ID
- Payload: Data
- CRC: Error check
USB Enumeration Process
Enumeration is the process where the host identifies and configures a device.
Step-by-step:
- Device connected
- Host detects pull-up resistor
- Host resets device
- Host requests descriptors
- Host assigns address
- Host sets configuration
Device Plugged
|
Host Detects Pull-up
|
Bus Reset
|
Get Device Descriptor
|
Assign Address
|
Get Configuration Descriptor
|
Set Configuration
After this → device is ready.
USB Descriptors
Descriptors describe device capabilities.
Device Descriptor
Contains:
- Vendor ID (VID)
- Product ID (PID)
- USB version
- Class type
Configuration Descriptor
- Power requirements
- Interfaces
Interface Descriptor
- Functional grouping
Endpoint Descriptor
- Transfer type
- Max packet size
Example simplified structure:
Device
└── Configuration
└── Interface
├── Endpoint IN
└── Endpoint OUT
USB Device Classes
USB classes allow generic drivers.
HID (Human Interface Device)
- Keyboard
- Mouse
CDC (Communication Device Class)
- Virtual COM port
Mass Storage
- Flash drive
Audio
- Microphone
- Speaker
In embedded firmware:
Example (STM32 + CDC):
PC <----USB----> STM32
|
Virtual COM
|
Firmware UART bridge
Very common for debugging.
USB in Microcontrollers
Modern MCUs integrate USB peripheral.
Example MCUs:
- STM32F4
- ESP32-S3
- NXP LPC series
Internal block:
AHB Bus
|
----------------
| USB Device |
| Controller |
----------------
|
PHY Interface
|
D+ / D-
Firmware must:
- Enable USB clock
- Configure GPIO pins
- Initialize stack
- Handle interrupts
USB Interrupts
USB is interrupt-driven.
Events:
- Reset
- Setup packet received
- Data IN complete
- Data OUT received
Typical ISR flow:
USB_IRQHandler()
{
if (RESET)
handle_reset();
if (SETUP)
process_setup();
if (DATA_IN)
transmit_next_packet();
}
USB Power Delivery
USB 2.0:
- Default: 100 mA
- After enumeration: 500 mA
USB-C:
- Can negotiate up to 100W
- Uses CC pins
In embedded design:
- Never draw >100mA before enumeration
- Implement current limiting
- Protect with TVS diodes
USB Type Connectors
USB-A
- Host side
USB-B
- Printer style
Micro USB
- Old embedded boards
USB-C
- Reversible
- High power
- High speed
Real Embedded Examples
Firmware Update via USB (DFU)
PC Tool ----USB----> MCU Bootloader
|
Flash new firmware
USB CDC Debug Console
printf("Hello\n");
UART -> USB CDC -> PC Terminal
USB Mass Storage Data Logger
Device appears as flash drive:
MCU logs data
|
FAT File System
|
PC reads CSV file
Design Considerations
PCB Layout:
- 90Ω differential impedance
- Keep D+ / D− matched length
- Avoid stubs
- Add ESD protection
Clock Accuracy:
- USB FS requires ±0.25% accuracy
- Use crystal or internal calibration
USB Host Mode in Embedded
Some MCUs support OTG (On-The-Go).
Example:
Embedded Linux Board
|
USB Host Mode
|
USB Flash Drive
In this case MCU becomes host and enumerates device.