logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

pmw3320.c (6524B)


  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_drivera = {
  25. .init = pmw3320_init,
  26. .get_report = pmw3320_get_report,
  27. .set_cpi = pmw3320_set_cpi,
  28. .get_cpi = pmw3320_get_cpi,
  29. };
  30. void 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. }
  53. // Perform a synchronization with sensor.
  54. // Just as with the serial protocol, this is used by the slave to send a
  55. // synchronization signal to the master.
  56. void pmw3320_sync(void) {
  57. gpio_write_pin_low(PMW3320_CS_PIN);
  58. wait_us(1);
  59. gpio_write_pin_high(PMW3320_CS_PIN);
  60. }
  61. void pmw3320_cs_select(void) {
  62. gpio_write_pin_low(PMW3320_CS_PIN);
  63. }
  64. void pmw3320_cs_deselect(void) {
  65. gpio_write_pin_high(PMW3320_CS_PIN);
  66. }
  67. uint8_t pmw3320_serial_read(void) {
  68. gpio_set_pin_input(PMW3320_SDIO_PIN);
  69. uint8_t byte = 0;
  70. for (uint8_t i = 0; i < 8; ++i) {
  71. gpio_write_pin_low(PMW3320_SCLK_PIN);
  72. wait_us(1);
  73. byte = (byte << 1) | gpio_read_pin(PMW3320_SDIO_PIN);
  74. gpio_write_pin_high(PMW3320_SCLK_PIN);
  75. wait_us(1);
  76. }
  77. return byte;
  78. }
  79. void pmw3320_serial_write(uint8_t data) {
  80. gpio_set_pin_output(PMW3320_SDIO_PIN);
  81. for (int8_t b = 7; b >= 0; b--) {
  82. gpio_write_pin_low(PMW3320_SCLK_PIN);
  83. if (data & (1 << b))
  84. gpio_write_pin_high(PMW3320_SDIO_PIN);
  85. else
  86. gpio_write_pin_low(PMW3320_SDIO_PIN);
  87. wait_us(2);
  88. gpio_write_pin_high(PMW3320_SCLK_PIN);
  89. }
  90. // This was taken from ADNS5050 driver.
  91. // There's no any info in PMW3320 datasheet about this...
  92. // tSWR. See page 15 of the ADNS5050 spec sheet.
  93. // Technically, this is only necessary if the next operation is an SDIO
  94. // read. This is not guaranteed to be the case, but we're being lazy.
  95. wait_us(4);
  96. // Note that tSWW is never necessary. All write operations require at
  97. // least 32us, which exceeds tSWW, so there's never a need to wait for it.
  98. }
  99. // Read a byte of data from a register on the sensor.
  100. uint8_t pmw3320_read_reg(uint8_t reg_addr) {
  101. pmw3320_cs_select();
  102. pmw3320_serial_write(reg_addr);
  103. uint8_t byte = pmw3320_serial_read();
  104. // This was taken directly from ADNS5050 driver...
  105. // tSRW & tSRR. See page 15 of the ADNS5050 spec sheet.
  106. // Technically, this is only necessary if the next operation is an SDIO
  107. // read or write. This is not guaranteed to be the case.
  108. // Honestly, this wait could probably be removed.
  109. wait_us(1);
  110. pmw3320_cs_deselect();
  111. return byte;
  112. }
  113. void pmw3320_write_reg(uint8_t reg_addr, uint8_t data) {
  114. pmw3320_cs_select();
  115. pmw3320_serial_write(0b10000000 | reg_addr);
  116. pmw3320_serial_write(data);
  117. pmw3320_cs_deselect();
  118. }
  119. report_pmw3320_t pmw3320_read_burst(void) {
  120. pmw3320_cs_select();
  121. report_pmw3320_t data;
  122. data.dx = 0;
  123. data.dy = 0;
  124. pmw3320_serial_write(REG_Motion_Burst);
  125. uint8_t x = pmw3320_serial_read();
  126. uint8_t y = pmw3320_serial_read();
  127. // Probably burst mode may include contents of delta_xy register,
  128. // which contain HI parts of x/y deltas, but I had no luck finding it.
  129. // Probably it's required to activate 12-bit mode to access this data.
  130. // So we end burst mode early to not read unneeded information.
  131. pmw3320_cs_deselect();
  132. data.dx = convert_twoscomp(x);
  133. data.dy = convert_twoscomp(y);
  134. return data;
  135. }
  136. // Convert a two's complement byte from an unsigned data type into a signed
  137. // data type.
  138. int8_t convert_twoscomp(uint8_t data) {
  139. if ((data & 0x80) == 0x80)
  140. return -128 + (data & 0x7F);
  141. else
  142. return data;
  143. }
  144. uint16_t pmw3320_get_cpi(void) {
  145. uint8_t cpival = pmw3320_read_reg(REG_Resolution);
  146. // 0x1F is an inversion of 0x20 which is 0b100000
  147. return (uint16_t)((cpival & 0x1F) * PMW3320_CPI_STEP);
  148. }
  149. void pmw3320_set_cpi(uint16_t cpi) {
  150. uint8_t cpival = constrain((cpi / PMW3320_CPI_STEP), (PMW3320_CPI_MIN / PMW3320_CPI_STEP), (PMW3320_CPI_MAX / PMW3320_CPI_STEP)) - 1U;
  151. // Fifth bit is probably a control bit.
  152. // PMW3320 datasheet don't have any info on this, so this is a pure guess.
  153. pmw3320_write_reg(REG_Resolution, 0x20 | cpival);
  154. }
  155. bool pmw3320_check_signature(void) {
  156. uint8_t pid = pmw3320_read_reg(REG_Product_ID);
  157. uint8_t pid2 = pmw3320_read_reg(REG_Inverse_Product_ID);
  158. return (pid == 0x3b && pid2 == 0xc4);
  159. }
  160. report_mouse_t pmw3320_get_report(report_mouse_t mouse_report) {
  161. report_pmw3320_t data = pmw3320_read_burst();
  162. if (data.dx != 0 || data.dy != 0) {
  163. pd_dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  164. mouse_report.x = (mouse_xy_report_t)data.dx;
  165. mouse_report.y = (mouse_xy_report_t)data.dy;
  166. }
  167. return mouse_report;
  168. }