Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow processing soc events and ble events in separate tasks #189

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nrf-softdevice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ critical-section = { version = "1.0", optional = true }

num_enum = { version = "0.6.1", default-features = false }
embassy-sync = { version = "0.2.0" }
embassy-futures = { version = "0.1.0" }
cortex-m = "0.7.2"
cortex-m-rt = ">=0.6.15,<0.8"
heapless = "0.7.1"
Expand Down
21 changes: 16 additions & 5 deletions nrf-softdevice/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use num_enum::{IntoPrimitive, TryFromPrimitive};
use crate::pac::interrupt;
use crate::{raw, RawError};

static SWI2_WAKER: AtomicWaker = AtomicWaker::new();
static SWI2_SOC_EVT_WAKER: AtomicWaker = AtomicWaker::new();
static SWI2_BLE_EVT_WAKER: AtomicWaker = AtomicWaker::new();

/// SoC events reported by the softdevice.
#[rustfmt::skip]
Expand Down Expand Up @@ -58,9 +59,9 @@ const BLE_EVT_MAX_SIZE: u16 = 256;
#[cfg(not(any(feature = "evt-max-size-256", feature = "evt-max-size-512")))]
const BLE_EVT_MAX_SIZE: u16 = 128;

pub(crate) async fn run<F: FnMut(SocEvent)>(mut soc_evt_handler: F) -> ! {
pub(crate) async fn run_soc<F: FnMut(SocEvent)>(mut soc_evt_handler: F) -> ! {
poll_fn(|cx| unsafe {
SWI2_WAKER.register(cx.waker());
SWI2_SOC_EVT_WAKER.register(cx.waker());

let mut evt: u32 = 0;
loop {
Expand All @@ -71,6 +72,14 @@ pub(crate) async fn run<F: FnMut(SocEvent)>(mut soc_evt_handler: F) -> ! {
}
}

Poll::Pending
})
.await
}

pub(crate) async fn run_ble() -> ! {
poll_fn(|cx| unsafe {
SWI2_BLE_EVT_WAKER.register(cx.waker());
// Using u32 since the buffer has to be aligned to 4
let mut evt: MaybeUninit<[u32; BLE_EVT_MAX_SIZE as usize / 4]> = MaybeUninit::uninit();

Expand All @@ -94,11 +103,13 @@ pub(crate) async fn run<F: FnMut(SocEvent)>(mut soc_evt_handler: F) -> ! {
#[cfg(any(feature = "nrf52805", feature = "nrf52810", feature = "nrf52811"))]
#[interrupt]
unsafe fn SWI2() {
SWI2_WAKER.wake();
SWI2_SOC_EVT_WAKER.wake();
SWI2_BLE_EVT_WAKER.wake();
}

#[cfg(not(any(feature = "nrf52805", feature = "nrf52810", feature = "nrf52811")))]
#[interrupt]
unsafe fn SWI2_EGU2() {
SWI2_WAKER.wake();
SWI2_SOC_EVT_WAKER.wake();
SWI2_BLE_EVT_WAKER.wake();
}
20 changes: 18 additions & 2 deletions nrf-softdevice/src/softdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ 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) -> ! {
self.run_with_callback(|_| {}).await
self.run_with_callback(|_| ()).await
}

/// Runs the softdevice event handling loop with a callback for [`SocEvent`]s.
Expand All @@ -316,6 +316,22 @@ impl Softdevice {
/// version allows the application to provide a callback to receive SoC events
/// from the softdevice (other than flash events which are handled by [`Flash`](crate::flash::Flash)).
pub async fn run_with_callback<F: FnMut(SocEvent)>(&self, f: F) -> ! {
crate::events::run(f).await
embassy_futures::join::join(self.run_ble(), crate::events::run_soc(f)).await;
// Should never get here
loop {}
}

/// Runs the softdevice soc event handler only.
///
/// It must be called under the same conditions as [`Softdevice::run()`].
pub async fn run_soc(&self) -> ! {
crate::events::run_soc(|_| ()).await
}

/// Runs the softdevice ble event handler only.
///
/// It must be called under the same conditions as [`Softdevice::run()`].
pub async fn run_ble(&self) -> ! {
crate::events::run_ble().await
}
}
Loading