From 95e6b2aa6799d1102f19347f3a825d5410cdb85b Mon Sep 17 00:00:00 2001
From: Per <Per Lindgren>
Date: Wed, 14 Feb 2018 00:32:30 +0100
Subject: [PATCH] bare 7

---
 examples/bare7.rs | 62 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 40 insertions(+), 22 deletions(-)

diff --git a/examples/bare7.rs b/examples/bare7.rs
index 3abdf49..ecce878 100644
--- a/examples/bare7.rs
+++ b/examples/bare7.rs
@@ -1,6 +1,6 @@
 //! Serial interface loopback
 #![deny(unsafe_code)]
-//#![deny(warnings)]
+#![deny(warnings)]
 #![feature(proc_macro)]
 #![no_std]
 
@@ -18,7 +18,7 @@ use f4::prelude::*;
 use f4::Serial;
 use f4::time::Hertz;
 use heapless::Vec;
-use rtfm::{app, Threshold};
+use rtfm::app;
 
 // CONFIGURATION
 const BAUD_RATE: Hertz = Hertz(115_200);
@@ -42,17 +42,21 @@ fn init(p: init::Peripherals) {
 
     let mut buffer: Vec<u8, [u8; 4]> = Vec::new();
     loop {
-        if let Ok(byte) = block!(serial.read()) {
-            buffer.push(byte);
-            ipln!("Ok {:?}", buffer);
-            block!(serial.write(byte)).ok();
-        } else {
-            ipln!("Error");
+        match block!(serial.read()) {
+            Ok(byte) => {
+                let _ = buffer.push(byte);
+                ipln!("Ok {:?}", buffer);
+                block!(serial.write(byte)).ok();
+            }
+            Err(err) => {
+                ipln!("Error {:?}", err);
+                p.USART2.dr.read(); // clear the error by reading the data register
+            }
         }
     }
 }
 
-// IDLE LOOP
+// We will never reach `idle` since we burn the CPU arduino style :)
 fn idle() -> ! {
     // Sleep
     loop {
@@ -76,27 +80,41 @@ fn idle() -> ! {
 //
 // you should now be able to send data and recive an echo from the MCU
 //
-// try sending: "abcd"
+// try sending: "abcd" as a single sequence (set the option No end in moserial)
+// (don't send the quation marks, just abcd)
 //
-// what did you receive
+// what did you receive, and what was the output of the ITM trace
 // ** your answer here **
 //
-// try changing to baud rate 57600, both in the program and terminal settings,
-// recompile and run
+// now try sending 'a', 'b', 'c', 'd' character by character
+// (just send the characters not the single quotes and commas)
+// what did you receive, and what was the output of the ITM trace
+// ** your answer here **
 //
-// try sending: "abcd"
-// what did you receive
+// why did the transmission fail? (hint, think about timing...)
 // ** your answer here **
 //
-// commit your answers as (bare7_1)
+// commit your answers (bare7_1)
+//
+// 2. now stress the buffer lengt sending a sequence
+// 'a', 'b', 'c', 'd', 'e' character by character
+// what did you receive, and what was the output of the ITM trace
+// ** your answer here **
 //
-// 2. now you should make a buffer
-// static BUFFER [u8;8]
-// to store the incoming data
+// if done correctly you see an evedince of Rust's memory safety
+// the buffer will be saturated (all elements occupied)
+// but no buffer owerwrite will occur (outside the buffer)
 //
-// this can be done in a resource
-// app
+// your job now is to check the API of `heapless`
+// https://docs.rs/heapless/0.2.1/heapless/
 //
-// when the buffer is full you should write the complete buffer
+// and catch the case we are trying to write to a full buffer
+// and write a suiteble error message
 //
+// commit your answers (bare7_2)
 //
+// !!!!! NOTICE !!!!!
+// here we are not solving the underlying problem
+// we are just mitigating the effects
+// in bare8 we will se how to use interrupts for reliable
+// high-speed communictation
-- 
GitLab