pub trait UsbBus: Sized {
const SET_ADDRESS_BEFORE_STATUS: bool = false;
const MAX_PACKET_SIZE: u16 = 64;
// Required methods
fn alloc_ep(
&mut self,
dir: Direction,
addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet: u16,
interval: u8,
) -> UsbResult<EndpointAddress>;
fn enable(&mut self);
fn reset(&self);
fn set_device_address(&self, addr: u8);
fn poll(&self) -> PollResult;
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);
// Provided method
fn force_reset(&self) -> UsbResult<()> { ... }
}Expand description
Low-level hardware abstraction trait for a USB peripheral.
Implement this trait to add support for a new microcontroller. All methods
that mutate hardware state take &self (not &mut self) because the bus is
shared by reference between UsbDevice and
endpoint handles. Implementations must use interior mutability (atomics,
UnsafeCell, etc.) where needed.
§alloc_ep vs later methods
alloc_ep takes &mut self and is only called before
enable. All other methods take &self.
Provided Associated Constants§
Sourceconst SET_ADDRESS_BEFORE_STATUS: bool = false
const SET_ADDRESS_BEFORE_STATUS: bool = false
When true, set_device_address must be
called before the status stage of a SET_ADDRESS request is completed
(non-standard, required by some peripherals).
Default: false (USB 2.0 spec §9.4.6 behaviour).
Sourceconst MAX_PACKET_SIZE: u16 = 64
const MAX_PACKET_SIZE: u16 = 64
Largest packet size bulk and interrupt endpoints support on this bus.
Classes use it to size their endpoints: 64 bytes for full-speed buses, 512 for high-speed ones. Default: 64.
Required Methods§
Sourcefn alloc_ep(
&mut self,
dir: Direction,
addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet: u16,
interval: u8,
) -> UsbResult<EndpointAddress>
fn alloc_ep( &mut self, dir: Direction, addr: Option<EndpointAddress>, ep_type: EndpointType, max_packet: u16, interval: u8, ) -> UsbResult<EndpointAddress>
Allocate an endpoint.
Called by UsbBusAllocator before enable.
dir— endpoint direction.addr— ifSome, request exactly that address; ifNone, use the next available one.ep_type— transfer type.max_packet— maximum packet size in bytes.interval— polling interval (interrupt/isochronous endpoints).
Sourcefn enable(&mut self)
fn enable(&mut self)
Enable the USB peripheral. Called once, after all endpoints have been allocated. The device will be reset shortly after by the host.
Sourcefn reset(&self)
fn reset(&self)
Reset endpoint state after a USB bus reset.
All previously allocated endpoints must be re-initialised to the state
they were in right after alloc_ep.
Sourcefn set_device_address(&self, addr: u8)
fn set_device_address(&self, addr: u8)
Set the device USB address.
Sourcefn poll(&self) -> PollResult
fn poll(&self) -> PollResult
Poll for bus events. Usually called from an interrupt handler or the main loop. Must be called at least once every 10 ms while connected.
Sourcefn write(&self, addr: EndpointAddress, buf: &[u8]) -> UsbResult<usize>
fn write(&self, addr: EndpointAddress, buf: &[u8]) -> UsbResult<usize>
Write a single packet to an IN endpoint.
Returns the number of bytes actually written.
§Errors
WouldBlock— previous packet still pending.BufferOverflow—bufexceedsmax_packet.
Sourcefn read(&self, addr: EndpointAddress, buf: &mut [u8]) -> UsbResult<usize>
fn read(&self, addr: EndpointAddress, buf: &mut [u8]) -> UsbResult<usize>
Read a single packet from an OUT endpoint into buf.
Returns the number of bytes read.
§Errors
WouldBlock— no packet available.BufferOverflow— packet exceedsbuf.
Sourcefn set_stalled(&self, addr: EndpointAddress, stalled: bool)
fn set_stalled(&self, addr: EndpointAddress, stalled: bool)
Set or clear the STALL condition on an endpoint.
Sourcefn is_stalled(&self, addr: EndpointAddress) -> bool
fn is_stalled(&self, addr: EndpointAddress) -> bool
Return true if the endpoint is currently stalled.
Provided Methods§
Sourcefn force_reset(&self) -> UsbResult<()>
fn force_reset(&self) -> UsbResult<()>
Simulate a USB disconnect (force re-enumeration).
The default implementation returns Unsupported.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".