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.

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