pmw3320.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Copyright 2021 Colin Lam (Ploopy Corporation)
  2. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  3. * Copyright 2019 Sunjun Kim
  4. * Copyright 2019 Hiroyuki Okada
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "pmw3320.h"
  20. #include "wait.h"
  21. #include "debug.h"
  22. #include "gpio.h"
  23. #include "pointing_device_internal.h"
  24. const pointing_device_driver_t pmw3320_pointing_device_driver = {
  25. .init = pmw3320_init,
  26. .get_report = pmw3320_get_report,
  27. .set_cpi = pmw3320_set_cpi,
  28. .get_cpi = pmw3320_get_cpi,
  29. };
  30. bool pmw3320_init(void) {
  31. // Initialize sensor serial pins.
  32. gpio_set_pin_output(PMW3320_SCLK_PIN);
  33. gpio_set_pin_output(PMW3320_SDIO_PIN);
  34. gpio_set_pin_output(PMW3320_CS_PIN);
  35. // reboot the sensor.
  36. pmw3320_write_reg(REG_Power_Up_Reset, 0x5a);
  37. // wait maximum time before sensor is ready.
  38. // this ensures that the sensor is actually ready after reset.
  39. wait_ms(55);
  40. // read a burst from the sensor and then discard it.
  41. // gets the sensor ready for write commands
  42. // (for example, setting the dpi).
  43. pmw3320_read_burst();
  44. // Pretty sure that this shouldn't be in the driver.
  45. // Probably device specific?
  46. // Set rest mode to default
  47. pmw3320_write_reg(REG_Rest_Mode_Status, 0x00);
  48. // Set LED to be always on
  49. pmw3320_write_reg(REG_Led_Control, 0x4);
  50. // Disable rest mode
  51. pmw3320_write_reg(REG_Performance, 0x80);
  52. return pmw3320_check_signature();
  53. }
  54. // Perform a synchronization with sensor.
  55. // Just as with the serial protocol, this is used by the slave to send a
  56. // synchronization signal to the master.
  57. void pmw3320_sync(void) {
  58. gpio_write_pin_low(PMW3320_CS_PIN);
  59. wait_us(1);
  60. gpio_write_pin_high(PMW3320_CS_PIN);
  61. }
  62. void pmw3320_cs_select(void) {
  63. gpio_write_pin_low(PMW3320_CS_PIN);
  64. }
  65. void pmw3320_cs_deselect(void) {
  66. gpio_write_pin_high(PMW3320_CS_PIN);
  67. }
  68. uint8_t pmw3320_serial_read(void) {
  69. gpio_set_pin_input(PMW3320_SDIO_PIN);
  70. uint8_t byte = 0;
  71. for (uint8_t i = 0; i < 8; ++i) {
  72. gpio_write_pin_low(PMW3320_SCLK_PIN);
  73. wait_us(1);
  74. byte = (byte << 1) | gpio_read_pin(PMW3320_SDIO_PIN);
  75. gpio_write_pin_high(PMW3320_SCLK_PIN);
  76. wait_us(1);
  77. }
  78. return byte;
  79. }
  80. void pmw3320_serial_write(uint8_t data) {
  81. gpio_set_pin_output(PMW3320_SDIO_PIN);
  82. for (int8_t b = 7; b >= 0; b--) {
  83. gpio_write_pin_low(PMW3320_SCLK_PIN);
  84. if (data & (1 << b))
  85. gpio_write_pin_high(PMW3320_SDIO_PIN);
  86. else
  87. gpio_write_pin_low(PMW3320_SDIO_PIN);
  88. wait_us(2);
  89. gpio_write_pin_high(PMW3320_SCLK_PIN);
  90. }
  91. // This was taken from ADNS5050 driver.
  92. // There's no any info in PMW3320 datasheet about this...
  93. // tSWR. See page 15 of the ADNS5050 spec sheet.
  94. // Technically, this is only necessary if the next operation is an SDIO
  95. // read. This is not guaranteed to be the case, but we're being lazy.
  96. wait_us(4);
  97. // Note that tSWW is never necessary. All write operations require at
  98. // least 32us, which exceeds tSWW, so there's never a need to wait for it.
  99. }
  100. // Read a byte of data from a register on the sensor.
  101. uint8_t pmw3320_read_reg(uint8_t reg_addr) {
  102. pmw3320_cs_select();
  103. pmw3320_serial_write(reg_addr);
  104. uint8_t byte = pmw3320_serial_read();
  105. // This was taken directly from ADNS5050 driver...
  106. // tSRW & tSRR. See page 15 of the ADNS5050 spec sheet.
  107. // Technically, this is only necessary if the next operation is an SDIO
  108. // read or write. This is not guaranteed to be the case.
  109. // Honestly, this wait could probably be removed.
  110. wait_us(1);
  111. pmw3320_cs_deselect();
  112. return byte;
  113. }
  114. void pmw3320_write_reg(uint8_t reg_addr, uint8_t data) {
  115. pmw3320_cs_select();
  116. pmw3320_serial_write(0b10000000 | reg_addr);
  117. pmw3320_serial_write(data);
  118. pmw3320_cs_deselect();
  119. }
  120. report_pmw3320_t pmw3320_read_burst(void) {
  121. pmw3320_cs_select();
  122. report_pmw3320_t data;
  123. data.dx = 0;
  124. data.dy = 0;
  125. pmw3320_serial_write(REG_Motion_Burst);
  126. uint8_t x = pmw3320_serial_read();
  127. uint8_t y = pmw3320_serial_read();
  128. // Probably burst mode may include contents of delta_xy register,
  129. // which contain HI parts of x/y deltas, but I had no luck finding it.
  130. // Probably it's required to activate 12-bit mode to access this data.
  131. // So we end burst mode early to not read unneeded information.
  132. pmw3320_cs_deselect();
  133. data.dx = convert_twoscomp(x);
  134. data.dy = convert_twoscomp(y);
  135. return data;
  136. }
  137. // Convert a two's complement byte from an unsigned data type into a signed
  138. // data type.
  139. int8_t convert_twoscomp(uint8_t data) {
  140. if ((data & 0x80) == 0x80)
  141. return -128 + (data & 0x7F);
  142. else
  143. return data;
  144. }
  145. uint16_t pmw3320_get_cpi(void) {
  146. uint8_t cpival = pmw3320_read_reg(REG_Resolution);
  147. // 0x1F is an inversion of 0x20 which is 0b100000
  148. return (uint16_t)((cpival & 0x1F) * PMW3320_CPI_STEP);
  149. }
  150. void pmw3320_set_cpi(uint16_t cpi) {
  151. uint8_t cpival = constrain((cpi / PMW3320_CPI_STEP), (PMW3320_CPI_MIN / PMW3320_CPI_STEP), (PMW3320_CPI_MAX / PMW3320_CPI_STEP)) - 1U;
  152. // Fifth bit is probably a control bit.
  153. // PMW3320 datasheet don't have any info on this, so this is a pure guess.
  154. pmw3320_write_reg(REG_Resolution, 0x20 | cpival);
  155. }
  156. bool __attribute__((weak)) pmw3320_check_signature(void) {
  157. uint8_t pid = pmw3320_read_reg(REG_Product_ID);
  158. uint8_t pid2 = pmw3320_read_reg(REG_Inverse_Product_ID);
  159. return (pid == 0x3b && pid2 == 0xc4);
  160. }
  161. report_mouse_t pmw3320_get_report(report_mouse_t mouse_report) {
  162. report_pmw3320_t data = pmw3320_read_burst();
  163. if (data.dx != 0 || data.dy != 0) {
  164. pd_dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  165. mouse_report.x = (mouse_xy_report_t)data.dx;
  166. mouse_report.y = (mouse_xy_report_t)data.dy;
  167. }
  168. return mouse_report;
  169. }