queue, async_systic2

This commit is contained in:
Per Lindgren 2020-10-26 15:50:10 +01:00
parent 809d3ba269
commit 28490f5caa

View file

@ -21,7 +21,6 @@ use rtic::Mutex;
#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] #[rtic::app(device = lm3s6965, dispatchers = [SSI0])]
mod app { mod app {
use crate::Timer;
use crate::*; use crate::*;
#[resources] #[resources]
@ -31,14 +30,14 @@ mod app {
#[init] #[init]
fn init(cx: init::Context) -> init::LateResources { fn init(cx: init::Context) -> init::LateResources {
hprintln!("init").unwrap(); hprintln!("init").ok();
foo::spawn().unwrap(); foo::spawn().unwrap();
init::LateResources { init::LateResources {
systick: Systick { systick: Systick {
syst: cx.core.SYST, syst: cx.core.SYST,
state: State::Done, state: State::Done,
queue: BinaryHeap::new(), queue: BinaryHeap::new(),
waker: None, // waker: None,
}, },
} }
} }
@ -47,7 +46,7 @@ mod app {
fn idle(_: idle::Context) -> ! { fn idle(_: idle::Context) -> ! {
// debug::exit(debug::EXIT_SUCCESS); // debug::exit(debug::EXIT_SUCCESS);
loop { loop {
hprintln!("idle"); hprintln!("idle").ok();
cortex_m::asm::wfi(); // put the MCU in sleep mode until interrupt occurs cortex_m::asm::wfi(); // put the MCU in sleep mode until interrupt occurs
} }
} }
@ -102,14 +101,14 @@ mod app {
} }
} }
// This the actual RTIC task, binds to systic. // RTIC task bound to the HW SysTick interrupt
#[task(binds = SysTick, resources = [systick], priority = 2)] #[task(binds = SysTick, resources = [systick], priority = 2)]
fn systic(mut cx: systic::Context) { fn systic(mut cx: systic::Context) {
hprintln!("systic interrupt").ok(); hprintln!("systic interrupt").ok();
cx.resources.systick.lock(|s| { cx.resources.systick.lock(|s| {
s.syst.disable_interrupt(); s.syst.disable_interrupt();
s.state = State::Done; s.state = State::Done;
s.waker.take().map(|w| w.wake()); s.queue.pop().map(|w| w.waker.wake());
}); });
} }
} }
@ -186,7 +185,7 @@ pub enum State {
struct Timeout { struct Timeout {
time: u32, time: u32,
waker: Option<Waker>, waker: Waker,
} }
impl Ord for Timeout { impl Ord for Timeout {
@ -213,7 +212,7 @@ pub struct Systick {
syst: cortex_m::peripheral::SYST, syst: cortex_m::peripheral::SYST,
state: State, state: State,
queue: BinaryHeap<Timeout, U8, Max>, queue: BinaryHeap<Timeout, U8, Max>,
waker: Option<Waker>, // waker: Option<Waker>,
} }
//============= //=============
@ -221,32 +220,31 @@ pub struct Systick {
// Later we want a proper queue // Later we want a proper queue
pub struct Timer<'a, T: Mutex<T = Systick>> { pub struct Timer<'a, T: Mutex<T = Systick>> {
started: bool, request: Option<u32>,
t: u32,
systick: &'a mut T, systick: &'a mut T,
} }
impl<'a, T: Mutex<T = Systick>> Future for Timer<'a, T> { impl<'a, T: Mutex<T = Systick>> Future for Timer<'a, T> {
type Output = (); type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let Self { let Self { request, systick } = &mut *self;
started,
t,
systick,
} = &mut *self;
systick.lock(|s| { systick.lock(|s| {
if !*started { // enqueue a new request
s.syst.set_reload(*t); request.take().map(|t| {
s.syst.set_reload(t);
s.syst.enable_counter(); s.syst.enable_counter();
s.syst.enable_interrupt(); s.syst.enable_interrupt();
s.state = State::Started; s.state = State::Started;
*started = true; s.queue.push(Timeout {
} time: t,
waker: cx.waker().clone(),
});
});
match s.state { match s.state {
State::Done => Poll::Ready(()), State::Done => Poll::Ready(()),
State::Started => { State::Started => {
s.waker = Some(cx.waker().clone()); // s.waker = Some(cx.waker().clone());
Poll::Pending Poll::Pending
} }
} }
@ -257,8 +255,7 @@ impl<'a, T: Mutex<T = Systick>> Future for Timer<'a, T> {
fn timer_delay<'a, T: Mutex<T = Systick>>(systick: &'a mut T, t: u32) -> Timer<'a, T> { fn timer_delay<'a, T: Mutex<T = Systick>>(systick: &'a mut T, t: u32) -> Timer<'a, T> {
hprintln!("timer_delay {}", t); hprintln!("timer_delay {}", t);
Timer { Timer {
started: false, request: Some(t),
t,
systick, systick,
} }
} }