From 1595f50fb03e5d46023e6faabab17cdfa60dcd2f Mon Sep 17 00:00:00 2001
From: Per <Per Lindgren>
Date: Tue, 15 Jan 2019 15:50:21 +0100
Subject: [PATCH] rtfm_blinky wip

---
 .vscode/.cortex-debug.peripherals.state.json |  2 +-
 .vscode/launch.json                          | 92 ++++++++++++++++++++
 .vscode/tasks.json                           | 36 ++++++++
 examples/bare9.rs                            |  7 +-
 examples/rtfm_blinky.rs                      | 46 ++++++++++
 5 files changed, 179 insertions(+), 4 deletions(-)
 create mode 100644 examples/rtfm_blinky.rs

diff --git a/.vscode/.cortex-debug.peripherals.state.json b/.vscode/.cortex-debug.peripherals.state.json
index 0637a08..e2f2139 100644
--- a/.vscode/.cortex-debug.peripherals.state.json
+++ b/.vscode/.cortex-debug.peripherals.state.json
@@ -1 +1 @@
-[]
\ No newline at end of file
+[{"node":"UART9","expanded":true,"format":0},{"node":"USART2.SR","expanded":true,"format":0}]
\ No newline at end of file
diff --git a/.vscode/launch.json b/.vscode/launch.json
index f7747fe..8a61829 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -184,6 +184,34 @@
             "svdFile": "STM32F413.svd",
             "cwd": "${workspaceRoot}"
         },
+        {
+            "type": "cortex-debug",
+            "request": "launch",
+            "servertype": "openocd",
+            "name": "rtfm_blinky (debug)",
+            "preLaunchTask": "cargo build --example rtfm_blinky",
+            "executable": "./target/thumbv7em-none-eabihf/debug/examples/rtfm_blinky",
+            // uses local config files
+            "configFiles": [
+                "./stlink.cfg",
+                "./stm32f4x.cfg"
+            ],
+            "swoConfig": {
+                "enabled": true,
+                "cpuFrequency": 16000000,
+                "swoFrequency": 2000000,
+                "source": "probe",
+                "decoders": [
+                    {
+                        "type": "console",
+                        "label": "ITM",
+                        "port": 0
+                    }
+                ]
+            },
+            "svdFile": "STM32F413.svd",
+            "cwd": "${workspaceRoot}"
+        },
         {
             "type": "cortex-debug",
             "request": "launch",
@@ -609,5 +637,69 @@
             "svdFile": "STM32F413.svd",
             "cwd": "${workspaceRoot}"
         },
+        {
+            "type": "cortex-debug",
+            "request": "launch",
+            "servertype": "openocd",
+            "name": "bare10 (reselase)",
+            "preLaunchTask": "cargo build --example bare10 --release",
+            "executable": "./target/thumbv7em-none-eabihf/release/examples/bare10",
+            "configFiles": [
+                "interface/stlink.cfg",
+                "target/stm32f4x.cfg"
+            ],
+            "swoConfig": {
+                "enabled": true,
+                "cpuFrequency": 16000000,
+                "swoFrequency": 2000000,
+                "source": "probe",
+                "decoders": [
+                    {
+                        "type": "console",
+                        "label": "ITM0",
+                        "port": 0
+                    },
+                    {
+                        "type": "console",
+                        "label": "ITM1",
+                        "port": 1
+                    }
+                ]
+            },
+            "svdFile": "STM32F413.svd",
+            "cwd": "${workspaceRoot}"
+        },
+        {
+            "type": "cortex-debug",
+            "request": "launch",
+            "servertype": "openocd",
+            "name": "bare10 (debug)",
+            "preLaunchTask": "cargo build --example bare10",
+            "executable": "./target/thumbv7em-none-eabihf/debug/examples/bare10",
+            "configFiles": [
+                "interface/stlink.cfg",
+                "target/stm32f4x.cfg"
+            ],
+            "swoConfig": {
+                "enabled": true,
+                "cpuFrequency": 16000000,
+                "swoFrequency": 2000000,
+                "source": "probe",
+                "decoders": [
+                    {
+                        "type": "console",
+                        "label": "ITM0",
+                        "port": 0
+                    },
+                    // {
+                    //     "type": "console",
+                    //     "label": "ITM1",
+                    //     "port": 1
+                    // }
+                ]
+            },
+            "svdFile": "STM32F413.svd",
+            "cwd": "${workspaceRoot}"
+        },
     ]
 }
