Skip to main content

Module host

Module host 

Source
Expand description

Host-side USB stack.

The host stack is split the same way as the device stack: everything below the controller is hardware independent, and a peripheral driver implements HostController.

  application                  class::hid::Keyboard, class::hub::Hub, your own driver
  ────────────────────────
  Device                       enumeration, control transfers
  Pipe                         bulk and interrupt transfers
  ────────────────────────
  HostController               trait: port, channels, transactions
  ────────────────────────
  (a peripheral driver)        implements HostController

Transfers are expressed at transaction level and driven synchronously: a call blocks until the controller halts the channel, so no interrupts, no DMA and no async are involved. Enumeration is a fixed sequence of control transfers (Host::enumerate runs it), and everything a device offers afterwards is reachable through Device::control and Pipe.

§What is covered

  • Root port and hubs: class::hub::Hub powers the downstream ports, resets them and enumerates what is attached; a hub found behind a hub can be opened the same way, so cascades work
  • Control, bulk and interrupt transfers at low, full and high speed
  • Device, configuration, interface, endpoint and string descriptors
  • Classes: HID boot keyboards (class::hid::Keyboard) and hubs (class::hub::Hub)

Isochronous transfers are not implemented. Split transactions (the transaction translator of a high-speed hub) are described by HubRoute::translator but no controller in this workspace implements them yet, so a FS/LS device behind a high-speed hub reports HostError::Unsupported. Put the hub on a full-speed host, or configure a high-speed host as FS/LS-only, and the same devices enumerate without split transactions.

§Example

ⓘ
use usb_stack::host::{Host, class::hid::Keyboard};

let bus = Host::new(&my_controller);

loop {
    let device = match bus.enumerate() {
        Ok(device) => device,
        Err(error) => continue, // nothing attached, or a device that misbehaves
    };
    if let Ok(keyboard) = Keyboard::open(&device) {
        let mut previous = Default::default();
        while let Ok(report) = keyboard.read() {
            for key in report.new_keys(&previous) {
                defmt::info!("key {}", key);
            }
            previous = report;
        }
    }
}

Re-exports§

pub use crate::request::Request;
pub use controller::Channel;
pub use controller::DeviceAddress;
pub use controller::EndpointConfig;
pub use controller::HostController;
pub use controller::HubRoute;
pub use controller::PortStatus;
pub use controller::Speed;
pub use controller::Toggle;
pub use controller::Translator;
pub use descriptor::Configuration;
pub use descriptor::Descriptor;
pub use descriptor::Descriptors;
pub use descriptor::DeviceDescriptor;
pub use descriptor::EndpointDescriptor;
pub use descriptor::Endpoints;
pub use descriptor::Interface;
pub use descriptor::Interfaces;
pub use device::CONFIG_MAX;
pub use device::Device;
pub use error::HostError;
pub use error::HostResult;
pub use pipe::Pipe;

Modules§

class
The classes that drive a device through the host stack.
controller
The hardware abstraction the host protocol layer runs on.
descriptor
Parsing of the descriptors a device returns.
device
Enumeration and control transfers.
error
Errors reported by the host stack.
pipe
Bulk and interrupt pipes.

Structs§

AddressPool
The addresses handed out to enumerated devices.
Host
The root port of a host controller.