aboutsummaryrefslogtreecommitdiff
path: root/src/examples/_3_preemption.rs
blob: 4360185afc7bea02f21bdb845aa4ff4539bafbbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Two tasks running at *different* priorities with access to the same resource
//!
//! ```
//! #![deny(unsafe_code)]
//! #![deny(warnings)]
//! #![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: {
//!         // The `SYS_TICK` task has higher priority than `TIM2`
//!         SYS_TICK: {
//!             path: sys_tick,
//!             priority: 2,
//!             resources: [COUNTER],
//!         },
//! 
//!         TIM2: {
//!             path: tim2,
//!             priority: 1,
//!             resources: [COUNTER],
//!         },
//!     },
//! }
//! 
//! fn init(_p: init::Peripherals, _r: init::Resources) {
//!     // ..
//! }
//! 
//! fn idle() -> ! {
//!     loop {
//!         rtfm::wfi();
//!     }
//! }
//! 
//! fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
//!     // ..
//! 
//!     // This task can't be preempted by `tim2` so it has direct access to the
//!     // resource data
//!     *r.COUNTER += 1;
//! 
//!     // ..
//! }
//! 
//! 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 undefined behavior.
//!     r.COUNTER.claim_mut(t, |counter, _t| {
//!         // `claim_mut` creates a critical section
//!         *counter += 1;
//!     });
//! 
//!     // ..
//! }
//! ```
// Auto-generated. Do not modify.