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.

209 lines
6.1 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) { writePinLow(ADNS5050_CS_PIN); }
  70. void adns5050_cs_deselect(void) { writePinHigh(ADNS5050_CS_PIN); }
  71. uint8_t adns5050_serial_read(void) {
  72. setPinInput(ADNS5050_SDIO_PIN);
  73. uint8_t byte = 0;
  74. for (uint8_t i = 0; i < 8; ++i) {
  75. writePinLow(ADNS5050_SCLK_PIN);
  76. wait_us(1);
  77. byte = (byte << 1) | readPin(ADNS5050_SDIO_PIN);
  78. writePinHigh(ADNS5050_SCLK_PIN);
  79. wait_us(1);
  80. }
  81. return byte;
  82. }
  83. void adns5050_serial_write(uint8_t data) {
  84. setPinOutput(ADNS5050_SDIO_PIN);
  85. for (int8_t b = 7; b >= 0; b--) {
  86. writePinLow(ADNS5050_SCLK_PIN);
  87. if (data & (1 << b))
  88. writePinHigh(ADNS5050_SDIO_PIN);
  89. else
  90. writePinLow(ADNS5050_SDIO_PIN);
  91. wait_us(2);
  92. writePinHigh(ADNS5050_SCLK_PIN);
  93. }
  94. // tSWR. See page 15 of the ADNS spec sheet.
  95. // Technically, this is only necessary if the next operation is an SDIO
  96. // read. This is not guaranteed to be the case, but we're being lazy.
  97. wait_us(4);
  98. // Note that tSWW is never necessary. All write operations require at
  99. // least 32us, which exceeds tSWW, so there's never a need to wait for it.
  100. }
  101. // Read a byte of data from a register on the ADNS.
  102. // Don't forget to use the register map (as defined in the header file).
  103. uint8_t adns5050_read_reg(uint8_t reg_addr) {
  104. adns5050_cs_select();
  105. adns5050_serial_write(reg_addr);
  106. // We don't need a minimum tSRAD here. That's because a 4ms wait time is
  107. // already included in adns5050_serial_write(), so we're good.
  108. // See page 10 and 15 of the ADNS spec sheet.
  109. // wait_us(4);
  110. uint8_t byte = adns5050_serial_read();
  111. // tSRW & tSRR. See page 15 of the ADNS spec sheet.
  112. // Technically, this is only necessary if the next operation is an SDIO
  113. // read or write. This is not guaranteed to be the case.
  114. // Honestly, this wait could probably be removed.
  115. wait_us(1);
  116. adns5050_cs_deselect();
  117. return byte;
  118. }
  119. void adns5050_write_reg(uint8_t reg_addr, uint8_t data) {
  120. adns5050_cs_select();
  121. adns5050_serial_write(0b10000000 | reg_addr);
  122. adns5050_serial_write(data);
  123. adns5050_cs_deselect();
  124. }
  125. report_adns5050_t adns5050_read_burst(void) {
  126. adns5050_cs_select();
  127. report_adns5050_t data;
  128. data.dx = 0;
  129. data.dy = 0;
  130. adns5050_serial_write(REG_MOTION_BURST);
  131. // We don't need a minimum tSRAD here. That's because a 4ms wait time is
  132. // already included in adns5050_serial_write(), so we're good.
  133. // See page 10 and 15 of the ADNS spec sheet.
  134. // wait_us(4);
  135. uint8_t x = adns5050_serial_read();
  136. uint8_t y = adns5050_serial_read();
  137. // Burst mode returns a bunch of other shit that we don't really need.
  138. // Setting CS to high ends burst mode early.
  139. adns5050_cs_deselect();
  140. data.dx = convert_twoscomp(x);
  141. data.dy = convert_twoscomp(y);
  142. return data;
  143. }
  144. // Convert a two's complement byte from an unsigned data type into a signed
  145. // data type.
  146. int8_t convert_twoscomp(uint8_t data) {
  147. if ((data & 0x80) == 0x80)
  148. return -128 + (data & 0x7F);
  149. else
  150. return data;
  151. }
  152. // Don't forget to use the definitions for CPI in the header file.
  153. void adns5050_set_cpi(uint16_t cpi) {
  154. uint8_t cpival = constrain((cpi / 125), 0x1, 0xD); // limits to 0--119
  155. adns5050_write_reg(REG_MOUSE_CONTROL2, 0b10000 | cpival);
  156. }
  157. uint16_t adns5050_get_cpi(void) {
  158. uint8_t cpival = adns5050_read_reg(REG_MOUSE_CONTROL2);
  159. return (uint16_t)((cpival & 0b10000) * 125);
  160. }
  161. bool adns5050_check_signature(void) {
  162. uint8_t pid = adns5050_read_reg(REG_PRODUCT_ID);
  163. uint8_t rid = adns5050_read_reg(REG_REVISION_ID);
  164. uint8_t pid2 = adns5050_read_reg(REG_PRODUCT_ID2);
  165. return (pid == 0x12 && rid == 0x01 && pid2 == 0x26);
  166. }