From 5ff57e48b43ec921333e2dc8b0dffbfce6fdf19b Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Sun, 19 Nov 2017 19:55:38 +0100
Subject: [PATCH] nested examples

---
 examples/nested_new.rs | 108 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)
 create mode 100644 examples/nested_new.rs

diff --git a/examples/nested_new.rs b/examples/nested_new.rs
new file mode 100644
index 0000000..b3046bf
--- /dev/null
+++ b/examples/nested_new.rs
@@ -0,0 +1,108 @@
+//! Nesting claims and how the preemption threshold works
+//!
+//! If you run this program you'll hit the breakpoints as indicated by the
+//! letters in the comments: A, then B, then C, etc.
+#![deny(unsafe_code)]
+#![feature(proc_macro)]
+#![no_std]
+
+extern crate cortex_m_rtfm as rtfm;
+extern crate stm32f40x;
+
+use stm32f40x::Interrupt;
+use rtfm::{app, Resource, Threshold};
+
+app! {
+    device: stm32f40x,
+
+    resources: {
+        static LOW: u64 = 0;
+        static HIGH: u64 = 0;
+    },
+
+    tasks: {
+        EXTI0: {
+            path: exti0,
+            priority: 1,
+            resources: [LOW, HIGH],
+        },
+
+        EXTI1: {
+            path: exti1,
+            priority: 2,
+            resources: [LOW],
+        },
+
+        EXTI2: {
+            path: exti2,
+            priority: 3,
+            resources: [HIGH],
+        },
+
+        EXTI3: {
+            path: exti0_new_stub,
+            priority: 1,
+            resources: [LOW, HIGH],
+        },
+    },
+}
+
+fn init(_p: init::Peripherals, _r: init::Resources) {}
+
+#[inline(never)]
+fn idle() -> ! {
+    // A
+    rtfm::bkpt();
+
+    // Sets task `exti0` as pending
+    //
+    // Because `exti0` has higher priority than `idle` it will be executed
+    // immediately
+    rtfm::set_pending(Interrupt::EXTI0); // ~> exti0
+    rtfm::set_pending(Interrupt::EXTI3); // ~> exti0
+
+    loop {
+        rtfm::wfi();
+    }
+}
+
+#[allow(non_snake_case)]
+fn exti0_new_stub(t: &mut Threshold, r: EXTI3::Resources) {
+    let mut b = false; // first claim
+    rtfm::bkpt();
+    exti0_new(&mut b, t, r)
+}
+
+#[allow(non_snake_case)]
+fn exti0_new(
+    b: &mut bool, // shulde be a newtype later
+    t: &mut Threshold,
+    EXTI3::Resources { mut LOW, mut HIGH }: EXTI3::Resources,
+) {
+    LOW.claim_mut_new(b, t, |_low, b, t| {
+        rtfm::bkpt();
+        HIGH.claim_mut_new(b, t, |_high, _, _| {
+            rtfm::bkpt();
+        });
+    });
+}
+
+#[allow(non_snake_case)]
+fn exti0(t: &mut Threshold, EXTI0::Resources { mut LOW, mut HIGH }: EXTI0::Resources) {
+    rtfm::bkpt();
+    LOW.claim_mut(t, |_low, t| {
+        rtfm::bkpt();
+        HIGH.claim_mut(t, |_high, _| {
+            rtfm::bkpt();
+        });
+    });
+}
+
+
+fn exti1(_t: &mut Threshold, _r: EXTI1::Resources) {
+    rtfm::bkpt();
+}
+
+fn exti2(_t: &mut Threshold, _r: EXTI2::Resources) {
+    rtfm::bkpt();
+}
-- 
GitLab