logo

qmk_firmware

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

matrix.c (5719B)


  1. /* Copyright 2023 @ 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 SHIFT_COL_START
  20. # define SHIFT_COL_START 8
  21. #endif
  22. #ifndef SHIFT_COL_END
  23. # define SHIFT_COL_END 15
  24. #endif
  25. #if defined(SHIFT_COL_START) && defined(SHIFT_COL_END)
  26. # if ((SHIFT_COL_END - SHIFT_COL_START + 1) > 16)
  27. # define SIZE_T uint32_t
  28. # define UNSELECT_ALL_COL 0xFFFFFFFF
  29. # elif ((SHIFT_COL_END - SHIFT_COL_START + 1) > 8)
  30. # define SIZE_T uint16_t
  31. # define UNSELECT_ALL_COL 0xFFFF
  32. # else
  33. # define SIZE_T uint8_t
  34. # define UNSELECT_ALL_COL 0xFF
  35. # endif
  36. #endif
  37. pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  38. pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  39. static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
  40. ATOMIC_BLOCK_FORCEON {
  41. gpio_set_pin_output(pin);
  42. gpio_write_pin_low(pin);
  43. }
  44. }
  45. static inline void gpio_atomic_set_pin_output_high(pin_t pin) {
  46. ATOMIC_BLOCK_FORCEON {
  47. gpio_set_pin_output(pin);
  48. gpio_write_pin_high(pin);
  49. }
  50. }
  51. static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
  52. ATOMIC_BLOCK_FORCEON {
  53. gpio_set_pin_input_high(pin);
  54. }
  55. }
  56. static inline uint8_t readMatrixPin(pin_t pin) {
  57. if (pin != NO_PIN) {
  58. return gpio_read_pin(pin);
  59. } else {
  60. return 1;
  61. }
  62. }
  63. static inline void HC595_delay(uint8_t n) {
  64. while (n-- > 0) {
  65. asm volatile("nop" ::: "memory");
  66. }
  67. }
  68. static void HC595_output(SIZE_T data, uint8_t bit) {
  69. uint8_t n = 1;
  70. ATOMIC_BLOCK_FORCEON {
  71. for (uint8_t i = 0; i < (SHIFT_COL_END - SHIFT_COL_START + 1); i++) {
  72. if (data & 0x1) {
  73. gpio_write_pin_high(HC595_DS);
  74. } else {
  75. gpio_write_pin_low(HC595_DS);
  76. }
  77. gpio_write_pin_high(HC595_SHCP);
  78. HC595_delay(n);
  79. gpio_write_pin_low(HC595_SHCP);
  80. HC595_delay(n);
  81. if (bit) {
  82. break;
  83. } else {
  84. data = data >> 1;
  85. }
  86. }
  87. gpio_write_pin_high(HC595_STCP);
  88. HC595_delay(n);
  89. gpio_write_pin_low(HC595_STCP);
  90. HC595_delay(n);
  91. }
  92. }
  93. static bool select_col(uint8_t col) {
  94. pin_t pin = col_pins[col];
  95. if (col < SHIFT_COL_START || col > SHIFT_COL_END) {
  96. gpio_atomic_set_pin_output_low(pin);
  97. return true;
  98. } else {
  99. if (col == SHIFT_COL_START) {
  100. HC595_output(0x00, 1);
  101. }
  102. return true;
  103. }
  104. return false;
  105. }
  106. static void unselect_col(uint8_t col) {
  107. pin_t pin = col_pins[col];
  108. if (col < SHIFT_COL_START || col > SHIFT_COL_END) {
  109. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  110. gpio_atomic_set_pin_output_high(pin);
  111. #else
  112. gpio_atomic_set_pin_input_high(pin);
  113. #endif
  114. } else {
  115. HC595_output(0x01, 1);
  116. }
  117. }
  118. static void unselect_cols(void) {
  119. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  120. pin_t pin = col_pins[x];
  121. if (x < SHIFT_COL_START || x > SHIFT_COL_END) {
  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. } else {
  128. if (x == SHIFT_COL_START) HC595_output(UNSELECT_ALL_COL, 0);
  129. }
  130. }
  131. }
  132. static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
  133. bool key_pressed = false;
  134. // Select col
  135. if (!select_col(current_col)) { // select col
  136. return; // skip NO_PIN col
  137. }
  138. matrix_output_select_delay();
  139. // For each row...
  140. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  141. // Check row pin state
  142. if (readMatrixPin(row_pins[row_index]) == 0) {
  143. // Pin LO, set col bit
  144. current_matrix[row_index] |= row_shifter;
  145. key_pressed = true;
  146. } else {
  147. // Pin HI, clear col bit
  148. current_matrix[row_index] &= ~row_shifter;
  149. }
  150. }
  151. // Unselect col
  152. unselect_col(current_col);
  153. matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
  154. }
  155. void matrix_init_custom(void) {
  156. gpio_set_pin_output(HC595_DS);
  157. gpio_set_pin_output(HC595_STCP);
  158. gpio_set_pin_output(HC595_SHCP);
  159. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  160. if (row_pins[x] != NO_PIN) {
  161. gpio_atomic_set_pin_input_high(row_pins[x]);
  162. }
  163. }
  164. unselect_cols();
  165. }
  166. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  167. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  168. // Set col, read rows
  169. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  170. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
  171. matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
  172. }
  173. bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  174. if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
  175. return changed;
  176. }