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.

243 lines
9.2 KiB

  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  3. 2020 Pierre Chevalier <pierrechevalier83@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. * This code was heavily inspired by the ergodox_ez keymap, and modernized
  17. * to take advantage of the quantum.h microcontroller agnostics gpio control
  18. * abstractions and use the macros defined in config.h for the wiring as opposed
  19. * to repeating that information all over the place.
  20. */
  21. #include "matrix.h"
  22. #include "debug.h"
  23. #include "wait.h"
  24. #include "i2c_master.h"
  25. extern i2c_status_t mcp23017_status;
  26. #define MCP23017_I2C_TIMEOUT 1000
  27. #define I2C_WRITE 0x00
  28. #define I2C_READ 0x01
  29. // For a better understanding of the i2c protocol, this is a good read:
  30. // https://www.robot-electronics.co.uk/i2c-tutorial
  31. // I2C address:
  32. // See the datasheet, section 3.3.1 on addressing I2C devices and figure 3-6 for an
  33. // illustration
  34. // http://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf
  35. // All address pins of the mcp23017 are connected to the ground on the ferris
  36. // | 0 | 1 | 0 | 0 | A2 | A1 | A0 |
  37. // | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
  38. #define I2C_ADDR (0b0100000 << 1)
  39. // Register addresses
  40. // See https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/Adafruit_MCP23017.h
  41. #define IODIRA 0x00 // i/o direction register
  42. #define IODIRB 0x01
  43. #define GPPUA 0x0C // GPIO pull-up resistor register
  44. #define GPPUB 0x0D
  45. #define MCP23017_GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  46. #define MCP23017_GPIOB 0x13
  47. #define OLATA 0x14 // output latch register
  48. #define OLATB 0x15
  49. bool i2c_initialized = 0;
  50. i2c_status_t mcp23017_status = I2C_ADDR;
  51. uint8_t init_mcp23017(void) {
  52. print("init mcp23017\n");
  53. mcp23017_status = I2C_ADDR;
  54. // I2C subsystem
  55. if (i2c_initialized == 0) {
  56. i2c_init(); // on pins D(1,0)
  57. i2c_initialized = true;
  58. wait_ms(MCP23017_I2C_TIMEOUT);
  59. }
  60. // set pin direction
  61. // - unused : input : 1
  62. // - input : input : 1
  63. // - driving : output : 0
  64. // This means: we will read all the bits on GPIOA
  65. // This means: we will write to the pins 0-4 on GPIOB (in select_rows)
  66. uint8_t buf[] = {0b11111111, 0b11110000};
  67. print("before transmit\n");
  68. mcp23017_status = i2c_writeReg(I2C_ADDR, IODIRA, buf, sizeof(buf), MCP23017_I2C_TIMEOUT)
  69. uprintf("after transmit %i\n", mcp23017_status);
  70. if (!mcp23017_status) {
  71. // set pull-up
  72. // - unused : on : 1
  73. // - input : on : 1
  74. // - driving : off : 0
  75. // This means: we will read all the bits on GPIOA
  76. // This means: we will write to the pins 0-4 on GPIOB (in select_rows)
  77. mcp23017_status = i2c_writeReg(I2C_ADDR, GPPUA, buf, sizeof(buf), MCP23017_I2C_TIMEOUT)
  78. uprintf("after transmit2 %i\n", mcp23017_status);
  79. }
  80. return mcp23017_status;
  81. }
  82. /* matrix state(1:on, 0:off) */
  83. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  84. static matrix_row_t read_cols(uint8_t row);
  85. static void unselect_row(uint8_t row);
  86. static void unselect_rows(void);
  87. static void unselect_cols(void);
  88. static void select_row(uint8_t row);
  89. static uint8_t mcp23017_reset_loop;
  90. static void init_mcu_pins(void) {
  91. unselect_rows();
  92. unselect_cols();
  93. }
  94. void matrix_init_custom(void) {
  95. debug_enable = true;
  96. debug_matrix = true;
  97. dprint("matrix_init_custom\n");
  98. // initialize row and col
  99. init_mcu_pins();
  100. mcp23017_status = init_mcp23017();
  101. // initialize matrix state: all keys off
  102. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  103. matrix[i] = 0;
  104. }
  105. }
  106. // Reads and stores a row, returning
  107. // whether a change occurred.
  108. static inline bool store_matrix_row(matrix_row_t current_matrix[], uint8_t index) {
  109. matrix_row_t temp = read_cols(index);
  110. if (current_matrix[index] != temp) {
  111. current_matrix[index] = temp;
  112. return true;
  113. }
  114. return false;
  115. }
  116. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  117. if (mcp23017_status) { // if there was an error
  118. if (++mcp23017_reset_loop == 0) {
  119. // if (++mcp23017_reset_loop >= 1300) {
  120. // since mcp23017_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  121. // this will be approx bit more frequent than once per second
  122. print("trying to reset mcp23017\n");
  123. mcp23017_status = init_mcp23017();
  124. if (mcp23017_status) {
  125. print("right side not responding\n");
  126. } else {
  127. print("right side attached\n");
  128. }
  129. }
  130. }
  131. bool changed = false;
  132. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  133. // select rows from left and right hands
  134. uint8_t left_index = i;
  135. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  136. changed |= store_matrix_row(current_matrix, left_index);
  137. changed |= store_matrix_row(current_matrix, right_index);
  138. unselect_rows();
  139. }
  140. return changed;
  141. }
  142. static matrix_row_t read_cols(uint8_t row) {
  143. select_row(row);
  144. if (row < MATRIX_ROWS_PER_SIDE) {
  145. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  146. matrix_row_t current_row_value = 0;
  147. matrix_io_delay();
  148. // For each col...
  149. for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) {
  150. // Select the col pin to read (active low)
  151. uint8_t pin_state = readPin(matrix_col_pins_mcu[col_index]);
  152. // Populate the matrix row with the state of the col pin
  153. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  154. }
  155. unselect_row(row);
  156. return current_row_value;
  157. } else {
  158. // we don't need a 30us delay anymore, because selecting a
  159. // right-hand row requires more than 30us for i2c.
  160. if (mcp23017_status) { // if there was an error
  161. return 0;
  162. } else {
  163. // We read all the pins on GPIOA.
  164. // The initial state was all ones and any depressed key at a given column for the currently selected row will have its bit flipped to zero.
  165. // The return value is a row as represented in the generic matrix code were the rightmost bits represent the lower columns and zeroes represent non-depressed keys while ones represent depressed keys.
  166. // Since the pins connected to eact columns are sequential, and counting from zero up (col 5 -> GPIOA0, col 6 -> GPIOA1 and so on), the only transformation needed is a bitwise not to swap all zeroes and ones.
  167. uint8_t data[] = {0};
  168. mcp23017_status = i2c_readReg(I2C_ADDR, MCP23017_GPIOA, data, sizeof(data), MCP23017_I2C_TIMEOUT);
  169. return ~data[0];
  170. }
  171. }
  172. }
  173. static void unselect_row(uint8_t row) {
  174. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  175. setPinInputHigh(matrix_row_pins_mcu[row]);
  176. }
  177. static void unselect_rows(void) {
  178. // no need to unselect on mcp23017, because the select step sets all
  179. // the other row bits high, and it's not changing to a different
  180. // direction
  181. // unselect rows on microcontroller
  182. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  183. for (int pin_index = 0; pin_index < MATRIX_ROWS_PER_SIDE; pin_index++) {
  184. pin_t pin = matrix_row_pins_mcu[pin_index];
  185. setPinInputHigh(pin);
  186. }
  187. }
  188. static void unselect_cols(void) {
  189. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  190. for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) {
  191. pin_t pin = matrix_col_pins_mcu[pin_index];
  192. setPinInputHigh(pin);
  193. }
  194. }
  195. static void select_row(uint8_t row) {
  196. if (row < MATRIX_ROWS_PER_SIDE) {
  197. // select on MCU
  198. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  199. pin_t pin = matrix_row_pins_mcu[row];
  200. setPinOutput(pin);
  201. writePinLow(pin);
  202. } else {
  203. // select on mcp23017
  204. if (mcp23017_status) { // if there was an error
  205. // do nothing
  206. } else {
  207. // Select the desired row by writing a byte for the entire GPIOB bus where only the bit representing the row we want to select is a zero (write instruction) and every other bit is a one.
  208. // Note that the row - MATRIX_ROWS_PER_SIDE reflects the fact that being on the right hand, the columns are numbered from MATRIX_ROWS_PER_SIDE to MATRIX_ROWS, but the pins we want to write to are indexed from zero up on the GPIOB bus.
  209. uint8_t buf[] = {0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))};
  210. mcp23017_status = i2c_writeReg(I2C_ADDR, MCP23017_GPIOB, buf, sizeof(buf), MCP23017_I2C_TIMEOUT);
  211. }
  212. }
  213. }