From e954f30eac9cf0a05a8adaf39a5015ce468e6d9f Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Mon, 12 Feb 2018 10:12:08 +0100
Subject: [PATCH] bare 5

---
 examples/bare5.rs | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/examples/bare5.rs b/examples/bare5.rs
index f3e6308..8d5b122 100644
--- a/examples/bare5.rs
+++ b/examples/bare5.rs
@@ -128,24 +128,25 @@ fn main() {
 
 // user application
 fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
+    let rcc_copy = &rcc;
     // power on GPIOA
-    let r = (*rcc).AHB1ENR.read(); // read
-    (*rcc).AHB1ENR.write(r | 1 << (0)); // set enable
+    let r = rcc.AHB1ENR.read(); // read
+    rcc.AHB1ENR.write(r | 1 << (0)); // set enable
 
     // configure PA5 as output
-    let r = (*gpioa).MODER.read() & !(0b11 << (5 * 2)); // read and mask
-    (*gpioa).MODER.write(r | 0b01 << (5 * 2)); // set output mode
+    let r = gpioa.MODER.read() & !(0b11 << (5 * 2)); // read and mask
+    gpioa.MODER.write(r | 0b01 << (5 * 2)); // set output mode
 
     // and alter the data output through the BSRR register
     // this is more efficient as the read register is not needed.
 
     loop {
         // set PA5 high
-        (*gpioa).BSRRH.write(1 << 5); // set bit, output hight (turn on led)
+        gpioa.BSRRH.write(1 << 5); // set bit, output hight (turn on led)
         wait(10_000);
 
         // set PA5 low
-        (*gpioa).BSRRL.write(1 << 5); // clear bit, output low (turn off led)
+        gpioa.BSRRL.write(1 << 5); // clear bit, output low (turn off led)
         wait(10_000);
     }
 }
@@ -157,8 +158,22 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
 // provided by ST (and other companies). Actually, the file presesnted here is mostly a
 // cut/paste/replace of the stm32f40x.h, just Rustified.
 //
-// Here all peripheral access is unsafe, (as we are dereferencing raw pointers). In the code
-// we have fairly large unsafe blocks. Your task here is to
+// I this case we pass mutable pointer to the `idle`.
+//
+// Rewrite the accesses in the loop to use the data registerf directly (and make a read/modify/write).
+//
+// Run and see that the program behaves the same.
+//
+// commit your answers (bare5_1)
+//
+// 2.
+// Extend the read/write API with a modify, taking the address, field offsbet, field width, and value.
+//
+// Change the code into using your new API.
+//
+// Run and see that the program behaves the same.
+//
+// commit your answers (bare5_2)
 
 // As we are not using interrupts, we just register a dummy catch all handler
 #[link_section = ".vector_table.interrupts"]
-- 
GitLab