aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb
diff options
context:
space:
mode:
authorhuntc <[email protected]>2022-06-15 12:17:04 +1000
committerhuntc <[email protected]>2022-07-07 10:08:57 +1000
commitc46e9b6cfc1375f5e5e2bade8eb2174cc47c4a28 (patch)
treee9f4064589cb4e56defd2ef5e3833db96e4e9eba /embassy-usb
parent1fb70e2fac11afb4139ec2ed89293ce7ae1f6e65 (diff)
Introduces EnabledUsbDevice
EnabledUsbDevice is a wrapper around the UsbDevice where their enablement is also subject to external events, such as POWER events for nRF. It is introduced generically to support other platforms should they also require external signalling for enablement.
Diffstat (limited to 'embassy-usb')
-rw-r--r--embassy-usb/src/lib.rs1
-rw-r--r--embassy-usb/src/util.rs68
2 files changed, 69 insertions, 0 deletions
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index f2577d4fc..eba46b892 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -11,6 +11,7 @@ pub mod descriptor;
11mod descriptor_reader; 11mod descriptor_reader;
12pub mod driver; 12pub mod driver;
13pub mod types; 13pub mod types;
14pub mod util;
14 15
15use embassy::util::{select, Either}; 16use embassy::util::{select, Either};
16use heapless::Vec; 17use heapless::Vec;
diff --git a/embassy-usb/src/util.rs b/embassy-usb/src/util.rs
new file mode 100644
index 000000000..ac56691b8
--- /dev/null
+++ b/embassy-usb/src/util.rs
@@ -0,0 +1,68 @@
1use embassy::channel::signal::Signal;
2use embassy::util::{select, Either};
3
4use crate::driver::Driver;
5use crate::UsbDevice;
6
7/// Am enabled usb device is a device that further receives external notifications
8/// regarding whether it is enabled or not. A common example of where this is
9/// required is when receiving notifications from the POWER peripheral that
10/// USB has been connected to or removed. The device here wraps an existing
11/// USB device, keeping it publically available so that device-oriented operations
12/// may still be performed. A signal is also provided that enables/disables the
13/// USB device, taking care of suspension and resumption. In the case of the POWER
14/// peripheral, this signal can be used from within a POWER_CLOCK interrupt
15/// handler. Alternatively, for softdevice usage where the POWER peripheral is not
16/// available, similar USB power events can be leveraged.
17pub struct EnabledUsbDevice<'d, D: Driver<'d>> {
18 pub underlying: UsbDevice<'d, D>,
19 enable_usb_signal: &'d Signal<bool>,
20}
21
22impl<'d, D: Driver<'d>> EnabledUsbDevice<'d, D> {
23 /// Wrap an existing UsbDevice and take a signal that will be used
24 /// to enable/disable it, perhaps from an external POWER_CLOCK
25 /// interrupt, or the equivalent when dealing with softdevices.
26 pub fn new(underlying: UsbDevice<'d, D>, enable_usb_signal: &'d Signal<bool>) -> Self {
27 Self {
28 underlying,
29 enable_usb_signal,
30 }
31 }
32
33 /// Runs the underlying `UsbDevice` taking care of reacting to USB becoming
34 /// enabled/disabled.
35 ///
36 /// This future may leave the bus in an invalid state if it is dropped.
37 /// After dropping the future, [`UsbDevice::disable()`] should be called
38 /// before calling any other `UsbDevice` methods to fully reset the
39 /// peripheral.
40 pub async fn run(&mut self) -> ! {
41 while !self.enable_usb_signal.wait().await {}
42 loop {
43 match select(
44 self.underlying.run_until_suspend(),
45 self.enable_usb_signal.wait(),
46 )
47 .await
48 {
49 Either::First(_) => {}
50 Either::Second(enable) => {
51 if !enable {
52 self.underlying.disable().await;
53 while !self.enable_usb_signal.wait().await {}
54 }
55 }
56 }
57 match select(self.underlying.wait_resume(), self.enable_usb_signal.wait()).await {
58 Either::First(_) => {}
59 Either::Second(enable) => {
60 if !enable {
61 self.underlying.disable().await;
62 while !self.enable_usb_signal.wait().await {}
63 }
64 }
65 }
66 }
67 }
68}