Expand description
§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
[dependencies]
usb-stack = "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 UsbBususe usb_stack::device::{PollResult, UsbBus, UsbError, UsbResult, UsbDeviceBuilder};
struct MyBus { /* peripheral state */ }
impl UsbBus for MyBus {
fn alloc_ep(&mut self, dir: Direction, addr: Option<EndpointAddress>,
ep_type: EndpointType, max_packet: u16, interval: u8)
-> UsbResult<EndpointAddress> { /* … */ Ok(EndpointAddress::EP0_IN) }
fn enable(&mut self) { /* attach + enable the peripheral */ }
fn reset(&self) { /* re-init endpoints after a bus reset */ }
fn set_device_address(&self, addr: u8) { /* … */ }
fn poll(&self) -> PollResult { /* decode pending tokens, return masks */ }
fn write(&self, addr: EndpointAddress, buf: &[u8]) -> UsbResult<usize> { /* … */ }
fn read(&self, addr: EndpointAddress, buf: &mut [u8]) -> UsbResult<usize> { /* … */ }
fn set_stalled(&self, addr: EndpointAddress, stalled: bool) { /* … */ }
fn is_stalled(&self, addr: EndpointAddress) -> bool { /* … */ }
fn suspend(&self) {}
fn resume(&self) {}
}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 HostControlleruse usb_stack::host::{HostController, HostResult, PortStatus, Channel, DeviceAddress, EndpointConfig, Toggle};
struct MyHost { /* peripheral state */ }
impl HostController for MyHost {
fn port_status(&self) -> PortStatus { /* … */ }
fn reset_port(&self) -> HostResult<PortStatus> { /* bus reset + speed */ }
fn alloc_channel(&self) -> HostResult<Channel> { /* … */ }
fn free_channel(&self, channel: Channel) { /* … */ }
fn configure(&self, channel: Channel, device: DeviceAddress,
endpoint: &EndpointConfig) -> HostResult<()> { /* … */ }
fn setup(&self, channel: Channel, packet: &[u8]) -> HostResult<()> { /* … */ }
fn transfer_in(&self, channel: Channel, toggle: Toggle,
buffer: &mut [u8]) -> HostResult<usize> { /* … */ }
fn transfer_out(&self, channel: Channel, toggle: Toggle,
data: &[u8]) -> HostResult<()> { /* … */ }
fn wait_millis(&self, millis: u32) { /* … */ }
}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 usb_stack::host::{Host, class::hid::Keyboard};
let host = Host::new(&my_host);
let device = host.enumerate()?; // resets the port and reads the descriptors
device.set_configuration(1)?;
let keyboard = Keyboard::open(&device)?; // boot protocol + interrupt endpointHubs 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 usb_stack::host::{AddressPool, class::hub::Hub};
let device = host.enumerate()?;
let mut addresses = AddressPool::new();
addresses.reserve(device.address());
let hub = Hub::open(&device)?;
hub.power_ports()?;
hub.wait_for_connection(1, 2_000)?;
hub.reset_port(1)?;
let child = hub.enumerate_child(1, addresses.take().unwrap())?;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.
Modules§
- descriptor
- USB descriptor vocabulary shared by both directions of the stack.
- device
- The device side of the stack.
- endpoint
- USB endpoint vocabulary.
- host
- Host-side USB stack.
- log
- Logging facade.
- request
- USB control requests.
Constants§
- BULK_
MAX_ PACKET_ SIZE - Maximum packet size for bulk/interrupt endpoints (full-speed USB 2.0).
- CONTROL_
MAX_ PACKET_ SIZE - Maximum packet size for EP0 (full-speed USB 2.0).
- MAX_
ENDPOINTS - Maximum number of endpoints the device stack can track (endpoint numbers 0–15, IN and OUT). Peripheral implementations may support fewer.