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.

254 lines
7.8 KiB

  1. /* Copyright 2020 Richard Sutherland <rich@brickbots.com>
  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 "adns.h"
  18. #include "debug.h"
  19. #include "quantum.h"
  20. #include "pointing_device.h"
  21. #include "adns9800_srom_A6.h"
  22. // registers
  23. #define REG_Product_ID 0x00
  24. #define REG_Revision_ID 0x01
  25. #define REG_Motion 0x02
  26. #define REG_Delta_X_L 0x03
  27. #define REG_Delta_X_H 0x04
  28. #define REG_Delta_Y_L 0x05
  29. #define REG_Delta_Y_H 0x06
  30. #define REG_SQUAL 0x07
  31. #define REG_Pixel_Sum 0x08
  32. #define REG_Maximum_Pixel 0x09
  33. #define REG_Minimum_Pixel 0x0a
  34. #define REG_Shutter_Lower 0x0b
  35. #define REG_Shutter_Upper 0x0c
  36. #define REG_Frame_Period_Lower 0x0d
  37. #define REG_Frame_Period_Upper 0x0e
  38. #define REG_Configuration_I 0x0f
  39. #define REG_Configuration_II 0x10
  40. #define REG_Frame_Capture 0x12
  41. #define REG_SROM_Enable 0x13
  42. #define REG_Run_Downshift 0x14
  43. #define REG_Rest1_Rate 0x15
  44. #define REG_Rest1_Downshift 0x16
  45. #define REG_Rest2_Rate 0x17
  46. #define REG_Rest2_Downshift 0x18
  47. #define REG_Rest3_Rate 0x19
  48. #define REG_Frame_Period_Max_Bound_Lower 0x1a
  49. #define REG_Frame_Period_Max_Bound_Upper 0x1b
  50. #define REG_Frame_Period_Min_Bound_Lower 0x1c
  51. #define REG_Frame_Period_Min_Bound_Upper 0x1d
  52. #define REG_Shutter_Max_Bound_Lower 0x1e
  53. #define REG_Shutter_Max_Bound_Upper 0x1f
  54. #define REG_LASER_CTRL0 0x20
  55. #define REG_Observation 0x24
  56. #define REG_Data_Out_Lower 0x25
  57. #define REG_Data_Out_Upper 0x26
  58. #define REG_SROM_ID 0x2a
  59. #define REG_Lift_Detection_Thr 0x2e
  60. #define REG_Configuration_V 0x2f
  61. #define REG_Configuration_IV 0x39
  62. #define REG_Power_Up_Reset 0x3a
  63. #define REG_Shutdown 0x3b
  64. #define REG_Inverse_Product_ID 0x3f
  65. #define REG_Motion_Burst 0x50
  66. #define REG_SROM_Load_Burst 0x62
  67. #define REG_Pixel_Burst 0x64
  68. // pins
  69. #define NCS F7
  70. extern const uint16_t firmware_length;
  71. extern const uint8_t firmware_data[];
  72. enum motion_burst_propertr{
  73. motion = 0,
  74. observation,
  75. delta_x_l,
  76. delta_x_h,
  77. delta_y_l,
  78. delta_y_h,
  79. squal,
  80. pixel_sum,
  81. maximum_pixel,
  82. minimum_pixel,
  83. shutter_upper,
  84. shutter_lower,
  85. frame_period_upper,
  86. frame_period_lower,
  87. end_data
  88. };
  89. void adns_begin(void){
  90. spi_start(NCS, false, 3, 8);
  91. }
  92. void adns_end(void){
  93. spi_stop();
  94. }
  95. void adns_write(uint8_t reg_addr, uint8_t data){
  96. adns_begin();
  97. //send address of the register, with MSBit = 1 to indicate it's a write
  98. spi_write(reg_addr | 0x80 );
  99. spi_write(data);
  100. // tSCLK-NCS for write operation
  101. wait_us(20);
  102. // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
  103. wait_us(100);
  104. adns_end();
  105. }
  106. uint8_t adns_read(uint8_t reg_addr){
  107. adns_begin();
  108. // send adress of the register, with MSBit = 0 to indicate it's a read
  109. spi_write(reg_addr & 0x7f );
  110. uint8_t data = spi_read();
  111. // tSCLK-NCS for read operation is 120ns
  112. wait_us(1);
  113. // tSRW/tSRR (=20us) minus tSCLK-NCS
  114. wait_us(19);
  115. adns_end();
  116. return data;
  117. }
  118. void pointing_device_init(void) {
  119. dprint("STARTING INTI\n");
  120. spi_init();
  121. // reset serial port
  122. adns_begin();
  123. adns_end();
  124. // reboot
  125. adns_write(REG_Power_Up_Reset, 0x5a);
  126. wait_ms(50);
  127. // read registers and discard
  128. adns_read(REG_Motion);
  129. adns_read(REG_Delta_X_L);
  130. adns_read(REG_Delta_X_H);
  131. adns_read(REG_Delta_Y_L);
  132. adns_read(REG_Delta_Y_H);
  133. // upload firmware
  134. // set the configuration_IV register in 3k firmware mode
  135. // bit 1 = 1 for 3k mode, other bits are reserved
  136. adns_write(REG_Configuration_IV, 0x02);
  137. // write 0x1d in SROM_enable reg for initializing
  138. adns_write(REG_SROM_Enable, 0x1d);
  139. // wait for more than one frame period
  140. // assume that the frame rate is as low as 100fps... even if it should never be that low
  141. wait_ms(10);
  142. // write 0x18 to SROM_enable to start SROM download
  143. adns_write(REG_SROM_Enable, 0x18);
  144. // write the SROM file (=firmware data)
  145. // write burst destination adress
  146. adns_begin();
  147. spi_write(REG_SROM_Load_Burst | 0x80);
  148. wait_us(15);
  149. // send all bytes of the firmware
  150. unsigned char c;
  151. for(int i = 0; i < firmware_length; i++){
  152. c = (unsigned char)pgm_read_byte(firmware_data + i);
  153. spi_write(c);
  154. wait_us(15);
  155. }
  156. adns_end();
  157. wait_ms(10);
  158. // enable laser(bit 0 = 0b), in normal mode (bits 3,2,1 = 000b)
  159. // reading the actual value of the register is important because the real
  160. // default value is different from what is said in the datasheet, and if you
  161. // change the reserved bytes (like by writing 0x00...) it would not work.
  162. uint8_t laser_ctrl0 = adns_read(REG_LASER_CTRL0);
  163. adns_write(REG_LASER_CTRL0, laser_ctrl0 & 0xf0);
  164. wait_ms(1);
  165. // set the configuration_I register to set the CPI
  166. // 0x01 = 50, minimum
  167. // 0x44 = 3400, default
  168. // 0x8e = 7100
  169. // 0xA4 = 8200, maximum
  170. adns_write(REG_Configuration_I, 0x10);
  171. wait_ms(100);
  172. dprint("INIT ENDED\n");
  173. }
  174. int16_t convertDeltaToInt(uint8_t high, uint8_t low){
  175. // join bytes into twos compliment
  176. //int16_t twos_comp = (high << 8) | low;
  177. //return twos_comp;
  178. return (high << 8) | low;
  179. }
  180. motion_delta_t readSensor(void) {
  181. adns_begin();
  182. // read from Motion_Burst to enable burt mode
  183. spi_write(REG_Motion_Burst & 0x7f);
  184. // Wait one frame per docs, thanks u/kbjunky
  185. wait_us(100);
  186. uint8_t burst_data[pixel_sum];
  187. for (int i = 0; i < pixel_sum; ++i) {
  188. burst_data[i] = spi_read();
  189. }
  190. uint16_t delta_x = convertDeltaToInt(burst_data[delta_x_h], burst_data[delta_x_l]);
  191. uint16_t delta_y = convertDeltaToInt(burst_data[delta_y_h], burst_data[delta_y_l]);
  192. // Only consider the MSB for motion as this byte has other status bits
  193. uint8_t motion_ind = burst_data[motion] & 0b10000000;
  194. adns_end();
  195. motion_delta_t delta = {delta_x, delta_y, motion_ind};
  196. return delta;
  197. }
  198. void pointing_device_task(void) {
  199. motion_delta_t delta = readSensor();
  200. report_mouse_t report = pointing_device_get_report();
  201. if(delta.motion_ind) {
  202. // clamp deltas from -127 to 127
  203. report.x = delta.delta_x < -127 ? -127 : delta.delta_x > 127 ? 127 : delta.delta_x;
  204. report.x = -report.x;
  205. report.y = delta.delta_y < -127 ? -127 : delta.delta_y > 127 ? 127 : delta.delta_y;
  206. }
  207. pointing_device_set_report(report);
  208. pointing_device_send();
  209. }