diff --git a/code/8 b/code/8 new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/code/examples/pmw3389.rs b/code/examples/pmw3389.rs index ae9d5d499a70434085b3f8d718f560ba631774eb..50ecc2ce08af3dc14a5848b8938a63913b63e355 100644 --- a/code/examples/pmw3389.rs +++ b/code/examples/pmw3389.rs @@ -59,7 +59,12 @@ const APP: () = { // setup clocks let rcc = device.RCC.constrain(); - let clocks = rcc.cfgr.freeze(); + let clocks = rcc + .cfgr + .sysclk(84.mhz()) + .pclk1(42.mhz()) + .pclk2(64.mhz()) + .freeze(); rprintln!("clocks:"); rprintln!("hclk {}", clocks.hclk().0); @@ -111,17 +116,18 @@ const APP: () = { static mut POS_X: i64 = 0; *COUNTER += 1; - if *COUNTER == 1000 / RATIO { + if *COUNTER == 10000 { cx.spawn.trace(*POS_X).unwrap(); *COUNTER = 0; } + let (x, _y) = cx.resources.pmw3389.read_status().unwrap(); *POS_X += x as i64; - // task should run each second N ms (16_000 cycles at 16MHz) + // task should run each Sclck/N times each second. cx.schedule - .poll(cx.scheduled + (RATIO * 16_000).cycles()) + .poll(cx.scheduled + (84_00).cycles()) .unwrap(); } diff --git a/code/examples/rtt_rtic_usb_mouse.rs b/code/examples/rtt_rtic_usb_mouse.rs index 9323c253d14b9b0c64023bc508f6c928175c9133..cb9ab38b6a70d86a45f755c53953adb39584709c 100644 --- a/code/examples/rtt_rtic_usb_mouse.rs +++ b/code/examples/rtt_rtic_usb_mouse.rs @@ -82,9 +82,9 @@ pub mod hid { 0xc0, // END_COLLECTION ]; - pub fn report(x: i8, y: i8) -> [u8; 3] { + pub fn report(b: u8,x: i8, y: i8) -> [u8; 3] { [ - 0x00, // button: none + b as u8, // button x as u8, // x-axis y as u8, // y-axis ] @@ -188,7 +188,7 @@ pub mod hid { REQ_GET_REPORT => { // USB host requests for report // I'm not sure what should we do here, so just send empty report - xfer.accept_with(&report(0, 0)).ok(); + xfer.accept_with(&report(0,0, 0)).ok(); } _ => { xfer.reject().ok(); @@ -214,10 +214,7 @@ pub mod hid { use hid::HIDClass; // PMW3389 -use app::{ - pmw3389::{self, Register}, - DwtDelay, -}; +use app::{DwtDelay, pmw3389::{self, Register}, pmw3389e}; use rtt_target::{rprintln, rtt_init_print}; type PMW3389T = pmw3389::Pmw3389< @@ -232,8 +229,6 @@ type PMW3389T = pmw3389::Pmw3389< PA15<Output<PushPull>>, >; -const PERIOD: u32 = 8_000_000; - #[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { struct Resources { @@ -243,6 +238,11 @@ const APP: () = { pmw3389: PMW3389T, + POS_X: i64, + POS_Y: i64, + OLD_POS_X: i64, + OLD_POS_Y: i64, + MB2: gpioc::PC6<Input<PullUp>>, MB1: gpioc::PC7<Input<PullUp>>, DPIB1: gpiob::PB13<Input<PullUp>>, @@ -251,7 +251,7 @@ const APP: () = { MB4: gpiob::PB14<Input<PullUp>>, } - #[init(schedule = [on_tick])] + #[init(schedule = [poll, tick])] fn init(mut cx: init::Context) -> init::LateResources { static mut USB_BUS: Option<bus::UsbBusAllocator<UsbBusType>> = None; static mut EP_MEMORY: [u32; 1024] = [0; 1024]; @@ -262,20 +262,20 @@ const APP: () = { let mut core = cx.core; let device = cx.device; - let rcc = cx.device.RCC.constrain(); + let rcc = device.RCC.constrain(); let clocks = rcc .cfgr - // .use_hse(8.mhz()) - .sysclk(48.mhz()) - .pclk1(24.mhz()) + .sysclk(84.mhz()) + .pclk1(42.mhz()) + .pclk2(64.mhz()) .freeze(); // assert!(clocks.usbclk_valid()); - let gpioa = cx.device.GPIOA.split(); - let gpioc = cx.device.GPIOC.split(); - let gpiob = cx.device.GPIOB.split(); + let gpioa = device.GPIOA.split(); + let gpioc = device.GPIOC.split(); + let gpiob = device.GPIOB.split(); // Buttons let mb2 = gpioc.pc6.into_pull_up_input(); @@ -299,11 +299,11 @@ const APP: () = { 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); @@ -316,9 +316,9 @@ const APP: () = { let usb_dm = gpioa.pa11; let usb = USB { - usb_global: cx.device.OTG_FS_GLOBAL, - usb_device: cx.device.OTG_FS_DEVICE, - usb_pwrclk: cx.device.OTG_FS_PWRCLK, + usb_global: device.OTG_FS_GLOBAL, + usb_device: device.OTG_FS_DEVICE, + usb_pwrclk: device.OTG_FS_PWRCLK, pin_dm: usb_dm.into_alternate_af10(), pin_dp: usb_dp.into_alternate_af10(), }; @@ -334,7 +334,8 @@ const APP: () = { .device_class(0) .build(); - cx.schedule.on_tick(cx.start + PERIOD.cycles()).ok(); + cx.schedule.poll(cx.start + (48_000).cycles()).ok(); + cx.schedule.tick(cx.start + (48_000).cycles()).ok(); init::LateResources { //counter: 0, @@ -342,6 +343,8 @@ const APP: () = { hid, MB1:mb1, MB2:mb2, MB4:mb4, MB5:mb5, DPIB1:dpib1, DPIB2:dpib2, pmw3389, + POS_X: 0, POS_Y: 0, OLD_POS_X: 0, OLD_POS_Y: 0, + } } @@ -353,28 +356,45 @@ const APP: () = { } } - #[task(schedule = [on_tick], resources = [hid, pmw3389])] - fn on_tick(mut cx: on_tick::Context) { - cx.schedule.on_tick(Instant::now() + PERIOD.cycles()).ok(); + #[task(resources = [pmw3389, POS_X, POS_Y], schedule = [poll])] + fn poll(mut cx: poll::Context) { + let pos_x = cx.resources.POS_X; + let pos_y = cx.resources.POS_Y; - //let counter: &mut u8 = &mut cx.resources.counter; - let hid = &mut cx.resources.hid; + let (x, y) = cx.resources.pmw3389.read_status().unwrap(); + *pos_x += x as i64; + *pos_y += y as i64; + + // task should run each Sclck/N times each second. 84M/8400 = 10_000 + cx.schedule + .poll(cx.scheduled + (84_00).cycles()) + .unwrap(); + } - const P: u8 = 2; - let x:i8; - let y:i8; - //*counter = (*counter + 1) % P; - (x, y) = cx.resources.pmw3389.read_status().unwrap(); + #[task(resources = [hid, POS_X, POS_Y, OLD_POS_X, OLD_POS_Y, MB1, MB2], schedule = [tick])] + fn tick(mut cx: tick::Context) { + cx.schedule.tick(cx.scheduled + (84_000).cycles()).unwrap(); + let pos_x = cx.resources.POS_X; + let pos_y = cx.resources.POS_Y; + let old_pos_x = cx.resources.OLD_POS_X; + let old_pos_y = cx.resources.OLD_POS_Y; + + let hid = &mut cx.resources.hid; + + // Calc movement + let diff_x = *pos_x-*old_pos_x; + let diff_y = *pos_y-*old_pos_y; - // move mouse cursor horizontally (x-axis) - hid.write(&hid::report(x, y)); + // Send to computer + hid.write(&hid::report(0x01 as u8,(diff_x) as i8, (diff_y) as i8)); + *old_pos_x = *pos_x; + *old_pos_y = *pos_y; } #[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.usb_dev, &mut cx.resources.hid, ); @@ -382,11 +402,11 @@ const APP: () = { extern "C" { fn EXTI0(); + fn EXTI1(); } }; fn usb_poll<B: bus::UsbBus>( - //_counter: &mut u8, usb_dev: &mut UsbDevice<'static, B>, hid: &mut HIDClass<'static, B>, ) {