aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2019-07-10 22:42:44 +0200
committerGravatar Jorge Aparicio <jorge@japaric.io> 2019-07-10 22:42:44 +0200
commit9195038c87703fc94b6e99f6de593886d51c2b19 (patch)
tree56855952357fe5bb689504ed8a6348dc3c1f3718 /examples
parent14d63f496118f4243f28ddf3218523aa36a80322 (diff)
downloadrtic-9195038c87703fc94b6e99f6de593886d51c2b19.tar.gz
rtic-9195038c87703fc94b6e99f6de593886d51c2b19.tar.zst
rtic-9195038c87703fc94b6e99f6de593886d51c2b19.zip
implement RFC #212
Diffstat (limited to 'examples')
-rw-r--r--examples/cfg.rs15
-rw-r--r--examples/generics.rs19
-rw-r--r--examples/late.rs16
-rw-r--r--examples/lock.rs19
-rw-r--r--examples/not-send.rs13
-rw-r--r--examples/not-sync.rs13
-rw-r--r--examples/resource.rs27
-rw-r--r--examples/shared-with-init.rs13
-rw-r--r--examples/static.rs14
-rw-r--r--examples/t-cfg.rs11
-rw-r--r--examples/t-late-not-send.rs16
-rw-r--r--examples/t-resource.rs70
-rw-r--r--examples/types.rs13
13 files changed, 148 insertions, 111 deletions
diff --git a/examples/cfg.rs b/examples/cfg.rs
index b1f65cfd..fb812cb0 100644
--- a/examples/cfg.rs
+++ b/examples/cfg.rs
@@ -11,25 +11,28 @@ use panic_semihosting as _;
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- #[cfg(debug_assertions)] // <- `true` when using the `dev` profile
- static mut COUNT: u32 = 0;
+ struct Resources {
+ #[cfg(debug_assertions)] // <- `true` when using the `dev` profile
+ #[init(0)]
+ count: u32,
+ }
#[init]
fn init(_: init::Context) {
// ..
}
- #[task(priority = 3, resources = [COUNT], spawn = [log])]
+ #[task(priority = 3, resources = [count], spawn = [log])]
fn foo(_c: foo::Context) {
#[cfg(debug_assertions)]
{
- *_c.resources.COUNT += 1;
+ *_c.resources.count += 1;
- _c.spawn.log(*_c.resources.COUNT).ok();
+ _c.spawn.log(*_c.resources.count).ok();
}
// this wouldn't compile in `release` mode
- // *resources.COUNT += 1;
+ // *resources.count += 1;
// ..
}
diff --git a/examples/generics.rs b/examples/generics.rs
index 562470d4..f0632d9a 100644
--- a/examples/generics.rs
+++ b/examples/generics.rs
@@ -12,7 +12,10 @@ use rtfm::{Exclusive, Mutex};
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- static mut SHARED: u32 = 0;
+ struct Resources {
+ #[init(0)]
+ shared: u32,
+ }
#[init]
fn init(_: init::Context) {
@@ -20,29 +23,29 @@ const APP: () = {
rtfm::pend(Interrupt::UART1);
}
- #[task(binds = UART0, resources = [SHARED])]
+ #[task(binds = UART0, resources = [shared])]
fn uart0(c: uart0::Context) {
static mut STATE: u32 = 0;
hprintln!("UART0(STATE = {})", *STATE).unwrap();
- advance(STATE, c.resources.SHARED);
+ advance(STATE, c.resources.shared);
rtfm::pend(Interrupt::UART1);
debug::exit(debug::EXIT_SUCCESS);
}
- #[task(binds = UART1, priority = 2, resources = [SHARED])]
+ #[task(binds = UART1, priority = 2, resources = [shared])]
fn uart1(c: uart1::Context) {
static mut STATE: u32 = 0;
hprintln!("UART1(STATE = {})", *STATE).unwrap();
- // just to show that `SHARED` can be accessed directly
- *c.resources.SHARED += 0;
+ // just to show that `shared` can be accessed directly
+ *c.resources.shared += 0;
- advance(STATE, Exclusive(c.resources.SHARED));
+ advance(STATE, Exclusive(c.resources.shared));
}
};
@@ -55,5 +58,5 @@ fn advance(state: &mut u32, mut shared: impl Mutex<T = u32>) {
(old, *shared)
});
- hprintln!("SHARED: {} -> {}", old, new).unwrap();
+ hprintln!("shared: {} -> {}", old, new).unwrap();
}
diff --git a/examples/late.rs b/examples/late.rs
index 19807ff7..536d71aa 100644
--- a/examples/late.rs
+++ b/examples/late.rs
@@ -16,9 +16,9 @@ use panic_semihosting as _;
#[rtfm::app(device = lm3s6965)]
const APP: () = {
// Late resources
- extern "C" {
- static mut P: Producer<'static, u32, U4>;
- static mut C: Consumer<'static, u32, U4>;
+ struct Resources {
+ p: Producer<'static, u32, U4>,
+ c: Consumer<'static, u32, U4>,
}
#[init]
@@ -31,13 +31,13 @@ const APP: () = {
let (p, c) = Q.as_mut().unwrap().split();
// Initialization of late resources
- init::LateResources { P: p, C: c }
+ init::LateResources { p, c }
}
- #[idle(resources = [C])]
+ #[idle(resources = [c])]
fn idle(c: idle::Context) -> ! {
loop {
- if let Some(byte) = c.resources.C.dequeue() {
+ if let Some(byte) = c.resources.c.dequeue() {
hprintln!("received message: {}", byte).unwrap();
debug::exit(debug::EXIT_SUCCESS);
@@ -47,8 +47,8 @@ const APP: () = {
}
}
- #[task(binds = UART0, resources = [P])]
+ #[task(binds = UART0, resources = [p])]
fn uart0(c: uart0::Context) {
- c.resources.P.enqueue(42).unwrap();
+ c.resources.p.enqueue(42).unwrap();
}
};
diff --git a/examples/lock.rs b/examples/lock.rs
index 17b6a58b..f33a60a4 100644
--- a/examples/lock.rs
+++ b/examples/lock.rs
@@ -11,7 +11,10 @@ use panic_semihosting as _;
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- static mut SHARED: u32 = 0;
+ struct Resources {
+ #[init(0)]
+ shared: u32,
+ }
#[init]
fn init(_: init::Context) {
@@ -19,21 +22,21 @@ const APP: () = {
}
// when omitted priority is assumed to be `1`
- #[task(binds = GPIOA, resources = [SHARED])]
+ #[task(binds = GPIOA, resources = [shared])]
fn gpioa(mut c: gpioa::Context) {
hprintln!("A").unwrap();
// the lower priority task requires a critical section to access the data
- c.resources.SHARED.lock(|shared| {
+ c.resources.shared.lock(|shared| {
// data can only be modified within this critical section (closure)
*shared += 1;
// GPIOB will *not* run right now due to the critical section
rtfm::pend(Interrupt::GPIOB);
- hprintln!("B - SHARED = {}", *shared).unwrap();
+ hprintln!("B - shared = {}", *shared).unwrap();
- // GPIOC does not contend for `SHARED` so it's allowed to run now
+ // GPIOC does not contend for `shared` so it's allowed to run now
rtfm::pend(Interrupt::GPIOC);
});
@@ -44,12 +47,12 @@ const APP: () = {
debug::exit(debug::EXIT_SUCCESS);
}
- #[task(binds = GPIOB, priority = 2, resources = [SHARED])]
+ #[task(binds = GPIOB, priority = 2, resources = [shared])]
fn gpiob(c: gpiob::Context) {
// the higher priority task does *not* need a critical section
- *c.resources.SHARED += 1;
+ *c.resources.shared += 1;
- hprintln!("D - SHARED = {}", *c.resources.SHARED).unwrap();
+ hprintln!("D - shared = {}", *c.resources.shared).unwrap();
}
#[task(binds = GPIOC, priority = 3)]
diff --git a/examples/not-send.rs b/examples/not-send.rs
index f240e511..d27cc82f 100644
--- a/examples/not-send.rs
+++ b/examples/not-send.rs
@@ -17,7 +17,10 @@ pub struct NotSend {
#[app(device = lm3s6965)]
const APP: () = {
- static mut SHARED: Option<NotSend> = None;
+ struct Resources {
+ #[init(None)]
+ shared: Option<NotSend>,
+ }
#[init(spawn = [baz, quux])]
fn init(c: init::Context) {
@@ -36,16 +39,16 @@ const APP: () = {
// scenario 1
}
- #[task(priority = 2, resources = [SHARED])]
+ #[task(priority = 2, resources = [shared])]
fn baz(c: baz::Context) {
// scenario 2: resource shared between tasks that run at the same priority
- *c.resources.SHARED = Some(NotSend { _0: PhantomData });
+ *c.resources.shared = Some(NotSend { _0: PhantomData });
}
- #[task(priority = 2, resources = [SHARED])]
+ #[task(priority = 2, resources = [shared])]
fn quux(c: quux::Context) {
// scenario 2
- let _not_send = c.resources.SHARED.take().unwrap();
+ let _not_send = c.resources.shared.take().unwrap();
debug::exit(debug::EXIT_SUCCESS);
}
diff --git a/examples/not-sync.rs b/examples/not-sync.rs
index 6b499111..f0f6075e 100644
--- a/examples/not-sync.rs
+++ b/examples/not-sync.rs
@@ -16,21 +16,24 @@ pub struct NotSync {
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- static SHARED: NotSync = NotSync { _0: PhantomData };
+ struct Resources {
+ #[init(NotSync { _0: PhantomData })]
+ shared: NotSync,
+ }
#[init]
fn init(_: init::Context) {
debug::exit(debug::EXIT_SUCCESS);
}
- #[task(resources = [SHARED])]
+ #[task(resources = [shared])]
fn foo(c: foo::Context) {
- let _: &NotSync = c.resources.SHARED;
+ let _: &NotSync = c.resources.shared;
}
- #[task(resources = [SHARED])]
+ #[task(resources = [shared])]
fn bar(c: bar::Context) {
- let _: &NotSync = c.resources.SHARED;
+ let _: &NotSync = c.resources.shared;
}
extern "C" {
diff --git a/examples/resource.rs b/examples/resource.rs
index 661f8c35..2506a2cb 100644
--- a/examples/resource.rs
+++ b/examples/resource.rs
@@ -11,8 +11,11 @@ use panic_semihosting as _;
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- // A resource
- static mut SHARED: u32 = 0;
+ struct Resources {
+ // A resource
+ #[init(0)]
+ shared: u32,
+ }
#[init]
fn init(_: init::Context) {
@@ -24,25 +27,25 @@ const APP: () = {
fn idle(_: idle::Context) -> ! {
debug::exit(debug::EXIT_SUCCESS);
- // error: `SHARED` can't be accessed from this context
- // SHARED += 1;
+ // error: `shared` can't be accessed from this context
+ // shared += 1;
loop {}
}
- // `SHARED` can be access from this context
- #[task(binds = UART0, resources = [SHARED])]
+ // `shared` can be access from this context
+ #[task(binds = UART0, resources = [shared])]
fn uart0(c: uart0::Context) {
- *c.resources.SHARED += 1;
+ *c.resources.shared += 1;
- hprintln!("UART0: SHARED = {}", c.resources.SHARED).unwrap();
+ hprintln!("UART0: shared = {}", c.resources.shared).unwrap();
}
- // `SHARED` can be access from this context
- #[task(binds = UART1, resources = [SHARED])]
+ // `shared` can be access from this context
+ #[task(binds = UART1, resources = [shared])]
fn uart1(c: uart1::Context) {
- *c.resources.SHARED += 1;
+ *c.resources.shared += 1;
- hprintln!("UART1: SHARED = {}", c.resources.SHARED).unwrap();
+ hprintln!("UART1: shared = {}", c.resources.shared).unwrap();
}
};
diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs
index ed73c8ba..14fa54b7 100644
--- a/examples/shared-with-init.rs
+++ b/examples/shared-with-init.rs
@@ -14,20 +14,23 @@ pub struct MustBeSend;
#[app(device = lm3s6965)]
const APP: () = {
- static mut SHARED: Option<MustBeSend> = None;
+ struct Resources {
+ #[init(None)]
+ shared: Option<MustBeSend>,
+ }
- #[init(resources = [SHARED])]
+ #[init(resources = [shared])]
fn init(c: init::Context) {
// this `message` will be sent to task `UART0`
let message = MustBeSend;
- *c.resources.SHARED = Some(message);
+ *c.resources.shared = Some(message);
rtfm::pend(Interrupt::UART0);
}
- #[task(binds = UART0, resources = [SHARED])]
+ #[task(binds = UART0, resources = [shared])]
fn uart0(c: uart0::Context) {
- if let Some(message) = c.resources.SHARED.take() {
+ if let Some(message) = c.resources.shared.take() {
// `message` has been received
drop(message);
diff --git a/examples/static.rs b/examples/static.rs
index 5eb7b197..4af7ee67 100644
--- a/examples/static.rs
+++ b/examples/static.rs
@@ -11,8 +11,8 @@ use panic_semihosting as _;
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- extern "C" {
- static KEY: u32;
+ struct Resources {
+ key: u32,
}
#[init]
@@ -20,18 +20,18 @@ const APP: () = {
rtfm::pend(Interrupt::UART0);
rtfm::pend(Interrupt::UART1);
- init::LateResources { KEY: 0xdeadbeef }
+ init::LateResources { key: 0xdeadbeef }
}
- #[task(binds = UART0, resources = [KEY])]
+ #[task(binds = UART0, resources = [&key])]
fn uart0(c: uart0::Context) {
- hprintln!("UART0(KEY = {:#x})", c.resources.KEY).unwrap();
+ hprintln!("UART0(key = {:#x})", c.resources.key).unwrap();
debug::exit(debug::EXIT_SUCCESS);
}
- #[task(binds = UART1, priority = 2, resources = [KEY])]
+ #[task(binds = UART1, priority = 2, resources = [&key])]
fn uart1(c: uart1::Context) {
- hprintln!("UART1(KEY = {:#x})", c.resources.KEY).unwrap();
+ hprintln!("UART1(key = {:#x})", c.resources.key).unwrap();
}
};
diff --git a/examples/t-cfg.rs b/examples/t-cfg.rs
index 158eef55..e61ec795 100644
--- a/examples/t-cfg.rs
+++ b/examples/t-cfg.rs
@@ -7,8 +7,11 @@ use panic_halt as _;
#[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)]
const APP: () = {
- #[cfg(never)]
- static mut FOO: u32 = 0;
+ struct Resources {
+ #[cfg(never)]
+ #[init(0)]
+ foo: u32,
+ }
#[init]
fn init(_: init::Context) {
@@ -24,13 +27,13 @@ const APP: () = {
loop {}
}
- #[task(resources = [FOO], schedule = [quux], spawn = [quux])]
+ #[task(resources = [foo], schedule = [quux], spawn = [quux])]
fn foo(_: foo::Context) {
#[cfg(never)]
static mut BAR: u32 = 0;
}
- #[task(priority = 3, resources = [FOO], schedule = [quux], spawn = [quux])]
+ #[task(priority = 3, resources = [foo], schedule = [quux], spawn = [quux])]
fn bar(_: bar::Context) {
#[cfg(never)]
static mut BAR: u32 = 0;
diff --git a/examples/t-late-not-send.rs b/examples/t-late-not-send.rs
index 55a053df..4fd3504e 100644
--- a/examples/t-late-not-send.rs
+++ b/examples/t-late-not-send.rs
@@ -13,23 +13,23 @@ pub struct NotSend {
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- extern "C" {
- static mut X: NotSend;
+ struct Resources {
+ x: NotSend,
+ #[init(None)]
+ y: Option<NotSend>,
}
- static mut Y: Option<NotSend> = None;
-
- #[init(resources = [Y])]
+ #[init(resources = [y])]
fn init(c: init::Context) -> init::LateResources {
// equivalent to late resource initialization
- *c.resources.Y = Some(NotSend { _0: PhantomData });
+ *c.resources.y = Some(NotSend { _0: PhantomData });
init::LateResources {
- X: NotSend { _0: PhantomData },
+ x: NotSend { _0: PhantomData },
}
}
- #[idle(resources = [X, Y])]
+ #[idle(resources = [x, y])]
fn idle(_: idle::Context) -> ! {
loop {}
}
diff --git a/examples/t-resource.rs b/examples/t-resource.rs
index adcc04b3..303340ed 100644
--- a/examples/t-resource.rs
+++ b/examples/t-resource.rs
@@ -9,69 +9,79 @@ use panic_halt as _;
#[rtfm::app(device = lm3s6965)]
const APP: () = {
- static mut O1: u32 = 0; // init
- static mut O2: u32 = 0; // idle
- static mut O3: u32 = 0; // EXTI0
- static O4: u32 = 0; // idle
- static O5: u32 = 0; // EXTI1
- static O6: u32 = 0; // init
-
- static mut S1: u32 = 0; // idle & EXTI0
- static mut S2: u32 = 0; // EXTI0 & EXTI1
- static S3: u32 = 0;
-
- #[init(resources = [O1, O4, O5, O6, S3])]
+ struct Resources {
+ #[init(0)]
+ o1: u32, // init
+ #[init(0)]
+ o2: u32, // idle
+ #[init(0)]
+ o3: u32, // EXTI0
+ #[init(0)]
+ o4: u32, // idle
+ #[init(0)]
+ o5: u32, // EXTI1
+ #[init(0)]
+ o6: u32, // init
+ #[init(0)]
+ s1: u32, // idle & uart0
+ #[init(0)]
+ s2: u32, // uart0 & uart1
+ #[init(0)]
+ s3: u32, // idle & uart0
+ }
+
+ #[init(resources = [o1, o4, o5, o6, s3])]
fn init(c: init::Context) {
// owned by `init` == `&'static mut`
- let _: &'static mut u32 = c.resources.O1;
+ let _: &'static mut u32 = c.resources.o1;
// owned by `init` == `&'static` if read-only
- let _: &'static u32 = c.resources.O6;
+ let _: &'static u32 = c.resources.o6;
// `init` has exclusive access to all resources
- let _: &mut u32 = c.resources.O4;
- let _: &mut u32 = c.resources.O5;
- let _: &mut u32 = c.resources.S3;
+ let _: &mut u32 = c.resources.o4;
+ let _: &mut u32 = c.resources.o5;
+ let _: &mut u32 = c.resources.s3;
}
- #[idle(resources = [O2, O4, S1, S3])]
+ #[idle(resources = [o2, &o4, s1, &s3])]
fn idle(mut c: idle::Context) -> ! {
// owned by `idle` == `&'static mut`
- let _: &'static mut u32 = c.resources.O2;
+ let _: &'static mut u32 = c.resources.o2;
// owned by `idle` == `&'static` if read-only
- let _: &'static u32 = c.resources.O4;
+ let _: &'static u32 = c.resources.o4;
// shared with `idle` == `Mutex`
- c.resources.S1.lock(|_| {});
+ c.resources.s1.lock(|_| {});
// `&` if read-only
- let _: &u32 = c.resources.S3;
+ let _: &u32 = c.resources.s3;
loop {}
}
- #[task(binds = UART0, resources = [O3, S1, S2, S3])]
+ #[task(binds = UART0, resources = [o3, s1, s2, &s3])]
fn uart0(c: uart0::Context) {
// owned by interrupt == `&mut`
- let _: &mut u32 = c.resources.O3;
+ let _: &mut u32 = c.resources.o3;
// no `Mutex` proxy when access from highest priority task
- let _: &mut u32 = c.resources.S1;
+ let _: &mut u32 = c.resources.s1;
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
- let _: &mut u32 = c.resources.S2;
+ let _: &mut u32 = c.resources.s2;
// `&` if read-only
- let _: &u32 = c.resources.S3;
+ let _: &u32 = c.resources.s3;
}
- #[task(binds = UART1, resources = [S2, O5])]
+ #[task(binds = UART1, resources = [s2, &o5])]
fn uart1(c: uart1::Context) {
// owned by interrupt == `&` if read-only
- let _: &u32 = c.resources.O5;
+ let _: &u32 = c.resources.o5;
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
- let _: &mut u32 = c.resources.S2;
+ let _: &mut u32 = c.resources.s2;
}
};
diff --git a/examples/types.rs b/examples/types.rs
index 3e9c7ea5..0c8097f3 100644
--- a/examples/types.rs
+++ b/examples/types.rs
@@ -11,7 +11,10 @@ use rtfm::cyccnt::Instant;
#[rtfm::app(device = lm3s6965, peripherals = true, monotonic = rtfm::cyccnt::CYCCNT)]
const APP: () = {
- static mut SHARED: u32 = 0;
+ struct Resources {
+ #[init(0)]
+ shared: u32,
+ }
#[init(schedule = [foo], spawn = [foo])]
fn init(c: init::Context) {
@@ -31,18 +34,18 @@ const APP: () = {
let _: svcall::Spawn = c.spawn;
}
- #[task(binds = UART0, resources = [SHARED], schedule = [foo], spawn = [foo])]
+ #[task(binds = UART0, resources = [shared], schedule = [foo], spawn = [foo])]
fn uart0(c: uart0::Context) {
let _: Instant = c.start;
- let _: resources::SHARED = c.resources.SHARED;
+ let _: resources::shared = c.resources.shared;
let _: uart0::Schedule = c.schedule;
let _: uart0::Spawn = c.spawn;
}
- #[task(priority = 2, resources = [SHARED], schedule = [foo], spawn = [foo])]
+ #[task(priority = 2, resources = [shared], schedule = [foo], spawn = [foo])]
fn foo(c: foo::Context) {
let _: Instant = c.scheduled;
- let _: &mut u32 = c.resources.SHARED;
+ let _: &mut u32 = c.resources.shared;
let _: foo::Resources = c.resources;
let _: foo::Schedule = c.schedule;
let _: foo::Spawn = c.spawn;