Skip to main content

UsbBus

Trait UsbBus 

Source
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§

Source

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

Source

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§

Source

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 — if Some, request exactly that address; if None, use the next available one.
  • ep_type — transfer type.
  • max_packet — maximum packet size in bytes.
  • interval — polling interval (interrupt/isochronous endpoints).
Source

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.

Source

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.

Source

fn set_device_address(&self, addr: u8)

Set the device USB address.

Source

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.

Source

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
Source

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
Source

fn set_stalled(&self, addr: EndpointAddress, stalled: bool)

Set or clear the STALL condition on an endpoint.

Source

fn is_stalled(&self, addr: EndpointAddress) -> bool

Return true if the endpoint is currently stalled.

Source

fn suspend(&self)

Enter USB suspend mode.

Source

fn resume(&self)

Resume from suspend mode.

Provided Methods§

Source

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

Implementors§