mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-24 04:32:52 +01:00
timer as future
This commit is contained in:
parent
22d7957485
commit
d7ca3efd03
1 changed files with 91 additions and 100 deletions
|
@ -20,43 +20,36 @@ use panic_semihosting as _;
|
||||||
|
|
||||||
#[rtic::app(device = lm3s6965, dispatchers = [SSI0], peripherals = true)]
|
#[rtic::app(device = lm3s6965, dispatchers = [SSI0], peripherals = true)]
|
||||||
mod app {
|
mod app {
|
||||||
|
use crate::Timer;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
#[resources]
|
#[resources]
|
||||||
struct Resources {
|
struct Resources {
|
||||||
syst: cortex_m::peripheral::SYST,
|
syst: cortex_m::peripheral::SYST,
|
||||||
#[init(None)]
|
|
||||||
waker: Option<Waker>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[init]
|
#[init]
|
||||||
fn init(cx: init::Context) -> init::LateResources {
|
fn init(cx: init::Context) -> init::LateResources {
|
||||||
// foo::spawn().unwrap();
|
|
||||||
let mut syst = cx.core.SYST;
|
|
||||||
syst.set_reload(100000);
|
|
||||||
syst.enable_interrupt();
|
|
||||||
syst.enable_counter();
|
|
||||||
|
|
||||||
hprintln!("init").unwrap();
|
hprintln!("init").unwrap();
|
||||||
|
foo::spawn().unwrap();
|
||||||
init::LateResources { syst }
|
init::LateResources { syst: cx.core.SYST }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle]
|
#[idle]
|
||||||
fn idle(_: idle::Context) -> ! {
|
fn idle(_: idle::Context) -> ! {
|
||||||
// foo::spawn().unwrap();
|
|
||||||
// debug::exit(debug::EXIT_SUCCESS);
|
// debug::exit(debug::EXIT_SUCCESS);
|
||||||
loop {
|
loop {
|
||||||
continue;
|
hprintln!("idle");
|
||||||
|
cortex_m::asm::wfi(); // put the MCU in sleep mode until interrupt occurs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task]
|
#[task(resources = [syst])]
|
||||||
fn foo(_c: foo::Context) {
|
fn foo(mut cx: foo::Context) {
|
||||||
// BEGIN BOILERPLATE
|
// BEGIN BOILERPLATE
|
||||||
type F = impl Future + 'static;
|
type F = impl Future + 'static;
|
||||||
fn create() -> F {
|
fn create(cx: foo::Context<'static>) -> F {
|
||||||
task()
|
task(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
static mut TASK: Task<F> = Task::new();
|
static mut TASK: Task<F> = Task::new();
|
||||||
|
@ -65,98 +58,95 @@ mod app {
|
||||||
unsafe {
|
unsafe {
|
||||||
match TASK {
|
match TASK {
|
||||||
Task::Idle | Task::Done(_) => {
|
Task::Idle | Task::Done(_) => {
|
||||||
hprintln!("create task").ok();
|
hprintln!("foo spawn task").ok();
|
||||||
TASK.spawn(create);
|
TASK.spawn(|| create(mem::transmute(cx)));
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
hprintln!("poll").ok();
|
|
||||||
|
|
||||||
|
hprintln!("foo trampoline poll").ok();
|
||||||
TASK.poll(|| {});
|
TASK.poll(|| {});
|
||||||
|
|
||||||
match TASK {
|
match TASK {
|
||||||
Task::Done(ref r) => {
|
Task::Done(ref r) => {
|
||||||
hprintln!("done").ok();
|
hprintln!("foo trampoline done").ok();
|
||||||
hprintln!("r = {:?}", mem::transmute::<_, &u32>(r)).ok();
|
// hprintln!("r = {:?}", mem::transmute::<_, &u32>(r)).ok();
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
hprintln!("running").ok();
|
hprintln!("foo trampoline running").ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// END BOILERPLATE
|
// END BOILERPLATE
|
||||||
|
|
||||||
async fn task() -> u32 {
|
async fn task(mut cx: foo::Context<'static>) {
|
||||||
hprintln!("foo1").ok();
|
hprintln!("foo task").ok();
|
||||||
// let a: u32 = bar::spawn().await;
|
|
||||||
// hprintln!("foo2 {}", a).ok();
|
hprintln!("delay long time").ok();
|
||||||
5
|
let fut = cx.resources.syst.lock(|syst| timer_delay(syst, 5000000));
|
||||||
|
|
||||||
|
hprintln!("we have just created the future");
|
||||||
|
fut.await; // this calls poll on the timer future
|
||||||
|
hprintln!("foo task resumed").ok();
|
||||||
|
|
||||||
|
hprintln!("delay short time").ok();
|
||||||
|
cx.resources
|
||||||
|
.syst
|
||||||
|
.lock(|syst| timer_delay(syst, 1000000))
|
||||||
|
.await;
|
||||||
|
hprintln!("foo task resumed").ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(resources = [syst, waker])]
|
// #[task(resources = [syst])]
|
||||||
fn timer(cx: timer::Context<'static>, ticks: u32) {
|
// fn timer(cx: timer::Context<'static>) {
|
||||||
// BEGIN BOILERPLATE
|
// // BEGIN BOILERPLATE
|
||||||
type F = impl Future + 'static;
|
// type F = impl Future + 'static;
|
||||||
fn create(cx: timer::Context<'static>, ticks: u32) -> F {
|
// fn create(cx: timer::Context<'static>) -> F {
|
||||||
task(cx, ticks)
|
// task(cx)
|
||||||
}
|
// }
|
||||||
|
|
||||||
static mut TASK: Task<F> = Task::new();
|
// static mut TASK: Task<F> = Task::new();
|
||||||
|
|
||||||
hprintln!("timer trampoline").ok();
|
// hprintln!("timer trampoline").ok();
|
||||||
unsafe {
|
// unsafe {
|
||||||
match TASK {
|
// match TASK {
|
||||||
Task::Idle | Task::Done(_) => {
|
// Task::Idle | Task::Done(_) => {
|
||||||
hprintln!("create task").ok();
|
// hprintln!("create task").ok();
|
||||||
TASK.spawn(|| create(mem::transmute(cx), ticks));
|
// TASK.spawn(|| create(mem::transmute(cx)));
|
||||||
}
|
// }
|
||||||
_ => {}
|
// _ => {}
|
||||||
};
|
// };
|
||||||
hprintln!("timer poll").ok();
|
// hprintln!("timer poll").ok();
|
||||||
|
|
||||||
TASK.poll(|| {});
|
// TASK.poll(|| {});
|
||||||
|
|
||||||
match TASK {
|
// match TASK {
|
||||||
Task::Done(_) => {
|
// Task::Done(_) => {
|
||||||
hprintln!("timer done").ok();
|
// hprintln!("timer done").ok();
|
||||||
}
|
// }
|
||||||
_ => {
|
// _ => {
|
||||||
hprintln!("running").ok();
|
// hprintln!("running").ok();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// END BOILERPLATE
|
// // END BOILERPLATE
|
||||||
|
|
||||||
// for now assume this async task is done directly
|
// // for now assume this async task is done directly
|
||||||
async fn task(mut cx: timer::Context<'static>, ticks: u32) {
|
// async fn task(mut cx: timer::Context<'static>) {
|
||||||
hprintln!("SysTick {:?}", ticks).ok();
|
// hprintln!("SysTick").ok();
|
||||||
// let q = Q {};
|
|
||||||
//q.await;
|
|
||||||
|
|
||||||
// cx.resources.waker.lock(|w| *w = Some())
|
// Timer::delay(100000).await;
|
||||||
}
|
|
||||||
|
|
||||||
// static mut Q: Q<F1> =Q
|
// // cx.resources.waker.lock(|w| *w = Some())
|
||||||
// struct Q {
|
// }
|
||||||
// Init
|
// }
|
||||||
// }
|
|
||||||
|
|
||||||
// impl Future for Q {
|
|
||||||
// type Output = ();
|
|
||||||
|
|
||||||
// fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
// // you get the waker in cx.waker()
|
|
||||||
|
|
||||||
// Poll::Pending
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
// This the actual RTIC task, binds to systic.
|
// This the actual RTIC task, binds to systic.
|
||||||
#[task(binds = SysTick, resources = [waker], priority = 2)]
|
#[task(binds = SysTick, resources = [], priority = 2)]
|
||||||
fn systic(mut cx: systic::Context) {
|
fn systic(mut cx: systic::Context) {
|
||||||
hprintln!("waker {}", cx.resources.waker.lock(|w| w.is_some())).ok();
|
hprintln!("systic interrupt").ok();
|
||||||
|
crate::app::foo::spawn(); // this should be from a Queue later
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,40 +204,41 @@ impl<F: Future + 'static> Task<F> {
|
||||||
Task::Done(_) => {}
|
Task::Done(_) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn get_waker(&mut self, wake: fn()) -> Waker {
|
|
||||||
match self {
|
|
||||||
Task::Running(future) => {
|
|
||||||
let future = Pin::new_unchecked(future);
|
|
||||||
let waker_data: *const () = mem::transmute(wake);
|
|
||||||
let waker = Waker::from_raw(RawWaker::new(waker_data, &WAKER_VTABLE));
|
|
||||||
waker
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//=============
|
//=============
|
||||||
// Yield
|
// Timer
|
||||||
|
// Later we want a proper queue
|
||||||
|
|
||||||
struct Yield {
|
use heapless;
|
||||||
done: bool,
|
pub struct Timer {
|
||||||
|
pub done: bool,
|
||||||
|
// pub waker_task: Option<fn() -> Result<(), ()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Future for Yield {
|
impl Future for Timer {
|
||||||
type Output = u32;
|
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> {
|
||||||
if self.done {
|
if self.done {
|
||||||
Poll::Ready(73)
|
Poll::Ready(())
|
||||||
} else {
|
} else {
|
||||||
|
hprintln!("timer polled");
|
||||||
cx.waker().wake_by_ref();
|
cx.waker().wake_by_ref();
|
||||||
|
hprintln!("after wake_by_ref");
|
||||||
self.done = true;
|
self.done = true;
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn please_yield() -> Yield {
|
fn timer_delay(syst: &mut cortex_m::peripheral::SYST, t: u32) -> Timer {
|
||||||
Yield { done: false }
|
hprintln!("timer_delay {}", t);
|
||||||
|
|
||||||
|
syst.set_reload(t);
|
||||||
|
syst.enable_counter();
|
||||||
|
syst.enable_interrupt();
|
||||||
|
Timer {
|
||||||
|
done: false,
|
||||||
|
// waker_task: Some(app::foo::spawn), // we should add waker field to async task context i RTIC
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue