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.

213 lines
6.2 KiB

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