From 249b8118aa51cfd8761e12fbfb7ae208870a8332 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:38:07 -0400 Subject: [PATCH] made the thing read the cards evan suggested that computers have two cores, which I had neglected up until now bitbanging is way easier than figuring out how to use pin interrupts in embassy, mA be damned --- outside/src/main.rs | 108 +++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 47 deletions(-) diff --git a/outside/src/main.rs b/outside/src/main.rs index f5a99ea..eb61ed4 100644 --- a/outside/src/main.rs +++ b/outside/src/main.rs @@ -18,7 +18,9 @@ use bt_hci::cmd::info; use critical_section::Mutex; use cyw43_pio::PioSpi; use embassy_futures::join::join; +use embassy_futures::yield_now; use embassy_rp::interrupt::typelevel::{Handler, Interrupt, IO_IRQ_BANK0}; +use embassy_rp::multicore::{spawn_core1, Stack}; use embassy_rp::pwm::{self, Pwm}; use fixed::FixedU16; use log::*; @@ -66,58 +68,42 @@ async fn net_task(mut runner: embassy_net::Runner<'static, cyw43::NetDriver<'sta runner.run().await } +static LAST_BIT: AtomicU8 = AtomicU8::new(0); +#[embassy_executor::task] +async fn print_task() -> ! { + let mut last_bit = 0; + loop { + Timer::after_millis(250).await; + let card = critical_section::with(|cs| { + READ_CARD.borrow(cs).clone().into_inner() + }); + let bit = BIT.load(core::sync::atomic::Ordering::SeqCst); + if bit == last_bit { + if card !=0 { + info!("thi ting: {card:#x}"); + } + critical_section::with(|cs| { + READ_CARD.replace(cs, 0); + }); + BIT.store(0, core::sync::atomic::Ordering::SeqCst); + } + last_bit = bit; + info!("ro: {card:#x}, bit: {bit}"); + } +} + #[panic_handler] fn panic( info: &PanicInfo) -> ! { error!("{}", info); loop { } } -#[embassy_executor::task] -async fn data_handler() { - info!("start"); - let (data_0, data_1) = critical_section::with(|cs| { - let borrow_ref_mut = DATA_IN.take(cs); - borrow_ref_mut.unzip() - }); - let mut data_0 = data_0.unwrap(); - let mut data_1 = data_1.unwrap(); - info!("gyatt"); - loop { - join(data_0.wait_for_falling_edge(), data_1.wait_for_falling_edge()).await; - info!("happen"); - - critical_section::with(|cs| { - let mut partial = READ_CARD.borrow_ref_mut(cs); - let bit = BIT.load(core::sync::atomic::Ordering::SeqCst); - - let borrow_ref_mut = DATA_IN.take(cs); - let (data_0, data_1) = borrow_ref_mut.as_ref().unwrap(); - - if data_1.is_low() { - *partial &= 1 << bit; - } - if data_0.is_low() { - *partial |= !1u64.rotate_left(bit as u32); - } - }); - } -} - #[embassy_executor::main] async fn main(spawner: Spawner) { - - let p = embassy_rp::init(Default::default()); let data_0= Input::new(p.PIN_21, embassy_rp::gpio::Pull::None); let data_1= Input::new(p.PIN_16, embassy_rp::gpio::Pull::None); - critical_section::with(|cs| { - DATA_IN.replace(cs, Some((data_0, data_1))); - }); - - //spawner.spawn(data_handler()).unwrap(); - - let mut rng = RoscRng; let driver = Driver::new(p.USB, Irqs); @@ -172,14 +158,42 @@ async fn main(spawner: Spawner) { let mut tx_buffer = [0; 4096]; let mut buf = [0; 4096]; - loop { + spawner.spawn(print_task()).unwrap(); + + let mut partial: u64 = 0; + let mut bit: u8 = 0; + let mut prev: (bool,bool) = (false,false); + + static mut CORE1_STACK: Stack<4096> = Stack::new(); + spawn_core1(p.CORE1, unsafe { &mut *core::ptr::addr_of_mut!(CORE1_STACK) }, move || { + loop { + let current = (data_0.is_low(), data_1.is_low()); + if current.1 && !prev.1 { + partial |= 1 << bit; + bit += 1; + } + if current.0 && !prev.0 { + partial &= !(1u64).rotate_left(bit as u32); + bit += 1; + } + prev=current; + + critical_section::with(|cs| { + let mut foreign_partial = READ_CARD.borrow_ref_mut(cs); + + // may or may not be sound, should work fine given no noise on the signal + if *foreign_partial != partial && !current.0 && !current.1 { + bit = 0; + partial = *foreign_partial; + } + + *foreign_partial = partial; + }); + + BIT.store(bit, core::sync::atomic::Ordering::SeqCst); + } + }); - let card = critical_section::with(|cs| { - READ_CARD.borrow_ref(cs).clone() - }); - //info!("datah: {}", card); - Timer::after_millis(100).await; - } loop { let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);