rtic/examples/cancel-reschedule.rs

74 lines
1.9 KiB
Rust
Raw Normal View History

2021-09-22 13:22:45 +02:00
//! examples/cancel-reschedule.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [SSI0])]
mod app {
use cortex_m_semihosting::{debug, hprintln};
2021-10-31 10:09:40 +01:00
use systick_monotonic::*;
2021-09-22 13:22:45 +02:00
#[monotonic(binds = SysTick, default = true)]
type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
#[shared]
struct Shared {}
#[local]
struct Local {}
#[init]
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
let systick = cx.core.SYST;
// Initialize the monotonic (SysTick rate in QEMU is 12 MHz)
2021-09-22 13:22:45 +02:00
let mono = Systick::new(systick, 12_000_000);
hprintln!("init");
2021-09-22 13:22:45 +02:00
// Schedule `foo` to run 1 second in the future
2021-10-31 10:09:40 +01:00
foo::spawn_after(1.secs()).unwrap();
2021-09-22 13:22:45 +02:00
(
Shared {},
Local {},
init::Monotonics(mono), // Give the monotonic to RTIC
)
}
#[task]
fn foo(_: foo::Context) {
hprintln!("foo");
2021-09-22 13:22:45 +02:00
// Schedule `bar` to run 2 seconds in the future (1 second after foo runs)
2021-10-31 10:09:40 +01:00
let spawn_handle = baz::spawn_after(2.secs()).unwrap();
bar::spawn_after(1.secs(), spawn_handle, false).unwrap(); // Change to true
2021-09-22 13:22:45 +02:00
}
#[task]
fn bar(_: bar::Context, baz_handle: baz::SpawnHandle, do_reschedule: bool) {
hprintln!("bar");
2021-09-22 13:22:45 +02:00
if do_reschedule {
// Reschedule baz 2 seconds from now, instead of the original 1 second
// from now.
2021-10-31 10:09:40 +01:00
baz_handle.reschedule_after(2.secs()).unwrap();
2021-09-22 13:22:45 +02:00
// Or baz_handle.reschedule_at(/* time */)
} else {
// Or cancel it
baz_handle.cancel().unwrap();
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}
#[task]
fn baz(_: baz::Context) {
hprintln!("baz");
2021-09-22 13:22:45 +02:00
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}