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