From 5200c3cc2c87feca2bcb973e32eaeef9808fe032 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Thu, 27 Oct 2022 20:09:08 -0400 Subject: touchpad now works over spi --- gdbinit | 1 + src/cirque.rs | 49 +++++++++++++++++++++++++++++-------------------- src/main.rs | 10 +++++----- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/gdbinit b/gdbinit index fe53799..881d4bc 100644 --- a/gdbinit +++ b/gdbinit @@ -11,3 +11,4 @@ set remote hardware-watchpoint-limit 2 target extended-remote psyduck:3334 monitor arm semihosting enable +mon reset init diff --git a/src/cirque.rs b/src/cirque.rs index 4a2686b..b19b1f4 100644 --- a/src/cirque.rs +++ b/src/cirque.rs @@ -1,9 +1,6 @@ /// Support for the Cirque Pinnacle 1CA027. use core::cmp::{max, min}; -use embedded_hal::{ - digital::v2::{InputPin, OutputPin}, - spi, -}; +use embedded_hal::{digital::v2::OutputPin, spi}; use nb; use stm32f1xx_hal::rcc::Clocks; @@ -116,41 +113,41 @@ impl TouchData { } pub fn scale_to(&mut self, width: u16, height: u16) { - let width_factor = width / (CLAMP_X_MAX - CLAMP_X_MIN); - let height_factor = height / (CLAMP_Y_MAX - CLAMP_Y_MIN); + let width_factor: f32 = f32::from(width) / f32::from(CLAMP_X_MAX - CLAMP_X_MIN); + let height_factor: f32 = f32::from(height) / f32::from(CLAMP_Y_MAX - CLAMP_Y_MIN); self.clamp(); self.x -= CLAMP_X_MIN; self.y -= CLAMP_Y_MIN; - self.x *= width_factor; - self.y *= height_factor; + assert!(!width_factor.is_nan() && width_factor.is_finite()); + assert!(!height_factor.is_nan() && height_factor.is_finite()); + unsafe { + self.x = (f32::from(self.x) * width_factor).to_int_unchecked(); + self.y = (f32::from(self.y) * height_factor).to_int_unchecked(); + } } } -pub struct Cirque +pub struct Cirque where C: OutputPin, - D: InputPin, { cs_pin: C, - dr_pin: D, sysclk_speed: u32, } -impl Cirque +impl Cirque where C: OutputPin, - D: InputPin, { - pub fn new(cs_pin: C, dr_pin: D, spi: &mut S, clocks: Clocks) -> nb::Result + pub fn new(cs_pin: C, spi: &mut S, clocks: Clocks) -> nb::Result where S: spi::FullDuplex, { let sysclk_speed = clocks.sysclk().raw(); let mut res = Self { cs_pin, - dr_pin, sysclk_speed, }; res.init(spi)?; @@ -162,10 +159,13 @@ where S: spi::FullDuplex, { self.cs_pin.set_high().ok(); + + // spin until power on reset is flagged. + // while self.read_flags(spi)? & 0x8 != 0x8 {} self.clear_flags(spi)?; - self.wr(spi, RAPAddress::SysConfig1, 0x02)?; - self.wr(spi, RAPAddress::FeedConfig2, 0x1f)?; + self.wr(spi, RAPAddress::SysConfig1, 0x00)?; + self.wr(spi, RAPAddress::FeedConfig2, 0x1e)?; self.wr(spi, RAPAddress::FeedConfig1, 0x03)?; self.wr(spi, RAPAddress::ZIdle, 0x05)?; @@ -184,12 +184,21 @@ where Ok(()) } + fn read_flags(&mut self, spi: &mut S) -> nb::Result + where + S: spi::FullDuplex, + { + let mut flags: [u8; 1] = [0xfb; 1]; + self.rd(spi, RAPAddress::Status1, &mut flags)?; + Ok(flags[0]) + } + pub fn poll(&mut self, spi: &mut S) -> nb::Result where S: spi::FullDuplex, { - if self.dr_pin.is_high().unwrap_or(false) { - self.read_coords(spi) + if self.read_flags(spi)? & 0x4 == 0x4 { + Ok(self.read_coords(spi)?) } else { Err(nb::Error::WouldBlock) } @@ -206,7 +215,7 @@ where let x = buf[2] as u16 | ((buf[4] as u16 & 0x0f) << 8); let y = buf[3] as u16 | ((buf[4] as u16 & 0xf0) << 4); let z = buf[5] & 0x3f; - let buttons = buf[0] & 0x3f; + let buttons = 0; let is_pressed = x != 0; assert!(x < MAX_X); diff --git a/src/main.rs b/src/main.rs index 02812bd..1697001 100755 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,8 @@ fn main() -> ! { // pa7 - mosi1 logln!("init trackpad"); - let dr_pin = gpiob.pb0; + // TODO: hook an interrupt up to the dr pin to trigger a poll. + //let dr_pin = gpiob.pb1.into_pull_down_input(&mut gpiob.crl); let sck_pin = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl); let miso_pin = gpioa.pa6; let mosi_pin = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl); @@ -86,10 +87,10 @@ fn main() -> ! { (sck_pin, miso_pin, mosi_pin), &mut afio.mapr, spi::MODE_1, - 1_500_000.Hz(), + 1_000_000.Hz(), // pinnacle supports up to 13mhz clocks, ); - let mut cirque = match Cirque::new(cs_pin, dr_pin, &mut spi, clocks) { + let mut cirque = match Cirque::new(cs_pin, &mut spi, clocks) { Ok(c) => c, Err(e) => { logln!("err: {:?}", e); @@ -126,8 +127,7 @@ fn main() -> ! { logln!("luchie started!"); loop { - logln!("."); - + // logln!("."); if let Ok(mut td) = cirque.poll(&mut spi) { td.scale_to(1920, 1080); logln!("td: {:?}", td); -- cgit v1.2.3