matrix.c 9.7 KB

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