diff --git a/serial.conf b/serial.conf index 39384db..a3a870b 100644 --- a/serial.conf +++ b/serial.conf @@ -1,5 +1,5 @@ -vendor_id: 0xc0de -product_id: 0xcafe +vendor_id: 0x16c0 +product_id: 0x27dd manufacturer: "MeMyselfAndI" product: "Pico Serial" serial_number: "13371337" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 628a8c3..8ce858b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #![no_main] #![feature(type_alias_impl_trait)] #![feature(never_type)] +#![feature(async_fn_in_trait)] #[cfg(not(feature = "serial"))] mod mouse_hid; @@ -18,6 +19,8 @@ use embassy_rp::{ spi::{CsPin, Instance}, }; +use embassy_time::{Duration, Timer}; +use mouse_sensor::PAW3212DB_TJDT; #[cfg(not(feature = "serial"))] use usbd_hid::descriptor::MouseReport; @@ -27,6 +30,8 @@ use crate::{mouse_hid::mouse_config::MouseConfig, mouse_hid::MouseHID}; #[cfg(feature = "serial")] use crate::usb_serial::{Serial, SerialConfig}; +use crate::usb_serial::Sender; + use crate::mouse_sensor::{MouseSensor, MouseSensorPins}; use {defmt_rtt as _, panic_probe as _}; @@ -35,18 +40,70 @@ use {defmt_rtt as _, panic_probe as _}; async fn main(_spawner: Spawner) { let p = embassy_rp::init(Default::default()); - let mouse_sensor = MouseSensor::new( - p.PIN_25, - MouseSensorPins::new(p.SPI0, p.PIN_2, p.PIN_3, p.PIN_4, p.PIN_5), - |_| (), - ) - .await; + // let mouse_sensor = MouseSensor::new( + // p.PIN_25, + // MouseSensorPins::new(p.SPI0, p.PIN_2, p.PIN_3, p.PIN_4, p.PIN_5), + // |_| (), + // ) + // .await; - #[cfg(not(feature = "serial"))] - mouse_hid(p.USB, mouse_sensor).await; + // #[cfg(not(feature = "serial"))] + // mouse_hid(p.USB, mouse_sensor).await; + + // #[cfg(feature = "serial")] + // serial_hid(p.USB, mouse_sensor).await; #[cfg(feature = "serial")] - serial_hid(p.USB, mouse_sensor).await; + { + let serial = Serial::new(p.USB, SerialConfig::new()); + + let usb_future = Serial::run_usb().await; + + // let mut mouse_sensor = MouseSensor::new( + // p.PIN_25, + // MouseSensorPins::new(p.SPI0, p.PIN_2, p.PIN_3, p.PIN_4, p.PIN_5), + // serial, + // ) + // .await; + + let sensor_pins = MouseSensorPins::new(p.SPI0, p.PIN_2, p.PIN_3, p.PIN_4, p.PIN_5); + + let mut spi_device = PAW3212DB_TJDT::new( + sensor_pins.spi, + sensor_pins.clk, + sensor_pins.mosi, + sensor_pins.miso, + sensor_pins.cs, + serial, + ) + .await; + + // Do stuff with the class! + let hid_future = async { + loop { + // if let Some((x, y)) = mouse_sensor.read_movement().await { + // // let report = MouseReport { + // // buttons: 0, + // // x, + // // y, + // // wheel: 0, + // // pan: 0, + // // }; + + // // serial.send_number(x, 10).await; + // // serial.send_number(y, 10).await; + // } + + let id_1 = spi_device.product_id_1().await; + spi_device.sender.send_msg("read pid1:\n").await; + spi_device.sender.send_number(id_1, 10).await; + spi_device.sender.send_msg("\n").await; + Timer::after(Duration::from_secs(1)).await; + } + }; + + join(hid_future, usb_future).await; + } } #[cfg(not(feature = "serial"))] @@ -82,9 +139,9 @@ where } #[cfg(feature = "serial")] -async fn serial_hid<'d, L, T, CSP>(usb: USB, mut mouse_sensor: MouseSensor<'d, L, T, CSP>) +async fn serial_hid<'d, S, T, CSP>(usb: USB, mut mouse_sensor: MouseSensor<'d, S, T, CSP>) where - L: FnMut(&str) + 'd, + S: Sender, T: Instance, CSP: CsPin + 'd, { diff --git a/src/mouse_sensor/mod.rs b/src/mouse_sensor/mod.rs index 12a4488..8e9b54c 100644 --- a/src/mouse_sensor/mod.rs +++ b/src/mouse_sensor/mod.rs @@ -1,4 +1,5 @@ mod paw3212db_tjdt; mod wrapper; +pub use paw3212db_tjdt::PAW3212DB_TJDT; pub use wrapper::{MouseSensor, MouseSensorPins}; diff --git a/src/mouse_sensor/paw3212db_tjdt.rs b/src/mouse_sensor/paw3212db_tjdt.rs index 74f870f..705035a 100644 --- a/src/mouse_sensor/paw3212db_tjdt.rs +++ b/src/mouse_sensor/paw3212db_tjdt.rs @@ -9,22 +9,24 @@ use embassy_rp::{ }; use embassy_time::{Duration, Timer}; +use crate::usb_serial::Sender; + #[allow(non_camel_case_types)] -pub struct PAW3212DB_TJDT<'a, L, CS, T> +pub struct PAW3212DB_TJDT<'a, S, CS, T> where - L: FnMut(&str), + S: Sender, CS: CsPin + 'a, T: SpiInstance, { cs: Output<'a, CS>, spi: Spi<'a, T, Blocking>, - pub log: L, + pub sender: S, } -impl<'a, L, CS, T> PAW3212DB_TJDT<'a, L, CS, T> +impl<'a, S, CS, T> PAW3212DB_TJDT<'a, S, CS, T> where - L: FnMut(&str), + S: Sender, CS: CsPin + 'a, T: SpiInstance, { @@ -57,10 +59,10 @@ where mosi: impl Peripheral

