You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
5.2 KiB

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