pmw3360.c 5.2 KB

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