From e7b3b44b7184cf18f96ccb39a528be7d974a7e2c Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Sat, 1 Oct 2022 00:02:07 +0800
Subject: [PATCH] getting there, don't have right feedback yet

---
 examples/rp/.cargo/config.toml    |   4 +-
 examples/rp/src/bin/usb_serial.rs | 154 +++++++++++-------------------
 2 files changed, 58 insertions(+), 100 deletions(-)

diff --git a/examples/rp/.cargo/config.toml b/examples/rp/.cargo/config.toml
index 3d605138..a0957848 100644
--- a/examples/rp/.cargo/config.toml
+++ b/examples/rp/.cargo/config.toml
@@ -1,5 +1,7 @@
 [target.'cfg(all(target_arch = "arm", target_os = "none"))']
-runner = "probe-run --chip RP2040"
+ runner = "probe-run --chip RP2040"
+#runner = "elf2uf2-rs -d"
+
 
 [build]
 target = "thumbv6m-none-eabi"        # Cortex-M0 and Cortex-M0+
diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs
index eb29b4e5..26be705d 100644
--- a/examples/rp/src/bin/usb_serial.rs
+++ b/examples/rp/src/bin/usb_serial.rs
@@ -28,54 +28,6 @@ async fn main(_spawner: Spawner) {
 
     let mut cp = cortex_m::peripheral::Peripherals::take().unwrap();
 
-    // Create the driver, from the HAL.
-    let irq = interrupt::take!(USBCTRL_IRQ);
-    let driver = Driver::new(p.USB, irq);
-
-    // Create embassy-usb Config
-    let mut config = Config::new(0xc0de, 0xcafe);
-    config.manufacturer = Some("Embassy");
-    config.product = Some("USB-serial example");
-    config.serial_number = Some("12345678");
-    config.max_power = 100;
-    config.max_packet_size_0 = MAX_CDC_PACKET;
-
-    // Required for windows compatiblity.
-    // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
-    config.device_class = 0xEF;
-    config.device_sub_class = 0x02;
-    config.device_protocol = 0x01;
-    config.composite_with_iads = true;
-
-    // Create embassy-usb DeviceBuilder using the driver and config.
-    // It needs some buffers for building the descriptors.
-    let mut device_descriptor = [0; 256];
-    let mut config_descriptor = [0; 256];
-    let mut bos_descriptor = [0; 256];
-    let mut control_buf = [0; 64];
-
-    let mut state = State::new();
-
-    let mut builder = Builder::new(
-        driver,
-        config,
-        &mut device_descriptor,
-        &mut config_descriptor,
-        &mut bos_descriptor,
-        &mut control_buf,
-        None,
-    );
-
-    // Create classes on the builder.
-    let mut class = CdcAcmClass::new(&mut builder, &mut state, MAX_CDC_PACKET as u16);
-
-    // Build the builder.
-    let mut usb = builder.build();
-
-    // Run the USB device.
-    let usb_fut = usb.run();
-    let _ = class.write_packet(b"top\r\n").await;
-
     let mut gpio6 = Flex::new(p.PIN_6);
 
     run(&mut gpio6, &mut cp.SYST).await;
@@ -134,39 +86,6 @@ impl core::fmt::Write for FmtBuf {
     }
 }
 
-async fn usb_echo<'d, P: Pin, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>,
-    pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), Disconnected> {
-    class.write_packet(b"hello\r\n").await?;
-
-    syst.set_reload(10_000_000-1);
-    syst.clear_current();
-    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))};
-
-    loop {
-        let (t1, t2) = critical_section::with(|_cs| {
-            syst.clear_current();
-
-            pin.set_pull(Pull::Down);
-            pin.set_as_output();
-            pin.set_high();
-            // // approx 1ms
-            // cortex_m::asm::delay(125_000);
-            cortex_m::asm::delay(600);
-            let t1 = SYST::get_current();
-            pin.set_as_input();
-            while pin.is_high() {}
-
-            let t2 = SYST::get_current();
-            (t1, t2)
-        });
-        let del = t1 - t2;
-        FmtBuf::write(class, format_args!("{del}\n")).await.map_err(|_| Disconnected{})?;
-    }
-}
-
 async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), Disconnected> {
     info!("hello");
 
@@ -175,26 +94,63 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
     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::ROSC.ctrl().modify(|s| s.set_enable(pac::rosc::vals::Enable::DISABLE)) };
+    unsafe{ pac::PADS_BANK0.gpio(6).modify(|s| s.set_schmitt(false)) };
+
+
+    // get it near the threshold
+    critical_section::with(|_cs| {
+        pin.set_as_output();
+        pin.set_high();
+        // // approx 1ms
+        // cortex_m::asm::delay(125_000);
+        cortex_m::asm::delay(1200);
+        pin.set_as_input();
+        pin.set_pull(Pull::Down);
+        while pin.is_high() {}
+    });
 
     loop {
-        let (t1, t2) = critical_section::with(|_cs| {
-            syst.clear_current();
-
-            pin.set_pull(Pull::Down);
-            pin.set_as_output();
-            pin.set_high();
-            // // approx 1ms
-            // cortex_m::asm::delay(125_000);
-            cortex_m::asm::delay(900);
-            let t1 = SYST::get_current();
-            pin.set_as_input();
-            while pin.is_high() {}
-
-            let t2 = SYST::get_current();
-            (t1, t2)
+        const LOOP: usize = 20;
+        let mut dels = [0u32; LOOP];
+        let mut ups = [0u32; LOOP];
+        let mut downs = [0u32; LOOP];
+        critical_section::with(|_cs| {
+
+            let mut down = 0;
+            let mut up = 0;
+            let mut last_up = 0;
+            let mut last_down = 0;
+
+            for i in 0..LOOP {
+                cortex_m::asm::delay(3*last_up);
+                pin.set_pull(Pull::Up);
+                syst.clear_current();
+                let t1 = SYST::get_current();
+                while pin.is_low() {}
+                let t2 = SYST::get_current();
+                let up = t1 - t2;
+                last_up = up;
+
+                cortex_m::asm::delay(3*last_down);
+                pin.set_pull(Pull::Down);
+                syst.clear_current();
+                let t1 = SYST::get_current();
+                while pin.is_high() {}
+                let t2 = SYST::get_current();
+                let down = t1 - t2;
+                last_down = down;
+
+                ups[i] = up;
+                downs[i] = down;
+                dels[i] = up + down;
+            }
+            pin.set_pull(Pull::None);
         });
-        let del = t1 - t2;
-        info!("{}", del);
+
+        for i in 0..LOOP {
+            info!("{}  U {}  D {}", dels[i], ups[i], downs[i]);
+        }
+        info!("===");
     }
 }
-- 
GitLab