diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/capacity.rs | 2 | ||||
-rw-r--r-- | examples/generics.rs | 74 | ||||
-rw-r--r-- | examples/types.rs | 3 |
3 files changed, 77 insertions, 2 deletions
diff --git a/examples/capacity.rs b/examples/capacity.rs index 2dea2c30..fca7fe2b 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 00000000..aee70611 --- /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<T = u32>) { + *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 66062085..c1b8cd69 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<u32> = resources.SHARED; let _: foo::Resources = resources; let _: foo::Schedule = schedule; let _: foo::Spawn = spawn; |