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.

218 lines
6.6 KiB

  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 matrix_bitpop(i) bitpop(matrix[i])
  72. # define ROW_SHIFTER ((uint8_t)1)
  73. #elif (MATRIX_COLS <= 16)
  74. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  75. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  76. # define matrix_bitpop(i) bitpop16(matrix[i])
  77. # define ROW_SHIFTER ((uint16_t)1)
  78. #elif (MATRIX_COLS <= 32)
  79. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  80. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  81. # define matrix_bitpop(i) bitpop32(matrix[i])
  82. # define ROW_SHIFTER ((uint32_t)1)
  83. #endif
  84. /* matrix state(1:on, 0:off) */
  85. static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  86. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  87. __attribute__((weak)) void matrix_init_quantum(void) { matrix_init_kb(); }
  88. __attribute__((weak)) void matrix_scan_quantum(void) { matrix_scan_kb(); }
  89. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  90. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  91. __attribute__((weak)) void matrix_init_user(void) {}
  92. __attribute__((weak)) void matrix_scan_user(void) {}
  93. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  94. inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
  95. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  96. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  97. void matrix_print(void) {
  98. print_matrix_header();
  99. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  100. print_hex8(row);
  101. print(": ");
  102. print_matrix_row(row);
  103. print("\n");
  104. }
  105. }
  106. uint8_t matrix_key_count(void) {
  107. uint8_t count = 0;
  108. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  109. count += matrix_bitpop(i);
  110. }
  111. return count;
  112. }
  113. static void init_i2c(void) {
  114. pca9555_init(IC1);
  115. pca9555_init(IC2);
  116. }
  117. static void init_pins(void) {
  118. // init cols - IC2 port0 & IC2 port1 input
  119. pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT);
  120. pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT);
  121. // init rows - IC1 port0 output
  122. pca9555_set_config(IC1, PCA9555_PORT0, ALL_OUTPUT);
  123. pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH);
  124. }
  125. static void select_row(uint8_t row) {
  126. // All rows are on the same IC and port
  127. uint8_t mask = 1 << row;
  128. // set active row low : 0
  129. // set other rows hi-Z : 1
  130. pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH & (~mask));
  131. }
  132. static uint16_t read_cols(void) {
  133. uint16_t state_1 = pca9555_readPins(IC2, PCA9555_PORT0);
  134. uint16_t state_2 = pca9555_readPins(IC2, PCA9555_PORT1);
  135. uint16_t state = ((state_1 & PORT0_COLS_MASK) << 3) | ((state_2 & PORT1_COLS_MASK));
  136. // A low pin indicates an active column
  137. return (~state) & COLS_MASK;
  138. }
  139. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  140. // Store last value of row prior to reading
  141. matrix_row_t last_row_value = current_matrix[current_row];
  142. // Clear data in matrix row
  143. current_matrix[current_row] = 0;
  144. // Select row and wait for row selecton to stabilize
  145. select_row(current_row);
  146. wait_us(30);
  147. current_matrix[current_row] |= read_cols();
  148. // No need to unselect as `select_row` sets all the pins.
  149. return (last_row_value != current_matrix[current_row]);
  150. }
  151. void matrix_init(void) {
  152. // initialize i2c
  153. init_i2c();
  154. // initialize key pins
  155. init_pins();
  156. // initialize matrix state: all keys off
  157. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  158. raw_matrix[i] = 0;
  159. matrix[i] = 0;
  160. }
  161. debounce_init(MATRIX_ROWS);
  162. matrix_init_quantum();
  163. }
  164. uint8_t matrix_scan(void) {
  165. bool changed = false;
  166. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  167. changed |= read_cols_on_row(raw_matrix, current_row);
  168. }
  169. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  170. matrix_scan_quantum();
  171. return (uint8_t)changed;
  172. }