2022-03-09 01:34:35 +01:00
|
|
|
use core::future::Future;
|
|
|
|
|
|
|
|
use super::types::*;
|
|
|
|
|
|
|
|
/// Driver for a specific USB peripheral. Implement this to add support for a new hardware
|
|
|
|
/// platform.
|
|
|
|
pub trait Driver<'a> {
|
|
|
|
type EndpointOut: EndpointOut + 'a;
|
|
|
|
type EndpointIn: EndpointIn + 'a;
|
2022-03-25 21:46:14 +01:00
|
|
|
type ControlPipe: ControlPipe + 'a;
|
2022-03-09 01:34:35 +01:00
|
|
|
type Bus: Bus + 'a;
|
|
|
|
|
|
|
|
/// Allocates an endpoint and specified endpoint parameters. This method is called by the device
|
|
|
|
/// and class implementations to allocate endpoints, and can only be called before
|
2022-05-10 16:53:42 +02:00
|
|
|
/// [`start`](UsbBus::start) is called.
|
2022-03-09 01:34:35 +01:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `ep_addr` - A static endpoint address to allocate. If Some, the implementation should
|
|
|
|
/// attempt to return an endpoint with the specified address. If None, the implementation
|
|
|
|
/// should return the next available one.
|
|
|
|
/// * `max_packet_size` - Maximum packet size in bytes.
|
|
|
|
/// * `interval` - Polling interval parameter for interrupt endpoints.
|
|
|
|
fn alloc_endpoint_out(
|
|
|
|
&mut self,
|
|
|
|
ep_type: EndpointType,
|
|
|
|
max_packet_size: u16,
|
|
|
|
interval: u8,
|
|
|
|
) -> Result<Self::EndpointOut, EndpointAllocError>;
|
|
|
|
|
|
|
|
fn alloc_endpoint_in(
|
|
|
|
&mut self,
|
|
|
|
ep_type: EndpointType,
|
|
|
|
max_packet_size: u16,
|
|
|
|
interval: u8,
|
|
|
|
) -> Result<Self::EndpointIn, EndpointAllocError>;
|
|
|
|
|
2022-05-10 16:53:42 +02:00
|
|
|
/// Start operation of the USB device.
|
|
|
|
///
|
|
|
|
/// This returns the `Bus` and `ControlPipe` instances that are used to operate
|
|
|
|
/// the USB device. Additionally, this makes all the previously allocated endpoints
|
|
|
|
/// start operating.
|
|
|
|
///
|
|
|
|
/// This consumes the `Driver` instance, so it's no longer possible to allocate more
|
|
|
|
/// endpoints.
|
|
|
|
fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe);
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
/// Indicates that `set_device_address` must be called before accepting the corresponding
|
|
|
|
/// control transfer, not after.
|
|
|
|
///
|
|
|
|
/// The default value for this constant is `false`, which corresponds to the USB 2.0 spec, 9.4.6
|
|
|
|
const QUIRK_SET_ADDRESS_BEFORE_STATUS: bool = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Bus {
|
2022-04-10 21:41:51 +02:00
|
|
|
type EnableFuture<'a>: Future<Output = ()> + 'a
|
2022-04-12 23:51:50 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
type DisableFuture<'a>: Future<Output = ()> + 'a
|
2022-04-10 21:41:51 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
2022-03-09 23:06:27 +01:00
|
|
|
type PollFuture<'a>: Future<Output = Event> + 'a
|
2022-04-10 21:41:51 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
type RemoteWakeupFuture<'a>: Future<Output = Result<(), Unsupported>> + 'a
|
2022-03-09 23:06:27 +01:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
/// Enables the USB peripheral. Soon after enabling the device will be reset, so
|
|
|
|
/// there is no need to perform a USB reset in this method.
|
|
|
|
fn enable(&mut self) -> Self::EnableFuture<'_>;
|
|
|
|
|
|
|
|
/// Disables and powers down the USB peripheral.
|
2022-04-12 23:51:50 +02:00
|
|
|
fn disable(&mut self) -> Self::DisableFuture<'_>;
|
2022-03-09 23:06:27 +01:00
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
fn poll<'a>(&'a mut self) -> Self::PollFuture<'a>;
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
/// Sets the device USB address to `addr`.
|
2022-04-16 04:47:27 +02:00
|
|
|
fn set_address(&mut self, addr: u8);
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-04-16 04:47:27 +02:00
|
|
|
/// Enables or disables an endpoint.
|
|
|
|
fn endpoint_set_enabled(&mut self, ep_addr: EndpointAddress, enabled: bool);
|
2022-04-02 22:35:03 +02:00
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
/// Sets or clears the STALL condition for an endpoint. If the endpoint is an OUT endpoint, it
|
|
|
|
/// should be prepared to receive data again. Only used during control transfers.
|
2022-04-16 04:47:27 +02:00
|
|
|
fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool);
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
/// Gets whether the STALL condition is set for an endpoint. Only used during control transfers.
|
2022-04-16 04:47:27 +02:00
|
|
|
fn endpoint_is_stalled(&mut self, ep_addr: EndpointAddress) -> bool;
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
/// Simulates a disconnect from the USB bus, causing the host to reset and re-enumerate the
|
|
|
|
/// device.
|
|
|
|
///
|
|
|
|
/// The default implementation just returns `Unsupported`.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// * [`Unsupported`](crate::UsbError::Unsupported) - This UsbBus implementation doesn't support
|
|
|
|
/// simulating a disconnect or it has not been enabled at creation time.
|
|
|
|
fn force_reset(&mut self) -> Result<(), Unsupported> {
|
|
|
|
Err(Unsupported)
|
|
|
|
}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
/// Initiates a remote wakeup of the host by the device.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// * [`Unsupported`](crate::UsbError::Unsupported) - This UsbBus implementation doesn't support
|
|
|
|
/// remote wakeup or it has not been enabled at creation time.
|
|
|
|
fn remote_wakeup(&mut self) -> Self::RemoteWakeupFuture<'_>;
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Endpoint {
|
2022-04-02 22:35:03 +02:00
|
|
|
type WaitEnabledFuture<'a>: Future<Output = ()> + 'a
|
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
/// Get the endpoint address
|
|
|
|
fn info(&self) -> &EndpointInfo;
|
|
|
|
|
2022-04-02 22:35:03 +02:00
|
|
|
/// Waits for the endpoint to be enabled.
|
|
|
|
fn wait_enabled(&mut self) -> Self::WaitEnabledFuture<'_>;
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait EndpointOut: Endpoint {
|
2022-04-06 04:04:11 +02:00
|
|
|
type ReadFuture<'a>: Future<Output = Result<usize, EndpointError>> + 'a
|
2022-03-09 01:34:35 +01:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
/// Reads a single packet of data from the endpoint, and returns the actual length of
|
|
|
|
/// the packet.
|
|
|
|
///
|
|
|
|
/// This should also clear any NAK flags and prepare the endpoint to receive the next packet.
|
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a>;
|
|
|
|
}
|
|
|
|
|
2022-03-25 21:46:14 +01:00
|
|
|
pub trait ControlPipe {
|
2022-05-12 18:14:48 +02:00
|
|
|
type SetupFuture<'a>: Future<Output = [u8; 8]> + 'a
|
2022-03-25 21:46:14 +01:00
|
|
|
where
|
|
|
|
Self: 'a;
|
2022-04-06 04:04:11 +02:00
|
|
|
type DataOutFuture<'a>: Future<Output = Result<usize, EndpointError>> + 'a
|
2022-03-25 21:46:14 +01:00
|
|
|
where
|
|
|
|
Self: 'a;
|
2022-04-06 04:04:11 +02:00
|
|
|
type DataInFuture<'a>: Future<Output = Result<(), EndpointError>> + 'a
|
2022-05-30 00:35:27 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
type AcceptFuture<'a>: Future<Output = ()> + 'a
|
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
type RejectFuture<'a>: Future<Output = ()> + 'a
|
2022-03-25 21:46:14 +01:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
2022-03-30 02:26:30 +02:00
|
|
|
/// Maximum packet size for the control pipe
|
|
|
|
fn max_packet_size(&self) -> usize;
|
|
|
|
|
2022-03-25 21:46:14 +01:00
|
|
|
/// Reads a single setup packet from the endpoint.
|
|
|
|
fn setup<'a>(&'a mut self) -> Self::SetupFuture<'a>;
|
|
|
|
|
2022-03-30 02:26:30 +02:00
|
|
|
/// Reads a DATA OUT packet into `buf` in response to a control write request.
|
2022-03-25 21:46:14 +01:00
|
|
|
///
|
|
|
|
/// Must be called after `setup()` for requests with `direction` of `Out`
|
|
|
|
/// and `length` greater than zero.
|
2022-06-12 22:15:44 +02:00
|
|
|
fn data_out<'a>(&'a mut self, buf: &'a mut [u8], first: bool, last: bool) -> Self::DataOutFuture<'a>;
|
2022-03-25 21:46:14 +01:00
|
|
|
|
2022-03-30 02:26:30 +02:00
|
|
|
/// Sends a DATA IN packet with `data` in response to a control read request.
|
|
|
|
///
|
|
|
|
/// If `last_packet` is true, the STATUS packet will be ACKed following the transfer of `data`.
|
2022-06-12 22:15:44 +02:00
|
|
|
fn data_in<'a>(&'a mut self, data: &'a [u8], first: bool, last: bool) -> Self::DataInFuture<'a>;
|
2022-03-25 21:46:14 +01:00
|
|
|
|
2022-03-30 02:26:30 +02:00
|
|
|
/// Accepts a control request.
|
2022-03-25 21:46:14 +01:00
|
|
|
///
|
2022-03-30 02:26:30 +02:00
|
|
|
/// Causes the STATUS packet for the current request to be ACKed.
|
2022-05-30 00:35:27 +02:00
|
|
|
fn accept<'a>(&'a mut self) -> Self::AcceptFuture<'a>;
|
2022-03-25 21:46:14 +01:00
|
|
|
|
|
|
|
/// Rejects a control request.
|
2022-03-30 02:26:30 +02:00
|
|
|
///
|
|
|
|
/// Sets a STALL condition on the pipe to indicate an error.
|
2022-05-30 00:35:27 +02:00
|
|
|
fn reject<'a>(&'a mut self) -> Self::RejectFuture<'a>;
|
2022-03-25 21:46:14 +01:00
|
|
|
}
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
pub trait EndpointIn: Endpoint {
|
2022-04-06 04:04:11 +02:00
|
|
|
type WriteFuture<'a>: Future<Output = Result<(), EndpointError>> + 'a
|
2022-03-09 01:34:35 +01:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
/// Writes a single packet of data to the endpoint.
|
|
|
|
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a>;
|
|
|
|
}
|
2022-03-09 23:06:27 +01:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
/// Event returned by [`Bus::poll`].
|
|
|
|
pub enum Event {
|
|
|
|
/// The USB reset condition has been detected.
|
|
|
|
Reset,
|
|
|
|
|
|
|
|
/// A USB suspend request has been detected or, in the case of self-powered devices, the device
|
|
|
|
/// has been disconnected from the USB bus.
|
|
|
|
Suspend,
|
|
|
|
|
|
|
|
/// A USB resume request has been detected after being suspended or, in the case of self-powered
|
|
|
|
/// devices, the device has been connected to the USB bus.
|
|
|
|
Resume,
|
2022-06-16 08:08:58 +02:00
|
|
|
|
|
|
|
/// The USB power has been detected.
|
|
|
|
PowerDetected,
|
|
|
|
|
|
|
|
/// The USB power has been removed. Not supported by all devices.
|
|
|
|
PowerRemoved,
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub struct EndpointAllocError;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
/// Operation is unsupported by the driver.
|
|
|
|
pub struct Unsupported;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2022-04-06 04:04:11 +02:00
|
|
|
/// Errors returned by [`EndpointIn::write`] and [`EndpointOut::read`]
|
|
|
|
pub enum EndpointError {
|
|
|
|
/// Either the packet to be written is too long to fit in the transmission
|
|
|
|
/// buffer or the received packet is too long to fit in `buf`.
|
2022-03-09 23:06:27 +01:00
|
|
|
BufferOverflow,
|
|
|
|
|
2022-04-06 04:04:11 +02:00
|
|
|
/// The endpoint is disabled.
|
2022-04-02 22:35:03 +02:00
|
|
|
Disabled,
|
2022-03-09 23:06:27 +01:00
|
|
|
}
|