matrix.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  2. // Copyright 2017 Erin Call <hello@erincall.com>
  3. // Copyright 2023 @frobiac
  4. // SPDX-License-Identifier: GPL-2.0-or-later
  5. // This implements a matrix scan (lite) for the BlackBowl keyboard.
  6. // Each side has a dedicated MCP23018 I2C expander.
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <avr/io.h>
  10. #include "wait.h"
  11. #include "action_layer.h"
  12. #include "print.h"
  13. #include "debug.h"
  14. #include "util.h"
  15. #include "matrix.h"
  16. #include "blackbowl.h"
  17. #include "i2c_master.h"
  18. #include "timer.h"
  19. #define MATRIX_ROWS_PER_SIDE (MATRIX_ROWS / 2)
  20. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  21. static uint8_t expander_reset_loop;
  22. uint8_t expander_status;
  23. const uint8_t expander_input_mask = ((1 << MATRIX_ROWS_PER_SIDE) - 1); // No special mapping, 5 bits [0..4] per side
  24. bool i2c_initialized = false;
  25. static const uint8_t I2C_ADDR_RIGHT = 0x4E;
  26. static const uint8_t I2C_ADDR_LEFT = 0x46;
  27. static const uint8_t i2c_addr[] = {I2C_ADDR_RIGHT, I2C_ADDR_LEFT};
  28. void matrix_init_custom(void) {
  29. if (!i2c_initialized) {
  30. i2c_init();
  31. wait_ms(1000);
  32. }
  33. // Pin direction and pull-up depends on diode direction and column register:
  34. // ROW2COL, GPIOA => input, output
  35. uint8_t direction[2] = {0, expander_input_mask};
  36. uint8_t pullup[2] = {0, expander_input_mask};
  37. for (uint8_t i = 0; i < 2; ++i) {
  38. expander_status = i2c_write_register(i2c_addr[i], IODIRA, direction, 2, I2C_TIMEOUT);
  39. if (expander_status) return;
  40. expander_status = i2c_write_register(i2c_addr[i], GPPUA, pullup, 2, I2C_TIMEOUT);
  41. }
  42. }
  43. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  44. bool matrix_has_changed = false;
  45. if (expander_status) { // if there was an error
  46. ++expander_reset_loop;
  47. if (++expander_reset_loop == 0) {
  48. // since expander_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  49. // this will be approx bit more frequent than once per second
  50. matrix_init_custom();
  51. }
  52. }
  53. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  54. matrix_has_changed |= read_rows_on_col(current_matrix, current_col);
  55. }
  56. return matrix_has_changed;
  57. }
  58. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  59. bool matrix_changed = false;
  60. uint8_t port = 0xFF & ~(1 << current_col);
  61. uint8_t column_state[] = {0, 0};
  62. // On both expanders: select col and read rows
  63. for (size_t i = 0; i < 2; ++i) {
  64. if (!expander_status) {
  65. expander_status = i2c_write_register(i2c_addr[i], EXPANDER_COL_REGISTER, &port, 1, I2C_TIMEOUT);
  66. }
  67. wait_us(30);
  68. if (expander_status) {
  69. return false;
  70. }
  71. expander_status = i2c_read_register(i2c_addr[i], EXPANDER_ROW_REGISTER, &column_state[i], 1, I2C_TIMEOUT);
  72. column_state[i] = (~column_state[i]) & ((1 << MATRIX_ROWS_PER_SIDE) - 1);
  73. }
  74. // now map rows 0..4 on each side to cumulative to 0..9
  75. uint16_t col_state = column_state[0] | ((column_state[1] << MATRIX_ROWS_PER_SIDE) /*& 0x3e0*/);
  76. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  77. // Store last value of row prior to reading
  78. matrix_row_t last_row_value = current_matrix[current_row];
  79. if (col_state & (1 << current_row)) {
  80. // key closed; set state bit in matrix
  81. current_matrix[current_row] |= (MATRIX_ROW_SHIFTER << current_col);
  82. } else {
  83. // key open; clear state bit in matrix
  84. current_matrix[current_row] &= ~(MATRIX_ROW_SHIFTER << current_col);
  85. }
  86. // Determine whether the matrix changed state
  87. if ((last_row_value != current_matrix[current_row]) && !(matrix_changed)) {
  88. matrix_changed = true;
  89. }
  90. }
  91. return matrix_changed;
  92. }