2017-07-28 05:40:47 +02:00
|
|
|
//! Two tasks running at *different* priorities with access to the same resource
|
2017-07-21 05:53:44 +02:00
|
|
|
#![deny(unsafe_code)]
|
|
|
|
#![feature(const_fn)]
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
extern crate cortex_m_rtfm as rtfm;
|
|
|
|
extern crate stm32f103xx;
|
|
|
|
|
|
|
|
use rtfm::{app, Resource, Threshold};
|
|
|
|
|
|
|
|
app! {
|
|
|
|
device: stm32f103xx,
|
|
|
|
|
|
|
|
resources: {
|
|
|
|
static COUNTER: u64 = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
tasks: {
|
2017-07-28 05:40:47 +02:00
|
|
|
// The `SYS_TICK` task has higher priority than `TIM2`
|
2017-07-21 05:53:44 +02:00
|
|
|
SYS_TICK: {
|
2017-07-27 18:40:15 +02:00
|
|
|
path: sys_tick,
|
2017-07-21 05:53:44 +02:00
|
|
|
priority: 2,
|
|
|
|
resources: [COUNTER],
|
|
|
|
},
|
|
|
|
|
|
|
|
TIM2: {
|
2017-07-27 18:40:15 +02:00
|
|
|
path: tim2,
|
2017-07-21 05:53:44 +02:00
|
|
|
priority: 1,
|
|
|
|
resources: [COUNTER],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init(_p: init::Peripherals, _r: init::Resources) {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
|
|
|
|
fn idle() -> ! {
|
|
|
|
loop {
|
|
|
|
rtfm::wfi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
|
|
|
|
// ..
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// This task can't be preempted by `tim2` so it has direct access to the
|
2017-07-21 05:53:44 +02:00
|
|
|
// resource data
|
|
|
|
**r.COUNTER += 1;
|
|
|
|
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tim2(t: &mut Threshold, mut r: TIM2::Resources) {
|
|
|
|
// ..
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// As this task runs at lower priority it needs a critical section to
|
2017-07-21 05:53:44 +02:00
|
|
|
// prevent `sys_tick` from preempting it while it modifies this resource
|
|
|
|
// data. The critical section is required to prevent data races which can
|
2017-07-28 05:40:47 +02:00
|
|
|
// lead to undefined behavior
|
2017-07-21 05:53:44 +02:00
|
|
|
r.COUNTER.claim_mut(t, |counter, _t| { **counter += 1; });
|
|
|
|
|
|
|
|
// ..
|
|
|
|
}
|