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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#![feature(prelude_import)]
//! examples/optlock.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
#[prelude_import]
use core::prelude::v1::*;
#[macro_use]
extern crate core;
#[macro_use]
extern crate compiler_builtins;
use cortex_m_semihosting::debug;
use lm3s6965::Interrupt;
use panic_semihosting as _;
use rtfm::Exclusive;
#[allow(non_snake_case)]
fn init(_: init::Context) {
rtfm::pend(Interrupt::GPIOA);
}
#[allow(non_snake_case)]
fn gpioa(
// when omitted priority is assumed to be `1`
mut c: gpioa::Context,
) {
use rtfm::Mutex as _;
// the lower priority task requires a critical section to access the data
c.resources.shared.lock(
// data can only be modified within this critical section (closure)
// GPIOB will *not* run right now due to the critical section
// hprintln!("B - shared = {}", *shared).unwrap();
// GPIOC does not contend for `shared` so it's allowed to run now
|shared| {
*shared += 1;
rtfm::pend(Interrupt::GPIOB);
rtfm::pend(Interrupt::GPIOC);
},
);
// critical section is over: GPIOB can now start
debug::exit(debug::EXIT_SUCCESS);
}
#[allow(non_snake_case)]
fn gpiob(mut c: gpiob::Context) {
use rtfm::Mutex as _;
// higher priority task, with critical section
c.resources.shared.lock(|shared| {
*shared += 1;
});
}
#[allow(non_snake_case)]
fn gpioc(c: gpioc::Context) {
use rtfm::Mutex as _;
// highest priority task with critical section
// we wrap resource.shared into an Exclusive
let mut exclusive = Exclusive(c.resources.shared);
// we can access through both lock ...
exclusive.lock(|shared| {
*shared += 1;
});
// and deref, i.e., non-orthogonal design
*exclusive += 1;
}
#[allow(non_snake_case)]
#[doc = "Initialization function"]
pub mod init {
#[doc = r" Execution context"]
pub struct Context {
#[doc = r" Core (Cortex-M) peripherals"]
pub core: rtfm::export::Peripherals,
}
impl Context {
#[inline(always)]
pub unsafe fn new(core: rtfm::export::Peripherals) -> Self {
Context { core }
}
}
}
mod resources {
use rtfm::export::Priority;
#[allow(non_camel_case_types)]
pub struct shared<'a> {
priority: &'a Priority,
}
impl<'a> shared<'a> {
#[inline(always)]
pub unsafe fn new(priority: &'a Priority) -> Self {
shared { priority }
}
#[inline(always)]
pub unsafe fn priority(&self) -> &Priority {
self.priority
}
}
}
#[allow(non_snake_case)]
#[doc = "Resources `gpioa` has access to"]
pub struct gpioaResources<'a> {
pub shared: resources::shared<'a>,
}
#[allow(non_snake_case)]
#[doc = "Hardware task"]
pub mod gpioa {
#[doc(inline)]
pub use super::gpioaResources as Resources;
#[doc = r" Execution context"]
pub struct Context<'a> {
#[doc = r" Resources this task has access to"]
pub resources: Resources<'a>,
}
impl<'a> Context<'a> {
#[inline(always)]
pub unsafe fn new(priority: &'a rtfm::export::Priority) -> Self {
Context {
resources: Resources::new(priority),
}
}
}
}
#[allow(non_snake_case)]
#[doc = "Resources `gpiob` has access to"]
pub struct gpiobResources<'a> {
pub shared: resources::shared<'a>,
}
#[allow(non_snake_case)]
#[doc = "Hardware task"]
pub mod gpiob {
#[doc(inline)]
pub use super::gpiobResources as Resources;
#[doc = r" Execution context"]
pub struct Context<'a> {
#[doc = r" Resources this task has access to"]
pub resources: Resources<'a>,
}
impl<'a> Context<'a> {
#[inline(always)]
pub unsafe fn new(priority: &'a rtfm::export::Priority) -> Self {
Context {
resources: Resources::new(priority),
}
}
}
}
#[allow(non_snake_case)]
#[doc = "Resources `gpioc` has access to"]
pub struct gpiocResources<'a> {
pub shared: &'a mut u32,
}
#[allow(non_snake_case)]
#[doc = "Hardware task"]
pub mod gpioc {
#[doc(inline)]
pub use super::gpiocResources as Resources;
#[doc = r" Execution context"]
pub struct Context<'a> {
#[doc = r" Resources this task has access to"]
pub resources: Resources<'a>,
}
impl<'a> Context<'a> {
#[inline(always)]
pub unsafe fn new(priority: &'a rtfm::export::Priority) -> Self {
Context {
resources: Resources::new(priority),
}
}
}
}
#[doc = r" Implementation details"]
const APP: () = {
#[doc = r" Always include the device crate which contains the vector table"]
use lm3s6965 as _;
#[allow(non_upper_case_globals)]
static mut shared: u32 = 0;
impl<'a> rtfm::Mutex for resources::shared<'a> {
type T = u32;
#[inline(always)]
fn lock<R>(&mut self, f: impl FnOnce(&mut u32) -> R) -> R {
#[doc = r" Priority ceiling"]
const CEILING: u8 = 3u8;
unsafe {
rtfm::export::lock(
&mut shared,
self.priority(),
CEILING,
lm3s6965::NVIC_PRIO_BITS,
f,
)
}
}
}
#[allow(non_snake_case)]
#[no_mangle]
unsafe fn GPIOA() {
const PRIORITY: u8 = 1u8;
crate::gpioa(gpioa::Context::new(&rtfm::export::Priority::new(PRIORITY)));
}
impl<'a> gpioaResources<'a> {
#[inline(always)]
unsafe fn new(priority: &'a rtfm::export::Priority) -> Self {
gpioaResources {
shared: resources::shared::new(priority),
}
}
}
#[allow(non_snake_case)]
#[no_mangle]
unsafe fn GPIOB() {
const PRIORITY: u8 = 2u8;
crate::gpiob(gpiob::Context::new(&rtfm::export::Priority::new(PRIORITY)));
}
impl<'a> gpiobResources<'a> {
#[inline(always)]
unsafe fn new(priority: &'a rtfm::export::Priority) -> Self {
gpiobResources {
shared: resources::shared::new(priority),
}
}
}
#[allow(non_snake_case)]
#[no_mangle]
unsafe fn GPIOC() {
const PRIORITY: u8 = 3u8;
crate::gpioc(gpioc::Context::new(&rtfm::export::Priority::new(PRIORITY)));
}
impl<'a> gpiocResources<'a> {
#[inline(always)]
unsafe fn new(priority: &'a rtfm::export::Priority) -> Self {
gpiocResources {
shared: &mut shared,
}
}
}
#[no_mangle]
unsafe extern "C" fn main() -> ! {
rtfm::export::interrupt::disable();
let mut core: rtfm::export::Peripherals = core::mem::transmute(());
let _ = [(); ((1 << lm3s6965::NVIC_PRIO_BITS) - 1u8 as usize)];
core.NVIC.set_priority(
lm3s6965::Interrupt::GPIOA,
rtfm::export::logical2hw(1u8, lm3s6965::NVIC_PRIO_BITS),
);
rtfm::export::NVIC::unmask(lm3s6965::Interrupt::GPIOA);
let _ = [(); ((1 << lm3s6965::NVIC_PRIO_BITS) - 2u8 as usize)];
core.NVIC.set_priority(
lm3s6965::Interrupt::GPIOB,
rtfm::export::logical2hw(2u8, lm3s6965::NVIC_PRIO_BITS),
);
rtfm::export::NVIC::unmask(lm3s6965::Interrupt::GPIOB);
let _ = [(); ((1 << lm3s6965::NVIC_PRIO_BITS) - 3u8 as usize)];
core.NVIC.set_priority(
lm3s6965::Interrupt::GPIOC,
rtfm::export::logical2hw(3u8, lm3s6965::NVIC_PRIO_BITS),
);
rtfm::export::NVIC::unmask(lm3s6965::Interrupt::GPIOC);
core.SCB.scr.modify(|r| r | 1 << 1);
let late = init(init::Context::new(core.into()));
rtfm::export::interrupt::enable();
loop {
rtfm::export::wfi()
}
}
};
|