From 2684bf605ac7c7937cb8db7bc9142b3d34c77b17 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 31 Oct 2020 22:45:35 +0100 Subject: [PATCH] update multiprio example. --- embassy/src/util/forever.rs | 6 ++++ examples/src/bin/multiprio.rs | 66 +++++++++++------------------------ 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/embassy/src/util/forever.rs b/embassy/src/util/forever.rs index 670f6f13..4d3e158a 100644 --- a/embassy/src/util/forever.rs +++ b/embassy/src/util/forever.rs @@ -30,4 +30,10 @@ impl Forever { &mut *p } } + + pub unsafe fn steal(&self) -> &'static mut T { + let p = self.t.get(); + let p = (&mut *p).as_mut_ptr(); + &mut *p + } } diff --git a/examples/src/bin/multiprio.rs b/examples/src/bin/multiprio.rs index 30a5e85e..dbca491e 100644 --- a/examples/src/bin/multiprio.rs +++ b/examples/src/bin/multiprio.rs @@ -61,13 +61,14 @@ mod example_common; use example_common::*; -use core::mem::MaybeUninit; 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 embassy::executor::{task, TimerExecutor}; +use embassy::time::{Duration, Instant, Timer}; +use embassy::util::Forever; +use embassy_nrf::{interrupt, pac, rtc}; + #[task] async fn run_high() { loop { @@ -110,10 +111,10 @@ async fn run_low() { } } -static mut RTC: MaybeUninit> = MaybeUninit::uninit(); -static mut EXECUTOR_LOW: MaybeUninit>> = MaybeUninit::uninit(); -static mut EXECUTOR_MED: MaybeUninit>> = MaybeUninit::uninit(); -static mut EXECUTOR_HIGH: MaybeUninit>> = MaybeUninit::uninit(); +static RTC: Forever> = Forever::new(); +static EXECUTOR_LOW: Forever>> = Forever::new(); +static EXECUTOR_MED: Forever>> = Forever::new(); +static EXECUTOR_HIGH: Forever>> = Forever::new(); #[entry] fn main() -> ! { @@ -126,60 +127,35 @@ fn main() -> ! { .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); - let rtc: &'static _ = unsafe { - let ptr = RTC.as_mut_ptr(); - ptr.write(rtc::RTC::new(p.RTC1)); - &*ptr - }; - + let rtc = RTC.put(rtc::RTC::new(p.RTC1)); rtc.start(); unsafe { embassy::time::set_clock(rtc) }; - let executor_low: &'static _ = unsafe { - let ptr = EXECUTOR_LOW.as_mut_ptr(); - ptr.write(Executor::new(rtc.alarm0(), 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 - }; + let executor_low = EXECUTOR_LOW.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev)); + let executor_med = EXECUTOR_MED.put(TimerExecutor::new(rtc.alarm1(), cortex_m::asm::sev)); + let executor_high = EXECUTOR_HIGH.put(TimerExecutor::new(rtc.alarm2(), cortex_m::asm::sev)); interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7); interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6); interrupt::enable(interrupt::SWI0_EGU0); interrupt::enable(interrupt::SWI1_EGU1); - unsafe { - executor_low.spawn(run_low()).dewrap(); - executor_med.spawn(run_med()).dewrap(); - executor_high.spawn(run_high()).dewrap(); + executor_low.spawn(run_low()).dewrap(); + executor_med.spawn(run_med()).dewrap(); + executor_high.spawn(run_high()).dewrap(); - loop { - executor_low.run(); - cortex_m::asm::wfe(); - } + loop { + executor_low.run(); + cortex_m::asm::wfe(); } } #[interrupt] unsafe fn SWI0_EGU0() { - EXECUTOR_MED.as_ptr().as_ref().unwrap().run() + EXECUTOR_MED.steal().run() } #[interrupt] unsafe fn SWI1_EGU1() { - EXECUTOR_HIGH.as_ptr().as_ref().unwrap().run() + EXECUTOR_HIGH.steal().run() }