matrix.c (9492B)
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 init_cols(void); 99 static void unselect_rows(void); 100 static void select_row(uint8_t row); 101 102 static uint8_t mcp23017_reset_loop; 103 104 void matrix_init_custom(void) { 105 // initialize row and col 106 107 mcp23017_status = init_mcp23017(); 108 109 unselect_rows(); 110 init_cols(); 111 112 // initialize matrix state: all keys off 113 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 114 matrix[i] = 0; 115 } 116 } 117 118 void matrix_power_up(void) { 119 mcp23017_status = init_mcp23017(); 120 121 unselect_rows(); 122 init_cols(); 123 124 // initialize matrix state: all keys off 125 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 126 matrix[i] = 0; 127 } 128 } 129 130 // Reads and stores a row, returning 131 // whether a change occurred. 132 static inline bool store_matrix_row(matrix_row_t current_matrix[], uint8_t index) { 133 matrix_row_t temp = read_cols(index); 134 if (current_matrix[index] != temp) { 135 current_matrix[index] = temp; 136 return true; 137 } 138 return false; 139 } 140 141 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 142 if (mcp23017_status) { // if there was an error 143 if (++mcp23017_reset_loop == 0) { 144 // if (++mcp23017_reset_loop >= 1300) { 145 // since mcp23017_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 146 // this will be approx bit more frequent than once per second 147 print("trying to reset mcp23017\n"); 148 mcp23017_status = init_mcp23017(); 149 if (mcp23017_status) { 150 print("right side not responding\n"); 151 } else { 152 print("right side attached\n"); 153 } 154 } 155 } 156 157 bool changed = false; 158 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) { 159 // select rows from left and right hands 160 uint8_t left_index = i; 161 uint8_t right_index = i + MATRIX_ROWS_PER_SIDE; 162 select_row(left_index); 163 select_row(right_index); 164 165 // we don't need a 30us delay anymore, because selecting a 166 // left-hand row requires more than 30us for i2c. 167 168 changed |= store_matrix_row(current_matrix, left_index); 169 changed |= store_matrix_row(current_matrix, right_index); 170 171 unselect_rows(); 172 } 173 174 return changed; 175 } 176 177 static void init_cols(void) { 178 // init on mcp23017 179 // not needed, already done as part of init_mcp23017() 180 181 // init on mcu 182 pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU; 183 for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) { 184 pin_t pin = matrix_col_pins_mcu[pin_index]; 185 gpio_set_pin_input(pin); 186 gpio_write_pin_high(pin); 187 } 188 } 189 190 static matrix_row_t read_cols(uint8_t row) { 191 if (row < MATRIX_ROWS_PER_SIDE) { 192 pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU; 193 matrix_row_t current_row_value = 0; 194 // For each col... 195 for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) { 196 // Select the col pin to read (active low) 197 uint8_t pin_state = gpio_read_pin(matrix_col_pins_mcu[col_index]); 198 199 // Populate the matrix row with the state of the col pin 200 current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); 201 } 202 return current_row_value; 203 } else { 204 if (mcp23017_status) { // if there was an error 205 return 0; 206 } else { 207 // We read all the pins on GPIOA. 208 // 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. 209 // 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. 210 // 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. 211 uint8_t data[] = {0}; 212 mcp23017_status = i2c_read_register(I2C_ADDR, MCP23017_GPIOA, data, sizeof(data), MCP23017_I2C_TIMEOUT); 213 return ~data[0]; 214 } 215 } 216 } 217 218 static void unselect_rows(void) { 219 // no need to unselect on mcp23017, because the select step sets all 220 // the other row bits high, and it's not changing to a different 221 // direction 222 223 // unselect rows on microcontroller 224 pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU; 225 for (int pin_index = 0; pin_index < MATRIX_ROWS_PER_SIDE; pin_index++) { 226 pin_t pin = matrix_row_pins_mcu[pin_index]; 227 gpio_set_pin_input(pin); 228 gpio_write_pin_low(pin); 229 } 230 } 231 232 static void select_row(uint8_t row) { 233 if (row < MATRIX_ROWS_PER_SIDE) { 234 // select on MCU 235 pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU; 236 pin_t pin = matrix_row_pins_mcu[row]; 237 gpio_set_pin_output(pin); 238 gpio_write_pin_low(pin); 239 } else { 240 // select on mcp23017 241 if (mcp23017_status) { // if there was an error 242 // do nothing 243 } else { 244 // 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. 245 // 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. 246 uint8_t buf[] = {0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))}; 247 mcp23017_status = i2c_write_register(I2C_ADDR, MCP23017_GPIOB, buf, sizeof(buf), MCP23017_I2C_TIMEOUT); 248 } 249 } 250 }