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.

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