From 37a0692a0fe5d9b41b65728d496b6856a1152dcc Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 4 Nov 2018 18:50:42 +0100 Subject: impl Mutex on all shared resources document how to write generic code that operates on resources --- examples/capacity.rs | 2 +- examples/generics.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ examples/types.rs | 3 ++- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 examples/generics.rs (limited to 'examples') diff --git a/examples/capacity.rs b/examples/capacity.rs index 2dea2c3..fca7fe2 100644 --- a/examples/capacity.rs +++ b/examples/capacity.rs @@ -23,7 +23,7 @@ macro_rules! println { #[app(device = lm3s6965)] const APP: () = { - #[init(spawn = [foo])] + #[init] fn init() { rtfm::pend(Interrupt::UART0); } diff --git a/examples/generics.rs b/examples/generics.rs new file mode 100644 index 0000000..aee7061 --- /dev/null +++ b/examples/generics.rs @@ -0,0 +1,74 @@ +//! examples/generics.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +extern crate panic_semihosting; + +use cortex_m_semihosting::debug; +use lm3s6965::Interrupt; +use rtfm::{app, Mutex}; + +// NOTE: This convenience macro will appear in all the other examples and +// will always look the same +macro_rules! println { + ($($tt:tt)*) => { + if let Ok(mut stdout) = cortex_m_semihosting::hio::hstdout() { + use core::fmt::Write; + + writeln!(stdout, $($tt)*).ok(); + } + }; +} + +#[app(device = lm3s6965)] +const APP: () = { + static mut SHARED: u32 = 0; + + #[init] + fn init() { + rtfm::pend(Interrupt::UART0); + rtfm::pend(Interrupt::UART1); + } + + #[interrupt(resources = [SHARED])] + fn UART0() { + static mut STATE: u32 = 0; + + println!("UART0(STATE = {})", *STATE); + + advance(STATE, resources.SHARED); + + rtfm::pend(Interrupt::UART1); + + debug::exit(debug::EXIT_SUCCESS); + } + + #[interrupt(priority = 2, resources = [SHARED])] + fn UART1() { + static mut STATE: u32 = 0; + + println!("UART1(STATE = {})", *STATE); + + // just to show that `SHARED` can be accessed directly and .. + *resources.SHARED += 0; + // .. also through a (no-op) `lock` + resources.SHARED.lock(|shared| *shared += 0); + + advance(STATE, resources.SHARED); + } +}; + +fn advance(state: &mut u32, mut shared: impl Mutex) { + *state += 1; + + let (old, new) = shared.lock(|shared| { + let old = *shared; + *shared += *state; + (old, *shared) + }); + + println!("SHARED: {} -> {}", old, new); +} diff --git a/examples/types.rs b/examples/types.rs index 6606208..c1b8cd6 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -8,7 +8,7 @@ extern crate panic_semihosting; use cortex_m_semihosting::debug; -use rtfm::{app, Instant}; +use rtfm::{app, Exclusive, Instant}; #[app(device = lm3s6965)] const APP: () = { @@ -43,6 +43,7 @@ const APP: () = { #[task(priority = 2, resources = [SHARED], schedule = [foo], spawn = [foo])] fn foo() { let _: Instant = scheduled; + let _: Exclusive = resources.SHARED; let _: foo::Resources = resources; let _: foo::Schedule = schedule; let _: foo::Spawn = spawn; -- cgit v1.2.3