Skip to main content

Crate usb_stack

Crate usb_stack 

Source
Expand description

§usb-stack

crates.io docs.rs license

MCU-independent USB device stack core, shared by the rp2350-usb, ch32x035-usb and esp32p4-usb HAL crates.

The bus trait is packet-oriented and speed agnostic: a full-speed driver leaves UsbBus::MAX_PACKET_SIZE at its 64-byte default, a high-speed one overrides it (the ESP32-P4 HS core uses 512), and the classes size their endpoints from it.

A microcontroller crate implements UsbBus for its USB peripheral and re-exports this crate; everything else (device state machine, control pipe, descriptors, classes) is hardware independent.

§Adding it to a project

[dependencies]
usb-stack = "0.1"

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).

§Layers

ModuleContents
busUsbBus trait, UsbBusAllocator, PollResult
endpointEndpointAddress, EndpointType, direction markers, endpoint handles
controlControlPipe (EP0 state machine) and the ControlIn / ControlOut handles classes answer requests with
descriptorDescriptorWriter and standard USB constants
deviceUsbDeviceBuilder / UsbDevice — device state machine, descriptor serving, control-transfer dispatch
classUsbClass trait, CDC-ACM, HID (boot keyboard, boot mouse, custom reports, HID Power Device), GS_USB (CAN), i2c-tiny-usb, UAC 1.0

§Implementing a bus

ⓘ
use usb_stack::{PollResult, UsbBus, Direction, EndpointAddress, EndpointType, UsbResult};

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.

§Features

  • defmt (off by default) — log through defmt.

§Status

Version 0.1 — the API is still settling. Classes currently shipped: CDC-ACM, HID (boot keyboard, boot mouse, custom reports, HID Power Device), GS_USB (CAN), i2c-tiny-usb and UAC 1.0.

See CHANGELOG.md for the release history.

§License

Licensed under either of

at your option.

Re-exports§

pub use bus::InterfaceNumber;
pub use bus::PollResult;
pub use bus::StringIndex;
pub use bus::UsbBus;
pub use bus::UsbBusAllocator;
pub use class::UsbClass;
pub use descriptor::DescriptorWriter;
pub use device::DeviceInfo;
pub use device::DeviceState;
pub use device::UsbDevice;
pub use device::UsbDeviceBuilder;
pub use endpoint::Direction;
pub use endpoint::Endpoint;
pub use endpoint::EndpointAddress;
pub use endpoint::EndpointIn;
pub use endpoint::EndpointOut;
pub use endpoint::EndpointType;
pub use endpoint::In;
pub use endpoint::IsoSynchronization;
pub use endpoint::IsoUsage;
pub use endpoint::Out;
pub use error::UsbError;
pub use error::UsbResult;

Modules§

bus
USB bus trait and allocator.
class
UsbClass trait and the class implementations.
control
Control transfer state machine.
descriptor
USB descriptor builder.
device
USB device state machine and builder.
endpoint
USB endpoint types and handles.
error
USB error types.
log
Logging facade.

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 stack can track (endpoint numbers 0–15, IN and OUT). Peripheral implementations may support fewer.