matrix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. extern i2c_status_t mcp23017_status;
  24. #define I2C_TIMEOUT 1000
  25. // For a better understanding of the i2c protocol, this is a good read:
  26. // https://www.robot-electronics.co.uk/i2c-tutorial
  27. // I2C address:
  28. // See the datasheet, section 3.3.1 on addressing I2C devices and figure 3-6 for an
  29. // illustration
  30. // http://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf
  31. // All address pins of the mcp23017 are connected to the ground on the ferris
  32. // | 0 | 1 | 0 | 0 | A2 | A1 | A0 |
  33. // | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
  34. #define I2C_ADDR 0b0100000
  35. #define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE)
  36. #define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ)
  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 GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  44. #define 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("starting init");
  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(I2C_TIMEOUT);
  57. }
  58. // set pin direction
  59. // - unused : input : 1
  60. // - input : input : 1
  61. // - driving : output : 0
  62. mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
  63. if (mcp23017_status) goto out;
  64. mcp23017_status = i2c_write(IODIRA, I2C_TIMEOUT);
  65. if (mcp23017_status) goto out;
  66. // This means: we will read all the bits on GPIOA
  67. mcp23017_status = i2c_write(0b11111111, I2C_TIMEOUT);
  68. if (mcp23017_status) goto out;
  69. // This means: we will write to the pins 0-4 on GPIOB (in select_rows)
  70. mcp23017_status = i2c_write(0b11110000, I2C_TIMEOUT);
  71. if (mcp23017_status) goto out;
  72. i2c_stop();
  73. // set pull-up
  74. // - unused : on : 1
  75. // - input : on : 1
  76. // - driving : off : 0
  77. mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
  78. if (mcp23017_status) goto out;
  79. mcp23017_status = i2c_write(GPPUA, I2C_TIMEOUT);
  80. if (mcp23017_status) goto out;
  81. // This means: we will read all the bits on GPIOA
  82. mcp23017_status = i2c_write(0b11111111, I2C_TIMEOUT);
  83. if (mcp23017_status) goto out;
  84. // This means: we will write to the pins 0-4 on GPIOB (in select_rows)
  85. mcp23017_status = i2c_write(0b11110000, I2C_TIMEOUT);
  86. if (mcp23017_status) goto out;
  87. out:
  88. i2c_stop();
  89. return mcp23017_status;
  90. }
  91. /* matrix state(1:on, 0:off) */
  92. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  93. static matrix_row_t read_cols(uint8_t row);
  94. static void init_cols(void);
  95. static void unselect_rows(void);
  96. static void select_row(uint8_t row);
  97. static uint8_t mcp23017_reset_loop;
  98. void matrix_init_custom(void) {
  99. // initialize row and col
  100. mcp23017_status = init_mcp23017();
  101. unselect_rows();
  102. init_cols();
  103. // initialize matrix state: all keys off
  104. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  105. matrix[i] = 0;
  106. }
  107. }
  108. void matrix_power_up(void) {
  109. mcp23017_status = init_mcp23017();
  110. unselect_rows();
  111. init_cols();
  112. // initialize matrix state: all keys off
  113. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  114. matrix[i] = 0;
  115. }
  116. }
  117. // Reads and stores a row, returning
  118. // whether a change occurred.
  119. static inline bool store_matrix_row(matrix_row_t current_matrix[], uint8_t index) {
  120. matrix_row_t temp = read_cols(index);
  121. if (current_matrix[index] != temp) {
  122. current_matrix[index] = temp;
  123. return true;
  124. }
  125. return false;
  126. }
  127. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  128. if (mcp23017_status) { // if there was an error
  129. if (++mcp23017_reset_loop == 0) {
  130. // if (++mcp23017_reset_loop >= 1300) {
  131. // since mcp23017_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  132. // this will be approx bit more frequent than once per second
  133. dprint("trying to reset mcp23017\n");
  134. mcp23017_status = init_mcp23017();
  135. if (mcp23017_status) {
  136. dprint("right side not responding\n");
  137. } else {
  138. dprint("right side attached\n");
  139. }
  140. }
  141. }
  142. bool changed = false;
  143. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  144. // select rows from left and right hands
  145. uint8_t left_index = i;
  146. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  147. select_row(left_index);
  148. select_row(right_index);
  149. // we don't need a 30us delay anymore, because selecting a
  150. // left-hand row requires more than 30us for i2c.
  151. changed |= store_matrix_row(current_matrix, left_index);
  152. changed |= store_matrix_row(current_matrix, right_index);
  153. unselect_rows();
  154. }
  155. return changed;
  156. }
  157. static void init_cols(void) {
  158. // init on mcp23017
  159. // not needed, already done as part of init_mcp23017()
  160. // init on mcu
  161. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  162. for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) {
  163. pin_t pin = matrix_col_pins_mcu[pin_index];
  164. setPinInput(pin);
  165. writePinHigh(pin);
  166. }
  167. }
  168. static matrix_row_t read_cols(uint8_t row) {
  169. if (row < MATRIX_ROWS_PER_SIDE) {
  170. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  171. matrix_row_t current_row_value = 0;
  172. // For each col...
  173. for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) {
  174. // Select the col pin to read (active low)
  175. uint8_t pin_state = readPin(matrix_col_pins_mcu[col_index]);
  176. // Populate the matrix row with the state of the col pin
  177. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  178. }
  179. return current_row_value;
  180. } else {
  181. if (mcp23017_status) { // if there was an error
  182. return 0;
  183. } else {
  184. uint8_t data = 0;
  185. mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
  186. if (mcp23017_status) goto out;
  187. mcp23017_status = i2c_write(GPIOA, I2C_TIMEOUT);
  188. if (mcp23017_status) goto out;
  189. mcp23017_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT);
  190. if (mcp23017_status) goto out;
  191. mcp23017_status = i2c_read_nack(I2C_TIMEOUT);
  192. if (mcp23017_status < 0) goto out;
  193. // We read all the pins on GPIOA.
  194. // 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.
  195. // 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.
  196. // 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.
  197. data = ~((uint8_t)mcp23017_status);
  198. mcp23017_status = I2C_STATUS_SUCCESS;
  199. out:
  200. i2c_stop();
  201. // return reverse_bits(data, MATRIX_COLS_PER_SIDE);
  202. return data;
  203. }
  204. }
  205. }
  206. static void unselect_rows(void) {
  207. // no need to unselect on mcp23017, because the select step sets all
  208. // the other row bits high, and it's not changing to a different
  209. // direction
  210. // unselect rows on microcontroller
  211. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  212. for (int pin_index = 0; pin_index < MATRIX_ROWS_PER_SIDE; pin_index++) {
  213. pin_t pin = matrix_row_pins_mcu[pin_index];
  214. setPinInput(pin);
  215. writePinLow(pin);
  216. }
  217. }
  218. static void select_row(uint8_t row) {
  219. if (row < MATRIX_ROWS_PER_SIDE) {
  220. // select on atmega32u4
  221. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  222. pin_t pin = matrix_row_pins_mcu[row];
  223. setPinOutput(pin);
  224. writePinLow(pin);
  225. } else {
  226. // select on mcp23017
  227. if (mcp23017_status) { // if there was an error
  228. // do nothing
  229. } else {
  230. mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
  231. if (mcp23017_status) goto out;
  232. mcp23017_status = i2c_write(GPIOB, I2C_TIMEOUT);
  233. if (mcp23017_status) goto out;
  234. // 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.
  235. // 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.
  236. mcp23017_status = i2c_write(0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE)), I2C_TIMEOUT);
  237. if (mcp23017_status) goto out;
  238. out:
  239. i2c_stop();
  240. }
  241. }
  242. }