pmw3320.h (4566B)
1 /* Copyright 2021 Colin Lam (Ploopy Corporation) 2 * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com> 3 * Copyright 2019 Sunjun Kim 4 * Copyright 2019 Hiroyuki Okada 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #pragma once 21 22 #include <stdint.h> 23 #include <stdbool.h> 24 #include "pointing_device.h" 25 26 #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) 27 28 // Definitions for the PMW3320 serial line. 29 #ifndef PMW3320_SCLK_PIN 30 # ifdef POINTING_DEVICE_SCLK_PIN 31 # define PMW3320_SCLK_PIN POINTING_DEVICE_SCLK_PIN 32 # else 33 # error "No clock pin defined -- missing POINTING_DEVICE_SCLK_PIN or PMW3320_SCLK_PIN" 34 # endif 35 #endif 36 37 #ifndef PMW3320_SDIO_PIN 38 # ifdef POINTING_DEVICE_SDIO_PIN 39 # define PMW3320_SDIO_PIN POINTING_DEVICE_SDIO_PIN 40 # else 41 # error "No data pin defined -- missing POINTING_DEVICE_SDIO_PIN or PMW3320_SDIO_PIN" 42 # endif 43 #endif 44 45 #ifndef PMW3320_CS_PIN 46 # ifdef POINTING_DEVICE_CS_PIN 47 # define PMW3320_CS_PIN POINTING_DEVICE_CS_PIN 48 # else 49 # error "No chip select pin defined -- missing POINTING_DEVICE_CS_PIN or PMW3320_CS_PIN define" 50 # endif 51 #endif 52 53 typedef struct { 54 int8_t dx; 55 int8_t dy; 56 } report_pmw3320_t; 57 58 extern const pointing_device_driver_t pmw3320_pointing_device_driver; 59 60 // A bunch of functions to implement the PMW3320-specific serial protocol. 61 // Mostly taken from ADNS5050 driver. 62 // Note that the "serial.h" driver is insufficient, because it does not 63 // manually manipulate a serial clock signal. 64 bool pmw3320_init(void); 65 void pmw3320_sync(void); 66 uint8_t pmw3320_serial_read(void); 67 void pmw3320_serial_write(uint8_t data); 68 uint8_t pmw3320_read_reg(uint8_t reg_addr); 69 void pmw3320_write_reg(uint8_t reg_addr, uint8_t data); 70 report_pmw3320_t pmw3320_read_burst(void); 71 void pmw3320_set_cpi(uint16_t cpi); 72 uint16_t pmw3320_get_cpi(void); 73 int8_t convert_twoscomp(uint8_t data); 74 bool pmw3320_check_signature(void); 75 report_mouse_t pmw3320_get_report(report_mouse_t mouse_report); 76 77 #if !defined(PMW3320_CPI) 78 # define PMW3320_CPI 1000 79 #endif 80 81 #define PMW3320_CPI_STEP 250 82 #define PMW3320_CPI_MIN 250 83 #define PMW3320_CPI_MAX 3500 84 85 // PMW3320 register addresses 86 // clang-format off 87 #define REG_Product_ID 0x00 88 #define REG_Revision_ID 0x01 89 #define REG_Motion 0x02 90 #define REG_Delta_X 0x03 91 #define REG_Delta_Y 0x04 92 #define REG_SQUAL 0x05 93 #define REG_Shutter_Upper 0x06 94 #define REG_Shutter_Lower 0x07 95 #define REG_Maximum_Pixel 0x08 96 #define REG_Pixel_Accum 0x09 97 #define REG_Minimum_Pixel 0x0a 98 #define REG_Pixel_Grab 0x0b 99 #define REG_Delta_XY 0x0c 100 #define REG_Resolution 0x0d 101 #define REG_Run_Downshift 0x0e 102 #define REG_Rest1_Period 0x0f 103 #define REG_Rest1_Downshift 0x10 104 #define REG_Rest2_Preiod 0x11 105 #define REG_Rest2_Downshift 0x12 106 #define REG_Rest3_Period 0x13 107 #define REG_Min_SQ_Run 0x17 108 #define REG_Axis_Control 0x1a 109 #define REG_Performance 0x22 110 #define REG_Low_Motion_Jitter 0x23 111 #define REG_Shutter_Max_HI 0x36 112 #define REG_Shutter_Max_LO 0x37 113 #define REG_Frame_Rate 0x39 114 #define REG_Power_Up_Reset 0x3a 115 #define REG_Shutdown 0x3b 116 #define REG_Inverse_Revision_ID 0x3f 117 #define REG_Led_Control 0x40 118 #define REG_Motion_Control 0x41 119 #define REG_Burst_Read_First 0x42 120 #define REG_Rest_Mode_Status 0x45 121 #define REG_Inverse_Product_ID 0x4f 122 #define REG_Motion_Burst 0x63 123 // clang-format on