matrix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Copyright 2021 @ 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 const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  26. #endif // MATRIX_ROW_PINS
  27. #ifdef MATRIX_COL_PINS
  28. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  29. #endif // MATRIX_COL_PINS
  30. #define ROWS_PER_HAND (MATRIX_ROWS)
  31. static inline void writePinLow_atomic(pin_t pin) {
  32. ATOMIC_BLOCK_FORCEON {
  33. writePinLow(pin);
  34. }
  35. }
  36. static inline void writePinHigh_atomic(pin_t pin) {
  37. ATOMIC_BLOCK_FORCEON {
  38. writePinHigh(pin);
  39. }
  40. }
  41. static inline void setPinOutput_writeLow(pin_t pin) {
  42. ATOMIC_BLOCK_FORCEON {
  43. setPinOutput(pin);
  44. writePinLow(pin);
  45. }
  46. }
  47. static inline void setPinInputHigh_atomic(pin_t pin) {
  48. ATOMIC_BLOCK_FORCEON {
  49. setPinInputHigh(pin);
  50. }
  51. }
  52. static inline uint8_t readMatrixPin(pin_t pin) {
  53. if (pin != NO_PIN) {
  54. return readPin(pin);
  55. } else {
  56. return 1;
  57. }
  58. }
  59. // At 3.6V input, three nops (37.5ns) should be enough for all signals
  60. #define small_delay() __asm__ __volatile__("nop;nop;nop;\n\t" ::: "memory")
  61. #define compiler_barrier() __asm__ __volatile__("" ::: "memory")
  62. static void shiftOut(uint8_t dataOut) {
  63. ATOMIC_BLOCK_FORCEON {
  64. for (uint8_t i = 0; i < 8; i++) {
  65. compiler_barrier();
  66. if (dataOut & 0x1) {
  67. writePinHigh(DATA_PIN);
  68. } else {
  69. writePinLow(DATA_PIN);
  70. }
  71. dataOut = dataOut >> 1;
  72. compiler_barrier();
  73. writePinHigh(CLOCK_PIN);
  74. small_delay();
  75. writePinLow(CLOCK_PIN);
  76. }
  77. compiler_barrier();
  78. writePinHigh(LATCH_PIN);
  79. small_delay();
  80. writePinLow(LATCH_PIN);
  81. compiler_barrier();
  82. }
  83. }
  84. static void shiftout_single(uint8_t data) {
  85. ATOMIC_BLOCK_FORCEON {
  86. compiler_barrier();
  87. if (data & 0x1) {
  88. writePinHigh(DATA_PIN);
  89. } else {
  90. writePinLow(DATA_PIN);
  91. }
  92. compiler_barrier();
  93. writePinHigh(CLOCK_PIN);
  94. small_delay();
  95. writePinLow(CLOCK_PIN);
  96. compiler_barrier();
  97. writePinHigh(LATCH_PIN);
  98. small_delay();
  99. writePinLow(LATCH_PIN);
  100. compiler_barrier();
  101. }
  102. }
  103. static bool select_col(uint8_t col) {
  104. pin_t pin = col_pins[col];
  105. if (pin != NO_PIN) {
  106. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  107. writePinLow_atomic(pin);
  108. #else
  109. setPinOutput_writeLow(pin);
  110. #endif
  111. return true;
  112. } else {
  113. if (col == 0) {
  114. shiftout_single(0x00);
  115. }
  116. return true;
  117. }
  118. return false;
  119. }
  120. static void unselect_col(uint8_t col) {
  121. pin_t pin = col_pins[col];
  122. if (pin != NO_PIN) {
  123. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  124. writePinHigh_atomic(pin);
  125. #else
  126. setPinInputHigh_atomic(pin);
  127. #endif
  128. } else {
  129. shiftout_single(0x01);
  130. }
  131. }
  132. static void unselect_cols(void) {
  133. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  134. pin_t pin = col_pins[x];
  135. if (pin != NO_PIN) {
  136. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  137. setPinOutput_writeHigh(pin);
  138. #else
  139. setPinInputHigh_atomic(pin);
  140. #endif
  141. } else {
  142. if (x == 0)
  143. // unselect shift Register
  144. shiftOut(0xFF);
  145. }
  146. }
  147. }
  148. static void matrix_init_pins(void) {
  149. setPinOutput(DATA_PIN);
  150. setPinOutput(CLOCK_PIN);
  151. setPinOutput(LATCH_PIN);
  152. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  153. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  154. if (col_pins[x] != NO_PIN) {
  155. setPinOutput(col_pins[x]);
  156. }
  157. }
  158. #endif
  159. unselect_cols();
  160. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  161. if (row_pins[x] != NO_PIN) {
  162. setPinInputHigh_atomic(row_pins[x]);
  163. }
  164. }
  165. }
  166. static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
  167. bool key_pressed = false;
  168. // Select col
  169. if (!select_col(current_col)) { // select col
  170. return; // skip NO_PIN col
  171. }
  172. matrix_output_select_delay();
  173. // For each row...
  174. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  175. // Check row pin state
  176. if (readMatrixPin(row_pins[row_index]) == 0) {
  177. // Pin LO, set col bit
  178. current_matrix[row_index] |= row_shifter;
  179. key_pressed = true;
  180. } else {
  181. // Pin HI, clear col bit
  182. current_matrix[row_index] &= ~row_shifter;
  183. }
  184. }
  185. // Unselect col
  186. unselect_col(current_col);
  187. matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
  188. }
  189. void matrix_init_custom(void) {
  190. // initialize key pins
  191. matrix_init_pins();
  192. }
  193. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  194. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  195. // Set col, read rows
  196. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  197. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
  198. matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
  199. }
  200. bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  201. if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
  202. return changed;
  203. }