2018-11-03 17:02:41 +01:00
|
|
|
//! examples/lock.rs
|
|
|
|
|
|
|
|
#![deny(unsafe_code)]
|
|
|
|
#![deny(warnings)]
|
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
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 {
|
2020-10-15 18:50:17 +02:00
|
|
|
use cortex_m_semihosting::{debug, hprintln};
|
|
|
|
use lm3s6965::Interrupt;
|
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
#[shared]
|
|
|
|
struct Shared {
|
2019-07-10 22:42:44 +02:00
|
|
|
shared: u32,
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
#[local]
|
|
|
|
struct Local {}
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
#[init]
|
2021-07-07 22:50:59 +02:00
|
|
|
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
|
2020-06-11 19:18:29 +02:00
|
|
|
rtic::pend(Interrupt::GPIOA);
|
2020-10-01 19:38:49 +02:00
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
(Shared { shared: 0 }, Local {}, init::Monotonics())
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// when omitted priority is assumed to be `1`
|
2021-07-07 22:50:59 +02:00
|
|
|
#[task(binds = GPIOA, shared = [shared])]
|
2019-06-20 06:19:59 +02:00
|
|
|
fn gpioa(mut c: gpioa::Context) {
|
2018-11-04 19:57:39 +01:00
|
|
|
hprintln!("A").unwrap();
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
// the lower priority task requires a critical section to access the data
|
2021-07-07 22:50:59 +02:00
|
|
|
c.shared.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
|
|
|
|
|
2018-11-04 19:57:39 +01:00
|
|
|
hprintln!("E").unwrap();
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
debug::exit(debug::EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
#[task(binds = GPIOB, priority = 2, shared = [shared])]
|
2020-10-22 21:36:32 +02:00
|
|
|
fn gpiob(mut c: gpiob::Context) {
|
|
|
|
// the higher priority task does still need a critical section
|
2021-07-07 22:50:59 +02:00
|
|
|
let shared = c.shared.shared.lock(|shared| {
|
2020-10-22 21:36:32 +02:00
|
|
|
*shared += 1;
|
|
|
|
|
|
|
|
*shared
|
|
|
|
});
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2020-10-22 21:36:32 +02:00
|
|
|
hprintln!("D - shared = {}", 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) {
|
2018-11-04 19:57:39 +01:00
|
|
|
hprintln!("C").unwrap();
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
2020-04-22 12:58:14 +02:00
|
|
|
}
|