\ No newline at end of file
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index 8436e31..bf7899a 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -87,6 +87,18 @@
                 "isDefault": true
             }
         },
+        {
+            "type": "shell",
+            "label": "cargo build --example rtfm_blinky",
+            "command": "cargo build --example rtfm_blinky --features \"hal rtfm\"",
+            "problemMatcher": [
+                "$rustc"
+            ],
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            }
+        },
         {
             "type": "shell",
             "label": "cargo build --example rtfm_interrupt --features \"pac rtfm\"",
@@ -267,5 +279,29 @@
                 "isDefault": true
             }
         },
+        {
+            "type": "shell",
+            "label": "cargo build --example bare10",
+            "command": "cargo build --example bare10 --features \"hal rtfm\"",
+            "problemMatcher": [
+                "$rustc"
+            ],
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            }
+        },
+        {
+            "type": "shell",
+            "label": "cargo build --example bare10 --release",
+            "command": "cargo build --example bare10 --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 5cbd3b7..6000b73 100644
--- a/examples/bare9.rs
+++ b/examples/bare9.rs
@@ -14,11 +14,11 @@ use hal::stm32::ITM;
 
 use heapless::consts::*;
 use heapless::spsc::{Consumer, Producer, Queue};
+use nb::block;
 
 use rtfm::app;
 
 #[app(device = hal::stm32)]
-
 const APP: () = {
     // Late resources
     static mut TX: Tx<hal::stm32::USART2> = ();
@@ -38,7 +38,7 @@ const APP: () = {
         let (producer, consumer) = RB.as_mut().unwrap().split();
 
         let stim = &mut core.ITM.stim[0];
-        iprintln!(stim, "start");
+        iprintln!(stim, "bare9");
 
         let rcc = device.RCC.constrain();
 
@@ -49,6 +49,7 @@ const APP: () = {
 
         let tx = gpioa.pa2.into_alternate_af7();
         let rx = gpioa.pa3.into_alternate_af7(); // try comment out
+                                                 // let rx = gpioa.pa3.into_alternate_af6(); // try uncomment
 
         let mut serial = Serial::usart2(
             device.USART2,
@@ -101,7 +102,7 @@ const APP: () = {
 
         match rx.read() {
             Ok(byte) => {
-                let _ = tx.write(byte);
+                block!(tx.write(byte)).unwrap();
                 match resources.PRODUCER.enqueue(byte) {
                     Ok(_) => {}
                     Err(_) => asm::bkpt(),
diff --git a/examples/rtfm_blinky.rs b/examples/rtfm_blinky.rs
new file mode 100644
index 0000000..7297c64
--- /dev/null
+++ b/examples/rtfm_blinky.rs
@@ -0,0 +1,46 @@
+#![no_main]
+#![no_std]
+
+extern crate panic_halt;
+
+use cortex_m::peripheral::syst::SystClkSource;
+use stm32f4::stm32f413::GPIOA;
+
+use rtfm::app;
+
+#[app(device = stm32f4::stm32f413)]
+const APP: () = {
+    // late resorce binding
+    static mut GPIOA: GPIOA = ();
+
+    // init runs in an interrupt free section
+    #[init]
+    fn init() {
+        // configures the system timer to trigger a SysTick exception every second
+        core.SYST.set_clock_source(SystClkSource::Core);
+        core.SYST.set_reload(16_000_000); // period = 1s
+        core.SYST.enable_counter();
+        core.SYST.enable_interrupt();
+
+        // power on GPIOA, RM0368 6.3.11
+        device.RCC.ahb1enr.modify(|_, w| w.gpioaen().set_bit());
+        // configure PA5 as output, RM0368 8.4.1
+        device.GPIOA.moder.modify(|_, w| w.moder5().bits(1));
+
+        // pass on late resources
+        GPIOA = device.GPIOA;
+    }
+
+    #[exception (resources = [GPIOA])]
+    fn SysTick() {
+        static mut TOGGLE: bool = false;
+
+        if *TOGGLE {
+            GPIOA.bsrr().write(|w| w.bs5().set_bit());
+        } else {
+            GPIOA.bsrr.write(|w| w.br5().set_bit());
+        }
+
+        *TOGGLE = !TOGGLE;
+    }
+  };
-- 
GitLab