From 7cef3f636e51a46bfc9bdfdad6004d065593d990 Mon Sep 17 00:00:00 2001 From: Matt Johnston <matt@ucc.asn.au> Date: Sun, 9 Oct 2022 10:18:22 +0800 Subject: [PATCH] fix setting function without Flex --- examples/noise.rs | 75 ++++++++++++++++++++++--------------------- src/cap.rs | 81 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 93 insertions(+), 63 deletions(-) diff --git a/examples/noise.rs b/examples/noise.rs index c089326..f3d2eb8 100644 --- a/examples/noise.rs +++ b/examples/noise.rs @@ -49,56 +49,57 @@ async fn main(_spawner: Spawner) { // let mut gpio = p.PIN_20.into(); let mut hist = [0u32; 200]; - let mut n = 0; - let PRINT = 50000; + let PRINT = 1000; // let PRINT = 50; + // let mut gpios = [ + // p.PIN_22.degrade().into_ref(), + // p.PIN_6.degrade().into_ref(), + // p.PIN_10.degrade().into_ref(), + // p.PIN_13.degrade().into_ref(), + // // p.PIN_23.degrade().into_ref(), + // // p.PIN_24.degrade().into_ref(), + // 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(), // p.PIN_23.degrade().into_ref(), // p.PIN_24.degrade().into_ref(), 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_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(), - // // p.PIN_23.degrade().into_ref(), - // // p.PIN_24.degrade().into_ref(), - // 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(), - // ]; - for gpio in gpios.iter_mut() { let pin = gpio.pin(); + let mut n = 0; caprand::noise(gpio.reborrow(), &mut cp.SYST, |v, overshoot| { // info!("{}", v); @@ -114,10 +115,8 @@ async fn main(_spawner: Spawner) { *h = 0; } } - false - } else { - true } + n < PRINT }).unwrap(); } diff --git a/src/cap.rs b/src/cap.rs index 07cfcb1..823ae68 100644 --- a/src/cap.rs +++ b/src/cap.rs @@ -106,28 +106,37 @@ impl<'t> SyTi<'t> { // Returns (ticks: u32, precise: bool) fn time_rise(pin: PeripheralRef<AnyPin>, wait: &mut u32, rpos: &mut u32, syst: &mut SYST) -> Result<(u32, bool), ()> { - let pin_num = pin.pin(); - // bank 0 single cycle IO in - let gpio_in = pac::SIO.gpio_in(0).ptr(); + let pin_num = pin.pin() as usize; let mask = 1u32 << pin_num; + // bank 0 single cycle IO in + let gpio_in = pac::SIO.gpio_in(0).ptr(); let so = pac::SIO.gpio_out(0); let soe = pac::SIO.gpio_oe(0); unsafe { - so.value_clr().write_value(1<<pin_num); - soe.value_set().write_value(1<<pin_num); + so.value_clr().write_value(1 << pin_num); + soe.value_set().write_value(1 << pin_num); } - // unsafe { - // asm!( - // "nop", - // ) - // } - // cortex_m::asm::delay(200); - cortex_m::asm::delay(1000); + // // unsafe { + // // asm!( + // // "nop", + // // ) + // // } + // cortex_m::asm::delay(20); + // cortex_m::asm::delay(1000); unsafe { - soe.value_clr().write_value(1<<pin_num); + soe.value_clr().write_value(1 << pin_num); } + unsafe { + pac::PADS_BANK0 + .gpio(pin_num) + .modify(|s| { + // Pullup + s.set_pue(true); + }); + }; + // for testing with logic analyzer // let mut out = Flex::new(unsafe { embassy_rp::peripherals::PIN_16::steal() }); // out.set_as_output(); @@ -145,8 +154,7 @@ fn time_rise(pin: PeripheralRef<AnyPin>, wait: &mut u32, rpos: &mut u32, syst: & let x3: u32; let x4: u32; let x5: u32; - let mut gpio = Flex::<AnyPin>::new(pin); - gpio.set_pull(Pull::Up); + unsafe { asm!( // save @@ -209,6 +217,15 @@ fn time_rise(pin: PeripheralRef<AnyPin>, wait: &mut u32, rpos: &mut u32, syst: & *wait = (*wait+1).max(10000); } + unsafe { + pac::PADS_BANK0 + .gpio(pin_num) + .modify(|s| { + // No pullup + s.set_pue(false); + }); + }; + *rpos = pos; Ok((tick, precise)) } @@ -233,13 +250,6 @@ where // XXX Somehow disabling ROSC breaks SWD, perhaps signal integrity issues? // unsafe{ pac::ROSC.ctrl().modify(|s| s.set_enable(pac::rosc::vals::Enable::DISABLE)) }; - // Disabling the Schmitt Trigger gives a clearer correlation between "overshoot" - // and measured values. - unsafe { - pac::PADS_BANK0 - .gpio(pin_num) - .modify(|s| s.set_schmitt(false)) - }; let mut gpio = Flex::<AnyPin>::new(pin.reborrow()); // Measure pullup time from 0 as a sanity check @@ -256,13 +266,14 @@ where t.done() })?; drop(gpio); - // info!("Initial pullup del is {}", del); + info!("Initial pullup del is {}", del); if del < MIN_CAPACITOR_DEL { error!("Capacitor seems small or missing?"); return Err(()) } + // The main loop let mut overshoot = 1u32; @@ -270,6 +281,26 @@ where let mut wait = 0; let mut pos = 0; + unsafe { + pac::PADS_BANK0 + .gpio(pin_num) + .modify(|s| { + // Disabling the Schmitt Trigger gives a clearer correlation between "overshoot" + // and measured values. + s.set_schmitt(false); + // Input enable + s.set_ie(true); + }); + + // Use SIO + pac::IO_BANK0 + .gpio(pin_num) + .ctrl() + .modify(|s| { + s.set_funcsel(pac::io::vals::Gpio0ctrlFuncsel::SIO_0.0) + }); + } + // After warmup we sample twice at each "overshoot" value. // One sample is returned as random output, the other is mixed // in to the overshoot value. @@ -293,8 +324,8 @@ where let (r, precise) = time_rise(pin.reborrow(), &mut wait, &mut pos, syst)?; - let mut gpio = Flex::<AnyPin>::new(pin.reborrow()); - gpio.set_pull(Pull::None); + // let mut gpio = Flex::<AnyPin>::new(pin.reborrow()); + // gpio.set_pull(Pull::None); // if (r > ) -- GitLab