matrix.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright 2022 @ Keychron (https://www.keychron.com)
  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. #include "matrix.h"
  17. #include "quantum.h"
  18. // Pin connected to DS of 74HC595
  19. #define DATA_PIN C15
  20. // Pin connected to SH_CP of 74HC595
  21. #define CLOCK_PIN A1
  22. // Pin connected to ST_CP of 74HC595
  23. #define LATCH_PIN A0
  24. #ifdef MATRIX_ROW_PINS
  25. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  26. #endif // MATRIX_ROW_PINS
  27. #ifdef MATRIX_COL_PINS
  28. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  29. #endif // MATRIX_COL_PINS
  30. #define ROWS_PER_HAND (MATRIX_ROWS)
  31. #ifndef NO_PIN_NUM
  32. # define NO_PIN_NUM 8
  33. #endif
  34. #ifndef NO_PIN_OFFSET
  35. # define NO_PIN_OFFSET 0
  36. #endif
  37. #ifndef CLR_REG_VAL
  38. # define CLR_REG_VAL 0xFF
  39. #endif
  40. static inline void setPinOutput_writeLow(pin_t pin) {
  41. ATOMIC_BLOCK_FORCEON {
  42. setPinOutput(pin);
  43. writePinLow(pin);
  44. }
  45. }
  46. static inline void setPinOutput_writeHigh(pin_t pin) {
  47. ATOMIC_BLOCK_FORCEON {
  48. setPinOutput(pin);
  49. writePinHigh(pin);
  50. }
  51. }
  52. static inline void setPinInputHigh_atomic(pin_t pin) {
  53. ATOMIC_BLOCK_FORCEON {
  54. setPinInputHigh(pin);
  55. }
  56. }
  57. static inline uint8_t readMatrixPin(pin_t pin) {
  58. if (pin != NO_PIN) {
  59. return readPin(pin);
  60. } else {
  61. return 1;
  62. }
  63. }
  64. static void shiftOut(uint16_t dataOut) {
  65. for (uint8_t i = 0; i < NO_PIN_NUM; i++) {
  66. if (dataOut & 0x1) {
  67. setPinOutput_writeHigh(DATA_PIN);
  68. } else {
  69. setPinOutput_writeLow(DATA_PIN);
  70. }
  71. dataOut = dataOut >> 1;
  72. setPinOutput_writeHigh(CLOCK_PIN);
  73. setPinOutput_writeLow(CLOCK_PIN);
  74. }
  75. setPinOutput_writeHigh(LATCH_PIN);
  76. setPinOutput_writeLow(LATCH_PIN);
  77. }
  78. static void shiftout_single(uint8_t data) {
  79. if (data & 0x1) {
  80. setPinOutput_writeHigh(DATA_PIN);
  81. } else {
  82. setPinOutput_writeLow(DATA_PIN);
  83. }
  84. setPinOutput_writeHigh(CLOCK_PIN);
  85. setPinOutput_writeLow(CLOCK_PIN);
  86. setPinOutput_writeHigh(LATCH_PIN);
  87. setPinOutput_writeLow(LATCH_PIN);
  88. }
  89. static bool select_col(uint8_t col) {
  90. pin_t pin = col_pins[col];
  91. if (pin != NO_PIN) {
  92. setPinOutput_writeLow(pin);
  93. return true;
  94. } else {
  95. if (col == NO_PIN_START) {
  96. shiftout_single(0x00);
  97. } else {
  98. shiftout_single(0x01);
  99. }
  100. return true;
  101. }
  102. return false;
  103. }
  104. static void unselect_col(uint8_t col) {
  105. pin_t pin = col_pins[col];
  106. if (pin != NO_PIN) {
  107. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  108. setPinOutput_writeHigh(pin);
  109. #else
  110. setPinInputHigh_atomic(pin);
  111. #endif
  112. } else {
  113. if (col == (MATRIX_COLS - NO_PIN_OFFSET - 1))
  114. setPinOutput_writeHigh(CLOCK_PIN);
  115. setPinOutput_writeLow(CLOCK_PIN);
  116. setPinOutput_writeHigh(LATCH_PIN);
  117. setPinOutput_writeLow(LATCH_PIN);
  118. }
  119. }
  120. static void unselect_cols(void) {
  121. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  122. pin_t pin = col_pins[x];
  123. if (pin != NO_PIN) {
  124. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  125. setPinOutput_writeHigh(pin);
  126. #else
  127. setPinInputHigh_atomic(pin);
  128. #endif
  129. }
  130. if (x == (MATRIX_COLS - NO_PIN_OFFSET - 1))
  131. // unselect shift Register
  132. shiftOut(CLR_REG_VAL);
  133. }
  134. }
  135. static void matrix_init_pins(void) {
  136. unselect_cols();
  137. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  138. if (row_pins[x] != NO_PIN) {
  139. setPinInputHigh_atomic(row_pins[x]);
  140. }
  141. }
  142. }
  143. static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
  144. bool key_pressed = false;
  145. // Select col
  146. if (!select_col(current_col)) { // select col
  147. return; // skip NO_PIN col
  148. }
  149. if (current_col < 10) {
  150. matrix_output_select_delay();
  151. } else {
  152. for (int8_t cycle = 4; cycle > 0; cycle--) {
  153. matrix_output_select_delay(); // 0.25us
  154. matrix_output_select_delay();
  155. matrix_output_select_delay();
  156. matrix_output_select_delay();
  157. }
  158. }
  159. // For each row...
  160. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  161. // Check row pin state
  162. if (readMatrixPin(row_pins[row_index]) == 0) {
  163. // Pin LO, set col bit
  164. current_matrix[row_index] |= row_shifter;
  165. key_pressed = true;
  166. } else {
  167. // Pin HI, clear col bit
  168. current_matrix[row_index] &= ~row_shifter;
  169. }
  170. }
  171. // Unselect col
  172. unselect_col(current_col);
  173. matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
  174. }
  175. void matrix_init_custom(void) {
  176. // initialize key pins
  177. matrix_init_pins();
  178. }
  179. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  180. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  181. // Set col, read rows
  182. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  183. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
  184. matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
  185. }
  186. bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  187. if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
  188. return changed;
  189. }