Module usb_device::device
source · [−]Expand description
USB composite device.
The UsbDevice type in this module is the core of this crate. It combines multiple USB class implementations and the USB bus driver and dispatches bus state changes and control messages between them.
To implement USB support for your own project, the required code is usually as follows:
ⓘ
use usb_device::prelude::*;
use usb_serial; // example class crate (not included)
// Create the device-specific USB peripheral driver. The exact name and arguments are device
// specific, so check the documentation for your device driver crate.
let usb_bus = device_specific_usb::UsbBus::new(...);
// Create one or more USB class implementation. The name and arguments depend on the class,
// however most classes require the UsbAllocator as the first argument in order to allocate
// the required shared resources.
let mut serial = usb_serial::SerialPort::new(&usb_bus.allocator());
// Build the final [UsbDevice](device::UsbDevice) instance. The required arguments are a
// reference to the peripheral driver created earlier, as well as a USB vendor ID/product ID
// pair. Additional builder arguments can specify parameters such as device class code or
// product name. If using an existing class, remember to check the class crate documentation
// for correct values.
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x5824, 0x27dd))
.product("Serial port")
.device_class(usb_serial::DEVICE_CLASS)
.build();
// At this point the USB peripheral is enabled and a connected host will attempt to enumerate
// it.
loop {
// Must be called more often than once every 10ms to handle events and stay USB compilant,
// or from a device-specific interrupt handler.
if (usb_dev.poll(&mut [&mut serial])) {
// Call class-specific methods here
serial.read(...);
}
}
Structs
A USB device consisting of one or more device classes.
Used to build new
UsbDevice
s.A USB vendor ID and product ID pair.
Enums
The global state of the USB device.
Constants
The bConfiguration value for the not configured state.
The bConfiguration value for the single configuration supported by this device.
The default value for bAlternateSetting for all interfaces.