update multiprio example.

This commit is contained in:
Dario Nieuwenhuis 2020-10-31 22:45:35 +01:00
parent b3858441f7
commit 2684bf605a
2 changed files with 27 additions and 45 deletions

View File

@ -30,4 +30,10 @@ impl<T> Forever<T> {
&mut *p &mut *p
} }
} }
pub unsafe fn steal(&self) -> &'static mut T {
let p = self.t.get();
let p = (&mut *p).as_mut_ptr();
&mut *p
}
} }

View File

@ -61,13 +61,14 @@
mod example_common; mod example_common;
use example_common::*; use example_common::*;
use core::mem::MaybeUninit;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embassy::executor::{task, Executor};
use embassy::time::{Duration, Instant, Timer};
use embassy_nrf::{interrupt, pac, rtc};
use nrf52840_hal::clocks; use nrf52840_hal::clocks;
use embassy::executor::{task, TimerExecutor};
use embassy::time::{Duration, Instant, Timer};
use embassy::util::Forever;
use embassy_nrf::{interrupt, pac, rtc};
#[task] #[task]
async fn run_high() { async fn run_high() {
loop { loop {
@ -110,10 +111,10 @@ async fn run_low() {
} }
} }
static mut RTC: MaybeUninit<rtc::RTC<pac::RTC1>> = MaybeUninit::uninit(); static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
static mut EXECUTOR_LOW: MaybeUninit<Executor<rtc::Alarm<pac::RTC1>>> = MaybeUninit::uninit(); static EXECUTOR_LOW: Forever<TimerExecutor<rtc::Alarm<pac::RTC1>>> = Forever::new();
static mut EXECUTOR_MED: MaybeUninit<Executor<rtc::Alarm<pac::RTC1>>> = MaybeUninit::uninit(); static EXECUTOR_MED: Forever<TimerExecutor<rtc::Alarm<pac::RTC1>>> = Forever::new();
static mut EXECUTOR_HIGH: MaybeUninit<Executor<rtc::Alarm<pac::RTC1>>> = MaybeUninit::uninit(); static EXECUTOR_HIGH: Forever<TimerExecutor<rtc::Alarm<pac::RTC1>>> = Forever::new();
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
@ -126,60 +127,35 @@ fn main() -> ! {
.set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
.start_lfclk(); .start_lfclk();
let rtc: &'static _ = unsafe { let rtc = RTC.put(rtc::RTC::new(p.RTC1));
let ptr = RTC.as_mut_ptr();
ptr.write(rtc::RTC::new(p.RTC1));
&*ptr
};
rtc.start(); rtc.start();
unsafe { embassy::time::set_clock(rtc) }; unsafe { embassy::time::set_clock(rtc) };
let executor_low: &'static _ = unsafe { let executor_low = EXECUTOR_LOW.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev));
let ptr = EXECUTOR_LOW.as_mut_ptr(); let executor_med = EXECUTOR_MED.put(TimerExecutor::new(rtc.alarm1(), cortex_m::asm::sev));
ptr.write(Executor::new(rtc.alarm0(), cortex_m::asm::sev)); let executor_high = EXECUTOR_HIGH.put(TimerExecutor::new(rtc.alarm2(), cortex_m::asm::sev));
&*ptr
};
let executor_med: &'static _ = unsafe {
let ptr = EXECUTOR_MED.as_mut_ptr();
ptr.write(Executor::new(rtc.alarm1(), || {
interrupt::pend(interrupt::SWI0_EGU0)
}));
&*ptr
};
let executor_high: &'static _ = unsafe {
let ptr = EXECUTOR_HIGH.as_mut_ptr();
ptr.write(Executor::new(rtc.alarm2(), || {
interrupt::pend(interrupt::SWI1_EGU1)
}));
&*ptr
};
interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7); interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7);
interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6); interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6);
interrupt::enable(interrupt::SWI0_EGU0); interrupt::enable(interrupt::SWI0_EGU0);
interrupt::enable(interrupt::SWI1_EGU1); interrupt::enable(interrupt::SWI1_EGU1);
unsafe { executor_low.spawn(run_low()).dewrap();
executor_low.spawn(run_low()).dewrap(); executor_med.spawn(run_med()).dewrap();
executor_med.spawn(run_med()).dewrap(); executor_high.spawn(run_high()).dewrap();
executor_high.spawn(run_high()).dewrap();
loop { loop {
executor_low.run(); executor_low.run();
cortex_m::asm::wfe(); cortex_m::asm::wfe();
}
} }
} }
#[interrupt] #[interrupt]
unsafe fn SWI0_EGU0() { unsafe fn SWI0_EGU0() {
EXECUTOR_MED.as_ptr().as_ref().unwrap().run() EXECUTOR_MED.steal().run()
} }
#[interrupt] #[interrupt]
unsafe fn SWI1_EGU1() { unsafe fn SWI1_EGU1() {
EXECUTOR_HIGH.as_ptr().as_ref().unwrap().run() EXECUTOR_HIGH.steal().run()
} }