2020-09-26 00:36:02 +02:00
|
|
|
//! This example showcases how to create multiple Executor instances to run tasks at
|
|
|
|
//! different priority levels.
|
|
|
|
//!
|
|
|
|
//! Low priority executor runs in thread mode (not interrupt), and uses `sev` for signaling
|
|
|
|
//! there's work in the queue, and `wfe` for waiting for work.
|
|
|
|
//!
|
|
|
|
//! Medium and high priority executors run in two interrupts with different priorities.
|
|
|
|
//! Signaling work is done by pending the interrupt. No "waiting" needs to be done explicitly, since
|
|
|
|
//! when there's work the interrupt will trigger and run the executor.
|
|
|
|
//!
|
|
|
|
//! Sample output below. Note that high priority ticks can interrupt everything else, and
|
|
|
|
//! medium priority computations can interrupt low priority computations, making them to appear
|
|
|
|
//! to take significantly longer time.
|
|
|
|
//!
|
|
|
|
//! ```not_rust
|
|
|
|
//! [med] Starting long computation
|
|
|
|
//! [med] done in 992 ms
|
|
|
|
//! [high] tick!
|
|
|
|
//! [low] Starting long computation
|
|
|
|
//! [med] Starting long computation
|
|
|
|
//! [high] tick!
|
|
|
|
//! [high] tick!
|
|
|
|
//! [med] done in 993 ms
|
|
|
|
//! [med] Starting long computation
|
|
|
|
//! [high] tick!
|
|
|
|
//! [high] tick!
|
|
|
|
//! [med] done in 993 ms
|
|
|
|
//! [low] done in 3972 ms
|
|
|
|
//! [med] Starting long computation
|
|
|
|
//! [high] tick!
|
|
|
|
//! [high] tick!
|
|
|
|
//! [med] done in 993 ms
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! For comparison, try changing the code so all 3 tasks get spawned on the low priority executor.
|
|
|
|
//! You will get an output like the following. Note that no computation is ever interrupted.
|
|
|
|
//!
|
|
|
|
//! ```not_rust
|
|
|
|
//! [high] tick!
|
|
|
|
//! [med] Starting long computation
|
|
|
|
//! [med] done in 496 ms
|
|
|
|
//! [low] Starting long computation
|
|
|
|
//! [low] done in 992 ms
|
|
|
|
//! [med] Starting long computation
|
|
|
|
//! [med] done in 496 ms
|
|
|
|
//! [high] tick!
|
|
|
|
//! [low] Starting long computation
|
|
|
|
//! [low] done in 992 ms
|
|
|
|
//! [high] tick!
|
|
|
|
//! [med] Starting long computation
|
|
|
|
//! [med] done in 496 ms
|
|
|
|
//! [high] tick!
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
|
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use cortex_m_rt::entry;
|
2022-04-02 04:35:06 +02:00
|
|
|
use defmt::{info, unwrap};
|
2022-06-11 05:08:57 +02:00
|
|
|
use embassy_nrf::executor::{Executor, InterruptExecutor};
|
2021-08-03 22:08:13 +02:00
|
|
|
use embassy_nrf::interrupt;
|
2022-06-11 05:08:57 +02:00
|
|
|
use embassy_nrf::interrupt::InterruptExt;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_time::{Duration, Instant, Timer};
|
2022-08-22 15:51:44 +02:00
|
|
|
use static_cell::StaticCell;
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-02 04:35:06 +02:00
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2020-09-26 00:36:02 +02:00
|
|
|
async fn run_high() {
|
|
|
|
loop {
|
|
|
|
info!(" [high] tick!");
|
|
|
|
Timer::after(Duration::from_ticks(27374)).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2020-09-26 00:36:02 +02:00
|
|
|
async fn run_med() {
|
|
|
|
loop {
|
|
|
|
let start = Instant::now();
|
|
|
|
info!(" [med] Starting long computation");
|
|
|
|
|
|
|
|
// Spin-wait to simulate a long CPU computation
|
|
|
|
cortex_m::asm::delay(32_000_000); // ~1 second
|
|
|
|
|
|
|
|
let end = Instant::now();
|
2020-10-31 22:37:24 +01:00
|
|
|
let ms = end.duration_since(start).as_ticks() / 33;
|
2021-02-24 08:57:06 +01:00
|
|
|
info!(" [med] done in {} ms", ms);
|
2020-09-26 00:36:02 +02:00
|
|
|
|
|
|
|
Timer::after(Duration::from_ticks(23421)).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2020-09-26 00:36:02 +02:00
|
|
|
async fn run_low() {
|
|
|
|
loop {
|
|
|
|
let start = Instant::now();
|
|
|
|
info!("[low] Starting long computation");
|
|
|
|
|
|
|
|
// Spin-wait to simulate a long CPU computation
|
|
|
|
cortex_m::asm::delay(64_000_000); // ~2 seconds
|
|
|
|
|
|
|
|
let end = Instant::now();
|
2020-10-31 22:37:24 +01:00
|
|
|
let ms = end.duration_since(start).as_ticks() / 33;
|
2021-02-24 08:57:06 +01:00
|
|
|
info!("[low] done in {} ms", ms);
|
2020-09-26 00:36:02 +02:00
|
|
|
|
|
|
|
Timer::after(Duration::from_ticks(32983)).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:51:44 +02:00
|
|
|
static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::SWI1_EGU1>> = StaticCell::new();
|
|
|
|
static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::SWI0_EGU0>> = StaticCell::new();
|
|
|
|
static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
|
2020-09-26 00:36:02 +02:00
|
|
|
|
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
info!("Hello World!");
|
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
let _p = embassy_nrf::init(Default::default());
|
2020-09-26 00:36:02 +02:00
|
|
|
|
2021-02-02 05:14:52 +01:00
|
|
|
// High-priority executor: SWI1_EGU1, priority level 6
|
|
|
|
let irq = interrupt::take!(SWI1_EGU1);
|
2021-05-11 01:27:46 +02:00
|
|
|
irq.set_priority(interrupt::Priority::P6);
|
2022-08-22 15:51:44 +02:00
|
|
|
let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq));
|
2022-04-25 22:09:04 +02:00
|
|
|
let spawner = executor.start();
|
|
|
|
unwrap!(spawner.spawn(run_high()));
|
2021-02-02 05:14:52 +01:00
|
|
|
|
|
|
|
// Medium-priority executor: SWI0_EGU0, priority level 7
|
|
|
|
let irq = interrupt::take!(SWI0_EGU0);
|
2021-05-11 01:27:46 +02:00
|
|
|
irq.set_priority(interrupt::Priority::P7);
|
2022-08-22 15:51:44 +02:00
|
|
|
let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq));
|
2022-04-25 22:09:04 +02:00
|
|
|
let spawner = executor.start();
|
|
|
|
unwrap!(spawner.spawn(run_med()));
|
2021-02-02 05:14:52 +01:00
|
|
|
|
|
|
|
// Low priority executor: runs in thread mode, using WFE/SEV
|
2022-08-22 15:51:44 +02:00
|
|
|
let executor = EXECUTOR_LOW.init(Executor::new());
|
2021-02-02 05:14:52 +01:00
|
|
|
executor.run(|spawner| {
|
|
|
|
unwrap!(spawner.spawn(run_low()));
|
|
|
|
});
|
2020-09-26 00:36:02 +02:00
|
|
|
}
|