aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/lock-free.rs60
-rw-r--r--examples/resource.rs66
2 files changed, 100 insertions, 26 deletions
diff --git a/examples/lock-free.rs b/examples/lock-free.rs
new file mode 100644
index 0000000..db74c7d
--- /dev/null
+++ b/examples/lock-free.rs
@@ -0,0 +1,60 @@
+//! examples/lock-free.rs
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+use panic_semihosting as _;
+
+#[rtic::app(device = lm3s6965)]
+mod app {
+ use cortex_m_semihosting::{debug, hprintln};
+ use lm3s6965::Interrupt;
+
+ #[shared]
+ struct Shared {
+ #[lock_free] // <- lock-free shared resource
+ counter: u64,
+ }
+
+ #[local]
+ struct Local {}
+
+ #[init]
+ fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
+ rtic::pend(Interrupt::GPIOA);
+
+ (Shared { counter: 0 }, Local {}, init::Monotonics())
+ }
+
+ #[task(binds = GPIOA, shared = [counter])] // <- same priority
+ fn gpioa(c: gpioa::Context) {
+ hprintln!("GPIOA/start").unwrap();
+ rtic::pend(Interrupt::GPIOB);
+
+ *c.shared.counter += 1; // <- no lock API required
+ let counter = *c.shared.counter;
+ hprintln!(" GPIOA/counter = {}", counter).unwrap();
+
+ if counter == 5 {
+ debug::exit(debug::EXIT_SUCCESS);
+ }
+ hprintln!("GPIOA/end").unwrap();
+ }
+
+ #[task(binds = GPIOB, shared = [counter])] // <- same priority
+ fn gpiob(c: gpiob::Context) {
+ hprintln!("GPIOB/start").unwrap();
+ rtic::pend(Interrupt::GPIOA);
+
+ *c.shared.counter += 1; // <- no lock API required
+ let counter = *c.shared.counter;
+ hprintln!(" GPIOB/counter = {}", counter).unwrap();
+
+ if counter == 5 {
+ debug::exit(debug::EXIT_SUCCESS);
+ }
+ hprintln!("GPIOB/end").unwrap();
+ }
+}
diff --git a/examples/resource.rs b/examples/resource.rs
index 2c7dffe..dca0b37 100644
--- a/examples/resource.rs
+++ b/examples/resource.rs
@@ -13,55 +13,69 @@ mod app {
use lm3s6965::Interrupt;
#[shared]
- struct Shared {
- shared: u32,
- }
+ struct Shared {}
#[local]
- struct Local {}
+ struct Local {
+ local_to_uart0: i64,
+ local_to_uart1: i64,
+ }
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
rtic::pend(Interrupt::UART0);
rtic::pend(Interrupt::UART1);
- (Shared { shared: 0 }, Local {}, init::Monotonics())
+ (
+ Shared {},
+ // initial values for the `#[local]` resources
+ Local {
+ local_to_uart0: 0,
+ local_to_uart1: 0,
+ },
+ init::Monotonics(),
+ )
}
- // `shared` cannot be accessed from this context
+ // `#[local]` resources cannot be accessed from this context
#[idle]
fn idle(_cx: idle::Context) -> ! {
debug::exit(debug::EXIT_SUCCESS);
- // error: no `shared` field in `idle::Context`
- // _cx.shared.shared += 1;
+ // error: no `local` field in `idle::Context`
+ // _cx.local.local_to_uart0 += 1;
+
+ // error: no `local` field in `idle::Context`
+ // _cx.local.local_to_uart1 += 1;
loop {
cortex_m::asm::nop();
}
}
- // `shared` can be accessed from this context
+ // `local_to_uart0` can only be accessed from this context
// defaults to priority 1
- #[task(binds = UART0, shared = [shared])]
- fn uart0(mut cx: uart0::Context) {
- let shared = cx.shared.shared.lock(|shared| {
- *shared += 1;
- *shared
- });
-
- hprintln!("UART0: shared = {}", shared).unwrap();
+ #[task(binds = UART0, local = [local_to_uart0])]
+ fn uart0(cx: uart0::Context) {
+ *cx.local.local_to_uart0 += 1;
+ let local_to_uart0 = cx.local.local_to_uart0;
+
+ // error: no `local_to_uart1` field in `uart0::LocalResources`
+ // cx.local.local_to_uart1 += 1;
+
+ hprintln!("UART0: local_to_uart0 = {}", local_to_uart0).unwrap();
}
- // `shared` can be accessed from this context
+ // `shared` can only be accessed from this context
// explicitly set to priority 2
- #[task(binds = UART1, shared = [shared], priority = 2)]
- fn uart1(mut cx: uart1::Context) {
- let shared = cx.shared.shared.lock(|shared| {
- *shared += 1;
- *shared
- });
-
- hprintln!("UART1: shared = {}", shared).unwrap();
+ #[task(binds = UART1, local = [local_to_uart1], priority = 2)]
+ fn uart1(cx: uart1::Context) {
+ *cx.local.local_to_uart1 += 1;
+ let local_to_uart1 = cx.local.local_to_uart1;
+
+ // error: no `local_to_uart0` field in `uart1::LocalResources`
+ // cx.local.local_to_uart0 += 1;
+
+ hprintln!("UART1: local_to_uart1 = {}", local_to_uart1).unwrap();
}
}