logo

qmk_firmware

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

matrix.c (6261B)


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