diff --git a/code/examples/rtt_rtic_usb_mouse.rs b/code/examples/rtt_rtic_usb_mouse.rs index c1c4d088f0c2f17f62c99fea184d3f79f9579e48..9323c253d14b9b0c64023bc508f6c928175c9133 100644 --- a/code/examples/rtt_rtic_usb_mouse.rs +++ b/code/examples/rtt_rtic_usb_mouse.rs @@ -5,18 +5,21 @@ #![no_std] #![no_main] +use core::i8; + use panic_halt as _; use cortex_m::{asm::delay, peripheral::DWT}; use embedded_hal::digital::v2::OutputPin; +use embedded_hal::spi::MODE_3; use rtic::cyccnt::{Instant, U32Ext as _}; use stm32f4xx_hal::{ dwt::Dwt, gpio::{ Speed, Alternate, Input, Output, PullUp, PushPull, gpioa::{PA15}, - gpiob::{}, - gpioc::{PC6, PC7, PC10, PC11, PC12}, + gpiob::{self,PB12, PB13, PB14, PB15}, + gpioc::{self,PC6, PC7, PC10, PC11, PC12}, }, prelude::*, rcc::Clocks, @@ -221,9 +224,9 @@ type PMW3389T = pmw3389::Pmw3389< Spi< stm32f4xx_hal::stm32::SPI3, ( - PC10<Alternate<stm32f4xx_hal::gpio::AF5>>, - PC11<Alternate<stm32f4xx_hal::gpio::AF5>>, - PC12<Alternate<stm32f4xx_hal::gpio::AF5>>, + PC10<Alternate<stm32f4xx_hal::gpio::AF6>>, + PC11<Alternate<stm32f4xx_hal::gpio::AF6>>, + PC12<Alternate<stm32f4xx_hal::gpio::AF6>>, ), >, PA15<Output<PushPull>>, @@ -234,10 +237,12 @@ const PERIOD: u32 = 8_000_000; #[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { struct Resources { - counter: u8, + //counter: u8, usb_dev: UsbDevice<'static, UsbBusType>, hid: HIDClass<'static, UsbBusType>, + pmw3389: PMW3389T, + MB2: gpioc::PC6<Input<PullUp>>, MB1: gpioc::PC7<Input<PullUp>>, DPIB1: gpiob::PB13<Input<PullUp>>, @@ -254,6 +259,9 @@ const APP: () = { DWT::unlock(); cx.core.DWT.enable_cycle_counter(); + let mut core = cx.core; + let device = cx.device; + let rcc = cx.device.RCC.constrain(); let clocks = rcc @@ -266,6 +274,38 @@ const APP: () = { // assert!(clocks.usbclk_valid()); let gpioa = cx.device.GPIOA.split(); + let gpioc = cx.device.GPIOC.split(); + let gpiob = cx.device.GPIOB.split(); + + // Buttons + let mb2 = gpioc.pc6.into_pull_up_input(); + let mb1 = gpioc.pc7.into_pull_up_input(); + let mb5 = gpiob.pb15.into_pull_up_input(); + let mb4 = gpiob.pb14.into_pull_up_input(); + let dpib1 = gpiob.pb13.into_pull_up_input(); + let dpib2 = gpiob.pb12.into_pull_up_input(); + + // Sensor communcation + let sck = gpioc.pc10.into_alternate_af6(); + let miso = gpioc.pc11.into_alternate_af6(); + let mosi = gpioc.pc12.into_alternate_af6(); + let cs = gpioa.pa15.into_push_pull_output().set_speed(Speed::High); + + let spi = Spi::spi3( + device.SPI3, + (sck, miso, mosi), + MODE_3, + stm32f4xx_hal::time::KiloHertz(2000).into(), + clocks, + ); + + static mut OLD_POS_X: i64 = 0; + static mut OLD_POS_Y: i64 = 0; + let mut delay_dwt = DwtDelay::new(&mut core.DWT, clocks); + let mut pmw3389 = pmw3389::Pmw3389::new(spi, cs, delay_dwt).unwrap(); + + // set in burst mode + pmw3389.write_register(Register::MotionBurst, 0x00); // Pull the D+ pin down to send a RESET condition to the USB bus. let mut usb_dp = gpioa.pa12.into_push_pull_output(); @@ -297,10 +337,11 @@ const APP: () = { cx.schedule.on_tick(cx.start + PERIOD.cycles()).ok(); init::LateResources { - counter: 0, + //counter: 0, usb_dev, hid, - MB1:mb1, MB2:mb2, MB4:mb4, MB5:mb5, DPIB1:dpib1, DPIB2:dpib2 + MB1:mb1, MB2:mb2, MB4:mb4, MB5:mb5, DPIB1:dpib1, DPIB2:dpib2, + pmw3389, } } @@ -312,28 +353,28 @@ const APP: () = { } } - #[task(schedule = [on_tick], resources = [counter, hid])] + #[task(schedule = [on_tick], resources = [hid, pmw3389])] fn on_tick(mut cx: on_tick::Context) { cx.schedule.on_tick(Instant::now() + PERIOD.cycles()).ok(); - let counter: &mut u8 = &mut cx.resources.counter; + //let counter: &mut u8 = &mut cx.resources.counter; let hid = &mut cx.resources.hid; const P: u8 = 2; - *counter = (*counter + 1) % P; + let x:i8; + let y:i8; + //*counter = (*counter + 1) % P; + (x, y) = cx.resources.pmw3389.read_status().unwrap(); // move mouse cursor horizontally (x-axis) - if *counter < P / 2 { - hid.write(&hid::report(10, 0)); - } else { - hid.write(&hid::report(-10, 0)); - } + hid.write(&hid::report(x, y)); + } - #[task(binds=OTG_FS, resources = [counter, usb_dev, hid])] + #[task(binds=OTG_FS, resources = [usb_dev, hid])] fn usb_fs(mut cx: usb_fs::Context) { usb_poll( - &mut cx.resources.counter, + //&mut cx.resources.counter, &mut cx.resources.usb_dev, &mut cx.resources.hid, ); @@ -345,7 +386,7 @@ const APP: () = { }; fn usb_poll<B: bus::UsbBus>( - _counter: &mut u8, + //_counter: &mut u8, usb_dev: &mut UsbDevice<'static, B>, hid: &mut HIDClass<'static, B>, ) {