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.

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