diff options
Diffstat (limited to 'keyboards/oddball/pmw/pmw.c')
| -rw-r--r-- | keyboards/oddball/pmw/pmw.c | 226 |
1 files changed, 0 insertions, 226 deletions
diff --git a/keyboards/oddball/pmw/pmw.c b/keyboards/oddball/pmw/pmw.c deleted file mode 100644 index 51d692702..000000000 --- a/keyboards/oddball/pmw/pmw.c +++ /dev/null | |||
| @@ -1,226 +0,0 @@ | |||
| 1 | /* Copyright 2020 Alexander Tulloh | ||
| 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 | #include "spi_master.h" | ||
| 18 | #include "quantum.h" | ||
| 19 | #include "pmw3360_srom_0x04.h" | ||
| 20 | #include "pmw.h" | ||
| 21 | |||
| 22 | // registers | ||
| 23 | #define Product_ID 0x00 | ||
| 24 | #define Revision_ID 0x01 | ||
| 25 | #define Motion 0x02 | ||
| 26 | #define Delta_X_L 0x03 | ||
| 27 | #define Delta_X_H 0x04 | ||
| 28 | #define Delta_Y_L 0x05 | ||
| 29 | #define Delta_Y_H 0x06 | ||
| 30 | #define SQUAL 0x07 | ||
| 31 | #define Raw_Data_Sum 0x08 | ||
| 32 | #define Maximum_Raw_data 0x09 | ||
| 33 | #define Minimum_Raw_data 0x0A | ||
| 34 | #define Shutter_Lower 0x0B | ||
| 35 | #define Shutter_Upper 0x0C | ||
| 36 | #define Control 0x0D | ||
| 37 | #define Config1 0x0F | ||
| 38 | #define Config2 0x10 | ||
| 39 | #define Angle_Tune 0x11 | ||
| 40 | #define Frame_Capture 0x12 | ||
| 41 | #define SROM_Enable 0x13 | ||
| 42 | #define Run_Downshift 0x14 | ||
| 43 | #define Rest1_Rate_Lower 0x15 | ||
| 44 | #define Rest1_Rate_Upper 0x16 | ||
| 45 | #define Rest1_Downshift 0x17 | ||
| 46 | #define Rest2_Rate_Lower 0x18 | ||
| 47 | #define Rest2_Rate_Upper 0x19 | ||
| 48 | #define Rest2_Downshift 0x1A | ||
| 49 | #define Rest3_Rate_Lower 0x1B | ||
| 50 | #define Rest3_Rate_Upper 0x1C | ||
| 51 | #define Observation 0x24 | ||
| 52 | #define Data_Out_Lower 0x25 | ||
| 53 | #define Data_Out_Upper 0x26 | ||
| 54 | #define Raw_Data_Dump 0x29 | ||
| 55 | #define SROM_ID 0x2A | ||
| 56 | #define Min_SQ_Run 0x2B | ||
| 57 | #define Raw_Data_Threshold 0x2C | ||
| 58 | #define Config5 0x2F | ||
| 59 | #define Power_Up_Reset 0x3A | ||
| 60 | #define Shutdown 0x3B | ||
| 61 | #define Inverse_Product_ID 0x3F | ||
| 62 | #define LiftCutoff_Tune3 0x41 | ||
| 63 | #define Angle_Snap 0x42 | ||
| 64 | #define LiftCutoff_Tune1 0x4A | ||
| 65 | #define Motion_Burst 0x50 | ||
| 66 | #define LiftCutoff_Tune_Timeout 0x58 | ||
| 67 | #define LiftCutoff_Tune_Min_Length 0x5A | ||
| 68 | #define SROM_Load_Burst 0x62 | ||
| 69 | #define Lift_Config 0x63 | ||
| 70 | #define Raw_Data_Burst 0x64 | ||
| 71 | #define LiftCutoff_Tune2 0x65 | ||
| 72 | |||
| 73 | #define PMW_CLOCK_SPEED 70000000 | ||
| 74 | #define MIN_CPI 100 | ||
| 75 | #define MAX_CPI 12000 | ||
| 76 | #define CPI_STEP 100 | ||
| 77 | #define CLAMP_CPI(value) value < MIN_CPI ? MIN_CPI : value > MAX_CPI ? MAX_CPI : value | ||
| 78 | #define SPI_MODE 3 | ||
| 79 | #define SPI_DIVISOR (F_CPU / PMW_CLOCK_SPEED) | ||
| 80 | #define US_BETWEEN_WRITES 180 | ||
| 81 | #define US_BETWEEN_READS 20 | ||
| 82 | #define US_BEFORE_MOTION 35 | ||
| 83 | |||
| 84 | #define MSB1 0x80 | ||
| 85 | |||
| 86 | extern const uint16_t pmw_firmware_length; | ||
| 87 | extern const uint8_t pmw_firmware_data[]; | ||
| 88 | |||
| 89 | void pmw_spi_start(void){ | ||
| 90 | spi_start(SPI_SS_PIN, false, SPI_MODE, SPI_DIVISOR); | ||
| 91 | } | ||
| 92 | |||
| 93 | void pmw_write(uint8_t reg_addr, uint8_t data){ | ||
| 94 | |||
| 95 | pmw_spi_start(); | ||
| 96 | spi_write(reg_addr | MSB1 ); | ||
| 97 | spi_write(data); | ||
| 98 | spi_stop(); | ||
| 99 | wait_us(US_BETWEEN_WRITES); | ||
| 100 | } | ||
| 101 | |||
| 102 | uint8_t pmw_read(uint8_t reg_addr){ | ||
| 103 | |||
| 104 | pmw_spi_start(); | ||
| 105 | spi_write(reg_addr & 0x7f ); | ||
| 106 | uint8_t data = spi_read(); | ||
| 107 | spi_stop(); | ||
| 108 | wait_us(US_BETWEEN_READS); | ||
| 109 | |||
| 110 | return data; | ||
| 111 | } | ||
| 112 | |||
| 113 | void pmw_init() { | ||
| 114 | |||
| 115 | setPinOutput(SPI_SS_PIN); | ||
| 116 | |||
| 117 | spi_init(); | ||
| 118 | |||
| 119 | // reboot | ||
| 120 | pmw_write(Power_Up_Reset, 0x5a); | ||
| 121 | wait_ms(50); | ||
| 122 | |||
| 123 | // read registers and discard | ||
| 124 | pmw_read(Motion); | ||
| 125 | pmw_read(Delta_X_L); | ||
| 126 | pmw_read(Delta_X_H); | ||
| 127 | pmw_read(Delta_Y_L); | ||
| 128 | pmw_read(Delta_Y_H); | ||
| 129 | |||
| 130 | // upload firmware | ||
| 131 | |||
| 132 | // disable rest mode | ||
| 133 | pmw_write(Config2, 0x20); | ||
| 134 | |||
| 135 | // enable initialisation | ||
| 136 | pmw_write(SROM_Enable, 0x1d); | ||
| 137 | |||
| 138 | // wait a frame | ||
| 139 | wait_ms(10); | ||
| 140 | |||
| 141 | // start SROM download | ||
| 142 | pmw_write(SROM_Enable, 0x18); | ||
| 143 | |||
| 144 | // write the SROM file | ||
| 145 | |||
| 146 | pmw_spi_start(); | ||
| 147 | |||
| 148 | spi_write(SROM_Load_Burst | 0x80); | ||
| 149 | wait_us(15); | ||
| 150 | |||
| 151 | // send all bytes of the firmware | ||
| 152 | unsigned char c; | ||
| 153 | for(int i = 0; i < pmw_firmware_length; i++){ | ||
| 154 | c = (unsigned char)pgm_read_byte(pmw_firmware_data + i); | ||
| 155 | spi_write(c); | ||
| 156 | wait_us(15); | ||
| 157 | } | ||
| 158 | |||
| 159 | spi_stop(); | ||
| 160 | wait_us(US_BETWEEN_WRITES); | ||
| 161 | |||
| 162 | // read id | ||
| 163 | pmw_read(SROM_ID); | ||
| 164 | |||
| 165 | // wired mouse | ||
| 166 | pmw_write(Config2, 0x00); | ||
| 167 | |||
| 168 | // first motion burst; write anything | ||
| 169 | pmw_write(Motion_Burst, 0xFF); | ||
| 170 | writePinLow(SPI_SS_PIN); | ||
| 171 | } | ||
| 172 | |||
| 173 | config_pmw_t pmw_get_config(void) { | ||
| 174 | uint8_t config_1 = pmw_read(Config1); | ||
| 175 | return (config_pmw_t){ (config_1 & 0xFF) * CPI_STEP }; | ||
| 176 | } | ||
| 177 | |||
| 178 | void pmw_set_config(config_pmw_t config) { | ||
| 179 | uint8_t config_1 = (CLAMP_CPI(config.cpi) / CPI_STEP) & 0xFF; | ||
| 180 | pmw_write(Config1, config_1); | ||
| 181 | } | ||
| 182 | |||
| 183 | static int16_t convertDeltaToInt(uint8_t high, uint8_t low){ | ||
| 184 | |||
| 185 | // join bytes into twos compliment | ||
| 186 | uint16_t twos_comp = (high << 8) | low; | ||
| 187 | |||
| 188 | // convert twos comp to int | ||
| 189 | if (twos_comp & 0x8000) | ||
| 190 | return -1 * (~twos_comp + 1); | ||
| 191 | |||
| 192 | return twos_comp; | ||
| 193 | } | ||
| 194 | |||
| 195 | report_pmw_t pmw_get_report(void) { | ||
| 196 | |||
| 197 | report_pmw_t report = {0, 0}; | ||
| 198 | |||
| 199 | pmw_spi_start(); | ||
| 200 | |||
| 201 | // start burst mode | ||
| 202 | spi_write(Motion_Burst & 0x7f); | ||
| 203 | |||
| 204 | wait_us(US_BEFORE_MOTION); | ||
| 205 | |||
| 206 | uint8_t motion = spi_read(); | ||
| 207 | |||
| 208 | if(motion & 0x80) { | ||
| 209 | |||
| 210 | // clear observation register | ||
| 211 | spi_read(); | ||
| 212 | |||
| 213 | // delta registers | ||
| 214 | uint8_t delta_x_l = spi_read(); | ||
| 215 | uint8_t delta_x_h = spi_read(); | ||
| 216 | uint8_t delta_y_l = spi_read(); | ||
| 217 | uint8_t delta_y_h = spi_read(); | ||
| 218 | |||
| 219 | report.x = convertDeltaToInt(delta_x_h, delta_x_l); | ||
| 220 | report.y = convertDeltaToInt(delta_y_h, delta_y_l); | ||
| 221 | } | ||
| 222 | |||
| 223 | spi_stop(); | ||
| 224 | |||
| 225 | return report; | ||
| 226 | } | ||
