More debugging
This commit is contained in:
parent
4e0b3845fb
commit
dcfaf9e044
6 changed files with 167 additions and 91 deletions
|
@ -1,5 +1,5 @@
|
|||
vendor_id: 0xc0de
|
||||
product_id: 0xcafe
|
||||
vendor_id: 0x16c0
|
||||
product_id: 0x27dd
|
||||
manufacturer: "MeMyselfAndI"
|
||||
product: "Pico Serial"
|
||||
serial_number: "13371337"
|
77
src/main.rs
77
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),
|
||||
|_| (),
|
||||
// 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(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;
|
||||
|
||||
#[cfg(not(feature = "serial"))]
|
||||
mouse_hid(p.USB, mouse_sensor).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,
|
||||
// // };
|
||||
|
||||
#[cfg(feature = "serial")]
|
||||
serial_hid(p.USB, mouse_sensor).await;
|
||||
// // 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<T> + 'd,
|
||||
{
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
mod paw3212db_tjdt;
|
||||
mod wrapper;
|
||||
|
||||
pub use paw3212db_tjdt::PAW3212DB_TJDT;
|
||||
pub use wrapper::{MouseSensor, MouseSensorPins};
|
||||
|
|
|
@ -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<T> + '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<T> + 'a,
|
||||
T: SpiInstance,
|
||||
{
|
||||
|
@ -57,10 +59,10 @@ where
|
|||
mosi: impl Peripheral<P = impl MosiPin<T> + 'a> + 'a,
|
||||
miso: impl Peripheral<P = impl MisoPin<T> + 'a> + 'a,
|
||||
cs: impl Peripheral<P = CS> + '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 }
|
||||
}
|
||||
|
||||
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
|
||||
Self { cs, spi, sender }
|
||||
}
|
||||
|
||||
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");
|
||||
self.cs.set_low();
|
||||
Timer::after(Duration::from_micros(1)).await;
|
||||
|
||||
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 me.spi.blocking_read(&mut buf).is_err() {
|
||||
(me.log)("read failed");
|
||||
if self.spi.blocking_read(&mut buf).is_err() {
|
||||
self.sender.send_msg("read failed").await;
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf[0] << 1
|
||||
})
|
||||
.await;
|
||||
self.sender.send_number(buf[0], 10).await;
|
||||
|
||||
if res != address {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
let r = buf[0];
|
||||
|
||||
self.cs.set_high();
|
||||
Timer::after(Duration::from_micros(2)).await;
|
||||
|
||||
r
|
||||
}
|
||||
|
||||
async fn write(&mut self, address: u8, value: u8) {
|
||||
self.access(move |me| {
|
||||
if me
|
||||
self.cs.set_low();
|
||||
Timer::after(Duration::from_micros(1)).await;
|
||||
|
||||
if self
|
||||
.spi
|
||||
.blocking_write(&[address | Self::WRITE, value])
|
||||
.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
|
||||
L: FnMut(&str),
|
||||
S: Sender,
|
||||
CS: CsPin<T> + '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
|
||||
}
|
||||
|
||||
|
|
|
@ -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<P = Miso> + 'a,
|
||||
CS: Peripheral<P = CSP> + '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<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>,
|
||||
}
|
||||
|
||||
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<T> + '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<T> + 'a,
|
||||
Mosi: MosiPin<T> + '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
|
||||
}
|
||||
|
|
|
@ -20,6 +20,14 @@ static mut CONTROL_BUFFER: [u8; 64] = [0; 64];
|
|||
static mut STATE: Option<State<'static>> = 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> {
|
||||
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<T, I>(&mut self, i: T, base: I)
|
||||
where
|
||||
T: NumToA<I>,
|
||||
{
|
||||
self.send_number(i, base).await;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue