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.3"
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, AddressPool, descriptor parsing; host::class holds the host-side classes (HID boot keyboard, hub) |
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, host::class::hub::Hub, 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 are supported through host::class::hub::Hub, which powers the downstream
ports, resets a port and enumerates the device behind it. Devices need an
address of their own; host::AddressPool hands them out:
use ;
let device = host.enumerate?;
let mut addresses = new;
addresses.reserve;
let hub = open?;
hub.power_ports?;
hub.wait_for_connection?;
hub.reset_port?;
let child = hub.enumerate_child?;
A full-speed or low-speed device behind a high-speed hub needs split
transactions. The stack records the hub and port in HubRoute::translator;
controllers without a transaction translator report HostError::Unsupported
for such devices. Put the hub on a full-speed host, or run a high-speed host in
FS/LS-only mode, and the same devices work without splits.
Isochronous transfers and SRP/HNP are not implemented.
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 and USB 2.0 hubs.
Features
defmt(off by default) — log throughdefmt.
Status
Version 0.3 — the API is still settling. 0.3 added hub support and moved the
per-device speed and hub route into EndpointConfig, which host controller
implementations have to honour.