RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/softdevice/
vbus.rs

1use embassy_executor::Spawner;
2use embassy_nrf::usb::vbus_detect::{SoftwareVbusDetect, VbusDetect};
3use nrf_softdevice::SocEvent;
4
5use crate::softdevice::SdEventChan;
6
7pub struct SoftdeviceVbusDetect(SoftwareVbusDetect);
8
9#[embassy_executor::task]
10async fn sd_vbus_task(d: &'static SoftdeviceVbusDetect, chan: &'static SdEventChan) {
11    loop {
12        let ev = chan.receive().await;
13        match ev {
14            SocEvent::PowerUsbRemoved => d.0.detected(false),
15            SocEvent::PowerUsbPowerReady => d.0.ready(),
16            SocEvent::PowerUsbDetected => d.0.detected(true),
17            _ => {}
18        }
19    }
20}
21
22impl SoftdeviceVbusDetect {
23    /// Initialize the SoftdeviceVbusDetect and return a static reference to it.
24    ///
25    /// This function must be called only once.
26    pub fn init(spawner: Spawner, chan: &'static SdEventChan) -> &'static Self {
27        static VBUS_DETECT: static_cell::StaticCell<SoftdeviceVbusDetect> =
28            static_cell::StaticCell::new();
29
30        let d = Self(SoftwareVbusDetect::new(true, false));
31        let d = VBUS_DETECT.init(d);
32
33        spawner.must_spawn(sd_vbus_task(d, chan));
34
35        d
36    }
37}
38
39impl VbusDetect for &SoftdeviceVbusDetect {
40    fn is_usb_detected(&self) -> bool {
41        VbusDetect::is_usb_detected(&&self.0)
42    }
43
44    async fn wait_power_ready(&mut self) -> Result<(), ()> {
45        VbusDetect::wait_power_ready(&mut &self.0).await
46    }
47}