From e867364d42477ecf3dc48e6fd787dff96cb6fadf Mon Sep 17 00:00:00 2001 From: alexmoon Date: Tue, 5 Apr 2022 22:04:11 -0400 Subject: [PATCH] Unify ReadError and WriteError into EndpointError --- embassy-nrf/src/usb.rs | 26 ++++++++--------- embassy-usb-hid/src/lib.rs | 14 ++++----- embassy-usb-serial/src/lib.rs | 6 ++-- embassy-usb/src/control.rs | 4 +-- embassy-usb/src/driver.rs | 30 ++++++-------------- examples/nrf/src/bin/usb_serial.rs | 19 ++++--------- examples/nrf/src/bin/usb_serial_multitask.rs | 19 ++++--------- 7 files changed, 44 insertions(+), 74 deletions(-) diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index 9f483d96..5e2f585f 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs @@ -10,7 +10,7 @@ use embassy::util::Unborrow; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::unborrow; use embassy_usb::control::Request; -use embassy_usb::driver::{self, Event, ReadError, WriteError}; +use embassy_usb::driver::{self, EndpointError, Event}; use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection}; use futures::future::poll_fn; use futures::Future; @@ -472,13 +472,13 @@ impl<'d, T: Instance, Dir> Endpoint<'d, T, Dir> { } } -unsafe fn read_dma(i: usize, buf: &mut [u8]) -> Result { +unsafe fn read_dma(i: usize, buf: &mut [u8]) -> Result { let regs = T::regs(); // Check that the packet fits into the buffer let size = regs.size.epout[i].read().bits() as usize; if size > buf.len() { - return Err(ReadError::BufferOverflow); + return Err(EndpointError::BufferOverflow); } if i == 0 { @@ -554,7 +554,7 @@ unsafe fn write_dma(i: usize, buf: &[u8]) { } impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> { - type ReadFuture<'a> = impl Future> + 'a where Self: 'a; + type ReadFuture<'a> = impl Future> + 'a where Self: 'a; fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { async move { @@ -563,7 +563,7 @@ impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> { self.wait_data_ready() .await - .map_err(|_| ReadError::Disabled)?; + .map_err(|_| EndpointError::Disabled)?; unsafe { read_dma::(i, buf) } } @@ -571,7 +571,7 @@ impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> { } impl<'d, T: Instance> driver::EndpointIn for Endpoint<'d, T, In> { - type WriteFuture<'a> = impl Future> + 'a where Self: 'a; + type WriteFuture<'a> = impl Future> + 'a where Self: 'a; fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { async move { @@ -580,7 +580,7 @@ impl<'d, T: Instance> driver::EndpointIn for Endpoint<'d, T, In> { self.wait_data_ready() .await - .map_err(|_| WriteError::Disabled)?; + .map_err(|_| EndpointError::Disabled)?; unsafe { write_dma::(i, buf) } @@ -596,8 +596,8 @@ pub struct ControlPipe<'d, T: Instance> { impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> { type SetupFuture<'a> = impl Future + 'a where Self: 'a; - type DataOutFuture<'a> = impl Future> + 'a where Self: 'a; - type DataInFuture<'a> = impl Future> + 'a where Self: 'a; + type DataOutFuture<'a> = impl Future> + 'a where Self: 'a; + type DataInFuture<'a> = impl Future> + 'a where Self: 'a; fn max_packet_size(&self) -> usize { usize::from(self.max_packet_size) @@ -666,10 +666,10 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> { Poll::Ready(Ok(())) } else if regs.events_usbreset.read().bits() != 0 { trace!("aborted control data_out: usb reset"); - Poll::Ready(Err(ReadError::Disabled)) + Poll::Ready(Err(EndpointError::Disabled)) } else if regs.events_ep0setup.read().bits() != 0 { trace!("aborted control data_out: received another SETUP"); - Poll::Ready(Err(ReadError::Disabled)) + Poll::Ready(Err(EndpointError::Disabled)) } else { Poll::Pending } @@ -705,10 +705,10 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> { Poll::Ready(Ok(())) } else if regs.events_usbreset.read().bits() != 0 { trace!("aborted control data_in: usb reset"); - Poll::Ready(Err(WriteError::Disabled)) + Poll::Ready(Err(EndpointError::Disabled)) } else if regs.events_ep0setup.read().bits() != 0 { trace!("aborted control data_in: received another SETUP"); - Poll::Ready(Err(WriteError::Disabled)) + Poll::Ready(Err(EndpointError::Disabled)) } else { Poll::Pending } diff --git a/embassy-usb-hid/src/lib.rs b/embassy-usb-hid/src/lib.rs index f50c5f8c..e870becf 100644 --- a/embassy-usb-hid/src/lib.rs +++ b/embassy-usb-hid/src/lib.rs @@ -15,7 +15,7 @@ use embassy::time::Duration; use embassy_usb::driver::EndpointOut; use embassy_usb::{ control::{ControlHandler, InResponse, OutResponse, Request, RequestType}, - driver::{Driver, Endpoint, EndpointIn, WriteError}, + driver::{Driver, Endpoint, EndpointError, EndpointIn}, UsbDeviceBuilder, }; @@ -187,9 +187,9 @@ pub enum ReadError { Sync(Range), } -impl From for ReadError { - fn from(val: embassy_usb::driver::ReadError) -> Self { - use embassy_usb::driver::ReadError::*; +impl From for ReadError { + fn from(val: embassy_usb::driver::EndpointError) -> Self { + use embassy_usb::driver::EndpointError::*; match val { BufferOverflow => ReadError::BufferOverflow, Disabled => ReadError::Disabled, @@ -207,11 +207,11 @@ impl<'d, D: Driver<'d>, const N: usize> ReportWriter<'d, D, N> { /// /// Panics if no endpoint is available. #[cfg(feature = "usbd-hid")] - pub async fn serialize(&mut self, r: &IR) -> Result<(), WriteError> { + pub async fn serialize(&mut self, r: &IR) -> Result<(), EndpointError> { let mut buf: [u8; N] = [0; N]; let size = match serialize(&mut buf, r) { Ok(size) => size, - Err(_) => return Err(WriteError::BufferOverflow), + Err(_) => return Err(EndpointError::BufferOverflow), }; self.write(&buf[0..size]).await } @@ -219,7 +219,7 @@ impl<'d, D: Driver<'d>, const N: usize> ReportWriter<'d, D, N> { /// Writes `report` to its interrupt endpoint. /// /// Panics if no endpoint is available. - pub async fn write(&mut self, report: &[u8]) -> Result<(), WriteError> { + pub async fn write(&mut self, report: &[u8]) -> Result<(), EndpointError> { assert!(report.len() <= N); let max_packet_size = usize::from(self.ep_in.info().max_packet_size); diff --git a/embassy-usb-serial/src/lib.rs b/embassy-usb-serial/src/lib.rs index 07352fac..7b25398d 100644 --- a/embassy-usb-serial/src/lib.rs +++ b/embassy-usb-serial/src/lib.rs @@ -10,7 +10,7 @@ use core::mem::{self, MaybeUninit}; use core::sync::atomic::{AtomicBool, Ordering}; use embassy::blocking_mutex::CriticalSectionMutex; use embassy_usb::control::{self, ControlHandler, InResponse, OutResponse, Request}; -use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError}; +use embassy_usb::driver::{Endpoint, EndpointError, EndpointIn, EndpointOut}; use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder}; /// This should be used as `device_class` when building the `UsbDevice`. @@ -265,12 +265,12 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> { } /// Writes a single packet into the IN endpoint. - pub async fn write_packet(&mut self, data: &[u8]) -> Result<(), WriteError> { + pub async fn write_packet(&mut self, data: &[u8]) -> Result<(), EndpointError> { self.write_ep.write(data).await } /// Reads a single packet from the OUT endpoint. - pub async fn read_packet(&mut self, data: &mut [u8]) -> Result { + pub async fn read_packet(&mut self, data: &mut [u8]) -> Result { self.read_ep.read(data).await } diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs index 7c46812b..a613f114 100644 --- a/embassy-usb/src/control.rs +++ b/embassy-usb/src/control.rs @@ -1,7 +1,7 @@ use core::mem; use crate::descriptor::DescriptorWriter; -use crate::driver::{self, ReadError}; +use crate::driver::{self, EndpointError}; use crate::DEFAULT_ALTERNATE_SETTING; use super::types::*; @@ -253,7 +253,7 @@ impl ControlPipe { &mut self, buf: &'a mut [u8], stage: DataOutStage, - ) -> Result<(&'a [u8], StatusStage), ReadError> { + ) -> Result<(&'a [u8], StatusStage), EndpointError> { if stage.length == 0 { Ok((&[], StatusStage {})) } else { diff --git a/embassy-usb/src/driver.rs b/embassy-usb/src/driver.rs index 01eb3d57..875ceafc 100644 --- a/embassy-usb/src/driver.rs +++ b/embassy-usb/src/driver.rs @@ -130,7 +130,7 @@ pub trait Endpoint { } pub trait EndpointOut: Endpoint { - type ReadFuture<'a>: Future> + 'a + type ReadFuture<'a>: Future> + 'a where Self: 'a; @@ -145,10 +145,10 @@ pub trait ControlPipe { type SetupFuture<'a>: Future + 'a where Self: 'a; - type DataOutFuture<'a>: Future> + 'a + type DataOutFuture<'a>: Future> + 'a where Self: 'a; - type DataInFuture<'a>: Future> + 'a + type DataInFuture<'a>: Future> + 'a where Self: 'a; @@ -181,7 +181,7 @@ pub trait ControlPipe { } pub trait EndpointIn: Endpoint { - type WriteFuture<'a>: Future> + 'a + type WriteFuture<'a>: Future> + 'a where Self: 'a; @@ -216,24 +216,12 @@ pub struct Unsupported; #[derive(Copy, Clone, Eq, PartialEq, Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] -/// Errors returned by [`EndpointIn::write`] -pub enum WriteError { - /// The packet is too long to fit in the - /// transmission buffer. This is generally an error in the class implementation, because the - /// class shouldn't provide more data than the `max_packet_size` it specified when allocating - /// the endpoint. +/// 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`. BufferOverflow, - Disabled, -} -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -/// Errors returned by [`EndpointOut::read`] -pub enum ReadError { - /// The received packet is too long to - /// fit in `buf`. This is generally an error in the class implementation, because the class - /// should use a buffer that is large enough for the `max_packet_size` it specified when - /// allocating the endpoint. - BufferOverflow, + /// The endpoint is disabled. Disabled, } diff --git a/examples/nrf/src/bin/usb_serial.rs b/examples/nrf/src/bin/usb_serial.rs index 500be2ce..68432283 100644 --- a/examples/nrf/src/bin/usb_serial.rs +++ b/examples/nrf/src/bin/usb_serial.rs @@ -10,7 +10,7 @@ use embassy_nrf::interrupt; use embassy_nrf::pac; use embassy_nrf::usb::{Driver, Instance}; use embassy_nrf::Peripherals; -use embassy_usb::driver::{ReadError, WriteError}; +use embassy_usb::driver::EndpointError; use embassy_usb::{Config, UsbDeviceBuilder}; use embassy_usb_serial::{CdcAcmClass, State}; use futures::future::join; @@ -82,20 +82,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { struct Disconnected {} -impl From for Disconnected { - fn from(val: ReadError) -> Self { +impl From for Disconnected { + fn from(val: EndpointError) -> Self { match val { - ReadError::BufferOverflow => panic!("Buffer overflow"), - ReadError::Disabled => Disconnected {}, - } - } -} - -impl From for Disconnected { - fn from(val: WriteError) -> Self { - match val { - WriteError::BufferOverflow => panic!("Buffer overflow"), - WriteError::Disabled => Disconnected {}, + EndpointError::BufferOverflow => panic!("Buffer overflow"), + EndpointError::Disabled => Disconnected {}, } } } diff --git a/examples/nrf/src/bin/usb_serial_multitask.rs b/examples/nrf/src/bin/usb_serial_multitask.rs index 1258bc53..bfb09014 100644 --- a/examples/nrf/src/bin/usb_serial_multitask.rs +++ b/examples/nrf/src/bin/usb_serial_multitask.rs @@ -11,7 +11,7 @@ use embassy_nrf::pac; use embassy_nrf::usb::Driver; use embassy_nrf::Peripherals; use embassy_nrf::{interrupt, peripherals}; -use embassy_usb::driver::{ReadError, WriteError}; +use embassy_usb::driver::EndpointError; use embassy_usb::{Config, UsbDevice, UsbDeviceBuilder}; use embassy_usb_serial::{CdcAcmClass, State}; @@ -93,20 +93,11 @@ async fn main(spawner: Spawner, p: Peripherals) { struct Disconnected {} -impl From for Disconnected { - fn from(val: ReadError) -> Self { +impl From for Disconnected { + fn from(val: EndpointError) -> Self { match val { - ReadError::BufferOverflow => panic!("Buffer overflow"), - ReadError::Disabled => Disconnected {}, - } - } -} - -impl From for Disconnected { - fn from(val: WriteError) -> Self { - match val { - WriteError::BufferOverflow => panic!("Buffer overflow"), - WriteError::Disabled => Disconnected {}, + EndpointError::BufferOverflow => panic!("Buffer overflow"), + EndpointError::Disabled => Disconnected {}, } } }