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 "quantum.h"
  18. #include "adns9800_srom_A6.h"
  19. #include "adns9800.h"
  20. // registers
  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 ADNS_CLOCK_SPEED 2000000
  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 SPI_MODE 3
  72. #define SPI_DIVISOR (F_CPU / ADNS_CLOCK_SPEED)
  73. #define US_BETWEEN_WRITES 120
  74. #define US_BETWEEN_READS 20
  75. #define US_BEFORE_MOTION 100
  76. #define MSB1 0x80
  77. extern const uint8_t firmware_data[];
  78. void adns_spi_start(void){
  79. spi_start(SPI_SS_PIN, false, SPI_MODE, SPI_DIVISOR);
  80. }
  81. void adns_write(uint8_t reg_addr, uint8_t data){
  82. adns_spi_start();
  83. spi_write(reg_addr | MSB1);
  84. spi_write(data);
  85. spi_stop();
  86. wait_us(US_BETWEEN_WRITES);
  87. }
  88. uint8_t adns_read(uint8_t reg_addr){
  89. adns_spi_start();
  90. spi_write(reg_addr & 0x7f );
  91. uint8_t data = spi_read();
  92. spi_stop();
  93. wait_us(US_BETWEEN_READS);
  94. return data;
  95. }
  96. void adns_init() {
  97. setPinOutput(SPI_SS_PIN);
  98. spi_init();
  99. // reboot
  100. adns_write(REG_Power_Up_Reset, 0x5a);
  101. wait_ms(50);
  102. // read registers and discard
  103. adns_read(REG_Motion);
  104. adns_read(REG_Delta_X_L);
  105. adns_read(REG_Delta_X_H);
  106. adns_read(REG_Delta_Y_L);
  107. adns_read(REG_Delta_Y_H);
  108. // upload firmware
  109. // 3k firmware mode
  110. adns_write(REG_Configuration_IV, 0x02);
  111. // enable initialisation
  112. adns_write(REG_SROM_Enable, 0x1d);
  113. // wait a frame
  114. wait_ms(10);
  115. // start SROM download
  116. adns_write(REG_SROM_Enable, 0x18);
  117. // write the SROM file
  118. adns_spi_start();
  119. spi_write(REG_SROM_Load_Burst | 0x80);
  120. wait_us(15);
  121. // send all bytes of the firmware
  122. unsigned char c;
  123. for(int i = 0; i < FIRMWARE_LENGTH; i++){
  124. c = (unsigned char)pgm_read_byte(firmware_data + i);
  125. spi_write(c);
  126. wait_us(15);
  127. }
  128. spi_stop();
  129. wait_ms(10);
  130. // enable laser
  131. uint8_t laser_ctrl0 = adns_read(REG_LASER_CTRL0);
  132. adns_write(REG_LASER_CTRL0, laser_ctrl0 & 0xf0);
  133. }
  134. config_adns_t adns_get_config(void) {
  135. uint8_t config_1 = adns_read(REG_Configuration_I);
  136. return (config_adns_t){ (config_1 & 0xFF) * CPI_STEP };
  137. }
  138. void adns_set_config(config_adns_t config) {
  139. uint8_t config_1 = (CLAMP_CPI(config.cpi) / CPI_STEP) & 0xFF;
  140. adns_write(REG_Configuration_I, config_1);
  141. }
  142. static int16_t convertDeltaToInt(uint8_t high, uint8_t low){
  143. // join bytes into twos compliment
  144. uint16_t twos_comp = (high << 8) | low;
  145. // convert twos comp to int
  146. if (twos_comp & 0x8000)
  147. return -1 * (~twos_comp + 1);
  148. return twos_comp;
  149. }
  150. report_adns_t adns_get_report(void) {
  151. report_adns_t report = {0, 0};
  152. adns_spi_start();
  153. // start burst mode
  154. spi_write(REG_Motion_Burst & 0x7f);
  155. wait_us(US_BEFORE_MOTION);
  156. uint8_t motion = spi_read();
  157. if(motion & 0x80) {
  158. // clear observation register
  159. spi_read();
  160. // delta registers
  161. uint8_t delta_x_l = spi_read();
  162. uint8_t delta_x_h = spi_read();
  163. uint8_t delta_y_l = spi_read();
  164. uint8_t delta_y_h = spi_read();
  165. report.x = convertDeltaToInt(delta_x_h, delta_x_l);
  166. report.y = convertDeltaToInt(delta_y_h, delta_y_l);
  167. }
  168. // clear residual motion
  169. spi_write(REG_Motion & 0x7f);
  170. spi_stop();
  171. return report;
  172. }