pmw3320.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. void pmw3320_init(void) {
  24. // Initialize sensor serial pins.
  25. gpio_set_pin_output(PMW3320_SCLK_PIN);
  26. gpio_set_pin_output(PMW3320_SDIO_PIN);
  27. gpio_set_pin_output(PMW3320_CS_PIN);
  28. // reboot the sensor.
  29. pmw3320_write_reg(REG_Power_Up_Reset, 0x5a);
  30. // wait maximum time before sensor is ready.
  31. // this ensures that the sensor is actually ready after reset.
  32. wait_ms(55);
  33. // read a burst from the sensor and then discard it.
  34. // gets the sensor ready for write commands
  35. // (for example, setting the dpi).
  36. pmw3320_read_burst();
  37. // Pretty sure that this shouldn't be in the driver.
  38. // Probably device specific?
  39. // Set rest mode to default
  40. pmw3320_write_reg(REG_Rest_Mode_Status, 0x00);
  41. // Set LED to be always on
  42. pmw3320_write_reg(REG_Led_Control, 0x4);
  43. // Disable rest mode
  44. pmw3320_write_reg(REG_Performance, 0x80);
  45. }
  46. // Perform a synchronization with sensor.
  47. // Just as with the serial protocol, this is used by the slave to send a
  48. // synchronization signal to the master.
  49. void pmw3320_sync(void) {
  50. gpio_write_pin_low(PMW3320_CS_PIN);
  51. wait_us(1);
  52. gpio_write_pin_high(PMW3320_CS_PIN);
  53. }
  54. void pmw3320_cs_select(void) {
  55. gpio_write_pin_low(PMW3320_CS_PIN);
  56. }
  57. void pmw3320_cs_deselect(void) {
  58. gpio_write_pin_high(PMW3320_CS_PIN);
  59. }
  60. uint8_t pmw3320_serial_read(void) {
  61. gpio_set_pin_input(PMW3320_SDIO_PIN);
  62. uint8_t byte = 0;
  63. for (uint8_t i = 0; i < 8; ++i) {
  64. gpio_write_pin_low(PMW3320_SCLK_PIN);
  65. wait_us(1);
  66. byte = (byte << 1) | gpio_read_pin(PMW3320_SDIO_PIN);
  67. gpio_write_pin_high(PMW3320_SCLK_PIN);
  68. wait_us(1);
  69. }
  70. return byte;
  71. }
  72. void pmw3320_serial_write(uint8_t data) {
  73. gpio_set_pin_output(PMW3320_SDIO_PIN);
  74. for (int8_t b = 7; b >= 0; b--) {
  75. gpio_write_pin_low(PMW3320_SCLK_PIN);
  76. if (data & (1 << b))
  77. gpio_write_pin_high(PMW3320_SDIO_PIN);
  78. else
  79. gpio_write_pin_low(PMW3320_SDIO_PIN);
  80. wait_us(2);
  81. gpio_write_pin_high(PMW3320_SCLK_PIN);
  82. }
  83. // This was taken from ADNS5050 driver.
  84. // There's no any info in PMW3320 datasheet about this...
  85. // tSWR. See page 15 of the ADNS5050 spec sheet.
  86. // Technically, this is only necessary if the next operation is an SDIO
  87. // read. This is not guaranteed to be the case, but we're being lazy.
  88. wait_us(4);
  89. // Note that tSWW is never necessary. All write operations require at
  90. // least 32us, which exceeds tSWW, so there's never a need to wait for it.
  91. }
  92. // Read a byte of data from a register on the sensor.
  93. uint8_t pmw3320_read_reg(uint8_t reg_addr) {
  94. pmw3320_cs_select();
  95. pmw3320_serial_write(reg_addr);
  96. uint8_t byte = pmw3320_serial_read();
  97. // This was taken directly from ADNS5050 driver...
  98. // tSRW & tSRR. See page 15 of the ADNS5050 spec sheet.
  99. // Technically, this is only necessary if the next operation is an SDIO
  100. // read or write. This is not guaranteed to be the case.
  101. // Honestly, this wait could probably be removed.
  102. wait_us(1);
  103. pmw3320_cs_deselect();
  104. return byte;
  105. }
  106. void pmw3320_write_reg(uint8_t reg_addr, uint8_t data) {
  107. pmw3320_cs_select();
  108. pmw3320_serial_write(0b10000000 | reg_addr);
  109. pmw3320_serial_write(data);
  110. pmw3320_cs_deselect();
  111. }
  112. report_pmw3320_t pmw3320_read_burst(void) {
  113. pmw3320_cs_select();
  114. report_pmw3320_t data;
  115. data.dx = 0;
  116. data.dy = 0;
  117. pmw3320_serial_write(REG_Motion_Burst);
  118. uint8_t x = pmw3320_serial_read();
  119. uint8_t y = pmw3320_serial_read();
  120. // Probably burst mode may include contents of delta_xy register,
  121. // which contain HI parts of x/y deltas, but I had no luck finding it.
  122. // Probably it's required to activate 12-bit mode to access this data.
  123. // So we end burst mode early to not read unneeded information.
  124. pmw3320_cs_deselect();
  125. data.dx = convert_twoscomp(x);
  126. data.dy = convert_twoscomp(y);
  127. return data;
  128. }
  129. // Convert a two's complement byte from an unsigned data type into a signed
  130. // data type.
  131. int8_t convert_twoscomp(uint8_t data) {
  132. if ((data & 0x80) == 0x80)
  133. return -128 + (data & 0x7F);
  134. else
  135. return data;
  136. }
  137. uint16_t pmw3320_get_cpi(void) {
  138. uint8_t cpival = pmw3320_read_reg(REG_Resolution);
  139. // 0x1F is an inversion of 0x20 which is 0b100000
  140. return (uint16_t)((cpival & 0x1F) * PMW3320_CPI_STEP);
  141. }
  142. void pmw3320_set_cpi(uint16_t cpi) {
  143. uint8_t cpival = constrain((cpi / PMW3320_CPI_STEP), (PMW3320_CPI_MIN / PMW3320_CPI_STEP), (PMW3320_CPI_MAX / PMW3320_CPI_STEP)) - 1U;
  144. // Fifth bit is probably a control bit.
  145. // PMW3320 datasheet don't have any info on this, so this is a pure guess.
  146. pmw3320_write_reg(REG_Resolution, 0x20 | cpival);
  147. }
  148. bool pmw3320_check_signature(void) {
  149. uint8_t pid = pmw3320_read_reg(REG_Product_ID);
  150. uint8_t pid2 = pmw3320_read_reg(REG_Inverse_Product_ID);
  151. return (pid == 0x3b && pid2 == 0xc4);
  152. }