pmw3360.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "wait.h"
  19. #include "debug.h"
  20. #include "print.h"
  21. #include "pmw3360.h"
  22. #include "pmw3360_firmware.h"
  23. bool _inBurst = false;
  24. #ifndef PMW_CPI
  25. # define PMW_CPI 1600
  26. #endif
  27. #ifndef PMW_CLOCK_SPEED
  28. # define PMW_CLOCK_SPEED 70000000
  29. #endif
  30. #ifndef SPI_MODE
  31. # define SPI_MODE 3
  32. #endif
  33. #ifndef SPI_DIVISOR
  34. # define SPI_DIVISOR (F_CPU / PMW_CLOCK_SPEED)
  35. #endif
  36. #ifndef ROTATIONAL_TRANSFORM_ANGLE
  37. # define ROTATIONAL_TRANSFORM_ANGLE 0x00
  38. #endif
  39. #ifndef PMW_CS_PIN
  40. # define PMW_CS_PIN SPI_SS_PIN
  41. #endif
  42. 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')); }
  43. bool spi_start_adv(void) {
  44. bool status = spi_start(PMW_CS_PIN, false, SPI_MODE, SPI_DIVISOR);
  45. wait_us(1);
  46. return status;
  47. }
  48. void spi_stop_adv(void) {
  49. wait_us(1);
  50. spi_stop();
  51. }
  52. spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data) {
  53. if (reg_addr != REG_Motion_Burst) {
  54. _inBurst = false;
  55. }
  56. spi_start_adv();
  57. // send address of the register, with MSBit = 1 to indicate it's a write
  58. spi_status_t status = spi_write(reg_addr | 0x80);
  59. status = spi_write(data);
  60. // tSCLK-NCS for write operation
  61. wait_us(20);
  62. // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
  63. wait_us(100);
  64. spi_stop();
  65. return status;
  66. }
  67. uint8_t spi_read_adv(uint8_t reg_addr) {
  68. spi_start_adv();
  69. // send adress of the register, with MSBit = 0 to indicate it's a read
  70. spi_write(reg_addr & 0x7f);
  71. uint8_t data = spi_read();
  72. // tSCLK-NCS for read operation is 120ns
  73. wait_us(1);
  74. // tSRW/tSRR (=20us) minus tSCLK-NCS
  75. wait_us(19);
  76. spi_stop();
  77. return data;
  78. }
  79. void pmw_set_cpi(uint16_t cpi) {
  80. uint8_t cpival = constrain((cpi / 100) - 1, 0, 0x77); // limits to 0--119
  81. spi_start_adv();
  82. spi_write_adv(REG_Config1, cpival);
  83. spi_stop();
  84. }
  85. uint16_t pmw_get_cpi(void) {
  86. uint8_t cpival = spi_read_adv(REG_Config1);
  87. return (uint16_t)(cpival & 0xFF) * 100;
  88. }
  89. bool pmw_spi_init(void) {
  90. setPinOutput(PMW_CS_PIN);
  91. spi_init();
  92. _inBurst = false;
  93. spi_stop();
  94. spi_start_adv();
  95. spi_stop();
  96. spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first
  97. wait_ms(300);
  98. spi_start_adv();
  99. wait_us(40);
  100. spi_stop_adv();
  101. wait_us(40);
  102. spi_write_adv(REG_Power_Up_Reset, 0x5a);
  103. wait_ms(50);
  104. spi_read_adv(REG_Motion);
  105. spi_read_adv(REG_Delta_X_L);
  106. spi_read_adv(REG_Delta_X_H);
  107. spi_read_adv(REG_Delta_Y_L);
  108. spi_read_adv(REG_Delta_Y_H);
  109. pmw_upload_firmware();
  110. spi_stop_adv();
  111. wait_ms(10);
  112. pmw_set_cpi(PMW_CPI);
  113. wait_ms(1);
  114. spi_write_adv(REG_Config2, 0x00);
  115. spi_write_adv(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -30, 30));
  116. bool init_success = pmw_check_signature();
  117. writePinLow(PMW_CS_PIN);
  118. return init_success;
  119. }
  120. void pmw_upload_firmware(void) {
  121. spi_write_adv(REG_SROM_Enable, 0x1d);
  122. wait_ms(10);
  123. spi_write_adv(REG_SROM_Enable, 0x18);
  124. spi_start_adv();
  125. spi_write(REG_SROM_Load_Burst | 0x80);
  126. wait_us(15);
  127. unsigned char c;
  128. for (int i = 0; i < firmware_length; i++) {
  129. c = (unsigned char)pgm_read_byte(firmware_data + i);
  130. spi_write(c);
  131. wait_us(15);
  132. }
  133. wait_us(200);
  134. spi_read_adv(REG_SROM_ID);
  135. spi_write_adv(REG_Config2, 0x00);
  136. spi_stop();
  137. wait_ms(10);
  138. }
  139. bool pmw_check_signature(void) {
  140. uint8_t pid = spi_read_adv(REG_Product_ID);
  141. uint8_t iv_pid = spi_read_adv(REG_Inverse_Product_ID);
  142. uint8_t SROM_ver = spi_read_adv(REG_SROM_ID);
  143. return (pid == 0x42 && iv_pid == 0xBD && SROM_ver == 0x04); // signature for SROM 0x04
  144. }
  145. report_pmw_t pmw_read_burst(void) {
  146. if (!_inBurst) {
  147. dprintf("burst on");
  148. spi_write_adv(REG_Motion_Burst, 0x00);
  149. _inBurst = true;
  150. }
  151. spi_start_adv();
  152. spi_write(REG_Motion_Burst);
  153. wait_us(35); // waits for tSRAD
  154. report_pmw_t data;
  155. data.motion = 0;
  156. data.dx = 0;
  157. data.mdx = 0;
  158. data.dy = 0;
  159. data.mdx = 0;
  160. data.motion = spi_read();
  161. spi_write(0x00); // skip Observation
  162. data.dx = spi_read();
  163. data.mdx = spi_read();
  164. data.dy = spi_read();
  165. data.mdy = spi_read();
  166. spi_stop();
  167. print_byte(data.motion);
  168. print_byte(data.dx);
  169. print_byte(data.mdx);
  170. print_byte(data.dy);
  171. print_byte(data.mdy);
  172. dprintf("\n");
  173. data.isMotion = (data.motion & 0x80) != 0;
  174. data.isOnSurface = (data.motion & 0x08) == 0;
  175. data.dx |= (data.mdx << 8);
  176. data.dx = data.dx * -1;
  177. data.dy |= (data.mdy << 8);
  178. data.dy = data.dy * -1;
  179. spi_stop();
  180. if (data.motion & 0b111) { // panic recovery, sometimes burst mode works weird.
  181. _inBurst = false;
  182. }
  183. return data;
  184. }