diff options
| author | huntc <[email protected]> | 2022-07-08 16:22:25 +1000 |
|---|---|---|
| committer | huntc <[email protected]> | 2022-07-08 16:22:25 +1000 |
| commit | 81796d29b44f1b052d7631bc8677525d5aa70b50 (patch) | |
| tree | 0301dd3e58a3b9564c3bbf1d586f61a338006d3a /embassy-nrf/src/usb.rs | |
| parent | 8d71a358c8fa0524fce564357d3d36f24be41353 (diff) | |
New constructor to cater for the softdevice
Also, correctly sets the initial power management state when using power management
Diffstat (limited to 'embassy-nrf/src/usb.rs')
| -rw-r--r-- | embassy-nrf/src/usb.rs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index 88af74fc0..973fe16f8 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs | |||
| @@ -35,6 +35,17 @@ pub struct Driver<'d, T: Instance> { | |||
| 35 | 35 | ||
| 36 | impl<'d, T: Instance> Driver<'d, T> { | 36 | impl<'d, T: Instance> Driver<'d, T> { |
| 37 | pub fn new(_usb: impl Unborrow<Target = T> + 'd, irq: impl Unborrow<Target = T::Interrupt> + 'd) -> Self { | 37 | pub fn new(_usb: impl Unborrow<Target = T> + 'd, irq: impl Unborrow<Target = T::Interrupt> + 'd) -> Self { |
| 38 | Self::with_power_state(_usb, irq, true) | ||
| 39 | } | ||
| 40 | |||
| 41 | /// Establish a new device that then puts the USB device into an initial power state. | ||
| 42 | /// Required when using the nRF softdevice where power is unavailable until | ||
| 43 | /// notified by it, at which time the the [`Self::power()`] method should then be called. | ||
| 44 | pub fn with_power_state( | ||
| 45 | _usb: impl Unborrow<Target = T> + 'd, | ||
| 46 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | ||
| 47 | power_available: bool, | ||
| 48 | ) -> Self { | ||
| 38 | unborrow!(irq); | 49 | unborrow!(irq); |
| 39 | irq.set_handler(Self::on_interrupt); | 50 | irq.set_handler(Self::on_interrupt); |
| 40 | irq.unpend(); | 51 | irq.unpend(); |
| @@ -42,7 +53,7 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 42 | 53 | ||
| 43 | // Initialize the bus so that it signals that power is available. | 54 | // Initialize the bus so that it signals that power is available. |
| 44 | // Not required when using with_power_management as we then rely on the irq. | 55 | // Not required when using with_power_management as we then rely on the irq. |
| 45 | POWER_AVAILABLE.store(true, Ordering::Relaxed); | 56 | POWER_AVAILABLE.store(power_available, Ordering::Relaxed); |
| 46 | BUS_WAKER.wake(); | 57 | BUS_WAKER.wake(); |
| 47 | 58 | ||
| 48 | Self { | 59 | Self { |
| @@ -68,7 +79,7 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 68 | 79 | ||
| 69 | regs.intenset.write(|w| w.usbdetected().set().usbremoved().set()); | 80 | regs.intenset.write(|w| w.usbdetected().set().usbremoved().set()); |
| 70 | 81 | ||
| 71 | Self::new(_usb, irq) | 82 | Self::with_power_state(_usb, irq, regs.usbregstatus.read().vbusdetect().is_vbus_present()) |
| 72 | } | 83 | } |
| 73 | 84 | ||
| 74 | fn on_interrupt(_: *mut ()) { | 85 | fn on_interrupt(_: *mut ()) { |
| @@ -145,6 +156,14 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 145 | BUS_WAKER.wake(); | 156 | BUS_WAKER.wake(); |
| 146 | } | 157 | } |
| 147 | } | 158 | } |
| 159 | |||
| 160 | /// Manually declare that USB power is available or unavailable. | ||
| 161 | /// Useful in scenarios where power management cannot be managed | ||
| 162 | /// automatically e.g. when dealing with the nrf-softdevice. | ||
| 163 | pub fn power(available: bool) { | ||
| 164 | POWER_AVAILABLE.store(available, Ordering::Relaxed); | ||
| 165 | BUS_WAKER.wake(); | ||
| 166 | } | ||
| 148 | } | 167 | } |
| 149 | 168 | ||
| 150 | impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> { | 169 | impl<'d, T: Instance> driver::Driver<'d> for Driver<'d, T> { |
