From 2c1d1b57f5049bb754c0051905a3fb1ba8086dbf Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Mon, 10 Oct 2022 23:06:36 +0800
Subject: [PATCH] more fiddling with cycles

---
 examples/hist.rs | 83 ++++++++++++++++++++++++++----------------------
 examples/rand.rs |  8 ++---
 src/cap.rs       |  2 +-
 src/rng.rs       | 17 +++++-----
 4 files changed, 59 insertions(+), 51 deletions(-)

diff --git a/examples/hist.rs b/examples/hist.rs
index da204f8..35a20f1 100644
--- a/examples/hist.rs
+++ b/examples/hist.rs
@@ -21,7 +21,7 @@ use embassy_executor::Spawner;
 #[embassy_executor::main]
 async fn main(_spawner: Spawner) {
     let p = embassy_rp::init(Default::default());
-    let mut cp = cortex_m::peripheral::Peripherals::take().unwrap();
+    // let mut cp = cortex_m::peripheral::Peripherals::take().unwrap();
 
     // 0.1uF
     // let mut gpio = (Flex::new(p.PIN_6), 6);
@@ -50,7 +50,7 @@ async fn main(_spawner: Spawner) {
 
     let mut hist = [0u32; 200];
 
-    let PRINT = 1000;
+    const PRINT: usize = 200000;
     // let PRINT = 50;
 
     // let mut gpios = [
@@ -63,45 +63,48 @@ async fn main(_spawner: Spawner) {
     //     p.PIN_25.degrade().into_ref(),
     // ];
 
-    // let mut gpios = [
-    //     // serial
-    //     // p.PIN_0.degrade().into_ref(),
-    //     // p.PIN_1.degrade().into_ref(),
-
-    //     p.PIN_2.degrade().into_ref(),
-    //     p.PIN_3.degrade().into_ref(),
-    //     p.PIN_4.degrade().into_ref(),
-    //     p.PIN_5.degrade().into_ref(),
-    //     p.PIN_6.degrade().into_ref(),
-    //     p.PIN_7.degrade().into_ref(),
-    //     p.PIN_8.degrade().into_ref(),
-    //     p.PIN_9.degrade().into_ref(),
-    //     p.PIN_10.degrade().into_ref(),
-    //     p.PIN_11.degrade().into_ref(),
-    //     p.PIN_12.degrade().into_ref(),
-    //     p.PIN_13.degrade().into_ref(),
-    //     p.PIN_14.degrade().into_ref(),
-    //     p.PIN_15.degrade().into_ref(),
-    //     p.PIN_16.degrade().into_ref(),
-    //     p.PIN_17.degrade().into_ref(),
-    //     p.PIN_18.degrade().into_ref(),
-    //     p.PIN_19.degrade().into_ref(),
-    //     p.PIN_20.degrade().into_ref(),
-    //     p.PIN_21.degrade().into_ref(),
-    //     p.PIN_22.degrade().into_ref(),
-    //     // wl_on
-    //     // p.PIN_23.degrade().into_ref(),
-    //     // p.PIN_24.degrade().into_ref(),
-    //     // wl_cs, gate of vsys adc mosfet
-    //     p.PIN_25.degrade().into_ref(),
-    //     p.PIN_26.degrade().into_ref(),
-    //     p.PIN_27.degrade().into_ref(),
-    //     p.PIN_28.degrade().into_ref(),
-    //     // p.PIN_29.degrade().into_ref(),
-    // ];
     let mut gpios = [
+        // serial
+        // p.PIN_0.degrade().into_ref(),
+        // p.PIN_1.degrade().into_ref(),
+
+        p.PIN_2.degrade().into_ref(),
+        p.PIN_3.degrade().into_ref(),
+        p.PIN_4.degrade().into_ref(),
+        p.PIN_5.degrade().into_ref(),
+        p.PIN_6.degrade().into_ref(),
+        p.PIN_7.degrade().into_ref(),
+        p.PIN_8.degrade().into_ref(),
+        p.PIN_9.degrade().into_ref(),
+        p.PIN_10.degrade().into_ref(),
+        p.PIN_11.degrade().into_ref(),
+        p.PIN_12.degrade().into_ref(),
+        p.PIN_13.degrade().into_ref(),
+        p.PIN_14.degrade().into_ref(),
+        p.PIN_15.degrade().into_ref(),
+        p.PIN_16.degrade().into_ref(),
+        p.PIN_17.degrade().into_ref(),
+        p.PIN_18.degrade().into_ref(),
+        p.PIN_19.degrade().into_ref(),
+        p.PIN_20.degrade().into_ref(),
+        p.PIN_21.degrade().into_ref(),
+        p.PIN_22.degrade().into_ref(),
+        // wl_on
+        // p.PIN_23.degrade().into_ref(),
+        // p.PIN_24.degrade().into_ref(),
+        // wl_cs, gate of vsys adc mosfet
         p.PIN_25.degrade().into_ref(),
+        p.PIN_26.degrade().into_ref(),
+        p.PIN_27.degrade().into_ref(),
+        p.PIN_28.degrade().into_ref(),
+        // p.PIN_29.degrade().into_ref(),
     ];
+    // let mut gpios = [
+    //     p.PIN_25.degrade().into_ref(),
+    // ];
+
+    const DUMPS: usize = (1<<17);
+    let mut dump  = [0u8; DUMPS];
 
     for gpio in gpios.iter_mut() {
         // for low_cycles in 1..10 {
@@ -111,6 +114,7 @@ async fn main(_spawner: Spawner) {
             caprand::noise(gpio.reborrow(), low_cycles,
                 |v| {
                     // info!("{}", v);
+                    dump[n % DUMPS] = v as u8;
 
                     let vu = v as usize;
                     hist[vu.min(hist.len()-1)] += 1;
@@ -129,6 +133,9 @@ async fn main(_spawner: Spawner) {
         // }
     }
 
+    info!("done");
+    loop {}
+
     // for gpio in gpios.iter_mut() {
     //     let lt = caprand::best_low_time(gpio.reborrow(), 30).unwrap();
     //     info!("gpio {} lt {}", gpio.pin(), lt);
diff --git a/examples/rand.rs b/examples/rand.rs
index f5698a4..4cd3358 100644
--- a/examples/rand.rs
+++ b/examples/rand.rs
@@ -10,7 +10,8 @@ use defmt::{debug, info, warn, panic, error};
 #[cfg(feature = "defmt")]
 use {defmt_rtt as _, panic_probe as _};
 
-use embassy_rp::gpio::Flex;
+use embassy_rp::gpio::Pin;
+use embassy_rp::Peripheral;
 use embassy_executor::Spawner;
 use embassy_time::{Timer, Duration};
 
@@ -19,11 +20,10 @@ use getrandom::register_custom_getrandom;
 #[embassy_executor::main]
 async fn main(_spawner: Spawner) {
     let p = embassy_rp::init(Default::default());
-    let mut cp = cortex_m::peripheral::Peripherals::take().unwrap();
 
-    let mut gpio = (Flex::new(p.PIN_10), 10);
+    let gpio = p.PIN_25.degrade().into_ref();
 
-    caprand::setup(&mut gpio.0, gpio.1, &mut cp.SYST).unwrap();
+    caprand::setup(gpio).unwrap();
 
     register_custom_getrandom!(caprand::random);
 
diff --git a/src/cap.rs b/src/cap.rs
index 5523f13..293bea5 100644
--- a/src/cap.rs
+++ b/src/cap.rs
@@ -307,7 +307,7 @@ fn time_rise(pin: PeripheralRef<AnyPin>, low_cycles: u32) -> Result<u32, ()> {
         | (x3 & mask).rotate_left(3)
         | (x4 & mask).rotate_left(4)
         | (x5 & mask).rotate_left(5);
-    let result = result >> pin_num;
+    let result = result.rotate_right(pin_num as u32);
 
     Ok(result)
 }
diff --git a/src/rng.rs b/src/rng.rs
index 4c82d9e..7a74adf 100644
--- a/src/rng.rs
+++ b/src/rng.rs
@@ -1,8 +1,8 @@
 #[cfg(not(feature = "defmt"))]
-use log::{debug, info, warn, error};
+use log::{debug, info, warn, error, trace};
 
 #[cfg(feature = "defmt")]
-use defmt::{debug, info, warn, panic, error};
+use defmt::{debug, info, warn, panic, error, trace};
 
 use core::cell::RefCell;
 use core::num::NonZeroU32;
@@ -49,9 +49,8 @@ pub fn random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
 /// `syst` will be modified.
 pub fn setup(
     pin: PeripheralRef<AnyPin>,
-    syst: &mut SYST,
 ) -> Result<(), getrandom::Error> {
-    let r = CapRng::new(pin, syst)?;
+    let r = CapRng::new(pin)?;
 
     critical_section::with(|cs| {
         let mut rng = RNG.borrow_ref_mut(cs);
@@ -66,17 +65,19 @@ struct CapRng(ChaCha20Rng);
 
 impl CapRng {
     // const SEED_SAMPLES: usize = 1024;
-    const SEED_SAMPLES: usize = 50000;
+    const SEED_SAMPLES: usize = 1024 * 100;
 
     /// Call this at early startup. If noisy interrupts or time slicing is happening the caller
     /// should disable interrupts.
     /// `syst` will be modified.
-    fn new(pin: PeripheralRef<AnyPin>,
-        syst: &mut SYST,
+    fn new(mut pin: PeripheralRef<AnyPin>,
     ) -> Result<Self, getrandom::Error> {
+        let low_cycles = crate::cap::best_low_time(pin.reborrow(), 100).unwrap();
+        trace!("low_cycles {}", low_cycles);
+
         let mut h = Sha256::new();
         let mut count = 0;
-        crate::cap::noise(pin, 0, |v| {
+        crate::cap::noise(pin, low_cycles, |v| {
             h.update(v.to_be_bytes());
             count += 1;
             count < Self::SEED_SAMPLES
-- 
GitLab