1
0

adns5050.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "quantum.h"
  21. #include "wait.h"
  22. #ifdef CONSOLE_ENABLE
  23. # include "print.h"
  24. #endif
  25. #ifndef OPTIC_ROTATED
  26. # define OPTIC_ROTATED false
  27. #endif
  28. // Definitions for the ADNS serial line.
  29. // These really ought to be defined in your config.h, but defaults are
  30. // here if you're really lazy.
  31. #ifndef ADNS_SCLK_PIN
  32. # define ADNS_SCLK_PIN B7
  33. #endif
  34. #ifndef ADNS_SDIO_PIN
  35. # define ADNS_SDIO_PIN C6
  36. #endif
  37. #ifndef ADNS_CS_PIN
  38. # define ADNS_CS_PIN B4
  39. #endif
  40. #ifdef CONSOLE_ENABLE
  41. void print_byte(uint8_t byte) { dprintf("%c%c%c%c%c%c%c%c|", (byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), (byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), (byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), (byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')); }
  42. #endif
  43. // Initialize the ADNS serial pins.
  44. void adns_init(void) {
  45. setPinOutput(ADNS_SCLK_PIN);
  46. setPinOutput(ADNS_SDIO_PIN);
  47. setPinOutput(ADNS_CS_PIN);
  48. }
  49. // Perform a synchronization with the ADNS.
  50. // Just as with the serial protocol, this is used by the slave to send a
  51. // synchronization signal to the master.
  52. void adns_sync(void) {
  53. writePinLow(ADNS_CS_PIN);
  54. wait_us(1);
  55. writePinHigh(ADNS_CS_PIN);
  56. }
  57. void adns_cs_select(void) {
  58. writePinLow(ADNS_CS_PIN);
  59. }
  60. void adns_cs_deselect(void) {
  61. writePinHigh(ADNS_CS_PIN);
  62. }
  63. uint8_t adns_serial_read(void) {
  64. setPinInput(ADNS_SDIO_PIN);
  65. uint8_t byte = 0;
  66. for (uint8_t i = 0; i < 8; ++i) {
  67. writePinLow(ADNS_SCLK_PIN);
  68. wait_us(1);
  69. byte = (byte << 1) | readPin(ADNS_SDIO_PIN);
  70. writePinHigh(ADNS_SCLK_PIN);
  71. wait_us(1);
  72. }
  73. return byte;
  74. }
  75. void adns_serial_write(uint8_t data) {
  76. setPinOutput(ADNS_SDIO_PIN);
  77. for (int8_t b = 7; b >= 0; b--) {
  78. writePinLow(ADNS_SCLK_PIN);
  79. if (data & (1 << b))
  80. writePinHigh(ADNS_SDIO_PIN);
  81. else
  82. writePinLow(ADNS_SDIO_PIN);
  83. wait_us(2);
  84. writePinHigh(ADNS_SCLK_PIN);
  85. }
  86. // tSWR. See page 15 of the ADNS spec sheet.
  87. // Technically, this is only necessary if the next operation is an SDIO
  88. // read. This is not guaranteed to be the case, but we're being lazy.
  89. wait_us(4);
  90. // Note that tSWW is never necessary. All write operations require at
  91. // least 32us, which exceeds tSWW, so there's never a need to wait for it.
  92. }
  93. // Read a byte of data from a register on the ADNS.
  94. // Don't forget to use the register map (as defined in the header file).
  95. uint8_t adns_read_reg(uint8_t reg_addr) {
  96. adns_cs_select();
  97. adns_serial_write(reg_addr);
  98. // We don't need a minimum tSRAD here. That's because a 4ms wait time is
  99. // already included in adns_serial_write(), so we're good.
  100. // See page 10 and 15 of the ADNS spec sheet.
  101. //wait_us(4);
  102. uint8_t byte = adns_serial_read();
  103. // tSRW & tSRR. See page 15 of the ADNS spec sheet.
  104. // Technically, this is only necessary if the next operation is an SDIO
  105. // read or write. This is not guaranteed to be the case.
  106. // Honestly, this wait could probably be removed.
  107. wait_us(1);
  108. adns_cs_deselect();
  109. return byte;
  110. }
  111. void adns_write_reg(uint8_t reg_addr, uint8_t data) {
  112. adns_cs_select();
  113. adns_serial_write(reg_addr);
  114. adns_serial_write(data);
  115. adns_cs_deselect();
  116. }
  117. report_adns_t adns_read_burst(void) {
  118. adns_cs_select();
  119. report_adns_t data;
  120. data.dx = 0;
  121. data.dy = 0;
  122. adns_serial_write(REG_MOTION_BURST);
  123. // We don't need a minimum tSRAD here. That's because a 4ms wait time is
  124. // already included in adns_serial_write(), so we're good.
  125. // See page 10 and 15 of the ADNS spec sheet.
  126. //wait_us(4);
  127. uint8_t x = adns_serial_read();
  128. uint8_t y = adns_serial_read();
  129. // Burst mode returns a bunch of other shit that we don't really need.
  130. // Setting CS to high ends burst mode early.
  131. adns_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. // Don't forget to use the definitions for CPI in the header file.
  145. void adns_set_cpi(uint8_t cpi) {
  146. adns_write_reg(REG_MOUSE_CONTROL2, cpi);
  147. }
  148. bool adns_check_signature(void) {
  149. uint8_t pid = adns_read_reg(REG_PRODUCT_ID);
  150. uint8_t rid = adns_read_reg(REG_REVISION_ID);
  151. uint8_t pid2 = adns_read_reg(REG_PRODUCT_ID2);
  152. return (pid == 0x12 && rid == 0x01 && pid2 == 0x26);
  153. }