cortex-m/executor: don't use the owned interrupts system.

Preparation for #1224.
This commit is contained in:
Dario Nieuwenhuis 2023-02-27 01:08:16 +01:00
parent 711ce10145
commit 4dfa32b1e0
6 changed files with 152 additions and 85 deletions

View File

@ -1,18 +1,22 @@
//! Executor specific to cortex-m devices. //! Executor specific to cortex-m devices.
use core::marker::PhantomData;
use core::cell::UnsafeCell;
use core::mem::MaybeUninit;
use atomic_polyfill::{AtomicBool, Ordering};
use cortex_m::interrupt::InterruptNumber;
use cortex_m::peripheral::NVIC;
pub use embassy_executor::*; pub use embassy_executor::*;
use crate::interrupt::{Interrupt, InterruptExt}; #[derive(Clone, Copy)]
struct N(u16);
unsafe impl cortex_m::interrupt::InterruptNumber for N {
fn number(self) -> u16 {
self.0
}
}
fn pend_by_number(n: u16) { fn pend_by_number(n: u16) {
#[derive(Clone, Copy)]
struct N(u16);
unsafe impl cortex_m::interrupt::InterruptNumber for N {
fn number(self) -> u16 {
self.0
}
}
cortex_m::peripheral::NVIC::pend(N(n)) cortex_m::peripheral::NVIC::pend(N(n))
} }
@ -37,26 +41,37 @@ fn pend_by_number(n: u16) {
/// ///
/// It is somewhat more complex to use, it's recommended to use the thread-mode /// It is somewhat more complex to use, it's recommended to use the thread-mode
/// [`Executor`] instead, if it works for your use case. /// [`Executor`] instead, if it works for your use case.
pub struct InterruptExecutor<I: Interrupt> { pub struct InterruptExecutor {
irq: I, started: AtomicBool,
inner: raw::Executor, executor: UnsafeCell<MaybeUninit<raw::Executor>>,
not_send: PhantomData<*mut ()>,
} }
impl<I: Interrupt> InterruptExecutor<I> { unsafe impl Send for InterruptExecutor {}
/// Create a new Executor. unsafe impl Sync for InterruptExecutor {}
pub fn new(irq: I) -> Self {
let ctx = irq.number() as *mut (); impl InterruptExecutor {
/// Create a new, not started `InterruptExecutor`.
#[inline]
pub const fn new() -> Self {
Self { Self {
irq, started: AtomicBool::new(false),
inner: raw::Executor::new(|ctx| pend_by_number(ctx as u16), ctx), executor: UnsafeCell::new(MaybeUninit::uninit()),
not_send: PhantomData,
} }
} }
/// Executor interrupt callback.
///
/// # Safety
///
/// You MUST call this from the interrupt handler, and from nowhere else.
pub unsafe fn on_interrupt(&'static self) {
let executor = unsafe { (&*self.executor.get()).assume_init_ref() };
executor.poll();
}
/// Start the executor. /// Start the executor.
/// ///
/// This initializes the executor, configures and enables the interrupt, and returns. /// This initializes the executor, enables the interrupt, and returns.
/// The executor keeps running in the background through the interrupt. /// The executor keeps running in the background through the interrupt.
/// ///
/// This returns a [`SendSpawner`] you can use to spawn tasks on it. A [`SendSpawner`] /// This returns a [`SendSpawner`] you can use to spawn tasks on it. A [`SendSpawner`]
@ -67,23 +82,35 @@ impl<I: Interrupt> InterruptExecutor<I> {
/// To obtain a [`Spawner`](embassy_executor::Spawner) for this executor, use [`Spawner::for_current_executor()`](embassy_executor::Spawner::for_current_executor()) from /// To obtain a [`Spawner`](embassy_executor::Spawner) for this executor, use [`Spawner::for_current_executor()`](embassy_executor::Spawner::for_current_executor()) from
/// a task running in it. /// a task running in it.
/// ///
/// This function requires `&'static mut self`. This means you have to store the /// # Interrupt requirements
/// Executor instance in a place where it'll live forever and grants you mutable
/// access. There's a few ways to do this:
/// ///
/// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) /// You must write the interrupt handler yourself, and make it call [`on_interrupt()`](Self::on_interrupt).
/// - a `static mut` (unsafe) ///
/// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) /// This method already enables (unmasks) the interrupt, you must NOT do it yourself.
pub fn start(&'static mut self) -> SendSpawner { ///
self.irq.disable(); /// You must set the interrupt priority before calling this method. You MUST NOT
/// do it after.
///
pub fn start(&'static self, irq: impl InterruptNumber) -> SendSpawner {
if self
.started
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_err()
{
panic!("InterruptExecutor::start() called multiple times on the same executor.");
}
self.irq.set_handler(|ctx| unsafe { unsafe {
let executor = &*(ctx as *const raw::Executor); (&mut *self.executor.get()).as_mut_ptr().write(raw::Executor::new(
executor.poll(); |ctx| pend_by_number(ctx as u16),
}); irq.number() as *mut (),
self.irq.set_handler_context(&self.inner as *const _ as _); ))
self.irq.enable(); }
self.inner.spawner().make_send() let executor = unsafe { (&*self.executor.get()).assume_init_ref() };
unsafe { NVIC::unmask(irq) }
executor.spawner().make_send()
} }
} }

View File

@ -57,11 +57,14 @@
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
use core::mem;
use cortex_m::peripheral::NVIC;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use defmt::{info, unwrap}; use defmt::{info, unwrap};
use embassy_nrf::executor::{Executor, InterruptExecutor}; use embassy_nrf::executor::{Executor, InterruptExecutor};
use embassy_nrf::interrupt; use embassy_nrf::interrupt;
use embassy_nrf::interrupt::InterruptExt; use embassy_nrf::pac::Interrupt;
use embassy_time::{Duration, Instant, Timer}; use embassy_time::{Duration, Instant, Timer};
use static_cell::StaticCell; use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -108,28 +111,35 @@ async fn run_low() {
} }
} }
static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::SWI1_EGU1>> = StaticCell::new(); static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::SWI0_EGU0>> = StaticCell::new(); static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
#[interrupt]
unsafe fn SWI1_EGU1() {
EXECUTOR_HIGH.on_interrupt()
}
#[interrupt]
unsafe fn SWI0_EGU0() {
EXECUTOR_MED.on_interrupt()
}
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
info!("Hello World!"); info!("Hello World!");
let _p = embassy_nrf::init(Default::default()); let _p = embassy_nrf::init(Default::default());
let mut nvic: NVIC = unsafe { mem::transmute(()) };
// High-priority executor: SWI1_EGU1, priority level 6 // High-priority executor: SWI1_EGU1, priority level 6
let irq = interrupt::take!(SWI1_EGU1); unsafe { nvic.set_priority(Interrupt::SWI1_EGU1, 6 << 5) };
irq.set_priority(interrupt::Priority::P6); let spawner = EXECUTOR_HIGH.start(Interrupt::SWI1_EGU1);
let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_high())); unwrap!(spawner.spawn(run_high()));
// Medium-priority executor: SWI0_EGU0, priority level 7 // Medium-priority executor: SWI0_EGU0, priority level 7
let irq = interrupt::take!(SWI0_EGU0); unsafe { nvic.set_priority(Interrupt::SWI0_EGU0, 7 << 5) };
irq.set_priority(interrupt::Priority::P7); let spawner = EXECUTOR_MED.start(Interrupt::SWI0_EGU0);
let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_med())); unwrap!(spawner.spawn(run_med()));
// Low priority executor: runs in thread mode, using WFE/SEV // Low priority executor: runs in thread mode, using WFE/SEV

