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