adns5050.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "adns5050.h"
  20. #include "wait.h"
  21. #include "debug.h"
  22. #include "gpio.h"
  23. #include "pointing_device_internal.h"
  24. // Registers
  25. // clang-format off
  26. #define REG_PRODUCT_ID 0x00
  27. #define REG_REVISION_ID 0x01
  28. #define REG_MOTION 0x02
  29. #define REG_DELTA_X 0x03
  30. #define REG_DELTA_Y 0x04
  31. #define REG_SQUAL 0x05
  32. #define REG_SHUTTER_UPPER 0x06
  33. #define REG_SHUTTER_LOWER 0x07
  34. #define REG_MAXIMUM_PIXEL 0x08
  35. #define REG_PIXEL_SUM 0x09
  36. #define REG_MINIMUM_PIXEL 0x0a
  37. #define REG_PIXEL_GRAB 0x0b
  38. #define REG_MOUSE_CONTROL 0x0d
  39. #define REG_MOUSE_CONTROL2 0x19
  40. #define REG_LED_DC_MODE 0x22
  41. #define REG_CHIP_RESET 0x3a
  42. #define REG_PRODUCT_ID2 0x3e
  43. #define REG_INV_REV_ID 0x3f
  44. #define REG_MOTION_BURST 0x63
  45. // clang-format on
  46. const pointing_device_driver_t adns5050_pointing_device_driver = {
  47. .init = adns5050_init,
  48. .get_report = adns5050_get_report,
  49. .set_cpi = adns5050_set_cpi,
  50. .get_cpi = adns5050_get_cpi,
  51. };
  52. static bool powered_down = false;
  53. bool adns5050_init(void) {
  54. // Initialize the ADNS serial pins.
  55. gpio_set_pin_output(ADNS5050_SCLK_PIN);
  56. gpio_set_pin_output(ADNS5050_SDIO_PIN);
  57. gpio_set_pin_output(ADNS5050_CS_PIN);
  58. // reboot the adns.
  59. // if the adns hasn't initialized yet, this is harmless.
  60. adns5050_write_reg(REG_CHIP_RESET, 0x5a);
  61. // wait maximum time before adns is ready.
  62. // this ensures that the adns is actuall ready after reset.
  63. wait_ms(55);
  64. powered_down = false;
  65. // read a burst from the adns and then discard it.
  66. // gets the adns ready for write commands
  67. // (for example, setting the dpi).
  68. adns5050_read_burst();
  69. return adns5050_check_signature();
  70. }
  71. // Perform a synchronization with the ADNS.
  72. // Just as with the serial protocol, this is used by the slave to send a
  73. // synchronization signal to the master.
  74. void adns5050_sync(void) {
  75. gpio_write_pin_low(ADNS5050_CS_PIN);
  76. wait_us(1);
  77. gpio_write_pin_high(ADNS5050_CS_PIN);
  78. }
  79. void adns5050_cs_select(void) {
  80. gpio_write_pin_low(ADNS5050_CS_PIN);
  81. }
  82. void adns5050_cs_deselect(void) {
  83. gpio_write_pin_high(ADNS5050_CS_PIN);
  84. }
  85. uint8_t adns5050_serial_read(void) {
  86. gpio_set_pin_input(ADNS5050_SDIO_PIN);
  87. uint8_t byte = 0;
  88. for (uint8_t i = 0; i < 8; ++i) {
  89. gpio_write_pin_low(ADNS5050_SCLK_PIN);
  90. wait_us(1);
  91. byte = (byte << 1) | gpio_read_pin(ADNS5050_SDIO_PIN);
  92. gpio_write_pin_high(ADNS5050_SCLK_PIN);
  93. wait_us(1);
  94. }
  95. return byte;
  96. }
  97. void adns5050_serial_write(uint8_t data) {
  98. gpio_set_pin_output(ADNS5050_SDIO_PIN);
  99. for (int8_t b = 7; b >= 0; b--) {
  100. gpio_write_pin_low(ADNS5050_SCLK_PIN);
  101. if (data & (1 << b))
  102. gpio_write_pin_high(ADNS5050_SDIO_PIN);
  103. else
  104. gpio_write_pin_low(ADNS5050_SDIO_PIN);
  105. wait_us(2);
  106. gpio_write_pin_high(ADNS5050_SCLK_PIN);
  107. }
  108. // tSWR. See page 15 of the ADNS spec sheet.
  109. // Technically, this is only necessary if the next operation is an SDIO
  110. // read. This is not guaranteed to be the case, but we're being lazy.
  111. wait_us(4);
  112. // Note that tSWW is never necessary. All write operations require at
  113. // least 32us, which exceeds tSWW, so there's never a need to wait for it.
  114. }
  115. // Read a byte of data from a register on the ADNS.
  116. // Don't forget to use the register map (as defined in the header file).
  117. uint8_t adns5050_read_reg(uint8_t reg_addr) {
  118. adns5050_cs_select();
  119. adns5050_serial_write(reg_addr);
  120. // We don't need a minimum tSRAD here. That's because a 4ms wait time is
  121. // already included in adns5050_serial_write(), so we're good.
  122. // See page 10 and 15 of the ADNS spec sheet.
  123. // wait_us(4);
  124. uint8_t byte = adns5050_serial_read();
  125. // tSRW & tSRR. See page 15 of the ADNS spec sheet.
  126. // Technically, this is only necessary if the next operation is an SDIO
  127. // read or write. This is not guaranteed to be the case.
  128. // Honestly, this wait could probably be removed.
  129. wait_us(1);
  130. adns5050_cs_deselect();
  131. return byte;
  132. }
  133. void adns5050_write_reg(uint8_t reg_addr, uint8_t data) {
  134. adns5050_cs_select();
  135. adns5050_serial_write(0b10000000 | reg_addr);
  136. adns5050_serial_write(data);
  137. adns5050_cs_deselect();
  138. }
  139. report_adns5050_t adns5050_read_burst(void) {
  140. adns5050_cs_select();
  141. report_adns5050_t data;
  142. data.dx = 0;
  143. data.dy = 0;
  144. if (powered_down) {
  145. return data;
  146. }
  147. adns5050_serial_write(REG_MOTION_BURST);
  148. // We don't need a minimum tSRAD here. That's because a 4ms wait time is
  149. // already included in adns5050_serial_write(), so we're good.
  150. // See page 10 and 15 of the ADNS spec sheet.
  151. // wait_us(4);
  152. uint8_t x = adns5050_serial_read();
  153. uint8_t y = adns5050_serial_read();
  154. // Burst mode returns a bunch of other shit that we don't really need.
  155. // Setting CS to high ends burst mode early.
  156. adns5050_cs_deselect();
  157. data.dx = convert_twoscomp(x);
  158. data.dy = convert_twoscomp(y);
  159. return data;
  160. }
  161. // Convert a two's complement byte from an unsigned data type into a signed
  162. // data type.
  163. int8_t convert_twoscomp(uint8_t data) {
  164. if ((data & 0x80) == 0x80)
  165. return -128 + (data & 0x7F);
  166. else
  167. return data;
  168. }
  169. // Don't forget to use the definitions for CPI in the header file.
  170. void adns5050_set_cpi(uint16_t cpi) {
  171. uint8_t cpival = constrain((cpi / 125), 0x1, 0xD); // limits to 0--119
  172. adns5050_write_reg(REG_MOUSE_CONTROL2, 0b10000 | cpival);
  173. }
  174. uint16_t adns5050_get_cpi(void) {
  175. uint8_t cpival = adns5050_read_reg(REG_MOUSE_CONTROL2);
  176. return (uint16_t)((cpival & 0b10000) * 125);
  177. }
  178. bool __attribute__((weak)) adns5050_check_signature(void) {
  179. uint8_t pid = adns5050_read_reg(REG_PRODUCT_ID);
  180. uint8_t rid = adns5050_read_reg(REG_REVISION_ID);
  181. uint8_t pid2 = adns5050_read_reg(REG_PRODUCT_ID2);
  182. return (pid == 0x12 && rid == 0x01 && pid2 == 0x26);
  183. }
  184. void adns5050_power_down(void) {
  185. if (!powered_down) {
  186. powered_down = true;
  187. adns5050_write_reg(REG_MOUSE_CONTROL, 0b10);
  188. }
  189. }
  190. report_mouse_t adns5050_get_report(report_mouse_t mouse_report) {
  191. report_adns5050_t data = adns5050_read_burst();
  192. if (data.dx != 0 || data.dy != 0) {
  193. pd_dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy);
  194. mouse_report.x = (mouse_xy_report_t)data.dx;
  195. mouse_report.y = (mouse_xy_report_t)data.dy;
  196. }
  197. return mouse_report;
  198. }