logo

qmk_firmware

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

matrix.c (6441B)


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