You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.0 KiB

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