paw3204.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright 2021 Gompa (@Gompa)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // https://github.com/shinoaliceKabocha/choco60_track/tree/master/keymaps/default
  17. #include "paw3204.h"
  18. #include "wait.h"
  19. #include "debug.h"
  20. #include "gpio.h"
  21. #define REG_PID1 0x00
  22. #define REG_PID2 0x01
  23. #define REG_STAT 0x02
  24. #define REG_X 0x03
  25. #define REG_Y 0x04
  26. #define REG_SETUP 0x06
  27. #define REG_IMGQUAL 0x07
  28. #define REG_IMGREC 0x0E
  29. #define REG_IMGTRASH 0x0D
  30. #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
  31. // CPI values
  32. enum cpi_values {
  33. CPI400, // 0b000
  34. CPI500, // 0b001
  35. CPI600, // 0b010
  36. CPI800, // 0b011
  37. CPI1000, // 0b100
  38. CPI1200, // 0b101
  39. CPI1600, // 0b110
  40. };
  41. uint8_t paw3204_serial_read(void);
  42. void paw3204_serial_write(uint8_t reg_addr);
  43. uint8_t paw3204_read_reg(uint8_t reg_addr);
  44. void paw3204_write_reg(uint8_t reg_addr, uint8_t data);
  45. void paw3204_init(void) {
  46. setPinOutput(PAW3204_SCLK_PIN); // setclockpin to output
  47. setPinInputHigh(PAW3204_SDIO_PIN); // set datapin input high
  48. paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi
  49. wait_us(5);
  50. paw3204_read_reg(0x00); // read id
  51. paw3204_read_reg(0x01); // read id2
  52. // PAW3204_write_reg(REG_SETUP,0x06); // dont reset sensor and set cpi 1600
  53. paw3204_write_reg(REG_IMGTRASH, 0x32); // write image trashhold
  54. }
  55. uint8_t paw3204_serial_read(void) {
  56. setPinInput(PAW3204_SDIO_PIN);
  57. uint8_t byte = 0;
  58. for (uint8_t i = 0; i < 8; ++i) {
  59. writePinLow(PAW3204_SCLK_PIN);
  60. wait_us(1);
  61. byte = (byte << 1) | readPin(PAW3204_SDIO_PIN);
  62. writePinHigh(PAW3204_SCLK_PIN);
  63. wait_us(1);
  64. }
  65. return byte;
  66. }
  67. void paw3204_serial_write(uint8_t data) {
  68. writePinLow(PAW3204_SDIO_PIN);
  69. setPinOutput(PAW3204_SDIO_PIN);
  70. for (int8_t b = 7; b >= 0; b--) {
  71. writePinLow(PAW3204_SCLK_PIN);
  72. if (data & (1 << b)) {
  73. writePinHigh(PAW3204_SDIO_PIN);
  74. } else {
  75. writePinLow(PAW3204_SDIO_PIN);
  76. }
  77. writePinHigh(PAW3204_SCLK_PIN);
  78. }
  79. wait_us(4);
  80. }
  81. report_paw3204_t paw3204_read(void) {
  82. report_paw3204_t data = {0};
  83. data.isMotion = paw3204_read_reg(REG_STAT) & (1 << 7); // check for motion only (bit 7 in field)
  84. data.x = (int8_t)paw3204_read_reg(REG_X);
  85. data.y = (int8_t)paw3204_read_reg(REG_Y);
  86. return data;
  87. }
  88. void paw3204_write_reg(uint8_t reg_addr, uint8_t data) {
  89. paw3204_serial_write(0b10000000 | reg_addr);
  90. paw3204_serial_write(data);
  91. }
  92. uint8_t paw3204_read_reg(uint8_t reg_addr) {
  93. paw3204_serial_write(reg_addr);
  94. wait_us(5);
  95. return paw3204_serial_read();
  96. }
  97. void paw3204_set_cpi(uint16_t cpi) {
  98. uint8_t cpival = CPI1000;
  99. if (cpi <= 450) {
  100. cpival = CPI400;
  101. } else if (cpi <= 550) {
  102. cpival = CPI500;
  103. } else if (cpi <= 700) {
  104. cpival = CPI600;
  105. } else if (cpi <= 900) {
  106. cpival = CPI800;
  107. } else if (cpi <= 1100) {
  108. cpival = CPI1000;
  109. } else if (cpi <= 1400) {
  110. cpival = CPI1200;
  111. } else if (cpi > 1400) {
  112. cpival = CPI1600;
  113. }
  114. paw3204_write_reg(REG_SETUP, cpival);
  115. }
  116. uint16_t paw3204_get_cpi(void) {
  117. uint16_t cpival = 1000;
  118. switch (paw3204_read_reg(REG_SETUP) & 0b111) {
  119. case CPI400:
  120. cpival = 400;
  121. break;
  122. case CPI500:
  123. cpival = 500;
  124. break;
  125. case CPI600:
  126. cpival = 600;
  127. break;
  128. case CPI800:
  129. cpival = 800;
  130. break;
  131. case CPI1000:
  132. cpival = 1000;
  133. break;
  134. case CPI1200:
  135. cpival = 1200;
  136. break;
  137. case CPI1600:
  138. cpival = 1600;
  139. break;
  140. }
  141. return cpival;
  142. }
  143. uint8_t read_pid_paw3204(void) {
  144. return paw3204_read_reg(REG_PID1);
  145. }