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.

178 lines
5.3 KiB

  1. /* Copyright 2020 zvecr<git@zvecr.com>
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "mcp23018.h"
  17. #include "quantum.h"
  18. // Optimize scanning code for speed as a slight mitigation for the port expander
  19. #pragma GCC push_options
  20. #pragma GCC optimize("-O3")
  21. #define I2C_ADDR 0x20
  22. static uint16_t mcp23018_reset_loop = 0;
  23. static uint8_t mcp23018_errors = 0;
  24. static const pin_t row_pins[MATRIX_ROWS / 2] = {B1, D5, D4, D6, D7, B4};
  25. static const pin_t col_pins[MATRIX_COLS] = {F5, F6, F7, C7, C6, B6, B5, D3, D2, B3, B2};
  26. //_____REGULAR funcs____________________________________________________________
  27. static void select_row(uint8_t row) {
  28. setPinOutput(row_pins[row]);
  29. writePinLow(row_pins[row]);
  30. }
  31. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  32. static void unselect_rows(void) {
  33. for (uint8_t x = 0; x < MATRIX_ROWS / 2; x++) {
  34. setPinInputHigh(row_pins[x]);
  35. }
  36. }
  37. static void init_pins(void) {
  38. unselect_rows();
  39. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  40. setPinInputHigh(col_pins[x]);
  41. }
  42. }
  43. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  44. // Store last value of row prior to reading
  45. matrix_row_t last_row_value = current_matrix[current_row];
  46. // Clear data in matrix row
  47. matrix_row_t current_row_value = 0;
  48. // Select row and wait for row selection to stabilize
  49. select_row(current_row);
  50. wait_us(5);
  51. // For each col...
  52. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  53. // Select the col pin to read (active low)
  54. uint8_t pin_state = readPin(col_pins[col_index]);
  55. // Populate the matrix row with the state of the col pin
  56. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  57. }
  58. // Unselect row
  59. unselect_row(current_row);
  60. if (last_row_value == current_row_value) {
  61. return false;
  62. }
  63. current_matrix[current_row] = current_row_value;
  64. return true;
  65. }
  66. //_____MCP23018 funcs___________________________________________________________
  67. static void init_pins_MCP23018(void) {
  68. mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTA, 0b11111111);
  69. mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTB, 0b01100000);
  70. }
  71. static void select_row_MCP23018(uint8_t row) {
  72. uint8_t mask = 0;
  73. switch (row) {
  74. case 6:
  75. mask = 0b10000000;
  76. break;
  77. case 7:
  78. mask = 0b00000001;
  79. break;
  80. case 8:
  81. mask = 0b00000010;
  82. break;
  83. case 9:
  84. mask = 0b00000100;
  85. break;
  86. case 10:
  87. mask = 0b00001000;
  88. break;
  89. case 11:
  90. mask = 0b00010000;
  91. break;
  92. }
  93. mcp23018_errors += !mcp23018_set_output(I2C_ADDR, mcp23018_PORTB, ~mask);
  94. }
  95. static uint16_t read_cols_MCP23018(void) {
  96. uint16_t tmp = 0xFFFF;
  97. mcp23018_errors += !mcp23018_readPins_all(I2C_ADDR, &tmp);
  98. uint16_t state = ((tmp & 0b11111111) << 2) | ((tmp & 0b0110000000000000) >> 13);
  99. return (~state) & 0b1111111111;
  100. }
  101. static bool read_cols_on_row_MCP23018(matrix_row_t current_matrix[], uint8_t current_row) {
  102. // Store last value of row prior to reading
  103. matrix_row_t last_row_value = current_matrix[current_row];
  104. // No need to Clear data in matrix row as we just replace in one go
  105. // Select row and wait for row selection to stabilize
  106. select_row_MCP23018(current_row);
  107. matrix_row_t current_row_value = read_cols_MCP23018();
  108. // No need to Unselect row as the next `select_row` will blank everything
  109. if (last_row_value == current_row_value) {
  110. return false;
  111. }
  112. current_matrix[current_row] = current_row_value;
  113. return true;
  114. }
  115. //_____CUSTOM MATRIX IMPLEMENTATION____________________________________________________
  116. void matrix_init_custom(void) {
  117. mcp23018_init(I2C_ADDR);
  118. init_pins();
  119. init_pins_MCP23018();
  120. }
  121. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  122. bool changed = false;
  123. for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
  124. changed |= read_cols_on_row(current_matrix, current_row);
  125. }
  126. if (mcp23018_errors) {
  127. if (++mcp23018_reset_loop > 0x7FFF) {
  128. // tuned to about 5s given the current scan rate
  129. print("trying to reset mcp23018\n");
  130. mcp23018_reset_loop = 0;
  131. mcp23018_errors = 0;
  132. init_pins_MCP23018();
  133. }
  134. return changed;
  135. }
  136. for (uint8_t current_row = MATRIX_ROWS / 2; current_row < MATRIX_ROWS; current_row++) {
  137. changed |= read_cols_on_row_MCP23018(current_matrix, current_row);
  138. }
  139. return changed;
  140. }
  141. #pragma GCC pop_options