aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/local.rs79
-rw-r--r--examples/local_err.rs82
-rw-r--r--examples/local_minimal.rs30
-rw-r--r--examples/static.rs54
4 files changed, 245 insertions, 0 deletions
diff --git a/examples/local.rs b/examples/local.rs
new file mode 100644
index 00000000..802ab20f
--- /dev/null
+++ b/examples/local.rs
@@ -0,0 +1,79 @@
+//! examples/local.rs
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+use cortex_m_semihosting::{debug, hprintln};
+use lm3s6965::Interrupt;
+use panic_semihosting as _;
+
+#[rtfm::app(device = lm3s6965)]
+const APP: () = {
+ struct Resources {
+ // An early resource
+ #[init(0)]
+ shared: u32,
+
+ // A local (move), early resource
+ #[task_local]
+ #[init(1)]
+ l1: u32,
+
+ // An exclusive, early resource
+ #[lock_free]
+ #[init(1)]
+ e1: u32,
+
+ // A local (move), late resource
+ #[task_local]
+ l2: u32,
+
+ // An exclusive, late resource
+ #[lock_free]
+ e2: u32,
+ }
+
+ #[init]
+ fn init(_: init::Context) -> init::LateResources {
+ rtfm::pend(Interrupt::UART0);
+ rtfm::pend(Interrupt::UART1);
+ init::LateResources { e2: 2, l2: 2 }
+ }
+
+ // `shared` cannot be accessed from this context
+ // l1 ok (task_local)
+ // e2 ok (lock_free)
+ #[idle(resources =[l1, e2])]
+ fn idle(cx: idle::Context) -> ! {
+ hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap();
+ hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap();
+ debug::exit(debug::EXIT_SUCCESS);
+ loop {}
+ }
+
+ // `shared` can be accessed from this context
+ // 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;
+ *cx.resources.e1 += 10;
+ hprintln!("UART0: shared = {}", shared).unwrap();
+ hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap();
+ hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap();
+ }
+
+ // `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;
+
+ hprintln!("UART1: shared = {}", shared).unwrap();
+ hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap();
+ }
+};
diff --git a/examples/local_err.rs b/examples/local_err.rs
new file mode 100644
index 00000000..3be593a8
--- /dev/null
+++ b/examples/local_err.rs
@@ -0,0 +1,82 @@
+//! examples/local_err.rs
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+// errors here, since we cannot bail compilation or generate stubs
+// run cargo expand, then you see the root of the problem...
+use cortex_m_semihosting::{debug, hprintln};
+use lm3s6965::Interrupt;
+use panic_semihosting as _;
+
+#[rtfm::app(device = lm3s6965)]
+const APP: () = {
+ struct Resources {
+ // An early resource
+ #[init(0)]
+ shared: u32,
+
+ // A local (move), early resource
+ #[task_local]
+ #[init(1)]
+ l1: u32,
+
+ // An exclusive, early resource
+ #[lock_free]
+ #[init(1)]
+ e1: u32,
+
+ // A local (move), late resource
+ #[task_local]
+ l2: u32,
+
+ // An exclusive, late resource
+ #[lock_free]
+ e2: u32,
+ }
+
+ #[init]
+ fn init(_: init::Context) -> init::LateResources {
+ rtfm::pend(Interrupt::UART0);
+ rtfm::pend(Interrupt::UART1);
+ init::LateResources { e2: 2, l2: 2 }
+ }
+
+ // `shared` cannot be accessed from this context
+ // l1 ok
+ // l2 rejeceted (not task_local)
+ // e2 ok
+ #[idle(resources =[l1, l2, e2])]
+ fn idle(cx: idle::Context) -> ! {
+ hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap();
+ hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap();
+ debug::exit(debug::EXIT_SUCCESS);
+ loop {}
+ }
+
+ // `shared` can be accessed from this context
+ // l2 rejected (not task_local)
+ // e1 rejected (not 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;
+ *cx.resources.e1 += 10;
+ hprintln!("UART0: shared = {}", shared).unwrap();
+ hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap();
+ hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap();
+ }
+
+ // l2 rejected (not task_local)
+ #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])]
+ fn uart1(cx: uart1::Context) {
+ let shared: &mut u32 = cx.resources.shared;
+ *shared += 1;
+
+ hprintln!("UART1: shared = {}", shared).unwrap();
+ hprintln!("UART1:l2 = {}", cx.resources.l2).unwrap();
+ hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap();
+ }
+};
diff --git a/examples/local_minimal.rs b/examples/local_minimal.rs
new file mode 100644
index 00000000..13531c5e
--- /dev/null
+++ b/examples/local_minimal.rs
@@ -0,0 +1,30 @@
+//! examples/local_minimal.rs
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+use cortex_m_semihosting::{debug, hprintln};
+use panic_semihosting as _;
+
+#[rtfm::app(device = lm3s6965)]
+const APP: () = {
+ struct Resources {
+ // A local (move), late resource
+ #[task_local]
+ l: u32,
+ }
+
+ #[init]
+ fn init(_: init::Context) -> init::LateResources {
+ init::LateResources { l: 42 }
+ }
+
+ // l is task_local
+ #[idle(resources =[l])]
+ fn idle(cx: idle::Context) -> ! {
+ hprintln!("IDLE:l = {}", cx.resources.l).unwrap();
+ debug::exit(debug::EXIT_SUCCESS);
+ loop {}
+ }
+};
diff --git a/examples/static.rs b/examples/static.rs
new file mode 100644
index 00000000..ddcb11e6
--- /dev/null
+++ b/examples/static.rs
@@ -0,0 +1,54 @@
+//! examples/late.rs
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+use cortex_m_semihosting::{debug, hprintln};
+use heapless::{
+ consts::*,
+ i,
+ spsc::{Consumer, Producer, Queue},
+};
+use lm3s6965::Interrupt;
+use panic_semihosting as _;
+
+#[rtfm::app(device = lm3s6965)]
+const APP: () = {
+ // Late resources
+ struct Resources {
+ p: Producer<'static, u32, U4>,
+ c: Consumer<'static, u32, U4>,
+ }
+
+ #[init]
+ fn init(_: init::Context) -> init::LateResources {
+ static mut Q: Queue<u32, U4> = Queue(i::Queue::new());
+
+ let (p, c) = Q.split();
+
+ // Initialization of late resources
+ init::LateResources { p, c }
+ }
+
+ #[idle(resources = [c])]
+ fn idle(c: idle::Context) -> ! {
+ loop {
+ if let Some(byte) = c.resources.c.dequeue() {
+ hprintln!("received message: {}", byte).unwrap();
+
+ debug::exit(debug::EXIT_SUCCESS);
+ } else {
+ rtfm::pend(Interrupt::UART0);
+ }
+ }
+ }
+
+ #[task(binds = UART0, resources = [p])]
+ fn uart0(c: uart0::Context) {
+ static mut KALLE: u32 = 0;
+ *KALLE += 1;
+ c.resources.p.enqueue(42).unwrap();
+ }
+};