More debugging

This commit is contained in:
hodasemi 2023-08-23 12:38:50 +02:00
parent 4e0b3845fb
commit dcfaf9e044
6 changed files with 167 additions and 91 deletions

View file

@ -1,5 +1,5 @@
vendor_id: 0xc0de vendor_id: 0x16c0
product_id: 0xcafe product_id: 0x27dd
manufacturer: "MeMyselfAndI" manufacturer: "MeMyselfAndI"
product: "Pico Serial" product: "Pico Serial"
serial_number: "13371337" serial_number: "13371337"

View file

@ -2,6 +2,7 @@
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
#![feature(never_type)] #![feature(never_type)]
#![feature(async_fn_in_trait)]
#[cfg(not(feature = "serial"))] #[cfg(not(feature = "serial"))]
mod mouse_hid; mod mouse_hid;
@ -18,6 +19,8 @@ use embassy_rp::{
spi::{CsPin, Instance}, spi::{CsPin, Instance},
}; };
use embassy_time::{Duration, Timer};
use mouse_sensor::PAW3212DB_TJDT;
#[cfg(not(feature = "serial"))] #[cfg(not(feature = "serial"))]
use usbd_hid::descriptor::MouseReport; use usbd_hid::descriptor::MouseReport;
@ -27,6 +30,8 @@ use crate::{mouse_hid::mouse_config::MouseConfig, mouse_hid::MouseHID};
#[cfg(feature = "serial")] #[cfg(feature = "serial")]
use crate::usb_serial::{Serial, SerialConfig}; use crate::usb_serial::{Serial, SerialConfig};
use crate::usb_serial::Sender;
use crate::mouse_sensor::{MouseSensor, MouseSensorPins}; use crate::mouse_sensor::{MouseSensor, MouseSensorPins};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -35,18 +40,70 @@ use {defmt_rtt as _, panic_probe as _};
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default()); let p = embassy_rp::init(Default::default());
let mouse_sensor = MouseSensor::new( // let mouse_sensor = MouseSensor::new(
p.PIN_25, // p.PIN_25,
MouseSensorPins::new(p.SPI0, p.PIN_2, p.PIN_3, p.PIN_4, p.PIN_5), // 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(feature = "serial")]
// serial_hid(p.USB, mouse_sensor).await;
#[cfg(feature = "serial")]
{
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; .await;
#[cfg(not(feature = "serial"))] // Do stuff with the class!
mouse_hid(p.USB, mouse_sensor).await; 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,
// // };
#[cfg(feature = "serial")] // // serial.send_number(x, 10).await;
serial_hid(p.USB, mouse_sensor).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"))] #[cfg(not(feature = "serial"))]
@ -82,9 +139,9 @@ where
} }
#[cfg(feature = "serial")] #[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 where
L: FnMut(&str) + 'd, S: Sender,
T: Instance, T: Instance,
CSP: CsPin<T> + 'd, CSP: CsPin<T> + 'd,
{ {

View file

@ -1,4 +1,5 @@
mod paw3212db_tjdt; mod paw3212db_tjdt;
mod wrapper; mod wrapper;
pub use paw3212db_tjdt::PAW3212DB_TJDT;
pub use wrapper::{MouseSensor, MouseSensorPins}; pub use wrapper::{MouseSensor, MouseSensorPins};

View file

@ -9,22 +9,24 @@ use embassy_rp::{
}; };
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use crate::usb_serial::Sender;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub struct PAW3212DB_TJDT<'a, L, CS, T> pub struct PAW3212DB_TJDT<'a, S, CS, T>
where where
L: FnMut(&str), S: Sender,
CS: CsPin<T> + 'a, CS: CsPin<T> + 'a,
T: SpiInstance, T: SpiInstance,
{ {
cs: Output<'a, CS>, cs: Output<'a, CS>,
spi: Spi<'a, T, Blocking>, 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 where
L: FnMut(&str), S: Sender,
CS: CsPin<T> + 'a, CS: CsPin<T> + 'a,
T: SpiInstance, T: SpiInstance,
{ {
@ -57,10 +59,10 @@ where
mosi: impl Peripheral<P = impl MosiPin<T> + 'a> + 'a, mosi: impl Peripheral<P = impl MosiPin<T> + 'a> + 'a,
miso: impl Peripheral<P = impl MisoPin<T> + 'a> + 'a, miso: impl Peripheral<P = impl MisoPin<T> + 'a> + 'a,
cs: impl Peripheral<P = CS> + 'a, cs: impl Peripheral<P = CS> + 'a,
log: L, sender: S,
) -> PAW3212DB_TJDT<'a, L, CS, T> { ) -> PAW3212DB_TJDT<'a, S, CS, T> {
let mut spi_config = Config::default(); let mut spi_config = Config::default();
spi_config.frequency = 2_000_000; spi_config.frequency = 1_000_000;
spi_config.polarity = Polarity::IdleHigh; spi_config.polarity = Polarity::IdleHigh;
let spi = Spi::new_blocking(spi, clk, mosi, miso, spi_config); let spi = Spi::new_blocking(spi, clk, mosi, miso, spi_config);
@ -73,66 +75,58 @@ where
cs.set_high(); cs.set_high();
Self { cs, spi, log } Self { cs, spi, sender }
}
async fn access<F, R>(&mut self, f: F) -> R
where
F: Fn(&mut Self) -> R,
{
self.cs.set_low();
Timer::after(Duration::from_micros(1)).await;
let res = f(self);
self.cs.set_high();
Timer::after(Duration::from_micros(2)).await;
res
} }
async fn read(&mut self, address: u8) -> u8 { async fn read(&mut self, address: u8) -> u8 {
loop { self.cs.set_low();
let res = self Timer::after(Duration::from_micros(1)).await;
.access(move |me| {
if me.spi.blocking_write(&[address & Self::READ, 0]).is_err() { if self.spi.blocking_write(&[address & Self::READ]).is_err() {
(me.log)("send address (read) failed"); 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]; let mut buf = [0];
if me.spi.blocking_read(&mut buf).is_err() { if self.spi.blocking_read(&mut buf).is_err() {
(me.log)("read failed"); self.sender.send_msg("read failed").await;
return 0; return 0;
} }
buf[0] << 1 self.sender.send_number(buf[0], 10).await;
})
.await;
if res != address { let r = buf[0];
return res;
} self.cs.set_high();
} Timer::after(Duration::from_micros(2)).await;
r
} }
async fn write(&mut self, address: u8, value: u8) { async fn write(&mut self, address: u8, value: u8) {
self.access(move |me| { self.cs.set_low();
if me Timer::after(Duration::from_micros(1)).await;
if self
.spi .spi
.blocking_write(&[address | Self::WRITE, value]) .blocking_write(&[address | Self::WRITE, value])
.is_err() .is_err()
{ {
(me.log)("send address (write) failed"); self.sender.send_msg("send address (write) failed");
} }
})
.await; 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 where
L: FnMut(&str), S: Sender,
CS: CsPin<T> + 'a, CS: CsPin<T> + 'a,
T: SpiInstance, T: SpiInstance,
{ {
@ -145,6 +139,7 @@ where
} }
pub async fn product_id_1(&mut self) -> u8 { 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 self.read(Self::REG_PRODUCT_ID1).await
} }

View file

@ -1,3 +1,5 @@
use crate::usb_serial::Sender;
use super::paw3212db_tjdt::PAW3212DB_TJDT; use super::paw3212db_tjdt::PAW3212DB_TJDT;
use embassy_rp::{ use embassy_rp::{
@ -23,11 +25,11 @@ where
MO: Peripheral<P = Miso> + 'a, MO: Peripheral<P = Miso> + 'a,
CS: Peripheral<P = CSP> + 'a, CS: Peripheral<P = CSP> + 'a,
{ {
spi: P, pub spi: P,
clk: C, pub clk: C,
mosi: MI, pub mosi: MI,
miso: MO, pub miso: MO,
cs: CS, pub cs: CS,
d: PhantomData<&'a ()>, d: PhantomData<&'a ()>,
} }
@ -60,28 +62,28 @@ where
} }
} }
pub struct MouseSensor<'d, L, T, CSP> pub struct MouseSensor<'d, S, T, CSP>
where where
L: FnMut(&str), S: Sender,
T: Instance, T: Instance,
CSP: CsPin<T> + 'd, CSP: CsPin<T> + 'd,
{ {
pub spi_device: PAW3212DB_TJDT<'d, L, CSP, T>, pub spi_device: PAW3212DB_TJDT<'d, S, CSP, T>,
led: Output<'d, PIN_25>, 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 where
L: FnMut(&str) + 'd, S: Sender,
T: Instance, T: Instance,
CSP: CsPin<T> + 'd, CSP: CsPin<T> + 'd,
{ {
pub async fn new<'a, Clk, Mosi, Miso, P, C, MI, MO, CS>( pub async fn new<'a, Clk, Mosi, Miso, P, C, MI, MO, CS>(
led: PIN_25, led: PIN_25,
sensor_pins: MouseSensorPins<'d, T, Clk, Mosi, Miso, CSP, P, C, MI, MO, CS>, sensor_pins: MouseSensorPins<'d, T, Clk, Mosi, Miso, CSP, P, C, MI, MO, CS>,
log: L, sender: S,
) -> MouseSensor<'d, L, T, CSP> ) -> MouseSensor<'d, S, T, CSP>
where where
Clk: ClkPin<T> + 'a, Clk: ClkPin<T> + 'a,
Mosi: MosiPin<T> + 'a, Mosi: MosiPin<T> + 'a,
@ -102,28 +104,28 @@ where
sensor_pins.mosi, sensor_pins.mosi,
sensor_pins.miso, sensor_pins.miso,
sensor_pins.cs, sensor_pins.cs,
log, sender,
) )
.await; .await;
let mut me = Self { spi_device, led }; let mut me = Self { spi_device, led };
me.led.set_high(); // me.led.set_high();
// verify initialization // // verify initialization
loop { // loop {
if me.verify_product_id_1().await { // if me.verify_product_id_1().await {
break; // break;
} // }
} // }
loop { // loop {
if me.verify_product_id_2().await { // if me.verify_product_id_2().await {
break; // break;
} // }
} // }
me.led.set_low(); // me.led.set_low();
me me
} }

View file

@ -20,6 +20,14 @@ static mut CONTROL_BUFFER: [u8; 64] = [0; 64];
static mut STATE: Option<State<'static>> = None; static mut STATE: Option<State<'static>> = None;
static mut USB: Option<UsbDevice<'static, Driver<'static, USB>>> = None; static mut USB: Option<UsbDevice<'static, Driver<'static, USB>>> = None;
pub trait Sender {
async fn send_msg(&mut self, msg: &str);
async fn send_number<T, I>(&mut self, i: T, base: I)
where
T: NumToA<I>;
}
pub struct Serial<'a> { pub struct Serial<'a> {
class: CdcAcmClass<'a, Driver<'a, USB>>, class: CdcAcmClass<'a, Driver<'a, USB>>,
} }
@ -92,3 +100,16 @@ impl Serial<'static> {
self.send_msg(s).await; 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<T, I>(&mut self, i: T, base: I)
where
T: NumToA<I>,
{
self.send_number(i, base).await;
}
}