aboutsummaryrefslogtreecommitdiff
path: root/examples/generics.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-11-04 18:58:45 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-11-04 18:58:45 +0000
commit8a27efeaeb5483dc12370add4b3786bdd20a4973 (patch)
tree00e10c34cc22f80aee45c51d945e31c237c74eeb /examples/generics.rs
parent16d473a9b6827aa7ffa9ce92e4e532eff9a091d2 (diff)
parenta2792182952c953aab2c442bb02ac1f2e4986e67 (diff)
Merge #99
99: impl Mutex on all shared resources r=japaric a=japaric document how to write generic code that operates on resources Co-authored-by: Jorge Aparicio <jorge@japaric.io>
Diffstat (limited to 'examples/generics.rs')
-rw-r--r--examples/generics.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/examples/generics.rs b/examples/generics.rs
new file mode 100644
index 0000000..c8ce839
--- /dev/null
+++ b/examples/generics.rs
@@ -0,0 +1,62 @@
+//! examples/generics.rs
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate panic_semihosting;
+
+use cortex_m_semihosting::{debug, hprintln};
+use lm3s6965::Interrupt;
+use rtfm::{app, Mutex};
+
+#[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;
+
+ hprintln!("UART0(STATE = {})", *STATE).unwrap();
+
+ 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;
+
+ hprintln!("UART1(STATE = {})", *STATE).unwrap();
+
+ // 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)
+ });
+
+ hprintln!("SHARED: {} -> {}", old, new).unwrap();
+}