matrix.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2019 Mathias Andersson <wraul@dbox.se>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "matrix.h"
  4. #include "pca9555.h"
  5. #include "wait.h"
  6. /*
  7. * IC1 (PCA9555) IC2 (PCA9555)
  8. * ,----------. ,----------.
  9. * SDA --| SDA P00 |-- P1 SDA --| SDA P00 |-- P17
  10. * SCL --| SCL P01 |-- P2 SCL --| SCL P01 |-- P18
  11. * INT --| INT P02 |-- P3 INT --| INT P02 |-- P19
  12. * | P03 |-- P4 | P03 |-- P20
  13. * GND --| A0 P04 |-- P5 VCC --| A0 P04 |-- P21
  14. * SJ1 --| A1 P05 |-- P6 SJ1 --| A1 P05 |-- P22
  15. * GND --| A2 P06 |-- P7 GND --| A2 P06 |-- P23
  16. * | P07 |-- P8 | P07 |-- P24
  17. * | | | |
  18. * | P10 |-- P9 | P10 |-- P25
  19. * | P11 |-- P10 | P11 |-- P26
  20. * | P12 |-- P11 | P12 |-- P27
  21. * | P13 |-- P12 | P13 |-- P28
  22. * | P14 |-- P13 | P14 |-- P29
  23. * | P15 |-- P14 | P15 |-- P30
  24. * | P16 |-- P15 | P16 |-- P31
  25. * | P17 |-- P16 | P17 |-- P32
  26. * `----------' `----------'
  27. */
  28. /*
  29. * | Row | Pin | | Col | Pin |
  30. * | --- | --- | | --- | --- |
  31. * | 0 | P1 | | 0 | P25 |
  32. * | 1 | P2 | | 1 | P26 |
  33. * | 2 | P3 | | 2 | P27 |
  34. * | 3 | P4 | | 3 | P28 |
  35. * | 4 | P5 | | 4 | P29 |
  36. * | 5 | P6 | | 5 | P30 |
  37. * | 6 | P7 | | 6 | P20 |
  38. * | 7 | P8 | | 7 | P21 |
  39. * | 8 | P22 |
  40. * | 9 | P23 |
  41. * | A | P24 |
  42. */
  43. // PCA9555 slave addresses
  44. #define IC1 0x20
  45. #define IC2 0x21
  46. // PCA9555 column pin masks
  47. #define PORT0_COLS_MASK 0b11111000
  48. #define PORT1_COLS_MASK 0b00111111
  49. #define COLS_MASK 0b0000011111111111
  50. static void init_pins(void) {
  51. // init cols - IC2 port0 & IC2 port1 input
  52. pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT);
  53. pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT);
  54. // init rows - IC1 port0 output
  55. pca9555_set_config(IC1, PCA9555_PORT0, ALL_OUTPUT);
  56. pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH);
  57. }
  58. static void select_row(uint8_t row) {
  59. // All rows are on the same IC and port
  60. uint8_t mask = 1 << row;
  61. // set active row low : 0
  62. // set other rows hi-Z : 1
  63. pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH & (~mask));
  64. }
  65. static uint16_t read_cols(void) {
  66. uint8_t state_1 = 0;
  67. uint8_t state_2 = 0;
  68. pca9555_read_pins(IC2, PCA9555_PORT0, &state_1);
  69. pca9555_read_pins(IC2, PCA9555_PORT1, &state_2);
  70. uint16_t state = (((uint16_t)state_1 & PORT0_COLS_MASK) << 3) | (((uint16_t)state_2 & PORT1_COLS_MASK));
  71. // A low pin indicates an active column
  72. return (~state) & COLS_MASK;
  73. }
  74. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  75. // Store last value of row prior to reading
  76. matrix_row_t last_row_value = current_matrix[current_row];
  77. // Clear data in matrix row
  78. current_matrix[current_row] = 0;
  79. // Select row and wait for row selection to stabilize
  80. select_row(current_row);
  81. wait_us(30);
  82. current_matrix[current_row] = read_cols();
  83. // No need to unselect as `select_row` sets all the pins.
  84. return (last_row_value != current_matrix[current_row]);
  85. }
  86. void matrix_init_custom(void) {
  87. pca9555_init(IC1);
  88. pca9555_init(IC2);
  89. init_pins();
  90. }
  91. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  92. bool changed = false;
  93. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  94. changed |= read_cols_on_row(current_matrix, current_row);
  95. }
  96. return changed;
  97. }