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/generics.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 examples/generics.rs (limited to 'examples/generics.rs') 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); +} -- cgit v1.2.3 From a2792182952c953aab2c442bb02ac1f2e4986e67 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 4 Nov 2018 19:57:39 +0100 Subject: use new cortex_m_semihosting::hprintln macro --- examples/generics.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'examples/generics.rs') diff --git a/examples/generics.rs b/examples/generics.rs index aee7061..c8ce839 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -7,22 +7,10 @@ extern crate panic_semihosting; -use cortex_m_semihosting::debug; +use cortex_m_semihosting::{debug, hprintln}; 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; @@ -37,7 +25,7 @@ const APP: () = { fn UART0() { static mut STATE: u32 = 0; - println!("UART0(STATE = {})", *STATE); + hprintln!("UART0(STATE = {})", *STATE).unwrap(); advance(STATE, resources.SHARED); @@ -50,7 +38,7 @@ const APP: () = { fn UART1() { static mut STATE: u32 = 0; - println!("UART1(STATE = {})", *STATE); + hprintln!("UART1(STATE = {})", *STATE).unwrap(); // just to show that `SHARED` can be accessed directly and .. *resources.SHARED += 0; @@ -70,5 +58,5 @@ fn advance(state: &mut u32, mut shared: impl Mutex) { (old, *shared) }); - println!("SHARED: {} -> {}", old, new); + hprintln!("SHARED: {} -> {}", old, new).unwrap(); } -- cgit v1.2.3