From 0cbae03625177078559c5f59fd643b961ceb1d19 Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Sun, 2 Oct 2022 13:17:02 +0800
Subject: [PATCH] Add imm, fix t12 vs t34

---
 examples/rp/src/bin/usb_serial.rs | 58 ++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 16 deletions(-)

diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs
index 6e7499ae..01906498 100644
--- a/examples/rp/src/bin/usb_serial.rs
+++ b/examples/rp/src/bin/usb_serial.rs
@@ -76,8 +76,8 @@ async fn main(_spawner: Spawner) {
     let usb_fut = usb.run();
 
     // XXX: Can we get the number from the pin?
-    let mut gpio = (Flex::new(p.PIN_6), 6);
-    // let mut gpio = (Flex::new(p.PIN_10), 10);
+    // let mut gpio = (Flex::new(p.PIN_6), 6);
+    let mut gpio = (Flex::new(p.PIN_10), 10);
     // let mut gpio = (Flex::new(p.PIN_13), 13);
 
     let usb = false;
@@ -98,6 +98,7 @@ async fn main(_spawner: Spawner) {
         join(usb_fut, echo_fut).await;
     } else {
         run(&mut gpio.0, gpio.1, &mut cp.SYST);
+        // run2()
     }
 }
 
@@ -144,6 +145,16 @@ async fn usb_run<'d, T: Instance + 'd, P: Pin>(pin: &mut Flex<'d, P>, pin_num: u
     todo!("impl me")
 }
 
+fn run2() {
+    loop {
+        for i in 100_000..1_000_000 {
+            info!("{}", i);
+            cortex_m::asm::delay(i);
+        }
+    }
+
+}
+
 fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Result<(), Disconnected> {
     info!("hello");
 
@@ -152,9 +163,12 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
     syst.set_clock_source(cortex_m::peripheral::syst::SystClkSource::Core);
     syst.enable_counter();
 
-    unsafe{ pac::ROSC.ctrl().modify(|s| s.set_enable(pac::rosc::vals::Enable::DISABLE)) };
-    unsafe{ pac::PADS_BANK0.gpio(pin_num).modify(|s| s.set_schmitt(false)) };
+    // Seemed to reduce noise (for characterising capacitor jitter).
+    // XXX Somehow disabling ROSC breaks SWD, perhaps signal integrity issues?
+    // unsafe{ pac::ROSC.ctrl().modify(|s| s.set_enable(pac::rosc::vals::Enable::DISABLE)) };
 
+    // TODO: test with/without schmitt disable
+    unsafe{ pac::PADS_BANK0.gpio(pin_num).modify(|s| s.set_schmitt(false)) };
 
     // get it near the threshold
     let del = critical_section::with(|_cs| {
@@ -245,8 +259,8 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
         // const UPRANGE: u32 = 50000;
         // const LOWRANGE: u32 = 15000;
         // nice integers
-        const LOW_OVER: u32 = 15000;
-        const HIGH_OVER: u32 = LOW_OVER + 8192;
+        const LOW_OVER: u32 = 1000;
+        const HIGH_OVER: u32 = LOW_OVER + 16384;
 
         // for debug printout
         // BUF iterations from the start (or end?) will be printed. must be <LOOP
@@ -288,26 +302,38 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
                 pin.set_pull(Pull::Up);
                 syst.clear_current();
                 let t1 = SYST::get_current();
+                let imm = pin.is_high();
                 while pin.is_low() {}
                 let t2 = SYST::get_current();
                 let t_up = t1 - t2;
                 if i % 2 == 0 {
                     down_x = step(down_x, t_up);
                 } else {
-                    // TODO: keep `t_up` as our random output
+                    if i > 4 {
+                        info!("t_up {} {} from down_x {}", t_up, imm, down_x);
+                        // TODO: keep `t_up` as our random output
+                    }
+                    // only for nodouble testing
+                    // down_x = step(down_x, t_up);
                 }
                 cortex_m::asm::delay(up_x);
 
                 pin.set_pull(Pull::Down);
                 syst.clear_current();
                 let t3 = SYST::get_current();
+                let imm = pin.is_low();
                 while pin.is_high() {}
                 let t4 = SYST::get_current();
-                let t_down = t1 - t2;
+                let t_down = t3 - t4;
                 if i % 2 == 0 {
                     up_x = step(up_x, t_down);
                 } else {
-                    // TODO: keep `t_down` as our random output
+                    if i > 4 {
+                        info!("t_down {} {} from up_x {}", t_down, imm, up_x);
+                        // TODO: keep t_down
+                    }
+                    // only for nodouble testing
+                    // up_x = step(up_x, t_down);
                 }
                 cortex_m::asm::delay(down_x);
 
@@ -325,12 +351,12 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
             pin.set_pull(Pull::None);
         });
 
-        for i in 0..BUF-1 {
-            if i % 2 == 1 {
-                info!("U {},{} x {}  D {},{} x {} use", ups[i], ups[i+1], upx[i], downs[i], downs[i+1], downx[i]);
-            } else {
-            }
-        }
-        info!("===");
+        // for i in 0..BUF-1 {
+        //     if i % 2 == 1 {
+        //         info!("U {},{} x {}  D {},{} x {} use", ups[i], ups[i+1], upx[i], downs[i], downs[i+1], downx[i]);
+        //     } else {
+        //     }
+        // }
+        // info!("===");
     }
 }
-- 
GitLab