From b53dd752258b14a2449681e4c572d83dafb805ca Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Sat, 1 Oct 2022 19:34:46 +0800
Subject: [PATCH] finished ascii art

---
 examples/rp/src/bin/usb_serial.rs | 51 ++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 17 deletions(-)

diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs
index 17dc04f4..c976bbec 100644
--- a/examples/rp/src/bin/usb_serial.rs
+++ b/examples/rp/src/bin/usb_serial.rs
@@ -107,25 +107,42 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
         cortex_m::asm::delay(1200);
         pin.set_as_input();
         pin.set_pull(Pull::Down);
+        // Get it near the threshold to begin.
         while pin.is_high() {}
     });
 
     let mut up_x = 1u32;
     let mut down_x = 1u32;
-
     /*
-   |prev    |       |
-   |iter    |       |
-   |        |       |
-            .       |
-          .   .     |                     .
-        .       .   |                   .   .
-      .           . |                 .       .
+                                           _ still pullup, =up_x
+           t3      t4       t1      t2    /
+   |prev    |pulldn |still  | pullup|    /
+   |iter    |active |pulldn,| active|   / | pulldn active, etc
+   |        |=t_down|=down_x| =t_up |  o  | measuring next t_down, etc...
+   |        .       |       |       |     |
+          .   .     |       |       |     .
+        .       .   |       |       |   .   .
+      .           . |       |       | .       .
     .---------------.---------------.-----------.------ GPIO threshold. Probably isn't really flat.
                       .           .               .
                         .       .
                           .   .
                             .
+
+    The GPIO pin is attached to a capacitor to ground. 0.1uF used for experiment.
+
+    The GPIO pin is left as input and is driven low/high with pulldown/pullup
+    resistors. The time taken to reach logical low/high is measured. The pulldown/pullup
+    resistor is left on for a further time down_x/up_x, which intends to drive
+    the voltage further so that a useful time can be measured for the next t_up/t_down
+    cycle.
+
+    The down_x/up_x overshoot cycle times are doubled each iteration and add in the measured time,
+    amplifying noise captured in t_down/t_up measurements.
+
+    down_x/up_x time is kept within a sensible range with modulo.
+
+    The random output is t_down and t_up time.
     */
 
     loop {
@@ -162,15 +179,15 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
         // It calculates the next overshoot #cycles.
         // `prev` is the #cycles returned from last step(), `meas` is the time
         // taken for the return to centre value (which is targetting `T`).
-        // eg `up_x = step(up_x, down);`
+        // eg `up_x = step(up_x, t_down);`
         let step = |prev: u32, meas: u32| {
-            // doubling step.
+            // Doubling step.
             // x[n] = 2*x[n-1] + meas
             let v = prev*2 + meas;
 
             // modulo to sensible range
-            if v > UPRANGE {
-                LOWRANGE + (v % (UPRANGE - LOWRANGE))
+            if v > HIGH_OVER {
+                LOW_OVER + (v % (HIGH_OVER - LOW_OVER))
             } else {
                 v
             }
@@ -192,24 +209,24 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
                 if i % 2 == 0 {
                     down_x = step(down_x, t_up);
                 } else {
-                    // TODO: keep `up` as our random output
+                    // TODO: keep `t_up` as our random output
                 }
                 cortex_m::asm::delay(up_x);
 
                 pin.set_pull(Pull::Down);
                 syst.clear_current();
-                let t1 = SYST::get_current();
+                let t3 = SYST::get_current();
                 while pin.is_high() {}
-                let t2 = SYST::get_current();
+                let t4 = SYST::get_current();
                 let t_down = t1 - t2;
                 if i % 2 == 0 {
                     up_x = step(up_x, t_down);
                 } else {
-                    // TODO: keep `down` as our random output
+                    // TODO: keep `t_down` as our random output
                 }
                 cortex_m::asm::delay(down_x);
 
-                if (BUF_START..BUF_START+BUF).contains(i) {
+                if (BUF_START..BUF_START+BUF).contains(&i) {
                     let n = i - BUF_START;
                     ups[n] = t_up;
                     upx[n] = up_x;
-- 
GitLab