adns9800.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "adns9800.h"
  18. #include "wait.h"
  19. // registers
  20. // clang-format off
  21. #define REG_Product_ID 0x00
  22. #define REG_Revision_ID 0x01
  23. #define REG_Motion 0x02
  24. #define REG_Delta_X_L 0x03
  25. #define REG_Delta_X_H 0x04
  26. #define REG_Delta_Y_L 0x05
  27. #define REG_Delta_Y_H 0x06
  28. #define REG_SQUAL 0x07
  29. #define REG_Pixel_Sum 0x08
  30. #define REG_Maximum_Pixel 0x09
  31. #define REG_Minimum_Pixel 0x0a
  32. #define REG_Shutter_Lower 0x0b
  33. #define REG_Shutter_Upper 0x0c
  34. #define REG_Frame_Period_Lower 0x0d
  35. #define REG_Frame_Period_Upper 0x0e
  36. #define REG_Configuration_I 0x0f
  37. #define REG_Configuration_II 0x10
  38. #define REG_Frame_Capture 0x12
  39. #define REG_SROM_Enable 0x13
  40. #define REG_Run_Downshift 0x14
  41. #define REG_Rest1_Rate 0x15
  42. #define REG_Rest1_Downshift 0x16
  43. #define REG_Rest2_Rate 0x17
  44. #define REG_Rest2_Downshift 0x18
  45. #define REG_Rest3_Rate 0x19
  46. #define REG_Frame_Period_Max_Bound_Lower 0x1a
  47. #define REG_Frame_Period_Max_Bound_Upper 0x1b
  48. #define REG_Frame_Period_Min_Bound_Lower 0x1c
  49. #define REG_Frame_Period_Min_Bound_Upper 0x1d
  50. #define REG_Shutter_Max_Bound_Lower 0x1e
  51. #define REG_Shutter_Max_Bound_Upper 0x1f
  52. #define REG_LASER_CTRL0 0x20
  53. #define REG_Observation 0x24
  54. #define REG_Data_Out_Lower 0x25
  55. #define REG_Data_Out_Upper 0x26
  56. #define REG_SROM_ID 0x2a
  57. #define REG_Lift_Detection_Thr 0x2e
  58. #define REG_Configuration_V 0x2f
  59. #define REG_Configuration_IV 0x39
  60. #define REG_Power_Up_Reset 0x3a
  61. #define REG_Shutdown 0x3b
  62. #define REG_Inverse_Product_ID 0x3f
  63. #define REG_Motion_Burst 0x50
  64. #define REG_SROM_Load_Burst 0x62
  65. #define REG_Pixel_Burst 0x64
  66. #define MIN_CPI 200
  67. #define MAX_CPI 8200
  68. #define CPI_STEP 200
  69. #define CLAMP_CPI(value) value<MIN_CPI ? MIN_CPI : value> MAX_CPI ? MAX_CPI : value
  70. #define US_BETWEEN_WRITES 120
  71. #define US_BETWEEN_READS 20
  72. #define US_DELAY_AFTER_ADDR 100
  73. #define US_BEFORE_MOTION 100
  74. #define MSB1 0x80
  75. // clang-format on
  76. const pointing_device_driver_t adns9800_pointing_device_driver = {
  77. .init = adns9800_init,
  78. .get_report = adns9800_get_report_driver,
  79. .set_cpi = adns9800_set_cpi,
  80. .get_cpi = adns9800_get_cpi,
  81. };
  82. uint16_t __attribute__((weak)) adns9800_srom_get_length(void) {
  83. return 0;
  84. }
  85. uint8_t __attribute__((weak)) adns9800_srom_get_byte(uint16_t position) {
  86. return 0;
  87. }
  88. void adns9800_spi_start(void) {
  89. spi_start(ADNS9800_CS_PIN, false, ADNS9800_SPI_MODE, ADNS9800_SPI_DIVISOR);
  90. }
  91. void adns9800_write(uint8_t reg_addr, uint8_t data) {
  92. adns9800_spi_start();
  93. spi_write(reg_addr | MSB1);
  94. spi_write(data);
  95. spi_stop();
  96. wait_us(US_BETWEEN_WRITES);
  97. }
  98. uint8_t adns9800_read(uint8_t reg_addr) {
  99. adns9800_spi_start();
  100. spi_write(reg_addr & 0x7f);
  101. wait_us(US_DELAY_AFTER_ADDR);
  102. uint8_t data = spi_read();
  103. spi_stop();
  104. wait_us(US_BETWEEN_READS);
  105. return data;
  106. }
  107. bool __attribute__((weak)) adns9800_check_signature(void) {
  108. if (adns9800_read(REG_Product_ID) != 0x33) {
  109. return false;
  110. }
  111. return true;
  112. }
  113. bool adns9800_init(void) {
  114. gpio_set_pin_output(ADNS9800_CS_PIN);
  115. spi_init();
  116. // reboot
  117. adns9800_write(REG_Power_Up_Reset, 0x5a);
  118. wait_ms(50);
  119. // read registers and discard
  120. adns9800_read(REG_Motion);
  121. adns9800_read(REG_Delta_X_L);
  122. adns9800_read(REG_Delta_X_H);
  123. adns9800_read(REG_Delta_Y_L);
  124. adns9800_read(REG_Delta_Y_H);
  125. if (adns9800_srom_get_length() != 0) {
  126. // upload firmware
  127. // 3k firmware mode
  128. adns9800_write(REG_Configuration_IV, 0x02);
  129. // enable initialisation
  130. adns9800_write(REG_SROM_Enable, 0x1d);
  131. // wait a frame
  132. wait_ms(10);
  133. // start SROM download
  134. adns9800_write(REG_SROM_Enable, 0x18);
  135. // write the SROM file
  136. adns9800_spi_start();
  137. spi_write(REG_SROM_Load_Burst | 0x80);
  138. wait_us(15);
  139. // send all bytes of the firmware
  140. for (uint16_t i = 0; i < adns9800_srom_get_length(); i++) {
  141. spi_write(adns9800_srom_get_byte(i));
  142. wait_us(15);
  143. }
  144. spi_stop();
  145. wait_ms(10);
  146. } else {
  147. // write reset value to REG_Configuration_IV
  148. adns9800_write(REG_Configuration_IV, 0x0);
  149. // write reset value to REG_SROM_Enable
  150. adns9800_write(REG_SROM_Enable, 0x0);
  151. // wait a frame
  152. wait_ms(10);
  153. }
  154. // enable laser
  155. uint8_t laser_ctrl0 = adns9800_read(REG_LASER_CTRL0);
  156. adns9800_write(REG_LASER_CTRL0, laser_ctrl0 & 0xf0);
  157. adns9800_set_cpi(ADNS9800_CPI);
  158. return adns9800_check_signature();
  159. }
  160. config_adns9800_t adns9800_get_config(void) {
  161. uint8_t cpival = adns9800_read(REG_Configuration_I);
  162. return (config_adns9800_t){(cpival & 0xFF) * CPI_STEP};
  163. }
  164. void adns9800_set_config(config_adns9800_t config) {
  165. uint8_t config_1 = (CLAMP_CPI(config.cpi) / CPI_STEP) & 0xFF;
  166. adns9800_write(REG_Configuration_I, config_1);
  167. }
  168. uint16_t adns9800_get_cpi(void) {
  169. uint8_t cpival = adns9800_read(REG_Configuration_I);
  170. return (uint16_t)(cpival & 0xFF) * CPI_STEP;
  171. }
  172. void adns9800_set_cpi(uint16_t cpi) {
  173. uint8_t config_1 = (CLAMP_CPI(cpi) / CPI_STEP) & 0xFF;
  174. adns9800_write(REG_Configuration_I, config_1);
  175. }
  176. static int16_t convertDeltaToInt(uint8_t high, uint8_t low) {
  177. // join bytes into twos compliment
  178. uint16_t twos_comp = (high << 8) | low;
  179. // convert twos comp to int
  180. if (twos_comp & 0x8000) return -1 * (~twos_comp + 1);
  181. return twos_comp;
  182. }
  183. report_adns9800_t adns9800_get_report(void) {
  184. report_adns9800_t report = {0};
  185. adns9800_spi_start();
  186. // start burst mode
  187. spi_write(REG_Motion_Burst & 0x7f);
  188. wait_us(US_BEFORE_MOTION);
  189. uint8_t motion = spi_read();
  190. if (motion & 0x80) {
  191. // clear observation register
  192. spi_read();
  193. // delta registers
  194. uint8_t delta_x_l = spi_read();
  195. uint8_t delta_x_h = spi_read();
  196. uint8_t delta_y_l = spi_read();
  197. uint8_t delta_y_h = spi_read();
  198. report.x = convertDeltaToInt(delta_x_h, delta_x_l);
  199. report.y = convertDeltaToInt(delta_y_h, delta_y_l);
  200. }
  201. // clear residual motion
  202. spi_write(REG_Motion & 0x7f);
  203. spi_stop();
  204. return report;
  205. }
  206. report_mouse_t adns9800_get_report_driver(report_mouse_t mouse_report) {
  207. report_adns9800_t sensor_report = adns9800_get_report();
  208. mouse_report.x = CONSTRAIN_HID_XY(sensor_report.x);
  209. mouse_report.y = CONSTRAIN_HID_XY(sensor_report.y);
  210. return mouse_report;
  211. }