logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (6189B)


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