From 6f5aedd8a62e17af50af42a96a07d802df1c2560 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 25 Aug 2023 13:38:30 +0200 Subject: [PATCH 1/4] Add actions file --- .gitea/workflows/pull_request.yaml | 19 +++++ src/main.rs | 112 ++++++++++------------------- src/mouse_sensor/paw3212db_tjdt.rs | 6 +- src/mouse_sensor/wrapper.rs | 30 ++++---- src/usb_serial/mod.rs | 9 ++- 5 files changed, 81 insertions(+), 95 deletions(-) create mode 100644 .gitea/workflows/pull_request.yaml diff --git a/.gitea/workflows/pull_request.yaml b/.gitea/workflows/pull_request.yaml new file mode 100644 index 0000000..d627078 --- /dev/null +++ b/.gitea/workflows/pull_request.yaml @@ -0,0 +1,19 @@ +name: Gavania Merge Build +run_name: Test successful gavania build on merge request + +on: + pull_request: + types: [opened, reopened, edited, review_requested, synchronize] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: https://github.com/actions/checkout@v3 + + - uses: https://github.com/dtolnay/rust-toolchain@stable + + - name: Build + run: cargo build diff --git a/src/main.rs b/src/main.rs index 8ce858b..ebf9001 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #![no_main] #![feature(type_alias_impl_trait)] #![feature(never_type)] +#![allow(incomplete_features)] #![feature(async_fn_in_trait)] #[cfg(not(feature = "serial"))] @@ -19,8 +20,11 @@ use embassy_rp::{ spi::{CsPin, Instance}, }; -use embassy_time::{Duration, Timer}; -use mouse_sensor::PAW3212DB_TJDT; +use embassy_rp::peripherals::PIN_25; +use embassy_rp::spi::{ClkPin, MisoPin, MosiPin}; +use embassy_rp::Peripheral; + +// use embassy_time::{Duration, Timer}; #[cfg(not(feature = "serial"))] use usbd_hid::descriptor::MouseReport; @@ -30,8 +34,6 @@ 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 _}; @@ -40,70 +42,16 @@ 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; - - // #[cfg(not(feature = "serial"))] - // mouse_hid(p.USB, mouse_sensor).await; - - // #[cfg(feature = "serial")] - // serial_hid(p.USB, mouse_sensor).await; + #[cfg(not(feature = "serial"))] + mouse_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; - - // 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; - } + serial_hid( + p.USB, + p.PIN_25, + MouseSensorPins::new(p.SPI0, p.PIN_2, p.PIN_3, p.PIN_4, p.PIN_5), + ) + .await; } #[cfg(not(feature = "serial"))] @@ -139,15 +87,27 @@ where } #[cfg(feature = "serial")] -async fn serial_hid<'d, S, T, CSP>(usb: USB, mut mouse_sensor: MouseSensor<'d, S, T, CSP>) -where - S: Sender, +async fn serial_hid<'d, T, CSP, Clk, Mosi, Miso, P, C, MI, MO, CS>( + usb: USB, + led: PIN_25, + sensor_pins: MouseSensorPins<'d, T, Clk, Mosi, Miso, CSP, P, C, MI, MO, CS>, +) where T: Instance, - CSP: CsPin + 'd, -{ - let mut serial = Serial::new(usb, SerialConfig::new()); - let usb_future = Serial::run_usb().await; + CSP: CsPin + 'd, + Clk: ClkPin + 'd, + Mosi: MosiPin + 'd, + Miso: MisoPin + 'd, + + P: Peripheral

+ 'd, + C: Peripheral

+ 'd, + MI: Peripheral

+ 'd, + MO: Peripheral

+ 'd, + CS: Peripheral

