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.

281 lines
7.9 KiB

  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. * Copyright 2019 Sunjun Kim
  3. * Copyright 2020 Ploopy Corporation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "pmw3360.h"
  19. #include "wait.h"
  20. #include "debug.h"
  21. #include "print.h"
  22. #include PMW3360_FIRMWARE_H
  23. // Registers
  24. // clang-format off
  25. #define REG_Product_ID 0x00
  26. #define REG_Revision_ID 0x01
  27. #define REG_Motion 0x02
  28. #define REG_Delta_X_L 0x03
  29. #define REG_Delta_X_H 0x04
  30. #define REG_Delta_Y_L 0x05
  31. #define REG_Delta_Y_H 0x06
  32. #define REG_SQUAL 0x07
  33. #define REG_Raw_Data_Sum 0x08
  34. #define REG_Maximum_Raw_data 0x09
  35. #define REG_Minimum_Raw_data 0x0A
  36. #define REG_Shutter_Lower 0x0B
  37. #define REG_Shutter_Upper 0x0C
  38. #define REG_Control 0x0D
  39. #define REG_Config1 0x0F
  40. #define REG_Config2 0x10
  41. #define REG_Angle_Tune 0x11
  42. #define REG_Frame_Capture 0x12
  43. #define REG_SROM_Enable 0x13
  44. #define REG_Run_Downshift 0x14
  45. #define REG_Rest1_Rate_Lower 0x15
  46. #define REG_Rest1_Rate_Upper 0x16
  47. #define REG_Rest1_Downshift 0x17
  48. #define REG_Rest2_Rate_Lower 0x18
  49. #define REG_Rest2_Rate_Upper 0x19
  50. #define REG_Rest2_Downshift 0x1A
  51. #define REG_Rest3_Rate_Lower 0x1B
  52. #define REG_Rest3_Rate_Upper 0x1C
  53. #define REG_Observation 0x24
  54. #define REG_Data_Out_Lower 0x25
  55. #define REG_Data_Out_Upper 0x26
  56. #define REG_Raw_Data_Dump 0x29
  57. #define REG_SROM_ID 0x2A
  58. #define REG_Min_SQ_Run 0x2B
  59. #define REG_Raw_Data_Threshold 0x2C
  60. #define REG_Config5 0x2F
  61. #define REG_Power_Up_Reset 0x3A
  62. #define REG_Shutdown 0x3B
  63. #define REG_Inverse_Product_ID 0x3F
  64. #define REG_LiftCutoff_Tune3 0x41
  65. #define REG_Angle_Snap 0x42
  66. #define REG_LiftCutoff_Tune1 0x4A
  67. #define REG_Motion_Burst 0x50
  68. #define REG_LiftCutoff_Tune_Timeout 0x58
  69. #define REG_LiftCutoff_Tune_Min_Length 0x5A
  70. #define REG_SROM_Load_Burst 0x62
  71. #define REG_Lift_Config 0x63
  72. #define REG_Raw_Data_Burst 0x64
  73. #define REG_LiftCutoff_Tune2 0x65
  74. // clang-format on
  75. #ifndef MAX_CPI
  76. # define MAX_CPI 0x77 // limits to 0--119, should be max cpi/100
  77. #endif
  78. bool _inBurst = false;
  79. #ifdef CONSOLE_ENABLE
  80. void print_byte(uint8_t byte) { dprintf("%c%c%c%c%c%c%c%c|", (byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), (byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), (byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), (byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')); }
  81. #endif
  82. #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  83. bool spi_start_adv(void) {
  84. bool status = spi_start(PMW3360_CS_PIN, PMW3360_SPI_LSBFIRST, PMW3360_SPI_MODE, PMW3360_SPI_DIVISOR);
  85. wait_us(1);
  86. return status;
  87. }
  88. void spi_stop_adv(void) {
  89. wait_us(1);
  90. spi_stop();
  91. }
  92. spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data) {
  93. if (reg_addr != REG_Motion_Burst) {
  94. _inBurst = false;
  95. }
  96. spi_start_adv();
  97. // send address of the register, with MSBit = 1 to indicate it's a write
  98. spi_status_t status = spi_write(reg_addr | 0x80);
  99. status = 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. spi_stop();
  105. return status;
  106. }
  107. uint8_t spi_read_adv(uint8_t reg_addr) {
  108. spi_start_adv();
  109. // send adress of the register, with MSBit = 0 to indicate it's a read
  110. spi_write(reg_addr & 0x7f);
  111. uint8_t data = spi_read();
  112. // tSCLK-NCS for read operation is 120ns
  113. wait_us(1);
  114. // tSRW/tSRR (=20us) minus tSCLK-NCS
  115. wait_us(19);
  116. spi_stop();
  117. return data;
  118. }
  119. void pmw3360_set_cpi(uint16_t cpi) {
  120. uint8_t cpival = constrain((cpi / 100) - 1, 0, MAX_CPI);
  121. spi_start_adv();
  122. spi_write_adv(REG_Config1, cpival);
  123. spi_stop();
  124. }
  125. uint16_t pmw3360_get_cpi(void) {
  126. uint8_t cpival = spi_read_adv(REG_Config1);
  127. return (uint16_t)(cpival & 0xFF) * 100;
  128. }
  129. bool pmw3360_init(void) {
  130. setPinOutput(PMW3360_CS_PIN);
  131. spi_init();
  132. _inBurst = false;
  133. spi_stop();
  134. spi_start_adv();
  135. spi_stop();
  136. spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first
  137. wait_ms(300);
  138. spi_start_adv();
  139. wait_us(40);
  140. spi_stop_adv();
  141. wait_us(40);
  142. spi_write_adv(REG_Power_Up_Reset, 0x5a);
  143. wait_ms(50);
  144. spi_read_adv(REG_Motion);
  145. spi_read_adv(REG_Delta_X_L);
  146. spi_read_adv(REG_Delta_X_H);
  147. spi_read_adv(REG_Delta_Y_L);
  148. spi_read_adv(REG_Delta_Y_H);
  149. pmw3360_upload_firmware();
  150. spi_stop_adv();
  151. wait_ms(10);
  152. pmw3360_set_cpi(PMW3360_CPI);
  153. wait_ms(1);
  154. spi_write_adv(REG_Config2, 0x00);
  155. spi_write_adv(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -30, 30));
  156. spi_write_adv(REG_Lift_Config, PMW3360_LIFTOFF_DISTANCE);
  157. bool init_success = pmw3360_check_signature();
  158. writePinLow(PMW3360_CS_PIN);
  159. return init_success;
  160. }
  161. void pmw3360_upload_firmware(void) {
  162. spi_write_adv(REG_SROM_Enable, 0x1d);
  163. wait_ms(10);
  164. spi_write_adv(REG_SROM_Enable, 0x18);
  165. spi_start_adv();
  166. spi_write(REG_SROM_Load_Burst | 0x80);
  167. wait_us(15);
  168. unsigned char c;
  169. for (int i = 0; i < FIRMWARE_LENGTH; i++) {
  170. c = (unsigned char)pgm_read_byte(firmware_data + i);
  171. spi_write(c);
  172. wait_us(15);
  173. }
  174. wait_us(200);
  175. spi_read_adv(REG_SROM_ID);
  176. spi_write_adv(REG_Config2, 0x00);
  177. spi_stop();
  178. wait_ms(10);
  179. }
  180. bool pmw3360_check_signature(void) {
  181. uint8_t pid = spi_read_adv(REG_Product_ID);
  182. uint8_t iv_pid = spi_read_adv(REG_Inverse_Product_ID);
  183. uint8_t SROM_ver = spi_read_adv(REG_SROM_ID);
  184. return (pid == firmware_signature[0] && iv_pid == firmware_signature[1] && SROM_ver == firmware_signature[2]); // signature for SROM 0x04
  185. }
  186. report_pmw3360_t pmw3360_read_burst(void) {
  187. if (!_inBurst) {
  188. #ifdef CONSOLE_ENABLE
  189. dprintf("burst on");
  190. #endif
  191. spi_write_adv(REG_Motion_Burst, 0x00);
  192. _inBurst = true;
  193. }
  194. spi_start_adv();
  195. spi_write(REG_Motion_Burst);
  196. wait_us(35); // waits for tSRAD
  197. report_pmw3360_t data = {0};
  198. data.motion = spi_read();
  199. spi_write(0x00); // skip Observation
  200. data.dx = spi_read();
  201. data.mdx = spi_read();
  202. data.dy = spi_read();
  203. data.mdy = spi_read();
  204. spi_stop();
  205. #ifdef CONSOLE_ENABLE
  206. if (debug_mouse) {
  207. print_byte(data.motion);
  208. print_byte(data.dx);
  209. print_byte(data.mdx);
  210. print_byte(data.dy);
  211. print_byte(data.mdy);
  212. dprintf("\n");
  213. }
  214. #endif
  215. data.isMotion = (data.motion & 0x80) != 0;
  216. data.isOnSurface = (data.motion & 0x08) == 0;
  217. data.dx |= (data.mdx << 8);
  218. data.dx = data.dx * -1;
  219. data.dy |= (data.mdy << 8);
  220. data.dy = data.dy * -1;
  221. spi_stop();
  222. if (data.motion & 0b111) { // panic recovery, sometimes burst mode works weird.
  223. _inBurst = false;
  224. }
  225. return data;
  226. }