USB
Enumeration, descriptors, endpoints, transfer types and device classes.
The host is in charge
USB is a host-centric bus. The host (a PC, phone, or an MCU in host mode) schedules every transfer; a device only speaks when asked. Most microcontroller work is on the device side: making the board appear as a serial port, a keyboard, a storage drive or a custom device.
| Speed | Rate | Common on |
|---|---|---|
| Low speed | 1.5 Mbit/s | Keyboards, mice |
| Full speed | 12 Mbit/s | Most MCUs with built-in USB |
| High speed | 480 Mbit/s | Higher-end MCUs (often with an external ULPI PHY) |
| SuperSpeed and beyond | 5 Gbit/s+ | Application processors |
The data lines (D+ and D−) are differential. Full speed needs a clock accurate to ±0.25%, so either a crystal or a clock-recovery system that trims an internal oscillator from the host's start-of-frame packets.
Endpoints and pipes
A device exposes endpoints: numbered buffers, each with a direction (IN means device to host, OUT means host to device). Endpoint 0 is mandatory and bidirectional; it is the control endpoint used for configuration. Other endpoints carry the actual data.
Transfer types
| Type | Guarantees | Used for |
|---|---|---|
| Control | Delivery, error-checked; bursty | Enumeration, configuration, commands (endpoint 0) |
| Bulk | Delivery, error-checked; no timing guarantee, uses spare bandwidth | Storage, serial data, printers |
| Interrupt | Delivery, error-checked; bounded latency (polled every N ms) | Keyboards, mice, small status updates |
| Isochronous | Reserved bandwidth and timing; no retry on error | Audio, video |
"Interrupt" transfers are not interrupts: the host polls the endpoint at a fixed interval. A device cannot push data to the host on its own.
Enumeration
When a device is plugged in:
- The device pulls D+ (full speed) or D− (low speed) high through 1.5 kΩ; the host detects the connection and speed.
- The host resets the bus; the device now answers at address 0.
- The host requests the first bytes of the device descriptor to learn the maximum packet size of endpoint 0.
- The host assigns a unique address (
SET_ADDRESS). - The host reads the full device descriptor, then the configuration descriptor with all its interface and endpoint descriptors, and string descriptors.
- The host picks a driver, by class code or by vendor and product IDs (VID/PID).
- The host sends
SET_CONFIGURATION; the device's data endpoints become active.
If any step fails, the operating system reports "device descriptor request failed" or "unknown device". Most bugs are in descriptor contents or in responding too slowly to setup requests.
Descriptors
Descriptors are byte structures in flash that tell the host what the device is.
| Descriptor | Key contents |
|---|---|
| Device | USB version, class, VID, PID, device version, max packet size for EP0, number of configurations |
| Configuration | Total length, number of interfaces, power (self or bus-powered, max current in 2 mA units) |
| Interface | Class, subclass, protocol, number of endpoints |
| Endpoint | Address and direction, transfer type, max packet size, polling interval |
| String | UTF-16 text: manufacturer, product, serial number |
#include <stdio.h>
#include <stdint.h>
/* A standard USB device descriptor: 18 bytes, little-endian multi-byte fields. */
static const uint8_t device_desc[18] = {
18, /* bLength */
0x01, /* bDescriptorType: device */
0x00, 0x02, /* bcdUSB 2.00 */
0xEF, 0x02, 0x01, /* class: miscellaneous / IAD (common for composite CDC) */
64, /* bMaxPacketSize0 */
0x83, 0x04, /* idVendor 0x0483 */
0x40, 0x57, /* idProduct 0x5740 */
0x00, 0x01, /* bcdDevice 1.00 */
1, 2, 3, /* string indices: manufacturer, product, serial */
1 /* bNumConfigurations */
};
int main(void) {
const uint8_t *d = device_desc;
printf("USB %x.%02x, EP0 max packet %u\n", d[3], d[2], d[7]);
printf("VID 0x%04X, PID 0x%04X\n", d[8] | (d[9] << 8), d[10] | (d[11] << 8));
return 0;
}
The VID/PID pair identifies the product. Using a vendor's example IDs is fine for development; shipping a product needs your own VID (from the USB-IF) or a PID allocated by your MCU vendor or a sub-licensing programme.
Device classes
Implementing a standard class means the operating system already has a driver:
| Class | Appears as | Typical use |
|---|---|---|
| CDC-ACM | Virtual serial port (COM port, /dev/ttyACM0) | Debug console, configuration, data logging |
| HID | Keyboard, mouse, game controller, or generic HID | Input devices; also simple driverless custom data (64-byte reports) |
| MSC | Mass storage drive | Drag-and-drop firmware update, data logs on flash or SD |
| Audio (UAC) | Sound card | Microphones, DACs, headsets |
| DFU | Firmware-update device | Bootloaders |
| Vendor-specific | Needs libusb or a WinUSB descriptor | Custom high-throughput interfaces |
A composite device combines several (CDC plus MSC is common), using interface association descriptors.
Software stacks
You rarely write USB protocol code from scratch. Common stacks: TinyUSB (open source, many MCUs), vendor stacks (STM32 USB device library, NXP, Microchip), and RTOS stacks (Zephyr USB, USBX). Your job is configuring descriptors and handling class callbacks.
Pitfall: USB interrupts must be serviced promptly; the host expects responses to setup packets within milliseconds. A long critical section or a blocking call in the USB callback context causes enumeration failures that appear random.
Power
A bus-powered device may draw 100 mA until configured, then up to the amount it declared (500 mA for USB 2.0). Suspend mode (no bus activity for 3 ms) limits it to 2.5 mA. USB-C adds the CC lines: a device signals its presence with 5.1 kΩ pull-downs on CC1 and CC2, without which a USB-C charger provides no power at all, a frequent board bug.
Interview question: Walk through USB enumeration.
Answer: The device connects its pull-up, so the host detects it and its speed, resets the port, and talks to it at address 0 on endpoint 0. The host reads the device descriptor (first to learn EP0's max packet size), assigns an address with SET_ADDRESS, reads the full device, configuration (with interface and endpoint) and string descriptors, chooses a driver by class or VID/PID, and sends SET_CONFIGURATION, after which the device's data endpoints are active.
Interview question: Your board should appear as a serial port to a PC. Which class, and what are common problems?
Answer: CDC-ACM, which every major OS supports without a custom driver. Common problems: no crystal or clock recovery so the 48 MHz USB clock is inaccurate; wrong or inconsistent descriptor lengths; the USB interrupt blocked by long critical sections so setup requests time out; missing pull-up on D+ (or, on USB-C, missing 5.1 kΩ CC pull-downs so the host never powers the port); and the firmware sending data before the host has opened the port, which with flow control enabled can block the transmit path.
Going deeper
Interview question: How would you make a MIDI keyboard work on any computer without a driver?
Answer: Implement the standard USB MIDI class (part of USB Audio), with correct descriptors, so every operating system's built-in driver recognises it. Send note events promptly with the keyboard's scan rate and velocity, handle suspend and resume, and test on Windows, macOS and Linux.
Interview question: What is the difference between USB full speed and high speed for a microcontroller?
Answer: Full speed is 12 Mbit/s and is supported by an on-chip PHY on most USB-capable MCUs, needing only an accurate 48 MHz clock. High speed is 480 Mbit/s, needs a high-speed PHY (on-chip on some parts, otherwise an external ULPI PHY) and careful board layout, and is used for large data such as audio interfaces and mass storage. Practical throughput is lower than the raw rate for both because of protocol overhead and the device's ability to keep up.
Interview question: How does a device implement firmware updates over USB without a custom PC driver?
Answer: Implement the standard DFU (Device Firmware Upgrade) class, which operating systems and tools like dfu-util support, or present as a mass storage device and accept a file copied to it (the UF2 bootloader approach), or use a HID-based protocol, which needs no driver on any OS. The update image should still be signed and verified by the bootloader.
Explain it: In your own words, explain what happens when a USB device is plugged into a computer.
Answer: The device connects a pull-up on one data line, which tells the host something was attached and at what speed. The host resets the port and talks to the device at the default address using the control endpoint, reads its device descriptor, gives it a unique address, then reads its configuration, interface, endpoint and string descriptors. From those it chooses a driver, by class or by vendor and product ID, and finally selects a configuration, after which the device's data endpoints start working.