matrix.c 6.2 KB

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