matrix.c (8562B)
1 /* 2 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com> 3 2020 Pierre Chevalier <pierrechevalier83@gmail.com> 4 2021 weteor 5 2023 beekeeb 6 */ 7 8 // SPDX-License-Identifier: GPL-2.0-or-later 9 10 /* 11 * This code was heavily inspired by the ergodox_ez keymap, and modernized 12 * to take advantage of the quantum.h microcontroller agnostics gpio control 13 * abstractions and use the macros defined in config.h for the wiring as opposed 14 * to repeating that information all over the place. 15 */ 16 17 #include "matrix.h" 18 #include "debug.h" 19 #include "wait.h" 20 #include "i2c_master.h" 21 22 extern i2c_status_t tca9555_status; 23 //#define I2C_TIMEOUT 1000 24 25 // I2C address: 26 // All address pins of the tca9555 are connected to the ground 27 // | 0 | 1 | 0 | 0 | A2 | A1 | A0 | 28 // | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 29 #define I2C_ADDR (0b0100000 << 1) 30 31 // Register addresses 32 #define IODIRA 0x06 // i/o direction register 33 #define IODIRB 0x07 34 #define IREGP0 0x00 // GPIO pull-up resistor register 35 #define IREGP1 0x01 36 #define OREGP0 0x02 // general purpose i/o port register (write modifies OLAT) 37 #define OREGP1 0x03 38 39 bool i2c_initialized = 0; 40 i2c_status_t tca9555_status = I2C_ADDR; 41 42 uint8_t init_tca9555(void) { 43 dprint("starting init\n"); 44 tca9555_status = I2C_ADDR; 45 46 // I2C subsystem 47 if (i2c_initialized == 0) { 48 i2c_init(); // on pins D(1,0) 49 i2c_initialized = true; 50 wait_ms(I2C_TIMEOUT); 51 } 52 53 // set pin direction 54 // - unused : input : 1 55 // - input : input : 1 56 // - driving : output : 0 57 uint8_t conf[2] = { 58 // This means: read all pins of port 0 59 0b11111111, 60 // This means: we will write on pins 0 to 3 on port 1. read rest 61 0b11110000, 62 }; 63 tca9555_status = i2c_write_register(I2C_ADDR, IODIRA, conf, 2, I2C_TIMEOUT); 64 65 return tca9555_status; 66 } 67 68 /* matrix state(1:on, 0:off) */ 69 static matrix_row_t matrix[MATRIX_ROWS]; // debounced values 70 71 static matrix_row_t read_cols(uint8_t row); 72 static void init_cols(void); 73 static void unselect_rows(void); 74 static void select_row(uint8_t row); 75 76 static uint8_t tca9555_reset_loop; 77 78 void matrix_init_custom(void) { 79 // initialize row and col 80 81 tca9555_status = init_tca9555(); 82 83 unselect_rows(); 84 init_cols(); 85 86 // initialize matrix state: all keys off 87 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 88 matrix[i] = 0; 89 } 90 } 91 92 void matrix_power_up(void) { 93 tca9555_status = init_tca9555(); 94 95 unselect_rows(); 96 init_cols(); 97 98 // initialize matrix state: all keys off 99 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 100 matrix[i] = 0; 101 } 102 } 103 104 // Reads and stores a row, returning 105 // whether a change occurred. 106 static inline bool store_matrix_row(matrix_row_t current_matrix[], uint8_t index) { 107 matrix_row_t temp = read_cols(index); 108 if (current_matrix[index] != temp) { 109 current_matrix[index] = temp; 110 return true; 111 } 112 return false; 113 } 114 115 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 116 if (tca9555_status) { // if there was an error 117 if (++tca9555_reset_loop == 0) { 118 // since tca9555_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 119 // this will be approx bit more frequent than once per second 120 dprint("trying to reset tca9555\n"); 121 tca9555_status = init_tca9555(); 122 if (tca9555_status) { 123 dprint("right side not responding\n"); 124 } else { 125 dprint("right side attached\n"); 126 } 127 } 128 } 129 130 bool changed = false; 131 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) { 132 // select rows from left and right hands 133 uint8_t left_index = i; 134 uint8_t right_index = i + MATRIX_ROWS_PER_SIDE; 135 select_row(left_index); 136 select_row(right_index); 137 138 // we don't need a 30us delay anymore, because selecting a 139 // left-hand row requires more than 30us for i2c. 140 141 changed |= store_matrix_row(current_matrix, left_index); 142 changed |= store_matrix_row(current_matrix, right_index); 143 144 unselect_rows(); 145 } 146 147 return changed; 148 } 149 150 static void init_cols(void) { 151 // init on tca9555 152 // not needed, already done as part of init_tca9555() 153 154 // init on mcu 155 pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_L; 156 for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) { 157 pin_t pin = matrix_col_pins_mcu[pin_index]; 158 gpio_set_pin_input_high(pin); 159 gpio_write_pin_high(pin); 160 } 161 } 162 163 static matrix_row_t read_cols(uint8_t row) { 164 if (row < MATRIX_ROWS_PER_SIDE) { 165 pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_L; 166 matrix_row_t current_row_value = 0; 167 // For each col... 168 for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) { 169 // Select the col pin to read (active low) 170 uint8_t pin_state = gpio_read_pin(matrix_col_pins_mcu[col_index]); 171 172 // Populate the matrix row with the state of the col pin 173 current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); 174 } 175 return current_row_value; 176 } else { 177 if (tca9555_status) { // if there was an error 178 return 0; 179 } else { 180 uint8_t data = 0; 181 uint8_t port0 = 0; 182 tca9555_status = i2c_read_register(I2C_ADDR, IREGP0, &port0, 1, I2C_TIMEOUT); 183 if (tca9555_status) { // if there was an error 184 // do nothing 185 return 0; 186 } else { 187 port0 = ~port0; 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 // the pins connected to eact columns are sequential, but in reverse order, and counting from zero down (col 5 -> GPIO04, col6 -> GPIO03 and so on). 192 data |= (port0 & 0x01) << 4; 193 data |= (port0 & 0x02) << 2; 194 data |= (port0 & 0x04); 195 data |= (port0 & 0x08) >> 2; 196 data |= (port0 & 0x10) >> 4; 197 198 tca9555_status = I2C_STATUS_SUCCESS; 199 return data; 200 } 201 } 202 } 203 } 204 205 static void unselect_rows(void) { 206 // no need to unselect on tca9555, because the select step sets all 207 // the other row bits high, and it's not changing to a different 208 // direction 209 210 // unselect rows on microcontroller 211 pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_L; 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 gpio_set_pin_input_high(pin); 215 gpio_write_pin_low(pin); 216 } 217 } 218 219 static void select_row(uint8_t row) { 220 uint8_t port1 = 0xff; 221 222 if (row < MATRIX_ROWS_PER_SIDE) { 223 // select on atmega32u4 224 pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_L; 225 pin_t pin = matrix_row_pins_mcu[row]; 226 gpio_set_pin_output(pin); 227 gpio_write_pin_low(pin); 228 } else { 229 // select on tca9555 230 if (tca9555_status) { // if there was an error 231 // do nothing 232 } else { 233 switch(row) { 234 case 4: port1 &= ~(1 << 0); break; 235 case 5: port1 &= ~(1 << 1); break; 236 case 6: port1 &= ~(1 << 2); break; 237 case 7: 238 port1 &= ~(1 << 3); 239 break; 240 default: break; 241 } 242 243 tca9555_status = i2c_write_register(I2C_ADDR, OREGP1, &port1, 1, I2C_TIMEOUT); 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 } 247 } 248 }