From a06a5e0b4754aca6b63465874c9afacf38be416d Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Thu, 10 Jan 2019 23:38:16 +0100
Subject: [PATCH] bare8, 9

---
 .vscode/tasks.json | 26 +++++++----------------
 Cargo.toml         | 13 ++++++++++--
 examples/bare6.rs  | 12 +++++------
 examples/bare9.rs  | 52 +++++++++++++++++++++++++++++-----------------
 4 files changed, 56 insertions(+), 47 deletions(-)

diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index e86a927..9822b8f 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -65,8 +65,8 @@
         },
         {
             "type": "shell",
-            "label": "cargo build --example device --features stm32f4",
-            "command": "cargo build --example device --features stm32f4",
+            "label": "cargo build --example device --features pac",
+            "command": "cargo build --example device --features pac",
             "problemMatcher": [
                 "$rustc"
             ],
@@ -77,8 +77,8 @@
         },
         {
             "type": "shell",
-            "label": "cargo build --example rtfm_interrupt --features stm32f4",
-            "command": "cargo build --example rtfm_interrupt --features stm32f4",
+            "label": "cargo build --example rtfm_interrupt --features \"pac rtfm\"",
+            "command": "cargo build --example rtfm_interrupt --features \"pac rtfm\"",
             "problemMatcher": [
                 "$rustc"
             ],
@@ -222,7 +222,7 @@
         {
             "type": "shell",
             "label": "cargo build --example bare7 --features stm32f4",
-            "command": "cargo build --example bare7 --features stm32f4",
+            "command": "cargo build --example bare7 --features \"hal pac\"",
             "problemMatcher": [
                 "$rustc"
             ],
@@ -234,19 +234,7 @@
         {
             "type": "shell",
             "label": "cargo build --example bare8 --features stm32f4",
-            "command": "cargo build --example bare8 --features stm32f4",
-            "problemMatcher": [
-                "$rustc"
-            ],
-            "group": {
-                "kind": "build",
-                "isDefault": true
-            }
-        },
-        {
-            "type": "shell",
-            "label": "cargo build --example bare8 --features stm32f4",
-            "command": "cargo build --example bare8 --features stm32f4",
+            "command": "cargo build --example bare8 --features \"hal rtfm\"",
             "problemMatcher": [
                 "$rustc"
             ],
@@ -258,7 +246,7 @@
         {
             "type": "shell",
             "label": "cargo build --example bare9 --features stm32f4",
-            "command": "cargo build --example bare9 --features stm32f4",
+            "command": "cargo build --example bare9 --features \"hal rtfm\"",
             "problemMatcher": [
                 "$rustc"
             ],
diff --git a/Cargo.toml b/Cargo.toml
index 6c509df..95f4dfc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,7 +19,10 @@ panic-semihosting = "0.5.1"
 panic-itm = "0.4.0"
 bare-metal = "0.2.4"
 nb = "0.1.1"
-cortex-m-rtfm = "0.4.0"
+
+[dependencies.cortex-m-rtfm]
+version = "0.4.0"
+optional = true
 
 [dependencies.cortex-m]
 version = "0.5.8"
@@ -31,12 +34,18 @@ features = ["inline-asm"] # <- currently requires nightly compiler
 [dependencies.stm32f4]
 version = "0.5.0"
 features = ["stm32f413", "rt"]
-# optional = true
+optional = true
 
 [dependencies.stm32f4xx-hal]
 git = "https://github.com/stm32-rs/stm32f4xx-hal.git"
 version = "0.2.8"
 features = ["stm32f413", "rt"]
+optional = true
+
+[features]
+hal = ["stm32f4", "stm32f4xx-hal"]
+rtfm = ["cortex-m-rtfm"]
+pac = ["stm32f4"]
 
 # this lets you use `cargo fix`!
 [[bin]]
diff --git a/examples/bare6.rs b/examples/bare6.rs
index db6977d..0ad8102 100644
--- a/examples/bare6.rs
+++ b/examples/bare6.rs
@@ -27,17 +27,15 @@
 extern crate panic_halt;
 
 use cortex_m::{iprintln, peripheral::itm::Stim};
-use cortex_m_rt::{entry};
+use cortex_m_rt::entry;
 
-use stm32f4::stm32f411;
-use stm32f411::{
-    DWT, GPIOA, GPIOC, RCC,
-};
+use stm32f4::stm32f413;
+use stm32f413::{DWT, GPIOA, GPIOC, RCC};
 
 #[entry]
 fn main() -> ! {
-    let p = stm32f411::Peripherals::take().unwrap();
-    let mut c = stm32f411::CorePeripherals::take().unwrap();
+    let p = stm32f413::Peripherals::take().unwrap();
+    let mut c = stm32f413::CorePeripherals::take().unwrap();
 
     let stim = &mut c.ITM.stim[0];
     iprintln!(stim, "Hello, bare6!");
diff --git a/examples/bare9.rs b/examples/bare9.rs
index 08f4d0c..4717612 100644
--- a/examples/bare9.rs
+++ b/examples/bare9.rs
@@ -5,25 +5,25 @@
 
 extern crate panic_halt;
 
-use cortex_m::iprintln;
+use cortex_m::{asm, iprintln};
 use nb::block;
 
 extern crate stm32f4xx_hal as hal;
 use crate::hal::prelude::*;
-use crate::hal::serial::{config::Config, Rx, Serial, Tx};
-use hal::stm32::{ITM, USART2};
+use crate::hal::serial::{config::Config, Event, Rx, Serial, Tx};
+use hal::stm32::ITM;
 
 // use crate::hal::stm32::Interrupt::EXTI0;
 use rtfm::app;
 // use hal::stm32::Interrupt::EXTI0;
 
-#[app(device = hal::stm32)]
 // #[app(device = stm32f4xx_hal::stm32)]
+#[app(device = hal::stm32)]
 
 const APP: () = {
     // Late resources
-    static mut TX: Tx<USART2> = ();
-    static mut RX: Rx<USART2> = ();
+    static mut TX: Tx<hal::stm32::USART2> = ();
+    static mut RX: Rx<hal::stm32::USART2> = ();
     static mut ITM: ITM = ();
 
     // init runs in an interrupt free section
@@ -42,7 +42,7 @@ const APP: () = {
         let tx = gpioa.pa2.into_alternate_af7();
         let rx = gpioa.pa3.into_alternate_af7(); // try comment out
 
-        let serial = Serial::usart2(
+        let mut serial = Serial::usart2(
             device.USART2,
             (tx, rx),
             Config::default().baudrate(115_200.bps()),
@@ -50,6 +50,8 @@ const APP: () = {
         )
         .unwrap();
 
+        // generate interrupt on Rxne
+        serial.listen(Event::Rxne);
         // Separate out the sender and receiver of the serial port
         let (tx, rx) = serial.split();
 
@@ -60,26 +62,38 @@ const APP: () = {
     }
 
     // idle may be interrupted by other interrupt/tasks in the system
-    #[idle(resources = [RX, TX, ITM])]
+    // #[idle(resources = [RX, TX, ITM])]
+    #[idle(resources = [ITM])]
     fn idle() -> ! {
+        loop {
+            resources.ITM.lock(|itm| {
+                let stim = &mut itm.stim[0];
+                iprintln!(stim, "goto sleep");
+            });
+            asm::wfi();
+            resources.ITM.lock(|itm| {
+                let stim = &mut itm.stim[0];
+                iprintln!(stim, "woken..");
+            });
+        }
+    }
+
+    #[interrupt(resources = [RX, TX, ITM])]
+    fn USART2() {
         let rx = resources.RX;
         let tx = resources.TX;
         let stim = &mut resources.ITM.stim[0];
 
-        loop {
-            match block!(rx.read()) {
-                Ok(byte) => {
-                    iprintln!(stim, "Ok {:?}", byte);
-                    let _ = tx.write(byte);
-                }
-                Err(err) => {
-                    iprintln!(stim, "Error {:?}", err);
-                }
+        match block!(rx.read()) {
+            Ok(byte) => {
+                iprintln!(stim, "Ok {:?}", byte);
+                let _ = tx.write(byte);
+            }
+            Err(err) => {
+                iprintln!(stim, "Error {:?}", err);
             }
         }
     }
-    // #[interrupt]
-    // fn EXTI0() {}
 };
 
 // extern crate cortex_m_rtfm as rtfm;
-- 
GitLab