+ 'd, +{ + let (serial, usb_future) = Serial::new(usb, SerialConfig::new()).await; + + let mut mouse_sensor = MouseSensor::new(led, sensor_pins, serial).await; // Do stuff with the class! let hid_future = async { @@ -161,8 +121,10 @@ where // pan: 0, // }; - serial.send_number(x, 10).await; - serial.send_number(y, 10).await; + mouse_sensor.sender().send_number(x, 10).await; + mouse_sensor.sender().send_msg("\n").await; + mouse_sensor.sender().send_number(y, 10).await; + mouse_sensor.sender().send_msg("\n").await; } } }; diff --git a/src/mouse_sensor/paw3212db_tjdt.rs b/src/mouse_sensor/paw3212db_tjdt.rs index 705035a..1c2f50c 100644 --- a/src/mouse_sensor/paw3212db_tjdt.rs +++ b/src/mouse_sensor/paw3212db_tjdt.rs @@ -2,9 +2,7 @@ use core::mem; use embassy_rp::{ gpio::{Level, Output}, - spi::{ - Blocking, ClkPin, Config, CsPin, Instance as SpiInstance, MisoPin, MosiPin, Polarity, Spi, - }, + spi::{Blocking, ClkPin, Config, CsPin, Instance as SpiInstance, MisoPin, MosiPin, Spi}, Peripheral, }; use embassy_time::{Duration, Timer}; @@ -63,7 +61,7 @@ where ) -> PAW3212DB_TJDT<'a, S, CS, T> { let mut spi_config = Config::default(); 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); diff --git a/src/mouse_sensor/wrapper.rs b/src/mouse_sensor/wrapper.rs index e5fc98a..94bc3c5 100644 --- a/src/mouse_sensor/wrapper.rs +++ b/src/mouse_sensor/wrapper.rs @@ -110,22 +110,22 @@ where 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 + let mut verified = false; - // loop { - // if me.verify_product_id_2().await { - // break; - // } - // } + for _ in 0..50 { + if me.verify_product_id_1().await && me.verify_product_id_2().await { + verified = true; + } + } - // me.led.set_low(); + if !verified { + panic!(); + } + + me.led.set_low(); me } @@ -168,4 +168,8 @@ where None } } + + pub fn sender(&mut self) -> &mut S { + &mut self.spi_device.sender + } } diff --git a/src/usb_serial/mod.rs b/src/usb_serial/mod.rs index 419e556..a3946be 100644 --- a/src/usb_serial/mod.rs +++ b/src/usb_serial/mod.rs @@ -33,7 +33,10 @@ pub struct Serial<'a> { } impl Serial<'static> { - pub fn new(usb: USB, serial_config: SerialConfig<'static>) -> Serial<'static> { + pub async fn new( + usb: USB, + serial_config: SerialConfig<'static>, + ) -> (Serial<'static>, impl Future) { // Create the driver, from the HAL. let irq = interrupt::take!(USBCTRL_IRQ); let driver = Driver::new(usb, irq); @@ -78,10 +81,10 @@ impl Serial<'static> { USB = Some(builder.build()); }; - Self { class } + (Self { class }, Self::run_usb().await) } - pub async fn run_usb() -> impl Future { + async fn run_usb() -> impl Future { unsafe { USB.as_mut().unwrap().run() } } -- 2.45.2 From dfcb6c338202047829f52213e4389e875147c336 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 25 Aug 2023 13:53:12 +0200 Subject: [PATCH 2/4] Update nightly version --- rust-toolchain.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index bfe6c04..9e450fc 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-04-11" +channel = "nightly-2023-08-19" components = [ "rust-src", "rustfmt", "llvm-tools-preview" ] targets = ["thumbv6m-none-eabi"] \ No newline at end of file -- 2.45.2 From 2d60a46e2b0725999b19e9e8cca2548bba519461 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 25 Aug 2023 14:14:35 +0200 Subject: [PATCH 3/4] Update dependencies --- Cargo.toml | 10 ++++------ src/usb_serial/mod.rs | 12 ++++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7515d93..6ef55ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,17 +6,15 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -embassy-executor = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } -embassy-rp = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } -embassy-usb = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "msos-descriptor"] } -embassy-time = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] } +embassy-executor = { version = "0.3.0", git = "https://github.com/embassy-rs/embassy", features = ["nightly", "arch-cortex-m", "executor-thread", "integrated-timers"] } +embassy-rp = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] } +embassy-usb = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["msos-descriptor"] } +embassy-time = { version = "0.1.2", git = "https://github.com/embassy-rs/embassy", features = ["defmt-timestamp-uptime"] } embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy" } numtoa = "0.2.4" usbd-hid = "0.6.1" - -defmt = "0.3" defmt-rtt = "0.4" cortex-m = { version = "0.7.6" } diff --git a/src/usb_serial/mod.rs b/src/usb_serial/mod.rs index a3946be..3d3b605 100644 --- a/src/usb_serial/mod.rs +++ b/src/usb_serial/mod.rs @@ -4,8 +4,9 @@ use numtoa::NumToA; use core::future::Future; -use embassy_rp::usb::Driver; -use embassy_rp::{interrupt, peripherals::USB}; +use embassy_rp::bind_interrupts; +use embassy_rp::peripherals::USB; +use embassy_rp::usb::{Driver, InterruptHandler}; use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; use embassy_usb::{Builder, Config, UsbDevice}; @@ -28,6 +29,10 @@ pub trait Sender { T: NumToA; } +bind_interrupts!(struct Irqs { + USBCTRL_IRQ => InterruptHandler; +}); + pub struct Serial<'a> { class: CdcAcmClass<'a, Driver<'a, USB>>, } @@ -38,8 +43,7 @@ impl Serial<'static> { serial_config: SerialConfig<'static>, ) -> (Serial<'static>, impl Future) { // Create the driver, from the HAL. - let irq = interrupt::take!(USBCTRL_IRQ); - let driver = Driver::new(usb, irq); + let driver = Driver::new(usb, Irqs); // Create embassy-usb Config let mut config = Config::new( -- 2.45.2 From 0cb7d0975e2fc2718cf026b6dbbe1fa680998faa Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 25 Aug 2023 14:38:45 +0200 Subject: [PATCH 4/4] .gitea/workflows/pull_request.yaml aktualisiert --- .gitea/workflows/pull_request.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitea/workflows/pull_request.yaml b/.gitea/workflows/pull_request.yaml index d627078..249009c 100644 --- a/.gitea/workflows/pull_request.yaml +++ b/.gitea/workflows/pull_request.yaml @@ -15,5 +15,8 @@ jobs: - uses: https://github.com/dtolnay/rust-toolchain@stable + - name: Install flip-link + run: cargo install flip-link + - name: Build run: cargo build -- 2.45.2