From 6abbfa9a92ec9feb03e30846bccb66272020601d Mon Sep 17 00:00:00 2001 From: alexmoon Date: Wed, 6 Apr 2022 22:10:18 -0400 Subject: Async-ify Driver::enable and UsbDeviceBuilder::build --- embassy-usb/src/builder.rs | 17 +++++++++-------- embassy-usb/src/driver.rs | 3 ++- embassy-usb/src/lib.rs | 8 ++++---- 3 files changed, 15 insertions(+), 13 deletions(-) (limited to 'embassy-usb/src') diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs index c8a9db7a4..4bbcd3e56 100644 --- a/embassy-usb/src/builder.rs +++ b/embassy-usb/src/builder.rs @@ -122,7 +122,7 @@ pub struct UsbDeviceBuilder<'d, D: Driver<'d>> { interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>, control_buf: &'d mut [u8], - bus: D, + driver: D, next_interface_number: u8, next_string_index: u8, @@ -139,7 +139,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { /// large enough for the length of the largest control request (in or out) /// anticipated by any class added to the device. pub fn new( - bus: D, + driver: D, config: Config<'d>, device_descriptor_buf: &'d mut [u8], config_descriptor_buf: &'d mut [u8], @@ -173,7 +173,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { bos_descriptor.bos(); UsbDeviceBuilder { - bus, + driver, config, interfaces: Vec::new(), control_buf, @@ -187,12 +187,12 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { } /// Creates the [`UsbDevice`] instance with the configuration in this builder. - pub fn build(mut self) -> UsbDevice<'d, D> { + pub async fn build(mut self) -> UsbDevice<'d, D> { self.config_descriptor.end_configuration(); self.bos_descriptor.end_bos(); UsbDevice::build( - self.bus, + self.driver, self.config, self.device_descriptor.into_buf(), self.config_descriptor.into_buf(), @@ -200,6 +200,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { self.interfaces, self.control_buf, ) + .await } /// Allocates a new interface number. @@ -251,7 +252,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { max_packet_size: u16, interval: u8, ) -> Result { - self.bus + self.driver .alloc_endpoint_in(ep_addr, ep_type, max_packet_size, interval) } @@ -266,7 +267,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { max_packet_size: u16, interval: u8, ) -> Result { - self.bus + self.driver .alloc_endpoint_out(ep_addr, ep_type, max_packet_size, interval) } @@ -306,7 +307,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { /// feasibly recoverable. #[inline] pub fn alloc_control_pipe(&mut self, max_packet_size: u16) -> D::ControlPipe { - self.bus + self.driver .alloc_control_pipe(max_packet_size) .expect("alloc_control_pipe failed") } diff --git a/embassy-usb/src/driver.rs b/embassy-usb/src/driver.rs index d3231cb45..01eb3d577 100644 --- a/embassy-usb/src/driver.rs +++ b/embassy-usb/src/driver.rs @@ -11,6 +11,7 @@ pub trait Driver<'a> { type EndpointIn: EndpointIn + 'a; type ControlPipe: ControlPipe + 'a; type Bus: Bus + 'a; + type EnableFuture: Future + '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 @@ -46,7 +47,7 @@ pub trait Driver<'a> { /// Enables and initializes 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(self) -> Self::Bus; + fn enable(self) -> Self::EnableFuture; /// Indicates that `set_device_address` must be called before accepting the corresponding /// control transfer, not after. diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index f833a86d8..e98cbdee3 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs @@ -72,7 +72,7 @@ pub struct UsbDevice<'d, D: Driver<'d>> { } impl<'d, D: Driver<'d>> UsbDevice<'d, D> { - pub(crate) fn build( + pub(crate) async fn build( mut driver: D, config: Config<'d>, device_descriptor: &'d [u8], @@ -80,17 +80,17 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { bos_descriptor: &'d [u8], interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>, control_buf: &'d mut [u8], - ) -> Self { + ) -> UsbDevice<'d, D> { let control = driver .alloc_control_pipe(config.max_packet_size_0 as u16) .expect("failed to alloc control endpoint"); // Enable the USB bus. // This prevent further allocation by consuming the driver. - let driver = driver.enable(); + let bus = driver.enable().await; Self { - bus: driver, + bus, config, control: ControlPipe::new(control), device_descriptor, -- cgit