View File

@ -15,5 +15,5 @@ panic-probe = "0.3"
embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] } embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] }
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "memory-x", "stm32f091rc", "time-driver-any", "exti"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "memory-x", "stm32f091rc", "time-driver-any", "exti", "unstable-pac"] }
static_cell = "1.0" static_cell = "1.0"

View File

@ -57,11 +57,14 @@
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
use core::mem;
use cortex_m::peripheral::NVIC;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use defmt::*; use defmt::*;
use embassy_stm32::executor::{Executor, InterruptExecutor}; use embassy_stm32::executor::{Executor, InterruptExecutor};
use embassy_stm32::interrupt; use embassy_stm32::interrupt;
use embassy_stm32::interrupt::InterruptExt; use embassy_stm32::pac::Interrupt;
use embassy_time::{Duration, Instant, Timer}; use embassy_time::{Duration, Instant, Timer};
use static_cell::StaticCell; use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -108,27 +111,34 @@ async fn run_low() {
} }
} }
static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::USART1>> = StaticCell::new(); static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::USART2>> = StaticCell::new(); static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
#[interrupt]
unsafe fn USART1() {
EXECUTOR_HIGH.on_interrupt()
}
#[interrupt]
unsafe fn USART2() {
EXECUTOR_MED.on_interrupt()
}
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
// Initialize and create handle for devicer peripherals // Initialize and create handle for devicer peripherals
let _p = embassy_stm32::init(Default::default()); let _p = embassy_stm32::init(Default::default());
let mut nvic: NVIC = unsafe { mem::transmute(()) };
// High-priority executor: USART1, priority level 6 // High-priority executor: USART1, priority level 6
let irq = interrupt::take!(USART1); unsafe { nvic.set_priority(Interrupt::USART1, 6 << 4) };
irq.set_priority(interrupt::Priority::P6); let spawner = EXECUTOR_HIGH.start(Interrupt::USART1);
let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_high())); unwrap!(spawner.spawn(run_high()));
// Medium-priority executor: USART2, priority level 7 // Medium-priority executor: USART2, priority level 7
let irq = interrupt::take!(USART2); unsafe { nvic.set_priority(Interrupt::USART2, 7 << 4) };
irq.set_priority(interrupt::Priority::P7); let spawner = EXECUTOR_MED.start(Interrupt::USART2);
let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_med())); unwrap!(spawner.spawn(run_med()));
// Low priority executor: runs in thread mode, using WFE/SEV // Low priority executor: runs in thread mode, using WFE/SEV

