From c7b9507a57f2ba28c18b15dd2719a1c56f74a302 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 20 Jul 2017 22:53:44 -0500 Subject: `Resource` trait, docs, examples and rtfm-syntax related changes --- src/examples/_3_preemption.rs | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/examples/_3_preemption.rs (limited to 'src/examples/_3_preemption.rs') diff --git a/src/examples/_3_preemption.rs b/src/examples/_3_preemption.rs new file mode 100644 index 0000000..b93ec08 --- /dev/null +++ b/src/examples/_3_preemption.rs @@ -0,0 +1,74 @@ +//! Two tasks running at different priorities with access to the same resource +//! +//! ``` +//! +//! #![deny(unsafe_code)] +//! #![feature(const_fn)] +//! #![feature(proc_macro)] +//! #![no_std] +//! +//! #[macro_use(task)] +//! extern crate cortex_m_rtfm as rtfm; +//! extern crate stm32f103xx; +//! +//! use rtfm::{app, Resource, Threshold}; +//! +//! app! { +//! device: stm32f103xx, +//! +//! resources: { +//! static COUNTER: u64 = 0; +//! }, +//! +//! tasks: { +//! // the task `SYS_TICK` has higher priority than `TIM2` +//! SYS_TICK: { +//! priority: 2, +//! resources: [COUNTER], +//! }, +//! +//! TIM2: { +//! enabled: true, +//! priority: 1, +//! resources: [COUNTER], +//! }, +//! }, +//! } +//! +//! fn init(_p: init::Peripherals, _r: init::Resources) { +//! // .. +//! } +//! +//! fn idle() -> ! { +//! loop { +//! rtfm::wfi(); +//! } +//! } +//! +//! task!(SYS_TICK, sys_tick); +//! +//! fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) { +//! // .. +//! +//! // this task can't be preempted by `tim2` so it has direct access to the +//! // resource data +//! **r.COUNTER += 1; +//! +//! // .. +//! } +//! +//! task!(TIM2, tim2); +//! +//! fn tim2(t: &mut Threshold, mut r: TIM2::Resources) { +//! // .. +//! +//! // as this task runs at lower priority it needs a critical section to +//! // prevent `sys_tick` from preempting it while it modifies this resource +//! // data. The critical section is required to prevent data races which can +//! // lead to data corruption or data loss +//! r.COUNTER.claim_mut(t, |counter, _t| { **counter += 1; }); +//! +//! // .. +//! } +//! ``` +// Auto-generated. Do not modify. -- cgit v1.2.3