From 7fde3abd5d7e04f6d79d325d4454d6eafabebc1f Mon Sep 17 00:00:00 2001 From: alexmoon Date: Mon, 11 Apr 2022 12:00:05 -0400 Subject: [PATCH] Remote wakeup bug fixes --- embassy-usb/src/lib.rs | 30 ++++++++++++++---------- examples/nrf/src/bin/usb_hid_keyboard.rs | 24 +++++++++---------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index 2c53d921..210908c2 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs @@ -171,21 +171,21 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDevice<'d, D, M> { h.reset(); } - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.reset(); } } Event::Resume => { trace!("usb: resume"); self.suspended = false; - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.suspended(false); } } Event::Suspend => { trace!("usb: suspend"); self.suspended = true; - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.suspended(true); } } @@ -200,19 +200,25 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDevice<'d, D, M> { trace!("usb: disable"); self.bus.disable(); self.device_state = UsbDeviceState::Disabled; - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.disabled(); } self.wait_for_enable().await; } DeviceCommand::RemoteWakeup => { - if self.remote_wakeup_enabled { + trace!("usb: remote wakeup"); + if self.suspended && self.remote_wakeup_enabled { match self.bus.remote_wakeup().await { - Ok(()) => (), + Ok(()) => { + self.suspended = false; + if let Some(h) = &self.handler { + h.suspended(false); + } + } Err(Unsupported) => warn!("Remote wakeup is unsupported!"), } } else { - warn!("Remote wakeup not enabled."); + warn!("Remote wakeup requested when not enabled or not suspended."); } } }, @@ -250,14 +256,14 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDevice<'d, D, M> { (RequestType::Standard, Recipient::Device) => match (req.request, req.value) { (Request::CLEAR_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => { self.remote_wakeup_enabled = false; - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.remote_wakeup_enabled(false); } self.control.accept(stage) } (Request::SET_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => { self.remote_wakeup_enabled = true; - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.remote_wakeup_enabled(true); } self.control.accept(stage) @@ -266,7 +272,7 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDevice<'d, D, M> { self.pending_address = addr as u8; self.bus.set_device_address(self.pending_address); self.device_state = UsbDeviceState::Addressed; - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.addressed(self.pending_address); } self.control.accept(stage) @@ -274,7 +280,7 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDevice<'d, D, M> { (Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => { self.device_state = UsbDeviceState::Configured; self.bus.set_configured(true); - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.configured(true); } self.control.accept(stage) @@ -284,7 +290,7 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDevice<'d, D, M> { _ => { self.device_state = UsbDeviceState::Addressed; self.bus.set_configured(false); - if let Some(h) = &mut self.handler { + if let Some(h) = &self.handler { h.configured(false); } self.control.accept(stage) diff --git a/examples/nrf/src/bin/usb_hid_keyboard.rs b/examples/nrf/src/bin/usb_hid_keyboard.rs index 483d86b8..fb3a198a 100644 --- a/examples/nrf/src/bin/usb_hid_keyboard.rs +++ b/examples/nrf/src/bin/usb_hid_keyboard.rs @@ -121,20 +121,20 @@ async fn main(_spawner: Spawner, p: Peripherals) { if SUSPENDED.load(Ordering::Acquire) { info!("Triggering remote wakeup"); - USB_COMMANDS.send(DeviceCommand::RemoteWakeup); + USB_COMMANDS.send(DeviceCommand::RemoteWakeup).await; + } else { + let report = KeyboardReport { + keycodes: [4, 0, 0, 0, 0, 0], + leds: 0, + modifier: 0, + reserved: 0, + }; + match hid_in.serialize(&report).await { + Ok(()) => {} + Err(e) => warn!("Failed to send report: {:?}", e), + }; } - let report = KeyboardReport { - keycodes: [4, 0, 0, 0, 0, 0], - leds: 0, - modifier: 0, - reserved: 0, - }; - match hid_in.serialize(&report).await { - Ok(()) => {} - Err(e) => warn!("Failed to send report: {:?}", e), - }; - button.wait_for_high().await; info!("RELEASED"); let report = KeyboardReport {