logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (3994B)


  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. #define ROW_SHIFTER ((matrix_row_t)1)
  21. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  22. static uint8_t expander_reset_loop;
  23. uint8_t expander_status;
  24. const uint8_t expander_input_mask = ((1 << MATRIX_ROWS_PER_SIDE) - 1); // No special mapping, 5 bits [0..4] per side
  25. bool i2c_initialized = false;
  26. static const uint8_t I2C_ADDR_RIGHT = 0x4E;
  27. static const uint8_t I2C_ADDR_LEFT = 0x46;
  28. static const uint8_t i2c_addr[] = {I2C_ADDR_RIGHT, I2C_ADDR_LEFT};
  29. void matrix_init_custom(void) {
  30. if (!i2c_initialized) {
  31. i2c_init();
  32. wait_ms(1000);
  33. }
  34. // Pin direction and pull-up depends on diode direction and column register:
  35. // ROW2COL, GPIOA => input, output
  36. uint8_t direction[2] = {0, expander_input_mask};
  37. uint8_t pullup[2] = {0, expander_input_mask};
  38. for (uint8_t i = 0; i < 2; ++i) {
  39. expander_status = i2c_write_register(i2c_addr[i], IODIRA, direction, 2, I2C_TIMEOUT);
  40. if (expander_status) return;
  41. expander_status = i2c_write_register(i2c_addr[i], GPPUA, pullup, 2, I2C_TIMEOUT);
  42. }
  43. }
  44. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  45. bool matrix_has_changed = false;
  46. if (expander_status) { // if there was an error
  47. ++expander_reset_loop;
  48. if (++expander_reset_loop == 0) {
  49. // since expander_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  50. // this will be approx bit more frequent than once per second
  51. matrix_init_custom();
  52. }
  53. }
  54. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  55. matrix_has_changed |= read_rows_on_col(current_matrix, current_col);
  56. }
  57. return matrix_has_changed;
  58. }
  59. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  60. bool matrix_changed = false;
  61. uint8_t port = 0xFF & ~(1 << current_col);
  62. uint8_t column_state[] = {0, 0};
  63. // On both expanders: select col and read rows
  64. for (size_t i = 0; i < 2; ++i) {
  65. if (!expander_status) {
  66. expander_status = i2c_write_register(i2c_addr[i], EXPANDER_COL_REGISTER, &port, 1, I2C_TIMEOUT);
  67. }
  68. wait_us(30);
  69. if (expander_status) {
  70. return false;
  71. }
  72. expander_status = i2c_read_register(i2c_addr[i], EXPANDER_ROW_REGISTER, &column_state[i], 1, I2C_TIMEOUT);
  73. column_state[i] = (~column_state[i]) & ((1 << MATRIX_ROWS_PER_SIDE) - 1);
  74. }
  75. // now map rows 0..4 on each side to cumulative to 0..9
  76. uint16_t col_state = column_state[0] | ((column_state[1] << MATRIX_ROWS_PER_SIDE) /*& 0x3e0*/);
  77. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  78. // Store last value of row prior to reading
  79. matrix_row_t last_row_value = current_matrix[current_row];
  80. if (col_state & (1 << current_row)) {
  81. // key closed; set state bit in matrix
  82. current_matrix[current_row] |= (ROW_SHIFTER << current_col);
  83. } else {
  84. // key open; clear state bit in matrix
  85. current_matrix[current_row] &= ~(ROW_SHIFTER << current_col);
  86. }
  87. // Determine whether the matrix changed state
  88. if ((last_row_value != current_matrix[current_row]) && !(matrix_changed)) {
  89. matrix_changed = true;
  90. }
  91. }
  92. return matrix_changed;
  93. }