usb-stack
MCU-independent USB device and host stack core, shared by the
rp2350-usb,
ch32x035-usb and
esp32p4-usb HAL crates.
Both directions follow the same rule: the protocol lives here, the peripheral
driver lives in the HAL crate and implements one trait. Device drivers implement
[device::UsbBus] (packet-oriented and speed agnostic: a full-speed driver
leaves MAX_PACKET_SIZE at its 64-byte default, a high-speed one overrides it),
host drivers implement [host::HostController] (port state, channels, one packet
per transaction).
Adding it to a project
[]
= "0.2"
The crate is no_std and has no mandatory dependencies; enable the defmt
feature to log through defmt. It requires Rust 1.85 (edition 2024).
Layout
The crate is split by direction, with the vocabulary both need at the root.
| Module | Contents |
|---|---|
endpoint |
EndpointAddress, Direction, EndpointType, direction markers |
descriptor |
bDescriptorType, class, CDC and language-ID codes |
request |
Request: the 8-byte SETUP packet, parsed and built |
device |
UsbBus trait, UsbBusAllocator, ControlPipe, UsbDevice state machine, DescriptorWriter, endpoint handles, UsbClass and the classes |
host |
HostController trait, Host, Device, Pipe, descriptor parsing; host::class holds the host-side classes (HID boot keyboard) |
device and host both re-export the shared vocabulary, so a class driver or a
peripheral driver usually only imports one of them.
Device side
application / class device::class::cdc_acm, your own UsbClass
────────────────────────
device::UsbDevice enumeration, standard requests, EP0
────────────────────────
device::UsbBus trait: allocate, poll, read, write
────────────────────────
(your HAL crate) implements UsbBus
use ;
The bus is packet-oriented: poll reports which endpoints completed an OUT
transfer, an IN transfer or received a SETUP packet, and the device layer
drives read/write from there. Implementations own their endpoint buffers
and must keep them alive for the lifetime of the bus.
Host side
application / class host::class::hid::Keyboard, your own driver
────────────────────────────
host::Device / host::Pipe enumeration, control transfers, bulk/interrupt
────────────────────────────
host::HostController trait: port, channels, transactions
────────────────────────────
(your HAL crate) implements HostController
use ;
The interface is transaction oriented: one call moves at most one packet, and the
protocol layer splits transfers into packets. Data toggles are tracked by the
stack (Toggle), not by the controller, so reprogramming a channel never
corrupts the sequence. Controllers retry NAKs internally and report a STALL as
HostError::Stall.
With a controller in hand, enumeration is one call:
use ;
let host = new;
let device = host.enumerate?; // resets the port and reads the descriptors
device.set_configuration?;
let keyboard = open?; // boot protocol + interrupt endpoint
Hubs, isochronous transfers and SRP/HNP are not implemented; a device must be plugged into the controller's own port.
Classes
HID is a device class, so it has a class driver on each side and they share no
code: device::class::hid makes this MCU act as a keyboard or mouse, while
host::class::hid drives a keyboard that is plugged in.
Device classes: CDC-ACM, HID (boot keyboard, boot mouse, custom reports, HID Power Device), GS_USB (CAN), i2c-tiny-usb, UAC 1.0. Host classes: HID boot keyboard.
Features
defmt(off by default) — log throughdefmt.
Status
Version 0.2 — the API is still settling, and 0.2 moved the device stack under
device without keeping the 0.1 paths.