usb_stack/host/mod.rs
1//! Host-side USB stack.
2//!
3//! The host stack is split the same way as the device stack: everything below
4//! the controller is hardware independent, and a peripheral driver implements
5//! [`HostController`].
6//!
7//! ```text
8//! application class::hid::Keyboard, your own class driver
9//! ────────────────────────
10//! Device enumeration, control transfers
11//! Pipe bulk and interrupt transfers
12//! ────────────────────────
13//! HostController trait: port, channels, transactions
14//! ────────────────────────
15//! (a peripheral driver) implements HostController
16//! ```
17//!
18//! Transfers are expressed at transaction level and driven synchronously: a
19//! call blocks until the controller halts the channel, so no interrupts, no DMA
20//! and no `async` are involved. Enumeration is a fixed sequence of control
21//! transfers ([`Host::enumerate`] runs it), and everything a device offers
22//! afterwards is reachable through [`Device::control`] and [`Pipe`].
23//!
24//! ## What is covered
25//!
26//! - Root port only: hubs are not supported, so a device must be plugged into
27//! the controller's own port
28//! - Control, bulk and interrupt transfers at low, full and high speed
29//! - Device, configuration, interface, endpoint and string descriptors
30//! - Classes: HID boot keyboards ([`class::hid::Keyboard`])
31//!
32//! Isochronous transfers and the hub class are not implemented.
33//!
34//! ## Example
35//!
36//! ```ignore
37//! use usb_stack::host::{Host, class::hid::Keyboard};
38//!
39//! let bus = Host::new(&my_controller);
40//!
41//! loop {
42//! let device = match bus.enumerate() {
43//! Ok(device) => device,
44//! Err(error) => continue, // nothing attached, or a device that misbehaves
45//! };
46//! if let Ok(keyboard) = Keyboard::open(&device) {
47//! let mut previous = Default::default();
48//! while let Ok(report) = keyboard.read() {
49//! for key in report.new_keys(&previous) {
50//! defmt::info!("key {}", key);
51//! }
52//! previous = report;
53//! }
54//! }
55//! }
56//! ```
57
58pub mod class;
59pub mod controller;
60pub mod descriptor;
61pub mod device;
62pub mod error;
63pub mod pipe;
64
65pub use crate::request::Request;
66pub use controller::{
67 Channel, DeviceAddress, EndpointConfig, HostController, PortStatus, Speed, Toggle,
68};
69pub use descriptor::{
70 Configuration, Descriptor, Descriptors, DeviceDescriptor, EndpointDescriptor, Endpoints,
71 Interface, Interfaces,
72};
73pub use device::{CONFIG_MAX, Device};
74pub use error::{HostError, HostResult};
75pub use pipe::Pipe;
76
77/// The root port of a host controller.
78///
79/// This is the entry point of the stack: it reports the port, resets it and
80/// enumerates what is attached. Everything else hangs off the [`Device`] that
81/// [`enumerate`](Self::enumerate) returns.
82pub struct Host<'d, C: HostController> {
83 controller: &'d C,
84}
85
86impl<'d, C: HostController> Host<'d, C> {
87 /// Wrap a controller. The controller must be powered up already.
88 #[inline]
89 pub const fn new(controller: &'d C) -> Self {
90 Self { controller }
91 }
92
93 /// Read the root port without changing anything.
94 #[inline]
95 pub fn port(&self) -> PortStatus {
96 self.controller.port_status()
97 }
98
99 /// Power the port, drive a bus reset and report the negotiated speed.
100 ///
101 /// A device has to be attached; this is what enumeration starts with.
102 #[inline]
103 pub fn reset(&self) -> HostResult<PortStatus> {
104 self.controller.reset_port()
105 }
106
107 /// Wait up to a second for a device to be attached.
108 pub fn wait_for_device(&self) -> HostResult<PortStatus> {
109 for _ in 0..1000 {
110 let status = self.controller.port_status();
111 if status.connected {
112 return Ok(status);
113 }
114 self.controller.wait_millis(1);
115 }
116 Err(HostError::Timeout)
117 }
118
119 /// Reset the port and enumerate the attached device at the first free
120 /// address.
121 pub fn enumerate(&self) -> HostResult<Device<'d, C>> {
122 self.enumerate_at(DeviceAddress::FIRST)
123 }
124
125 /// Reset the port and enumerate the attached device at `address`.
126 pub fn enumerate_at(&self, address: DeviceAddress) -> HostResult<Device<'d, C>> {
127 let status = self.controller.reset_port()?;
128 if !status.enabled {
129 return Err(HostError::Disconnected);
130 }
131 Device::enumerate(self.controller, address, status.speed)
132 }
133}