rtic/examples/lock.rs

63 lines
1.6 KiB
Rust
Raw Normal View History

2018-11-03 17:02:41 +01:00
//! examples/lock.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_semihosting::{debug, hprintln};
2018-11-03 17:02:41 +01:00
use lm3s6965::Interrupt;
use panic_semihosting as _;
2018-11-03 17:02:41 +01:00
2020-06-11 19:18:29 +02:00
#[rtic::app(device = lm3s6965)]
2020-05-19 20:00:13 +02:00
mod app {
2019-07-10 22:42:44 +02:00
struct Resources {
#[init(0)]
shared: u32,
}
2018-11-03 17:02:41 +01:00
#[init]
2019-04-21 20:10:40 +02:00
fn init(_: init::Context) {
2020-06-11 19:18:29 +02:00
rtic::pend(Interrupt::GPIOA);
2018-11-03 17:02:41 +01:00
}
// when omitted priority is assumed to be `1`
2019-07-10 22:42:44 +02:00
#[task(binds = GPIOA, resources = [shared])]
2019-06-20 06:19:59 +02:00
fn gpioa(mut c: gpioa::Context) {
hprintln!("A").unwrap();
2018-11-03 17:02:41 +01:00
// the lower priority task requires a critical section to access the data
2019-07-10 22:42:44 +02:00
c.resources.shared.lock(|shared| {
2018-11-03 17:02:41 +01:00
// data can only be modified within this critical section (closure)
*shared += 1;
// GPIOB will *not* run right now due to the critical section
2020-06-11 19:18:29 +02:00
rtic::pend(Interrupt::GPIOB);
2018-11-03 17:02:41 +01:00
2019-07-10 22:42:44 +02:00
hprintln!("B - shared = {}", *shared).unwrap();
2018-11-03 17:02:41 +01:00
2019-07-10 22:42:44 +02:00
// GPIOC does not contend for `shared` so it's allowed to run now
2020-06-11 19:18:29 +02:00
rtic::pend(Interrupt::GPIOC);
2018-11-03 17:02:41 +01:00
});
// critical section is over: GPIOB can now start
hprintln!("E").unwrap();
2018-11-03 17:02:41 +01:00
debug::exit(debug::EXIT_SUCCESS);
}
2019-07-10 22:42:44 +02:00
#[task(binds = GPIOB, priority = 2, resources = [shared])]
2019-06-20 06:19:59 +02:00
fn gpiob(c: gpiob::Context) {
2018-11-03 17:02:41 +01:00
// the higher priority task does *not* need a critical section
2019-07-10 22:42:44 +02:00
*c.resources.shared += 1;
2018-11-03 17:02:41 +01:00
2019-07-10 22:42:44 +02:00
hprintln!("D - shared = {}", *c.resources.shared).unwrap();
2018-11-03 17:02:41 +01:00
}
2019-06-20 06:19:59 +02:00
#[task(binds = GPIOC, priority = 3)]
fn gpioc(_: gpioc::Context) {
hprintln!("C").unwrap();
2018-11-03 17:02:41 +01:00
}
2020-04-22 12:58:14 +02:00
}