From 23ddecd9a6b2f336c5b13709c8817d5f228d7cdc Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Sun, 13 Jan 2019 22:54:12 +0100
Subject: [PATCH] bare9

---
 .vscode/launch.json | 27 +++++++++++++++++++++++
 .vscode/tasks.json  | 12 ++++++++++
 examples/bare9.rs   | 53 ++++++++++++++++++++++++++++++---------------
 3 files changed, 75 insertions(+), 17 deletions(-)

diff --git a/.vscode/launch.json b/.vscode/launch.json
index bc6f39c..f7747fe 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -582,5 +582,32 @@
             "svdFile": "STM32F413.svd",
             "cwd": "${workspaceRoot}"
         },
+        {
+            "type": "cortex-debug",
+            "request": "launch",
+            "servertype": "openocd",
+            "name": "bare9 (reselase)",
+            "preLaunchTask": "cargo build --example bare9 --release",
+            "executable": "./target/thumbv7em-none-eabihf/release/examples/bare9",
+            "configFiles": [
+                "interface/stlink.cfg",
+                "target/stm32f4x.cfg"
+            ],
+            "swoConfig": {
+                "enabled": true,
+                "cpuFrequency": 16000000,
+                "swoFrequency": 2000000,
+                "source": "probe",
+                "decoders": [
+                    {
+                        "type": "console",
+                        "label": "ITM",
+                        "port": 0
+                    }
+                ]
+            },
+            "svdFile": "STM32F413.svd",
+            "cwd": "${workspaceRoot}"
+        },
     ]
 }
\ No newline at end of file
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index fab7e52..8436e31 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -255,5 +255,17 @@
                 "isDefault": true
             }
         },
+        {
+            "type": "shell",
+            "label": "cargo build --example bare9 --release",
+            "command": "cargo build --example bare9 --release --features \"hal rtfm\"",
+            "problemMatcher": [
+                "$rustc"
+            ],
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            }
+        },
     ]
 }
\ No newline at end of file
diff --git a/examples/bare9.rs b/examples/bare9.rs
index ef8f240..5cbd3b7 100644
--- a/examples/bare9.rs
+++ b/examples/bare9.rs
@@ -6,13 +6,15 @@
 extern crate panic_halt;
 
 use cortex_m::{asm, iprintln};
-use nb::block;
 
 extern crate stm32f4xx_hal as hal;
 use crate::hal::prelude::*;
 use crate::hal::serial::{config::Config, Event, Rx, Serial, Tx};
 use hal::stm32::ITM;
 
+use heapless::consts::*;
+use heapless::spsc::{Consumer, Producer, Queue};
+
 use rtfm::app;
 
 #[app(device = hal::stm32)]
@@ -21,11 +23,20 @@ const APP: () = {
     // Late resources
     static mut TX: Tx<hal::stm32::USART2> = ();
     static mut RX: Rx<hal::stm32::USART2> = ();
+    static mut PRODUCER: Producer<'static, u8, U3> = ();
+    static mut CONSUMER: Consumer<'static, u8, U3> = ();
     static mut ITM: ITM = ();
 
     // init runs in an interrupt free section
     #[init]
     fn init() {
+        // A ring buffer for our data
+        static mut RB: Option<Queue<u8, U3>> = None;
+        *RB = Some(Queue::new());
+
+        // Split into producer/consumer pair
+        let (producer, consumer) = RB.as_mut().unwrap().split();
+
         let stim = &mut core.ITM.stim[0];
         iprintln!(stim, "start");
 
@@ -53,42 +64,50 @@ const APP: () = {
         let (tx, rx) = serial.split();
 
         // Late resources
+        // Our split queue
+        PRODUCER = producer;
+        CONSUMER = consumer;
+
+        // Our split serial
         TX = tx;
         RX = rx;
+
+        // For debugging
         ITM = core.ITM;
     }
 
     // idle may be interrupted by other interrupt/tasks in the system
     // #[idle(resources = [RX, TX, ITM])]
-    #[idle(resources = [ITM])]
+    #[idle(resources = [ITM, CONSUMER])]
     fn idle() -> ! {
+        let stim = &mut resources.ITM.stim[0];
+
         loop {
-            resources.ITM.lock(|itm| {
-                let stim = &mut itm.stim[0];
-                iprintln!(stim, "goto sleep");
-            });
+            while let Some(byte) = resources.CONSUMER.dequeue() {
+                iprintln!(stim, "data {}", byte);
+            }
+
+            iprintln!(stim, "goto sleep");
             asm::wfi();
-            resources.ITM.lock(|itm| {
-                let stim = &mut itm.stim[0];
-                iprintln!(stim, "woken..");
-            });
+
+            iprintln!(stim, "woken..");
         }
     }
 
-    #[interrupt(resources = [RX, TX, ITM])]
+    #[interrupt(resources = [RX, TX, PRODUCER])]
     fn USART2() {
         let rx = resources.RX;
         let tx = resources.TX;
-        let stim = &mut resources.ITM.stim[0];
 
-        match block!(rx.read()) {
+        match rx.read() {
             Ok(byte) => {
-                iprintln!(stim, "Ok {:?}", byte);
                 let _ = tx.write(byte);
+                match resources.PRODUCER.enqueue(byte) {
+                    Ok(_) => {}
+                    Err(_) => asm::bkpt(),
+                }
             }
-            Err(err) => {
-                iprintln!(stim, "Error {:?}", err);
-            }
+            Err(_err) => asm::bkpt(),
         }
     }
 };
-- 
GitLab