logo

qmk_firmware

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

matrix.c (6271B)


  1. /*
  2. Copyright 2012-2019 Jun Wako, Jack Humbert, Yiancar, Mathias Andersson <wraul@dbox.se>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "wait.h"
  15. #include "print.h"
  16. #include "debug.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "debounce.h"
  20. #include "pca9555.h"
  21. /*
  22. * IC1 (PCA9555) IC2 (PCA9555)
  23. * ,----------. ,----------.
  24. * SDA --| SDA P00 |-- P1 SDA --| SDA P00 |-- P17
  25. * SCL --| SCL P01 |-- P2 SCL --| SCL P01 |-- P18
  26. * INT --| INT P02 |-- P3 INT --| INT P02 |-- P19
  27. * | P03 |-- P4 | P03 |-- P20
  28. * GND --| A0 P04 |-- P5 VCC --| A0 P04 |-- P21
  29. * SJ1 --| A1 P05 |-- P6 SJ1 --| A1 P05 |-- P22
  30. * GND --| A2 P06 |-- P7 GND --| A2 P06 |-- P23
  31. * | P07 |-- P8 | P07 |-- P24
  32. * | | | |
  33. * | P10 |-- P9 | P10 |-- P25
  34. * | P11 |-- P10 | P11 |-- P26
  35. * | P12 |-- P11 | P12 |-- P27
  36. * | P13 |-- P12 | P13 |-- P28
  37. * | P14 |-- P13 | P14 |-- P29
  38. * | P15 |-- P14 | P15 |-- P30
  39. * | P16 |-- P15 | P16 |-- P31
  40. * | P17 |-- P16 | P17 |-- P32
  41. * `----------' `----------'
  42. */
  43. /*
  44. * | Row | Pin | | Col | Pin |
  45. * | --- | --- | | --- | --- |
  46. * | 0 | P1 | | 0 | P25 |
  47. * | 1 | P2 | | 1 | P26 |
  48. * | 2 | P3 | | 2 | P27 |
  49. * | 3 | P4 | | 3 | P28 |
  50. * | 4 | P5 | | 4 | P29 |
  51. * | 5 | P6 | | 5 | P30 |
  52. * | 6 | P7 | | 6 | P20 |
  53. * | 7 | P8 | | 7 | P21 |
  54. * | 8 | P22 |
  55. * | 9 | P23 |
  56. * | A | P24 |
  57. */
  58. // PCA9555 slave addresses
  59. #define IC1 0x20
  60. #define IC2 0x21
  61. // PCA9555 column pin masks
  62. #define PORT0_COLS_MASK 0b11111000
  63. #define PORT1_COLS_MASK 0b00111111
  64. #define COLS_MASK 0b0000011111111111
  65. #if (MATRIX_COLS <= 8)
  66. # define print_matrix_header() print("\nr/c 01234567\n")
  67. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  68. # define ROW_SHIFTER ((uint8_t)1)
  69. #elif (MATRIX_COLS <= 16)
  70. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  71. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  72. # define ROW_SHIFTER ((uint16_t)1)
  73. #elif (MATRIX_COLS <= 32)
  74. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  75. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  76. # define ROW_SHIFTER ((uint32_t)1)
  77. #endif
  78. /* matrix state(1:on, 0:off) */
  79. static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  80. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  81. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  82. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  83. __attribute__((weak)) void matrix_init_user(void) {}
  84. __attribute__((weak)) void matrix_scan_user(void) {}
  85. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  86. inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
  87. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  88. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  89. void matrix_print(void) {
  90. print_matrix_header();
  91. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  92. print_hex8(row);
  93. print(": ");
  94. print_matrix_row(row);
  95. print("\n");
  96. }
  97. }
  98. static void init_i2c(void) {
  99. pca9555_init(IC1);
  100. pca9555_init(IC2);
  101. }
  102. static void init_pins(void) {
  103. // init cols - IC2 port0 & IC2 port1 input
  104. pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT);
  105. pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT);
  106. // init rows - IC1 port0 output
  107. pca9555_set_config(IC1, PCA9555_PORT0, ALL_OUTPUT);
  108. pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH);
  109. }
  110. static void select_row(uint8_t row) {
  111. // All rows are on the same IC and port
  112. uint8_t mask = 1 << row;
  113. // set active row low : 0
  114. // set other rows hi-Z : 1
  115. pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH & (~mask));
  116. }
  117. static uint16_t read_cols(void) {
  118. uint8_t state_1 = 0;
  119. uint8_t state_2 = 0;
  120. pca9555_read_pins(IC2, PCA9555_PORT0, &state_1);
  121. pca9555_read_pins(IC2, PCA9555_PORT1, &state_2);
  122. uint16_t state = (((uint16_t)state_1 & PORT0_COLS_MASK) << 3) | (((uint16_t)state_2 & PORT1_COLS_MASK));
  123. // A low pin indicates an active column
  124. return (~state) & COLS_MASK;
  125. }
  126. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  127. // Store last value of row prior to reading
  128. matrix_row_t last_row_value = current_matrix[current_row];
  129. // Clear data in matrix row
  130. current_matrix[current_row] = 0;
  131. // Select row and wait for row selecton to stabilize
  132. select_row(current_row);
  133. wait_us(30);
  134. current_matrix[current_row] |= read_cols();
  135. // No need to unselect as `select_row` sets all the pins.
  136. return (last_row_value != current_matrix[current_row]);
  137. }
  138. void matrix_init(void) {
  139. // initialize i2c
  140. init_i2c();
  141. // initialize key pins
  142. init_pins();
  143. // initialize matrix state: all keys off
  144. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  145. raw_matrix[i] = 0;
  146. matrix[i] = 0;
  147. }
  148. debounce_init(MATRIX_ROWS);
  149. matrix_init_kb();
  150. }
  151. uint8_t matrix_scan(void) {
  152. bool changed = false;
  153. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  154. changed |= read_cols_on_row(raw_matrix, current_row);
  155. }
  156. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  157. matrix_scan_kb();
  158. return (uint8_t)changed;
  159. }