Skip to content

Commit 022330b

Browse files
committed
Add example cfg-ing a Monotonic, showing limitations imposed by rtic-syntax
1 parent be74469 commit 022330b

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

examples/cfg-monotonic.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//! examples/cfg-monotonic.rs
2+
3+
#![deny(unsafe_code)]
4+
#![deny(warnings)]
5+
#![deny(missing_docs)]
6+
#![no_main]
7+
#![no_std]
8+
9+
use panic_semihosting as _;
10+
11+
#[rtic::app(device = lm3s6965, dispatchers = [SSI0, QEI0])]
12+
mod app {
13+
use cortex_m_semihosting::{debug, hprintln};
14+
use systick_monotonic::*; // Implements the `Monotonic` trait
15+
16+
// A monotonic timer to enable scheduling in RTIC
17+
#[cfg(feature = "killmono")]
18+
#[monotonic(binds = SysTick, default = true)]
19+
type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
20+
21+
// Not allowed by current rtic-syntax:
22+
// error: `#[monotonic(...)]` on a specific type must appear at most once
23+
// --> examples/cfg-monotonic.rs:23:10
24+
// |
25+
// 23 | type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
26+
// | ^^^^^^
27+
// #[monotonic(binds = SysTick, default = true)]
28+
// type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
29+
30+
// Not allowed by current rtic-syntax:
31+
// error: this interrupt is already bound
32+
// --> examples/cfg-monotonic.rs:31:25
33+
// |
34+
// 31 | #[monotonic(binds = SysTick, default = true)]
35+
// | ^^^^^^^
36+
// #[monotonic(binds = SysTick, default = true)]
37+
// type MyMono2 = DwtSystick<100>; // 100 Hz / 10 ms granularity
38+
39+
// Resources shared between tasks
40+
#[shared]
41+
struct Shared {
42+
s1: u32,
43+
s2: i32,
44+
}
45+
46+
// Local resources to specific tasks (cannot be shared)
47+
#[local]
48+
struct Local {
49+
l1: u8,
50+
l2: i8,
51+
}
52+
53+
#[init]
54+
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
55+
let _systick = cx.core.SYST;
56+
57+
// Initialize the monotonic (SysTick rate in QEMU is 12 MHz)
58+
#[cfg(feature = "killmono")]
59+
let mono = Systick::new(systick, 12_000_000);
60+
61+
// Spawn the task `foo` directly after `init` finishes
62+
foo::spawn().unwrap();
63+
64+
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
65+
66+
(
67+
// Initialization of shared resources
68+
Shared { s1: 0, s2: 1 },
69+
// Initialization of task local resources
70+
Local { l1: 2, l2: 3 },
71+
// Move the monotonic timer to the RTIC run-time, this enables
72+
// scheduling
73+
#[cfg(feature = "killmono")]
74+
init::Monotonics(mono),
75+
init::Monotonics(),
76+
)
77+
}
78+
79+
// Background task, runs whenever no other tasks are running
80+
#[idle]
81+
fn idle(_: idle::Context) -> ! {
82+
loop {
83+
continue;
84+
}
85+
}
86+
87+
// Software task, not bound to a hardware interrupt.
88+
// This task takes the task local resource `l1`
89+
// The resources `s1` and `s2` are shared between all other tasks.
90+
#[task(shared = [s1, s2], local = [l1])]
91+
fn foo(_: foo::Context) {
92+
// This task is only spawned once in `init`, hence this task will run
93+
// only once
94+
95+
hprintln!("foo");
96+
}
97+
98+
// Software task, also not bound to a hardware interrupt
99+
// This task takes the task local resource `l2`
100+
// The resources `s1` and `s2` are shared between all other tasks.
101+
#[task(shared = [s1, s2], local = [l2])]
102+
fn bar(_: bar::Context) {
103+
hprintln!("bar");
104+
105+
// Run `bar` once per second
106+
// bar::spawn_after(1.secs()).unwrap();
107+
}
108+
109+
// Hardware task, bound to a hardware interrupt
110+
// The resources `s1` and `s2` are shared between all other tasks.
111+
#[task(binds = UART0, priority = 3, shared = [s1, s2])]
112+
fn uart0_interrupt(_: uart0_interrupt::Context) {
113+
// This task is bound to the interrupt `UART0` and will run
114+
// whenever the interrupt fires
115+
116+
// Note that RTIC does NOT clear the interrupt flag, this is up to the
117+
// user
118+
119+
hprintln!("UART0 interrupt!");
120+
}
121+
}

0 commit comments

Comments
 (0)