paw3204.c (5260B)
1 /* Copyright 2021 Gompa (@Gompa) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 // https://github.com/shinoaliceKabocha/choco60_track/tree/master/keymaps/default 18 19 #include "paw3204.h" 20 #include "wait.h" 21 #include "debug.h" 22 #include "gpio.h" 23 #include "pointing_device_internal.h" 24 25 #define REG_PID1 0x00 26 #define REG_PID2 0x01 27 #define REG_STAT 0x02 28 #define REG_X 0x03 29 #define REG_Y 0x04 30 31 #define REG_SETUP 0x06 32 #define REG_IMGQUAL 0x07 33 #define REG_IMGREC 0x0E 34 #define REG_IMGTRASH 0x0D 35 36 #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) 37 38 // CPI values 39 enum cpi_values { 40 CPI400, // 0b000 41 CPI500, // 0b001 42 CPI600, // 0b010 43 CPI800, // 0b011 44 CPI1000, // 0b100 45 CPI1200, // 0b101 46 CPI1600, // 0b110 47 }; 48 49 uint8_t paw3204_serial_read(void); 50 void paw3204_serial_write(uint8_t reg_addr); 51 uint8_t paw3204_read_reg(uint8_t reg_addr); 52 void paw3204_write_reg(uint8_t reg_addr, uint8_t data); 53 54 const pointing_device_driver_t paw3204_pointing_device_driver = { 55 .init = paw3204_init, 56 .get_report = paw3204_get_report, 57 .set_cpi = paw3204_set_cpi, 58 .get_cpi = paw3204_get_cpi, 59 }; 60 61 uint8_t read_pid_paw3204(void) { 62 return paw3204_read_reg(REG_PID1); 63 } 64 65 bool __attribute__((weak)) paw3204_check_signature(void) { 66 return (read_pid_paw3204() == 0x30); 67 } 68 69 bool paw3204_init(void) { 70 gpio_set_pin_output(PAW3204_SCLK_PIN); // setclockpin to output 71 gpio_set_pin_input_high(PAW3204_SDIO_PIN); // set datapin input high 72 73 paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi 74 wait_us(5); 75 76 paw3204_read_reg(0x00); // read id 77 paw3204_read_reg(0x01); // read id2 78 // PAW3204_write_reg(REG_SETUP,0x06); // dont reset sensor and set cpi 1600 79 paw3204_write_reg(REG_IMGTRASH, 0x32); // write image trashhold 80 81 return paw3204_check_signature(); 82 } 83 84 uint8_t paw3204_serial_read(void) { 85 gpio_set_pin_input(PAW3204_SDIO_PIN); 86 uint8_t byte = 0; 87 88 for (uint8_t i = 0; i < 8; ++i) { 89 gpio_write_pin_low(PAW3204_SCLK_PIN); 90 wait_us(1); 91 92 byte = (byte << 1) | gpio_read_pin(PAW3204_SDIO_PIN); 93 94 gpio_write_pin_high(PAW3204_SCLK_PIN); 95 wait_us(1); 96 } 97 98 return byte; 99 } 100 101 void paw3204_serial_write(uint8_t data) { 102 gpio_write_pin_low(PAW3204_SDIO_PIN); 103 gpio_set_pin_output(PAW3204_SDIO_PIN); 104 105 for (int8_t b = 7; b >= 0; b--) { 106 gpio_write_pin_low(PAW3204_SCLK_PIN); 107 if (data & (1 << b)) { 108 gpio_write_pin_high(PAW3204_SDIO_PIN); 109 } else { 110 gpio_write_pin_low(PAW3204_SDIO_PIN); 111 } 112 gpio_write_pin_high(PAW3204_SCLK_PIN); 113 } 114 115 wait_us(4); 116 } 117 118 report_paw3204_t paw3204_read(void) { 119 report_paw3204_t data = {0}; 120 121 data.isMotion = paw3204_read_reg(REG_STAT) & (1 << 7); // check for motion only (bit 7 in field) 122 data.x = (int8_t)paw3204_read_reg(REG_X); 123 data.y = (int8_t)paw3204_read_reg(REG_Y); 124 125 return data; 126 } 127 128 void paw3204_write_reg(uint8_t reg_addr, uint8_t data) { 129 paw3204_serial_write(0b10000000 | reg_addr); 130 paw3204_serial_write(data); 131 } 132 133 uint8_t paw3204_read_reg(uint8_t reg_addr) { 134 paw3204_serial_write(reg_addr); 135 wait_us(5); 136 return paw3204_serial_read(); 137 } 138 139 void paw3204_set_cpi(uint16_t cpi) { 140 uint8_t cpival = CPI1000; 141 if (cpi <= 450) { 142 cpival = CPI400; 143 } else if (cpi <= 550) { 144 cpival = CPI500; 145 } else if (cpi <= 700) { 146 cpival = CPI600; 147 } else if (cpi <= 900) { 148 cpival = CPI800; 149 } else if (cpi <= 1100) { 150 cpival = CPI1000; 151 } else if (cpi <= 1400) { 152 cpival = CPI1200; 153 } else if (cpi > 1400) { 154 cpival = CPI1600; 155 } 156 paw3204_write_reg(REG_SETUP, cpival); 157 } 158 159 uint16_t paw3204_get_cpi(void) { 160 uint16_t cpival = 1000; 161 162 switch (paw3204_read_reg(REG_SETUP) & 0b111) { 163 case CPI400: 164 cpival = 400; 165 break; 166 case CPI500: 167 cpival = 500; 168 break; 169 case CPI600: 170 cpival = 600; 171 break; 172 case CPI800: 173 cpival = 800; 174 break; 175 case CPI1000: 176 cpival = 1000; 177 break; 178 case CPI1200: 179 cpival = 1200; 180 break; 181 case CPI1600: 182 cpival = 1600; 183 break; 184 } 185 return cpival; 186 } 187 188 report_mouse_t paw3204_get_report(report_mouse_t mouse_report) { 189 report_paw3204_t data = paw3204_read(); 190 if (data.isMotion) { 191 pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y); 192 193 mouse_report.x = data.x; 194 mouse_report.y = data.y; 195 } 196 197 return mouse_report; 198 }