From 47812ef586b0a1fa7e6b3b1fe75140e6b20e9161 Mon Sep 17 00:00:00 2001
From: Per <Per Lindgren>
Date: Thu, 4 Jan 2018 17:14:05 +0100
Subject: [PATCH] added GPIO

---
 .vscode/launch.json | 18 +++++++++++++
 .vscode/tasks.json  | 14 +++++++++-
 Cargo.toml          |  4 +++
 examples/gpio.rs    | 64 +++++++++++++++++++++++++++++++++++++++++++++
 examples/loop.rs    |  5 +---
 5 files changed, 100 insertions(+), 5 deletions(-)
 create mode 100644 examples/gpio.rs

diff --git a/.vscode/launch.json b/.vscode/launch.json
index d3c2e2a..8227339 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -58,5 +58,23 @@
             ],
             "cwd": "${workspaceRoot}"
         },
+        {
+            "type": "gdb",
+            "request": "attach",
+            "name": "gpio",
+            "gdbpath": "/usr/bin/arm-none-eabi-gdb",
+            "executable": "./target/thumbv7em-none-eabihf/debug/examples/gpio",
+            "target": ":3333",
+            "remote": true,
+            "autorun": [
+                "monitor reset init",
+                "monitor arm semihosting enable",
+                "monitor tpiu config internal /tmp/itm.log uart off 64000000",
+                "monitor itm port 0 on",
+                "load",
+                "monitor reset init"
+            ],
+            "cwd": "${workspaceRoot}"
+        },
     ]
 }
\ No newline at end of file
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index c825d3b..d2d5a46 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -30,7 +30,19 @@
         {
             "type": "shell",
             "label": "xargo build --example loop",
-            "command": "xargo build --example loop ",
+            "command": "xargo build --example loop",
+            "problemMatcher": [
+                "$rustc"
+            ],
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            }
+        },
+        {
+            "type": "shell",
+            "label": "xargo build --example gpio",
+            "command": "xargo build --example gpio",
             "problemMatcher": [
                 "$rustc"
             ],
diff --git a/Cargo.toml b/Cargo.toml
index c0f4057..22657d4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -30,6 +30,10 @@ git = "https://gitlab.henriktjader.com/pln/STM32F40x.git"
 features = ["rt"]
 version = "0.1.0"
 
+[dependencies.f4]
+git = "https://github.com/jsjolund/f4"
+version = "0.1.0"
+
 [features]
 wcet_bkpt = [] 
 wcet_nop = [] 
diff --git a/examples/gpio.rs b/examples/gpio.rs
new file mode 100644
index 0000000..eb398dc
--- /dev/null
+++ b/examples/gpio.rs
@@ -0,0 +1,64 @@
+//! Simple access to gpio
+#![deny(unsafe_code)]
+#![feature(proc_macro)]
+#![no_std]
+
+extern crate cortex_m;
+extern crate cortex_m_rtfm as rtfm;
+extern crate stm32f40x;
+
+use rtfm::app;
+
+app! {
+    device: stm32f40x,
+}
+
+fn wait(i: u32) {
+    for _ in 0..i {
+        cortex_m::asm::nop(); // no operation (cannot be optimized out)
+    }
+}
+
+// see the Reference Manual RM0368 (www.st.com/resource/en/reference_manual/dm00096844.pdf)
+// rcc,     chapter 6
+// gpio,    chapter 8
+
+fn init(p: init::Peripherals) {
+    // power on GPIOA, RM0368 6.3.11
+    p.RCC.ahb1enr.modify(|_, w| w.gpioaen().set_bit());
+
+    // configure PA5 as output, RM0368 8.4.1
+    p.GPIOA.moder.modify(|_, w| w.moder5().bits(1));
+
+    // loop {
+    //     // set PA5 high, RM0368 8.4.6
+    //     p.GPIOA.odr.modify(|_, w| w.odr5().bit(true));
+    //     wait(10_000);
+
+    //     // set PA5 low, RM0368 8.4.6
+    //     p.GPIOA.odr.modify(|_, w| w.odr5().bit(false));
+    //     wait(10_000);
+    // }
+
+    // rewrite the above code to have the GPIO as output
+    // and alter the data output through the BSRR register
+    // this is more efficient as the read register (in modify)
+    // is not needed.
+
+    loop {
+        // set PA5 high, RM0368 8.4.7
+        p.GPIOA.bsrr.write(|w| w.bs5().set_bit());
+        wait(10_000);
+
+        // set PA5 low, RM0368 8.4.7
+        p.GPIOA.bsrr.write(|w| w.br5().set_bit());
+        wait(10_000);
+    }
+}
+
+#[inline(never)]
+fn idle() -> ! {
+    loop {
+        rtfm::wfi();
+    }
+}
diff --git a/examples/loop.rs b/examples/loop.rs
index c98cd2d..30ea0f4 100644
--- a/examples/loop.rs
+++ b/examples/loop.rs
@@ -1,7 +1,4 @@
-//! 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.
+//! Simple loop to calculate the sum of integers 0..10
 #![deny(unsafe_code)]
 #![feature(proc_macro)]
 #![no_std]
-- 
GitLab