diff options
author | huntc <huntchr@gmail.com> | 2022-06-15 12:17:04 +1000 |
---|---|---|
committer | huntc <huntchr@gmail.com> | 2022-07-07 10:08:57 +1000 |
commit | c46e9b6cfc1375f5e5e2bade8eb2174cc47c4a28 (patch) | |
tree | e9f4064589cb4e56defd2ef5e3833db96e4e9eba /examples | |
parent | 1fb70e2fac11afb4139ec2ed89293ce7ae1f6e65 (diff) | |
download | embassy-c46e9b6cfc1375f5e5e2bade8eb2174cc47c4a28.zip |
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 'examples')
-rw-r--r-- | examples/nrf/src/bin/usb_serial.rs | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/examples/nrf/src/bin/usb_serial.rs b/examples/nrf/src/bin/usb_serial.rs index f108db46..377ae8c3 100644 --- a/examples/nrf/src/bin/usb_serial.rs +++ b/examples/nrf/src/bin/usb_serial.rs @@ -6,28 +6,52 @@ use core::mem; use defmt::{info, panic}; +use embassy::channel::signal::Signal; use embassy::executor::Spawner; +use embassy::interrupt::InterruptExt; use embassy_nrf::usb::{Driver, Instance}; -use embassy_nrf::{interrupt, pac, Peripherals}; +use embassy_nrf::{interrupt, interrupt, pac, pac, Peripherals}; use embassy_usb::driver::EndpointError; +use embassy_usb::util::EnabledUsbDevice; use embassy_usb::{Builder, Config}; use embassy_usb_serial::{CdcAcmClass, State}; use futures::future::join; -use {defmt_rtt as _, panic_probe as _}; +use {defmt_rtt as _, panic_probe as _}; // global logger + +static ENABLE_USB: Signal<bool> = Signal::new(); + +fn on_power_interrupt(_: *mut ()) { + let regs = unsafe { &*pac::POWER::ptr() }; + + if regs.events_usbdetected.read().bits() != 0 { + regs.events_usbdetected.reset(); + info!("Vbus detected, enabling USB..."); + ENABLE_USB.signal(true); + } + + if regs.events_usbremoved.read().bits() != 0 { + regs.events_usbremoved.reset(); + info!("Vbus removed, disabling USB..."); + ENABLE_USB.signal(false); + } +} #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { let clock: pac::CLOCK = unsafe { mem::transmute(()) }; let power: pac::POWER = unsafe { mem::transmute(()) }; + let power_irq = interrupt::take!(POWER_CLOCK); + power_irq.set_handler(on_power_interrupt); + power_irq.unpend(); + power_irq.enable(); + + power.intenset.write(|w| w.usbdetected().set().usbremoved().set()); + info!("Enabling ext hfosc..."); clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) }); while clock.events_hfclkstarted.read().bits() != 1 {} - info!("Waiting for vbus..."); - while !power.usbregstatus.read().vbusdetect().is_vbus_present() {} - info!("vbus OK"); - // Create the driver, from the HAL. let irq = interrupt::take!(USBD); let driver = Driver::new(p.USBD, irq); @@ -70,7 +94,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); // Build the builder. - let mut usb = builder.build(); + let mut usb = EnabledUsbDevice::new(builder.build(), &ENABLE_USB); // Run the USB device. let usb_fut = usb.run(); |