From 141f97911ad3ac63b0f1f53eb55ad87e78c12ebf Mon Sep 17 00:00:00 2001
From: DevDoggo <devdoggo@protonmail.com>
Date: Wed, 23 May 2018 23:14:38 +0200
Subject: [PATCH] Made LED blink with Modify

---
 examples/bare5.rs | 48 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 9 deletions(-)

diff --git a/examples/bare5.rs b/examples/bare5.rs
index e8f9110..3f79194 100644
--- a/examples/bare5.rs
+++ b/examples/bare5.rs
@@ -7,6 +7,8 @@
 
 extern crate cortex_m;
 extern crate cortex_m_rt;
+
+#[macro_use]
 extern crate cortex_m_debug;
 
 
@@ -135,6 +137,7 @@ fn main() {
 pub const G_BASE: u32 = 0x40000000 + 0x00020000;
 pub const GPIOA_MODER: u32 = G_BASE;
 pub const GPIOA_BSRR: u32 = G_BASE + 0x18;
+pub const GPIOA_ODR: u32 = G_BASE + 0x14;
 
 fn w_rite(addr: u32, val: u32) { 
     unsafe { 
@@ -144,7 +147,7 @@ fn w_rite(addr: u32, val: u32) {
 
 fn r_ead(addr: u32) -> u32 {
     unsafe {
-        core::ptr::read_volatile(addr as *const _)
+        core::ptr::read_volatile(addr as *const u32)
     }
 }
 
@@ -158,8 +161,19 @@ fn r_ead(addr: u32) -> u32 {
 // commit your answers (bare5_2)
 
 fn m_odify(addr: u32, offset: u32, width: u32, val: u32) {
-    //something like this, it's a start
-    addr = addr | (val << offset);
+    
+    let r = r_ead(addr); //- 0x4); //ODR registry commented out
+    let v: u32 = (!(1 << offset) as u16) as u32;
+    sprintln!("r::ODR: {:#b}", r);
+    sprintln!("v: {:#b}", v);
+    sprintln!("r&v: {:#b}", (r & v));
+
+    //Experimental XOR to heck up 0s
+    //let xor = 
+
+    let out = (r & v) | (val << offset);
+    sprintln!("out: {:#b}", out);
+    w_rite(addr, out);
 }
 
 
@@ -178,14 +192,30 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
     // this is more efficient as the read register is not needed.
 
     loop {
+        w_rite(GPIOA_BSRR, 1 << 3);
+
+        //w_rite(GPIOA_BSRR, 1 << 5);
+        //wait(10_000);
+        //w_rite(GPIOA_BSRR, 1 << (5 + 16));
+        //wait(10_000);
+        //w_rite(GPIOA_BSRR, 1 << 5);
+        //wait(10_000);
+       
+        // As it turns out, GPIOA_BSRR is a Write-Only registry
+        // Use ODR, which is a read/write registry
+        // let x = r_ead(GPIOA_ODR);
+        // sprintln!("ODR: {:#b}", x);
 
-
-        w_rite(GPIOA_BSRR, 1 << 5);
-        wait(10_000);
-        // sprintln!("BOYE");
-        // ipln!("Boye!");
         
-        w_rite(GPIOA_BSRR, 1 << (5 + 16));
+        //cortex_m::asm::bkpt();
+        // Turn LED on
+        sprintln!("\n--- Turn On!");
+        m_odify(GPIOA_ODR, 5, 1, 0b1);
+        wait(10_000);
+
+        // Turn LED off
+        sprintln!("\n--- Turn Off!");
+        m_odify(GPIOA_ODR, 5, 1, 0b0);
         wait(10_000);
 
 
-- 
GitLab