logo

qmk_firmware

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

matrix.c (6016B)


  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. #ifndef PIN_USED_74HC595
  20. # define PIN_USED_74HC595 8
  21. #endif
  22. #ifndef PIN_START_74HC595
  23. # define PIN_START_74HC595 8
  24. #endif
  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. static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
  33. ATOMIC_BLOCK_FORCEON {
  34. gpio_set_pin_output(pin);
  35. gpio_write_pin_low(pin);
  36. }
  37. }
  38. static inline void gpio_atomic_set_pin_output_high(pin_t pin) {
  39. ATOMIC_BLOCK_FORCEON {
  40. gpio_set_pin_output(pin);
  41. gpio_write_pin_high(pin);
  42. }
  43. }
  44. static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
  45. ATOMIC_BLOCK_FORCEON {
  46. gpio_set_pin_input_high(pin);
  47. }
  48. }
  49. static inline uint8_t readMatrixPin(pin_t pin) {
  50. if (pin != NO_PIN) {
  51. return gpio_read_pin(pin);
  52. } else {
  53. return 1;
  54. }
  55. }
  56. void small_delay(volatile uint8_t timeout) {
  57. while (timeout--);
  58. }
  59. static void shiftOut(uint16_t dataOut) {
  60. ATOMIC_BLOCK_FORCEON {
  61. for (uint8_t i = 0; i < PIN_USED_74HC595; i++) {
  62. if (dataOut & 0x1) {
  63. gpio_write_pin_high(DATA_PIN_74HC595);
  64. } else {
  65. gpio_write_pin_low(DATA_PIN_74HC595);
  66. }
  67. dataOut = dataOut >> 1;
  68. gpio_write_pin_high(CLOCK_PIN_74HC595);
  69. small_delay(2);
  70. gpio_write_pin_low(CLOCK_PIN_74HC595);
  71. }
  72. gpio_write_pin_high(LATCH_PIN_74HC595);
  73. small_delay(2);
  74. gpio_write_pin_low(LATCH_PIN_74HC595);
  75. }
  76. }
  77. static void shiftOut_single(uint8_t data) {
  78. ATOMIC_BLOCK_FORCEON {
  79. if (data & 0x1) {
  80. gpio_write_pin_high(DATA_PIN_74HC595);
  81. } else {
  82. gpio_write_pin_low(DATA_PIN_74HC595);
  83. }
  84. gpio_write_pin_high(CLOCK_PIN_74HC595);
  85. small_delay(2);
  86. gpio_write_pin_low(CLOCK_PIN_74HC595);
  87. gpio_write_pin_high(LATCH_PIN_74HC595);
  88. small_delay(2);
  89. gpio_write_pin_low(LATCH_PIN_74HC595);
  90. }
  91. }
  92. static bool select_col(uint8_t col) {
  93. pin_t pin = col_pins[col];
  94. if (pin != NO_PIN) {
  95. gpio_atomic_set_pin_output_low(pin);
  96. return true;
  97. } else {
  98. if (col == PIN_START_74HC595) {
  99. shiftOut_single(0x00);
  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. shiftOut_single(0x01);
  115. }
  116. }
  117. static void unselect_cols(void) {
  118. // unselect column pins
  119. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  120. pin_t pin = col_pins[x];
  121. if (pin != NO_PIN) {
  122. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  123. gpio_atomic_set_pin_output_high(pin);
  124. #else
  125. gpio_atomic_set_pin_input_high(pin);
  126. #endif
  127. }
  128. if (x == PIN_START_74HC595)
  129. // unselect Shift Register
  130. shiftOut(0xFFFF);
  131. }
  132. }
  133. static void matrix_init_pins(void) {
  134. gpio_set_pin_output(DATA_PIN_74HC595);
  135. gpio_set_pin_output(CLOCK_PIN_74HC595);
  136. gpio_set_pin_output(LATCH_PIN_74HC595);
  137. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  138. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  139. if (col_pins[x] != NO_PIN) {
  140. gpio_set_pin_output(col_pins[x]);
  141. }
  142. }
  143. #endif
  144. unselect_cols();
  145. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  146. if (row_pins[x] != NO_PIN) {
  147. gpio_atomic_set_pin_input_high(row_pins[x]);
  148. }
  149. }
  150. }
  151. static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
  152. bool key_pressed = false;
  153. // Select col
  154. if (!select_col(current_col)) { // select col
  155. return; // skip NO_PIN col
  156. }
  157. matrix_output_select_delay();
  158. // For each row...
  159. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  160. // Check row pin state
  161. if (readMatrixPin(row_pins[row_index]) == 0) {
  162. // Pin LO, set col bit
  163. current_matrix[row_index] |= row_shifter;
  164. key_pressed = true;
  165. } else {
  166. // Pin HI, clear col bit
  167. current_matrix[row_index] &= ~row_shifter;
  168. }
  169. }
  170. // Unselect col
  171. unselect_col(current_col);
  172. matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
  173. }
  174. void matrix_init_custom(void) {
  175. // initialize key pins
  176. matrix_init_pins();
  177. }
  178. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  179. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  180. // Set col, read rows
  181. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  182. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
  183. matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
  184. }
  185. bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  186. if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
  187. return changed;
  188. }