View File

@ -57,11 +57,14 @@
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
use core::mem;
use cortex_m::peripheral::NVIC;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use defmt::*; use defmt::*;
use embassy_stm32::executor::{Executor, InterruptExecutor}; use embassy_stm32::executor::{Executor, InterruptExecutor};
use embassy_stm32::interrupt; use embassy_stm32::interrupt;
use embassy_stm32::interrupt::InterruptExt; use embassy_stm32::pac::Interrupt;
use embassy_time::{Duration, Instant, Timer}; use embassy_time::{Duration, Instant, Timer};
use static_cell::StaticCell; use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -108,28 +111,35 @@ async fn run_low() {
} }
} }
static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::UART4>> = StaticCell::new(); static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::UART5>> = StaticCell::new(); static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
#[interrupt]
unsafe fn UART4() {
EXECUTOR_HIGH.on_interrupt()
}
#[interrupt]
unsafe fn UART5() {
EXECUTOR_MED.on_interrupt()
}
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
info!("Hello World!"); info!("Hello World!");
let _p = embassy_stm32::init(Default::default()); let _p = embassy_stm32::init(Default::default());
let mut nvic: NVIC = unsafe { mem::transmute(()) };
// High-priority executor: SWI1_EGU1, priority level 6 // High-priority executor: UART4, priority level 6
let irq = interrupt::take!(UART4); unsafe { nvic.set_priority(Interrupt::UART4, 6 << 4) };
irq.set_priority(interrupt::Priority::P6); let spawner = EXECUTOR_HIGH.start(Interrupt::UART4);
let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_high())); unwrap!(spawner.spawn(run_high()));
// Medium-priority executor: SWI0_EGU0, priority level 7 // Medium-priority executor: UART5, priority level 7
let irq = interrupt::take!(UART5); unsafe { nvic.set_priority(Interrupt::UART5, 7 << 4) };
irq.set_priority(interrupt::Priority::P7); let spawner = EXECUTOR_MED.start(Interrupt::UART5);
let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_med())); unwrap!(spawner.spawn(run_med()));
// Low priority executor: runs in thread mode, using WFE/SEV // Low priority executor: runs in thread mode, using WFE/SEV

View File

@ -57,11 +57,14 @@
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
use core::mem;
use cortex_m::peripheral::NVIC;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use defmt::*; use defmt::*;
use embassy_stm32::executor::{Executor, InterruptExecutor}; use embassy_stm32::executor::{Executor, InterruptExecutor};
use embassy_stm32::interrupt; use embassy_stm32::interrupt;
use embassy_stm32::interrupt::InterruptExt; use embassy_stm32::pac::Interrupt;
use embassy_time::{Duration, Instant, Timer}; use embassy_time::{Duration, Instant, Timer};
use static_cell::StaticCell; use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -108,28 +111,35 @@ async fn run_low() {
} }
} }
static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::UART4>> = StaticCell::new(); static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::UART5>> = StaticCell::new(); static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
#[interrupt]
unsafe fn UART4() {
EXECUTOR_HIGH.on_interrupt()
}
#[interrupt]
unsafe fn UART5() {
EXECUTOR_MED.on_interrupt()
}
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
info!("Hello World!"); info!("Hello World!");
let _p = embassy_stm32::init(Default::default()); let _p = embassy_stm32::init(Default::default());
let mut nvic: NVIC = unsafe { mem::transmute(()) };
// High-priority executor: SWI1_EGU1, priority level 6 // High-priority executor: UART4, priority level 6
let irq = interrupt::take!(UART4); unsafe { nvic.set_priority(Interrupt::UART4, 6 << 4) };
irq.set_priority(interrupt::Priority::P6); let spawner = EXECUTOR_HIGH.start(Interrupt::UART4);
let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_high())); unwrap!(spawner.spawn(run_high()));
// Medium-priority executor: SWI0_EGU0, priority level 7 // Medium-priority executor: UART5, priority level 7
let irq = interrupt::take!(UART5); unsafe { nvic.set_priority(Interrupt::UART5, 7 << 4) };
irq.set_priority(interrupt::Priority::P7); let spawner = EXECUTOR_MED.start(Interrupt::UART5);
let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq));
let spawner = executor.start();
unwrap!(spawner.spawn(run_med())); unwrap!(spawner.spawn(run_med()));
// Low priority executor: runs in thread mode, using WFE/SEV // Low priority executor: runs in thread mode, using WFE/SEV