matrix.c 9.4 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 "matrix.h"
  22. #include "debug.h"
  23. #include "wait.h"
  24. #include "i2c_master.h"
  25. extern i2c_status_t mcp23017_status;
  26. #define 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
  37. #define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE)
  38. #define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ)
  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 GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  46. #define 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("starting init");
  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(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[] = {IODIRA, 0b11111111, 0b11110000};
  67. mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT);
  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. uint8_t pullup_buf[] = {GPPUA, 0b11111111, 0b11110000};
  76. mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, pullup_buf, sizeof(pullup_buf), I2C_TIMEOUT);
  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 init_cols(void);
  84. static void unselect_rows(void);
  85. static void select_row(uint8_t row);
  86. static uint8_t mcp23017_reset_loop;
  87. void matrix_init_custom(void) {
  88. // initialize row and col
  89. mcp23017_status = init_mcp23017();
  90. unselect_rows();
  91. init_cols();
  92. // initialize matrix state: all keys off
  93. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  94. matrix[i] = 0;
  95. }
  96. }
  97. void matrix_power_up(void) {
  98. mcp23017_status = init_mcp23017();
  99. unselect_rows();
  100. init_cols();
  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. dprint("trying to reset mcp23017\n");
  123. mcp23017_status = init_mcp23017();
  124. if (mcp23017_status) {
  125. dprint("right side not responding\n");
  126. } else {
  127. dprint("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. select_row(left_index);
  137. select_row(right_index);
  138. // we don't need a 30us delay anymore, because selecting a
  139. // left-hand row requires more than 30us for i2c.
  140. changed |= store_matrix_row(current_matrix, left_index);
  141. changed |= store_matrix_row(current_matrix, right_index);
  142. unselect_rows();
  143. }
  144. return changed;
  145. }
  146. static void init_cols(void) {
  147. // init on mcp23017
  148. // not needed, already done as part of init_mcp23017()
  149. // init on mcu
  150. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  151. for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) {
  152. pin_t pin = matrix_col_pins_mcu[pin_index];
  153. gpio_set_pin_input(pin);
  154. gpio_write_pin_high(pin);
  155. }
  156. }
  157. static matrix_row_t read_cols(uint8_t row) {
  158. if (row < MATRIX_ROWS_PER_SIDE) {
  159. pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
  160. matrix_row_t current_row_value = 0;
  161. // For each col...
  162. for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) {
  163. // Select the col pin to read (active low)
  164. uint8_t pin_state = gpio_read_pin(matrix_col_pins_mcu[col_index]);
  165. // Populate the matrix row with the state of the col pin
  166. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  167. }
  168. return current_row_value;
  169. } else {
  170. if (mcp23017_status) { // if there was an error
  171. return 0;
  172. } else {
  173. uint8_t buf[] = {GPIOA};
  174. mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT);
  175. // We read all the pins on GPIOA.
  176. // 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.
  177. // 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.
  178. // 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.
  179. uint8_t data[] = {0};
  180. if (!mcp23017_status) {
  181. mcp23017_status = i2c_receive(I2C_ADDR_READ, data, sizeof(data), I2C_TIMEOUT);
  182. data[0] = ~(data[0]);
  183. }
  184. return data[0];
  185. }
  186. }
  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. gpio_set_pin_input(pin);
  197. gpio_write_pin_low(pin);
  198. }
  199. }
  200. static void select_row(uint8_t row) {
  201. if (row < MATRIX_ROWS_PER_SIDE) {
  202. // select on atmega32u4
  203. pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
  204. pin_t pin = matrix_row_pins_mcu[row];
  205. gpio_set_pin_output(pin);
  206. gpio_write_pin_low(pin);
  207. } else {
  208. // select on mcp23017
  209. if (mcp23017_status) { // if there was an error
  210. // do nothing
  211. } else {
  212. // 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.
  213. // 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.
  214. uint8_t buf[] = {GPIOB, 0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))};
  215. mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT);
  216. }
  217. }
  218. }