From a9af20b6ce1276addd04f8a7681a84b3d569d3e7 Mon Sep 17 00:00:00 2001 From: alexmoon Date: Fri, 25 Mar 2022 19:22:00 -0400 Subject: Add callback to `Softdevice::run()` to receive `SocEvent`s that are not handled by nrf-softdevice. --- nrf-softdevice/src/events.rs | 10 +++++----- nrf-softdevice/src/softdevice.rs | 12 ++++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/nrf-softdevice/src/events.rs b/nrf-softdevice/src/events.rs index bf7c5d6..f45538e 100644 --- a/nrf-softdevice/src/events.rs +++ b/nrf-softdevice/src/events.rs @@ -15,7 +15,7 @@ static SWI2_WAKER: AtomicWaker = AtomicWaker::new(); #[repr(u32)] #[derive(Debug, PartialEq, Eq, Clone, Copy, IntoPrimitive, TryFromPrimitive)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] -enum SocEvent { +pub enum SocEvent { Hfclkstarted = raw::NRF_SOC_EVTS_NRF_EVT_HFCLKSTARTED, PowerFailureWarning = raw::NRF_SOC_EVTS_NRF_EVT_POWER_FAILURE_WARNING, FlashOperationSuccess = raw::NRF_SOC_EVTS_NRF_EVT_FLASH_OPERATION_SUCCESS, @@ -33,7 +33,7 @@ enum SocEvent { PowerUsbRemoved = raw::NRF_SOC_EVTS_NRF_EVT_POWER_USB_REMOVED, } -fn on_soc_evt(evt: u32) { +fn on_soc_evt(evt: u32, evt_handler: &mut F) { let evt = match SocEvent::try_from(evt) { Ok(evt) => evt, Err(_) => panic!("Unknown soc evt {:?}", evt), @@ -43,21 +43,21 @@ fn on_soc_evt(evt: u32) { match evt { SocEvent::FlashOperationError => crate::flash::on_flash_error(), SocEvent::FlashOperationSuccess => crate::flash::on_flash_success(), - _ => {} + _ => evt_handler(evt), } } // TODO actually derive this from the headers + the ATT_MTU const BLE_EVT_MAX_SIZE: u16 = 128; -pub(crate) async fn run() -> ! { +pub(crate) async fn run(mut soc_evt_handler: F) -> ! { poll_fn(|cx| unsafe { SWI2_WAKER.register(cx.waker()); let mut evt: u32 = 0; loop { match RawError::convert(raw::sd_evt_get(&mut evt as _)) { - Ok(()) => on_soc_evt(evt), + Ok(()) => on_soc_evt(evt, &mut soc_evt_handler), Err(RawError::NotFound) => break, Err(err) => panic!("sd_evt_get err {:?}", err), } diff --git a/nrf-softdevice/src/softdevice.rs b/nrf-softdevice/src/softdevice.rs index cbcfe8b..af8a0b4 100644 --- a/nrf-softdevice/src/softdevice.rs +++ b/nrf-softdevice/src/softdevice.rs @@ -3,9 +3,9 @@ use core::ptr; use core::sync::atomic::{AtomicBool, Ordering}; use embassy::util::Forever; -use crate::pac; use crate::raw; use crate::RawError; +use crate::{pac, SocEvent}; unsafe extern "C" fn fault_handler(id: u32, pc: u32, info: u32) { match (id, info) { @@ -320,6 +320,14 @@ impl Softdevice { /// It must be called in its own async task after enabling the softdevice /// and before doing any operation. Failure to doing so will cause async operations to never finish. pub async fn run(&self) -> ! { - crate::events::run().await + self.run_with_callback(|_| {}).await + } + + /// Runs the softdevice event handling loop. + /// + /// It must be called in its own async task after enabling the softdevice + /// and before doing any operation. Failure to doing so will cause async operations to never finish. + pub async fn run_with_callback(&self, f: F) -> ! { + crate::events::run(f).await } } -- cgit v1.2.3