aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/big-struct-opt.rs48
-rw-r--r--examples/cfg-whole-task.rs12
-rw-r--r--examples/cfg.rs6
-rw-r--r--examples/destructure.rs4
-rw-r--r--examples/generics.rs8
-rw-r--r--examples/late.rs8
-rw-r--r--examples/lock.rs12
-rw-r--r--examples/resource-user-struct.rs17
-rw-r--r--examples/resource.rs17
-rw-r--r--examples/shared-with-init.rs45
-rw-r--r--examples/static.rs8
-rw-r--r--examples/t-late-not-send.rs7
-rw-r--r--examples/t-resource.rs27
-rw-r--r--examples/task-local.rs16
-rw-r--r--examples/types.rs2
15 files changed, 120 insertions, 117 deletions
diff --git a/examples/big-struct-opt.rs b/examples/big-struct-opt.rs
new file mode 100644
index 00000000..85ec5e61
--- /dev/null
+++ b/examples/big-struct-opt.rs
@@ -0,0 +1,48 @@
+//! examples/big-struct-opt.rs
+//!
+//! Example on how to initialize a large struct without needing to copy it via `LateResources`,
+//! effectively saving stack space needed for the copies.
+
+#![no_main]
+#![no_std]
+
+use panic_halt as _;
+
+/// Some big struct
+pub struct BigStruct {
+ /// Big content
+ pub data: [u8; 2048],
+}
+
+impl BigStruct {
+ fn new() -> Self {
+ BigStruct { data: [22; 2048] }
+ }
+}
+
+#[rtic::app(device = lm3s6965)]
+mod app {
+ use super::BigStruct;
+ use core::mem::MaybeUninit;
+
+ #[resources]
+ struct Resources {
+ big_struct: &'static mut BigStruct,
+ }
+
+ #[init]
+ fn init(_: init::Context) -> init::LateResources {
+ let big_struct = unsafe {
+ static mut BIG_STRUCT: MaybeUninit<BigStruct> = MaybeUninit::uninit();
+
+ // write directly into the static storage
+ BIG_STRUCT.as_mut_ptr().write(BigStruct::new());
+ &mut *BIG_STRUCT.as_mut_ptr()
+ };
+
+ init::LateResources {
+ // assign the reference so we can use the resource
+ big_struct,
+ }
+ }
+}
diff --git a/examples/cfg-whole-task.rs b/examples/cfg-whole-task.rs
index 7a7fc48b..e225da33 100644
--- a/examples/cfg-whole-task.rs
+++ b/examples/cfg-whole-task.rs
@@ -41,12 +41,12 @@ mod app {
}
#[task(capacity = 2, resources = [count])]
- fn foo(_cx: foo::Context) {
+ fn foo(mut _cx: foo::Context) {
#[cfg(debug_assertions)]
{
- *_cx.resources.count += 1;
+ _cx.resources.count.lock(|count| *count += 1);
- log::spawn(*_cx.resources.count).unwrap();
+ log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
}
// this wouldn't compile in `release` mode
@@ -59,12 +59,12 @@ mod app {
// currently still present in the Tasks enum
#[cfg(never)]
#[task(capacity = 2, resources = [count])]
- fn foo2(_cx: foo2::Context) {
+ fn foo2(mut _cx: foo2::Context) {
#[cfg(debug_assertions)]
{
- *_cx.resources.count += 10;
+ _cx.resources.count.lock(|count| *count += 10);
- log::spawn(*_cx.resources.count).unwrap();
+ log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
}
// this wouldn't compile in `release` mode
diff --git a/examples/cfg.rs b/examples/cfg.rs
index f900286b..fae13e5b 100644
--- a/examples/cfg.rs
+++ b/examples/cfg.rs
@@ -38,12 +38,12 @@ mod app {
}
#[task(capacity = 2, resources = [count])]
- fn foo(_cx: foo::Context) {
+ fn foo(mut _cx: foo::Context) {
#[cfg(debug_assertions)]
{
- *_cx.resources.count += 1;
+ _cx.resources.count.lock(|count| *count += 1);
- log::spawn(*_cx.resources.count).unwrap();
+ log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
}
// this wouldn't compile in `release` mode
diff --git a/examples/destructure.rs b/examples/destructure.rs
index 3c5eabf7..d843978b 100644
--- a/examples/destructure.rs
+++ b/examples/destructure.rs
@@ -32,7 +32,7 @@ mod app {
}
// Direct destructure
- #[task(binds = UART0, resources = [a, b, c])]
+ #[task(binds = UART0, resources = [&a, &b, &c])]
fn uart0(cx: uart0::Context) {
let a = cx.resources.a;
let b = cx.resources.b;
@@ -42,7 +42,7 @@ mod app {
}
// De-structure-ing syntax
- #[task(binds = UART1, resources = [a, b, c])]
+ #[task(binds = UART1, resources = [&a, &b, &c])]
fn uart1(cx: uart1::Context) {
let uart1::Resources { a, b, c } = cx.resources;
diff --git a/examples/generics.rs b/examples/generics.rs
index 16327fb3..f3829a06 100644
--- a/examples/generics.rs
+++ b/examples/generics.rs
@@ -13,7 +13,6 @@ use rtic::Mutex;
mod app {
use cortex_m_semihosting::{debug, hprintln};
use lm3s6965::Interrupt;
- use rtic::Exclusive;
#[resources]
struct Resources {
@@ -49,11 +48,8 @@ mod app {
hprintln!("UART1(STATE = {})", *STATE).unwrap();
- // just to show that `shared` can be accessed directly
- *c.resources.shared += 0;
-
- // second argument has type `Exclusive<u32>`
- super::advance(STATE, Exclusive(c.resources.shared));
+ // second argument has type `resources::shared`
+ super::advance(STATE, c.resources.shared);
}
}
diff --git a/examples/late.rs b/examples/late.rs
index d20a69c5..d4efaba3 100644
--- a/examples/late.rs
+++ b/examples/late.rs
@@ -35,9 +35,9 @@ mod app {
}
#[idle(resources = [c])]
- fn idle(c: idle::Context) -> ! {
+ fn idle(mut c: idle::Context) -> ! {
loop {
- if let Some(byte) = c.resources.c.dequeue() {
+ if let Some(byte) = c.resources.c.lock(|c| c.dequeue()) {
hprintln!("received message: {}", byte).unwrap();
debug::exit(debug::EXIT_SUCCESS);
@@ -48,7 +48,7 @@ mod app {
}
#[task(binds = UART0, resources = [p])]
- fn uart0(c: uart0::Context) {
- c.resources.p.enqueue(42).unwrap();
+ fn uart0(mut c: uart0::Context) {
+ c.resources.p.lock(|p| p.enqueue(42).unwrap());
}
}
diff --git a/examples/lock.rs b/examples/lock.rs
index c4930a28..2fbf7608 100644
--- a/examples/lock.rs
+++ b/examples/lock.rs
@@ -52,11 +52,15 @@ mod app {
}
#[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;
+ fn gpiob(mut c: gpiob::Context) {
+ // the higher priority task does still need a critical section
+ let shared = c.resources.shared.lock(|shared| {
+ *shared += 1;
+
+ *shared
+ });
- hprintln!("D - shared = {}", *c.resources.shared).unwrap();
+ hprintln!("D - shared = {}", shared).unwrap();
}
#[task(binds = GPIOC, priority = 3)]
diff --git a/examples/resource-user-struct.rs b/examples/resource-user-struct.rs
index ca4ca2af..a550bb2a 100644
--- a/examples/resource-user-struct.rs
+++ b/examples/resource-user-struct.rs
@@ -47,18 +47,23 @@ mod app {
// `shared` can be accessed from this context
#[task(binds = UART0, resources = [shared])]
- fn uart0(cx: uart0::Context) {
- let shared: &mut u32 = cx.resources.shared;
- *shared += 1;
+ fn uart0(mut cx: uart0::Context) {
+ let shared = cx.resources.shared.lock(|shared| {
+ *shared += 1;
+ *shared
+ });
hprintln!("UART0: shared = {}", shared).unwrap();
}
// `shared` can be accessed from this context
#[task(binds = UART1, resources = [shared])]
- fn uart1(cx: uart1::Context) {
- *cx.resources.shared += 1;
+ fn uart1(mut cx: uart1::Context) {
+ let shared = cx.resources.shared.lock(|shared| {
+ *shared += 1;
+ *shared
+ });
- hprintln!("UART1: shared = {}", cx.resources.shared).unwrap();
+ hprintln!("UART1: shared = {}", shared).unwrap();
}
}
diff --git a/examples/resource.rs b/examples/resource.rs
index 87ba3367..d86d46fb 100644
--- a/examples/resource.rs
+++ b/examples/resource.rs
@@ -42,18 +42,23 @@ mod app {
// `shared` can be accessed from this context
#[task(binds = UART0, resources = [shared])]
- fn uart0(cx: uart0::Context) {
- let shared: &mut u32 = cx.resources.shared;
- *shared += 1;
+ fn uart0(mut cx: uart0::Context) {
+ let shared = cx.resources.shared.lock(|shared| {
+ *shared += 1;
+ *shared
+ });
hprintln!("UART0: shared = {}", shared).unwrap();
}
// `shared` can be accessed from this context
#[task(binds = UART1, resources = [shared])]
- fn uart1(cx: uart1::Context) {
- *cx.resources.shared += 1;
+ fn uart1(mut cx: uart1::Context) {
+ let shared = cx.resources.shared.lock(|shared| {
+ *shared += 1;
+ *shared
+ });
- hprintln!("UART1: shared = {}", cx.resources.shared).unwrap();
+ hprintln!("UART1: shared = {}", shared).unwrap();
}
}
diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs
deleted file mode 100644
index ec055886..00000000
--- a/examples/shared-with-init.rs
+++ /dev/null
@@ -1,45 +0,0 @@
-//! `examples/shared-with-init.rs`
-
-#![deny(unsafe_code)]
-#![deny(warnings)]
-#![no_main]
-#![no_std]
-
-use panic_halt as _;
-use rtic::app;
-
-pub struct MustBeSend;
-
-#[app(device = lm3s6965)]
-mod app {
- use super::MustBeSend;
- use cortex_m_semihosting::debug;
- use lm3s6965::Interrupt;
-
- #[resources]
- struct Resources {
- #[init(None)]
- shared: Option<MustBeSend>,
- }
-
- #[init(resources = [shared])]
- fn init(c: init::Context) -> init::LateResources {
- // this `message` will be sent to task `UART0`
- let message = MustBeSend;
- *c.resources.shared = Some(message);
-
- rtic::pend(Interrupt::UART0);
-
- init::LateResources {}
- }
-
- #[task(binds = UART0, resources = [shared])]
- fn uart0(c: uart0::Context) {
- if let Some(message) = c.resources.shared.take() {
- // `message` has been received
- drop(message);
-
- debug::exit(debug::EXIT_SUCCESS);
- }
- }
-}
diff --git a/examples/static.rs b/examples/static.rs
index cd46145a..7626c712 100644
--- a/examples/static.rs
+++ b/examples/static.rs
@@ -36,9 +36,9 @@ mod app {
}
#[idle(resources = [c])]
- fn idle(c: idle::Context) -> ! {
+ fn idle(mut c: idle::Context) -> ! {
loop {
- if let Some(byte) = c.resources.c.dequeue() {
+ if let Some(byte) = c.resources.c.lock(|c| c.dequeue()) {
hprintln!("received message: {}", byte).unwrap();
debug::exit(debug::EXIT_SUCCESS);
@@ -49,9 +49,9 @@ mod app {
}
#[task(binds = UART0, resources = [p])]
- fn uart0(c: uart0::Context) {
+ fn uart0(mut c: uart0::Context) {
static mut KALLE: u32 = 0;
*KALLE += 1;
- c.resources.p.enqueue(42).unwrap();
+ c.resources.p.lock(|p| p.enqueue(42).unwrap());
}
}
diff --git a/examples/t-late-not-send.rs b/examples/t-late-not-send.rs
index 8b7b986f..ce3bcbac 100644
--- a/examples/t-late-not-send.rs
+++ b/examples/t-late-not-send.rs
@@ -23,11 +23,8 @@ mod app {
y: Option<NotSend>,
}
- #[init(resources = [y])]
- fn init(c: init::Context) -> init::LateResources {
- // equivalent to late resource initialization
- *c.resources.y = Some(NotSend { _0: PhantomData });
-
+ #[init]
+ fn init(_: init::Context) -> init::LateResources {
init::LateResources {
x: NotSend { _0: PhantomData },
}
diff --git a/examples/t-resource.rs b/examples/t-resource.rs
index 91950d3e..0a9f3bad 100644
--- a/examples/t-resource.rs
+++ b/examples/t-resource.rs
@@ -31,29 +31,18 @@ mod app {
s3: u32, // idle & uart0
}
- #[init(resources = [o1, o4, o5, o6, s3])]
- fn init(c: init::Context) -> init::LateResources {
- // owned by `init` == `&'static mut`
- let _: &'static mut u32 = c.resources.o1;
-
- // owned by `init` == `&'static` if read-only
- 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;
-
+ #[init]
+ fn init(_: init::Context) -> init::LateResources {
init::LateResources {}
}
#[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 _: resources::o2 = c.resources.o2;
// owned by `idle` == `&'static` if read-only
- let _: &'static u32 = c.resources.o4;
+ let _: &u32 = c.resources.o4;
// shared with `idle` == `Mutex`
c.resources.s1.lock(|_| {});
@@ -69,13 +58,13 @@ mod app {
#[task(binds = UART0, resources = [o3, s1, s2, &s3])]
fn uart0(c: uart0::Context) {
// owned by interrupt == `&mut`
- let _: &mut u32 = c.resources.o3;
+ let _: resources::o3 = c.resources.o3;
// no `Mutex` proxy when access from highest priority task
- let _: &mut u32 = c.resources.s1;
+ let _: resources::s1 = c.resources.s1;
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
- let _: &mut u32 = c.resources.s2;
+ let _: resources::s2 = c.resources.s2;
// `&` if read-only
let _: &u32 = c.resources.s3;
@@ -87,6 +76,6 @@ mod app {
let _: &u32 = c.resources.o5;
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
- let _: &mut u32 = c.resources.s2;
+ let _: resources::s2 = c.resources.s2;
}
}
diff --git a/examples/task-local.rs b/examples/task-local.rs
index 462a0d31..e86197a0 100644
--- a/examples/task-local.rs
+++ b/examples/task-local.rs
@@ -61,9 +61,11 @@ mod app {
// l2 ok (task_local)
// e1 ok (lock_free)
#[task(priority = 1, binds = UART0, resources = [shared, l2, e1])]
- fn uart0(cx: uart0::Context) {
- let shared: &mut u32 = cx.resources.shared;
- *shared += 1;
+ fn uart0(mut cx: uart0::Context) {
+ let shared = cx.resources.shared.lock(|shared| {
+ *shared += 1;
+ *shared
+ });
*cx.resources.e1 += 10;
hprintln!("UART0: shared = {}", shared).unwrap();
hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap();
@@ -73,9 +75,11 @@ mod app {
// `shared` can be accessed from this context
// e1 ok (lock_free)
#[task(priority = 1, binds = UART1, resources = [shared, e1])]
- fn uart1(cx: uart1::Context) {
- let shared: &mut u32 = cx.resources.shared;
- *shared += 1;
+ fn uart1(mut cx: uart1::Context) {
+ let shared = cx.resources.shared.lock(|shared| {
+ *shared += 1;
+ *shared
+ });
hprintln!("UART1: shared = {}", shared).unwrap();
hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap();
diff --git a/examples/types.rs b/examples/types.rs
index 815d309a..cb93a36f 100644
--- a/examples/types.rs
+++ b/examples/types.rs
@@ -45,7 +45,7 @@ mod app {
#[task(priority = 2, resources = [shared])]
fn foo(cx: foo::Context) {
let _: cyccnt::Instant = cx.scheduled;
- let _: &mut u32 = cx.resources.shared;
+ let _: resources::shared = cx.resources.shared;
let _: foo::Resources = cx.resources;
}