+ 'a> + 'a, miso: impl Peripheral

+ 'a> + 'a, cs: impl Peripheral

+ 'a, - log: L, - ) -> PAW3212DB_TJDT<'a, L, CS, T> { + sender: S, + ) -> PAW3212DB_TJDT<'a, S, CS, T> { let mut spi_config = Config::default(); - spi_config.frequency = 2_000_000; + spi_config.frequency = 1_000_000; spi_config.polarity = Polarity::IdleHigh; let spi = Spi::new_blocking(spi, clk, mosi, miso, spi_config); @@ -73,66 +75,58 @@ where cs.set_high(); - Self { cs, spi, log } + Self { cs, spi, sender } } - async fn access(&mut self, f: F) -> R - where - F: Fn(&mut Self) -> R, - { + async fn read(&mut self, address: u8) -> u8 { self.cs.set_low(); Timer::after(Duration::from_micros(1)).await; - let res = f(self); + if self.spi.blocking_write(&[address & Self::READ]).is_err() { + self.sender.send_msg("send address (read) failed").await; + } + + if self.spi.blocking_write(&[0]).is_err() { + self.sender.send_msg("send address (read) failed").await; + } + + let mut buf = [0]; + + if self.spi.blocking_read(&mut buf).is_err() { + self.sender.send_msg("read failed").await; + return 0; + } + + self.sender.send_number(buf[0], 10).await; + + let r = buf[0]; self.cs.set_high(); Timer::after(Duration::from_micros(2)).await; - res - } - - async fn read(&mut self, address: u8) -> u8 { - loop { - let res = self - .access(move |me| { - if me.spi.blocking_write(&[address & Self::READ, 0]).is_err() { - (me.log)("send address (read) failed"); - } - - let mut buf = [0]; - - if me.spi.blocking_read(&mut buf).is_err() { - (me.log)("read failed"); - return 0; - } - - buf[0] << 1 - }) - .await; - - if res != address { - return res; - } - } + r } async fn write(&mut self, address: u8, value: u8) { - self.access(move |me| { - if me - .spi - .blocking_write(&[address | Self::WRITE, value]) - .is_err() - { - (me.log)("send address (write) failed"); - } - }) - .await; + self.cs.set_low(); + Timer::after(Duration::from_micros(1)).await; + + if self + .spi + .blocking_write(&[address | Self::WRITE, value]) + .is_err() + { + self.sender.send_msg("send address (write) failed"); + } + + self.cs.set_high(); + Timer::after(Duration::from_micros(2)).await; } } -impl<'a, L, CS, T> PAW3212DB_TJDT<'a, L, CS, T> +impl<'a, S, CS, T> PAW3212DB_TJDT<'a, S, CS, T> where - L: FnMut(&str), + S: Sender, CS: CsPin + 'a, T: SpiInstance, { @@ -145,6 +139,7 @@ where } pub async fn product_id_1(&mut self) -> u8 { + self.sender.send_msg("reading: product id 1").await; self.read(Self::REG_PRODUCT_ID1).await } diff --git a/src/mouse_sensor/wrapper.rs b/src/mouse_sensor/wrapper.rs index 8425704..e5fc98a 100644 --- a/src/mouse_sensor/wrapper.rs +++ b/src/mouse_sensor/wrapper.rs @@ -1,3 +1,5 @@ +use crate::usb_serial::Sender; + use super::paw3212db_tjdt::PAW3212DB_TJDT; use embassy_rp::{ @@ -23,11 +25,11 @@ where MO: Peripheral

+ 'a, CS: Peripheral

+ 'a, { - spi: P, - clk: C, - mosi: MI, - miso: MO, - cs: CS, + pub spi: P, + pub clk: C, + pub mosi: MI, + pub miso: MO, + pub cs: CS, d: PhantomData<&'a ()>, } @@ -60,28 +62,28 @@ where } } -pub struct MouseSensor<'d, L, T, CSP> +pub struct MouseSensor<'d, S, T, CSP> where - L: FnMut(&str), + S: Sender, T: Instance, CSP: CsPin + 'd, { - pub spi_device: PAW3212DB_TJDT<'d, L, CSP, T>, + pub spi_device: PAW3212DB_TJDT<'d, S, CSP, T>, led: Output<'d, PIN_25>, } -impl<'d, L, T, CSP> MouseSensor<'d, L, T, CSP> +impl<'d, S, T, CSP> MouseSensor<'d, S, T, CSP> where - L: FnMut(&str) + 'd, + S: Sender, T: Instance, CSP: CsPin + 'd, { pub async fn new<'a, Clk, Mosi, Miso, P, C, MI, MO, CS>( led: PIN_25, sensor_pins: MouseSensorPins<'d, T, Clk, Mosi, Miso, CSP, P, C, MI, MO, CS>, - log: L, - ) -> MouseSensor<'d, L, T, CSP> + sender: S, + ) -> MouseSensor<'d, S, T, CSP> where Clk: ClkPin + 'a, Mosi: MosiPin + 'a, @@ -102,28 +104,28 @@ where sensor_pins.mosi, sensor_pins.miso, sensor_pins.cs, - log, + sender, ) .await; let mut me = Self { spi_device, led }; - me.led.set_high(); + // me.led.set_high(); - // verify initialization - loop { - if me.verify_product_id_1().await { - break; - } - } + // // verify initialization + // loop { + // if me.verify_product_id_1().await { + // break; + // } + // } - loop { - if me.verify_product_id_2().await { - break; - } - } + // loop { + // if me.verify_product_id_2().await { + // break; + // } + // } - me.led.set_low(); + // me.led.set_low(); me } diff --git a/src/usb_serial/mod.rs b/src/usb_serial/mod.rs index 44e28e0..419e556 100644 --- a/src/usb_serial/mod.rs +++ b/src/usb_serial/mod.rs @@ -20,6 +20,14 @@ static mut CONTROL_BUFFER: [u8; 64] = [0; 64]; static mut STATE: Option> = None; static mut USB: Option>> = None; +pub trait Sender { + async fn send_msg(&mut self, msg: &str); + + async fn send_number(&mut self, i: T, base: I) + where + T: NumToA; +} + pub struct Serial<'a> { class: CdcAcmClass<'a, Driver<'a, USB>>, } @@ -92,3 +100,16 @@ impl Serial<'static> { self.send_msg(s).await; } } + +impl Sender for Serial<'static> { + async fn send_msg(&mut self, msg: &str) { + self.send_msg(msg).await; + } + + async fn send_number(&mut self, i: T, base: I) + where + T: NumToA, + { + self.send_number(i, base).await; + } +}