logo

qmk_firmware

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

matrix.c (9482B)


  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. // For a better understanding of the i2c protocol, this is a good read:
  28. // https://www.robot-electronics.co.uk/i2c-tutorial
  29. // I2C address:
  30. // See the datasheet, section 3.3.1 on addressing I2C devices and figure 3-6 for an
  31. // illustration
  32. // http://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf
  33. // All address pins of the mcp23017 are connected to the ground on the ferris
  34. // | 0 | 1 | 0 | 0 | A2 | A1 | A0 |
  35. // | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
  36. #define I2C_ADDR (0b0100000 << 1)
  37. // Register addresses
  38. // See https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/Adafruit_MCP23017.h
  39. #define IODIRA 0x00 // i/o direction register
  40. #define IODIRB 0x01
  41. #define GPPUA 0x0C // GPIO pull-up resistor register
  42. #define GPPUB 0x0D
  43. #define MCP23017_GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  44. #define MCP23017_GPIOB 0x13
  45. #define OLATA 0x14 // output latch register
  46. #define OLATB 0x15
  47. bool i2c_initialized = 0;
  48. i2c_status_t mcp23017_status = I2C_ADDR;
  49. uint8_t init_mcp23017(void) {
  50. print("init mcp23017\n");
  51. mcp23017_status = I2C_ADDR;
  52. // I2C subsystem
  53. if (i2c_initialized == 0) {
  54. i2c_init(); // on pins D(1,0)
  55. i2c_initialized = true;
  56. wait_ms(MCP23017_I2C_TIMEOUT);
  57. }
  58. // set pin direction
  59. // - unused : input : 1
  60. // - input : input : 1
  61. // - driving : output : 0
  62. // This means: we will read all the bits on GPIOA
  63. // This means: we will write to the pins 0-4 on GPIOB (in select_rows)
  64. uint8_t buf[] = {0b11111111, 0b11110000};
  65. print("before transmit\n");
  66. mcp23017_status = i2c_write_register(I2C_ADDR, IODIRA, buf, sizeof(buf), MCP23017_I2C_TIMEOUT);
  67. uprintf("after transmit %i\n", mcp23017_status);
  68. if (!mcp23017_status) {
  69. // set pull-up
  70. // - unused : on : 1
  71. // - input : on : 1
  72. // - driving : off : 0
  73. // This means: we will read all the bits on GPIOA
  74. // This means: we will write to the pins 0-4 on GPIOB (in select_rows)
  75. mcp23017_status = i2c_write_register(I2C_ADDR, GPPUA, buf, sizeof(buf), MCP23017_I2C_TIMEOUT);
  76. uprintf("after transmit2 %i\n", mcp23017_status);
  77. }
  78. return mcp23017_status;
  79. }
  80. /* matrix state(1:on, 0:off) */
  81. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  82. static matrix_row_t read_cols(uint8_t row);
  83. static void unselect_row(uint8_t row);
  84. static void unselect_rows(void);
  85. static void unselect_cols(void);
  86. static void select_row(uint8_t row);
  87. static uint8_t mcp23017_reset_loop;
  88. static void init_mcu_pins(void) {
  89. unselect_rows();
  90. unselect_cols();
  91. }
  92. void matrix_init_custom(void) {
  93. debug_enable = true;
  94. debug_matrix = true;
  95. dprint("matrix_init_custom\n");
  96. // initialize row and col
  97. init_mcu_pins();
  98. mcp23017_status = init_mcp23017();
  99. // initialize matrix state: all keys off
  100. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  101. matrix[i] = 0;
  102. }
  103. }
  104. // Reads and stores a row, returning
  105. // whether a change occurred.
  106. static inline bool store_matrix_row(matrix_row_t current_matrix[], uint8_t index) {
  107. matrix_row_t temp = read_cols(index);
  108. if (current_matrix[index] != temp) {
  109. current_matrix[index] = temp;
  110. return true;
  111. }
  112. return false;
  113. }
  114. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  115. if (mcp23017_status) { // if there was an error
  116. if (++mcp23017_reset_loop == 0) {
  117. // if (++mcp23017_reset_loop >= 1300) {
  118. // since mcp23017_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  119. // this will be approx bit more frequent than once per second
  120. print("trying to reset mcp23017\n");
  121. mcp23017_status = init_mcp23017();
  122. if (mcp23017_status) {
  123. print("right side not responding\n");
  124. } else {
  125. print("right side attached\n");
  126. }
  127. }
  128. }
  129. bool changed = false;
  130. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  131. // select rows from left and right hands
  132. uint8_t left_index = i;
  133. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  134. changed |= store_matrix_row(current_matrix, left_index);
  135. changed |= store_matrix_row(current_matrix, right_index);
  136. unselect_rows();
  137. }
  138. return changed;
  139. }
  140. static matrix_row_t read_cols(uint8_t row) {
  141. select_row(row);
  142. if (row < MATRIX_ROWS_PER_SIDE) {
  143. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  144. matrix_row_t current_row_value = 0;
  145. matrix_io_delay();
  146. // For each col...
  147. for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) {
  148. // Select the col pin to read (active low)
  149. uint8_t pin_state = gpio_read_pin(matrix_col_pins_mcu[col_index]);
  150. // Populate the matrix row with the state of the col pin
  151. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  152. }
  153. unselect_row(row);
  154. return current_row_value;
  155. } else {
  156. // we don't need a 30us delay anymore, because selecting a
  157. // right-hand row requires more than 30us for i2c.
  158. if (mcp23017_status) { // if there was an error
  159. return 0;
  160. } else {
  161. // We read all the pins on GPIOA.
  162. // 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.
  163. // 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.
  164. // 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.
  165. uint8_t data[] = {0};
  166. mcp23017_status = i2c_read_register(I2C_ADDR, MCP23017_GPIOA, data, sizeof(data), MCP23017_I2C_TIMEOUT);
  167. return ~data[0];
  168. }
  169. }
  170. }
  171. static void unselect_row(uint8_t row) {
  172. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  173. gpio_set_pin_input_high(matrix_row_pins_mcu[row]);
  174. }
  175. static void unselect_rows(void) {
  176. // no need to unselect on mcp23017, because the select step sets all
  177. // the other row bits high, and it's not changing to a different
  178. // direction
  179. // unselect rows on microcontroller
  180. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  181. for (int pin_index = 0; pin_index < MATRIX_ROWS_PER_SIDE; pin_index++) {
  182. pin_t pin = matrix_row_pins_mcu[pin_index];
  183. gpio_set_pin_input_high(pin);
  184. }
  185. }
  186. static void unselect_cols(void) {
  187. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  188. for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) {
  189. pin_t pin = matrix_col_pins_mcu[pin_index];
  190. gpio_set_pin_input_high(pin);
  191. }
  192. }
  193. static void select_row(uint8_t row) {
  194. if (row < MATRIX_ROWS_PER_SIDE) {
  195. // select on MCU
  196. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  197. pin_t pin = matrix_row_pins_mcu[row];
  198. gpio_set_pin_output(pin);
  199. gpio_write_pin_low(pin);
  200. } else {
  201. // select on mcp23017
  202. if (mcp23017_status) { // if there was an error
  203. // do nothing
  204. } else {
  205. // 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.
  206. // 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.
  207. uint8_t buf[] = {0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))};
  208. mcp23017_status = i2c_write_register(I2C_ADDR, MCP23017_GPIOB, buf, sizeof(buf), MCP23017_I2C_TIMEOUT);
  209. }
  210. }
  211. }