diff options
Diffstat (limited to 'keyboards/ferris/0_2/matrix.c')
| -rw-r--r-- | keyboards/ferris/0_2/matrix.c | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/keyboards/ferris/0_2/matrix.c b/keyboards/ferris/0_2/matrix.c new file mode 100644 index 000000000..afa8a344c --- /dev/null +++ b/keyboards/ferris/0_2/matrix.c | |||
| @@ -0,0 +1,255 @@ | |||
| 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 QMK_KEYBOARD_H | ||
| 27 | #include "i2c_master.h" | ||
| 28 | #include <print.h> | ||
| 29 | |||
| 30 | extern i2c_status_t mcp23017_status; | ||
| 31 | #define MCP23017_I2C_TIMEOUT 1000 | ||
| 32 | #define I2C_WRITE 0x00 | ||
| 33 | #define I2C_READ 0x01 | ||
| 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 | ||
| 45 | #define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE) | ||
| 46 | #define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ) | ||
| 47 | |||
| 48 | // Register addresses | ||
| 49 | // See https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/Adafruit_MCP23017.h | ||
| 50 | #define IODIRA 0x00 // i/o direction register | ||
| 51 | #define IODIRB 0x01 | ||
| 52 | #define GPPUA 0x0C // GPIO pull-up resistor register | ||
| 53 | #define GPPUB 0x0D | ||
| 54 | #define MCP23017_GPIOA 0x12 // general purpose i/o port register (write modifies OLAT) | ||
| 55 | #define MCP23017_GPIOB 0x13 | ||
| 56 | #define OLATA 0x14 // output latch register | ||
| 57 | #define OLATB 0x15 | ||
| 58 | |||
| 59 | bool i2c_initialized = 0; | ||
| 60 | i2c_status_t mcp23017_status = I2C_ADDR; | ||
| 61 | |||
| 62 | #define I2C2_SCL_BANK GPIOB | ||
| 63 | #define I2C2_SCL 10 | ||
| 64 | #define I2C2_SDA_BANK GPIOB | ||
| 65 | #define I2C2_SDA 11 | ||
| 66 | |||
| 67 | uint8_t init_mcp23017(void) { | ||
| 68 | print("init mcp23017\n"); | ||
| 69 | mcp23017_status = I2C_ADDR; | ||
| 70 | |||
| 71 | // I2C subsystem | ||
| 72 | if (i2c_initialized == 0) { | ||
| 73 | i2c_init(); // on pins D(1,0) | ||
| 74 | i2c_initialized = true; | ||
| 75 | wait_ms(MCP23017_I2C_TIMEOUT); | ||
| 76 | } | ||
| 77 | |||
| 78 | // set pin direction | ||
| 79 | // - unused : input : 1 | ||
| 80 | // - input : input : 1 | ||
| 81 | // - driving : output : 0 | ||
| 82 | // This means: we will read all the bits on GPIOA | ||
| 83 | // This means: we will write to the pins 0-4 on GPIOB (in select_rows) | ||
| 84 | uint8_t buf[] = {IODIRA, 0b11111111, 0b11110000}; | ||
| 85 | print("before transmit\n"); | ||
| 86 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), MCP23017_I2C_TIMEOUT); | ||
| 87 | uprintf("after transmit %i\n", mcp23017_status); | ||
| 88 | if (!mcp23017_status) { | ||
| 89 | // set pull-up | ||
| 90 | // - unused : on : 1 | ||
| 91 | // - input : on : 1 | ||
| 92 | // - driving : off : 0 | ||
| 93 | // This means: we will read all the bits on GPIOA | ||
| 94 | // This means: we will write to the pins 0-4 on GPIOB (in select_rows) | ||
| 95 | uint8_t pullup_buf[] = {GPPUA, 0b11111111, 0b11110000}; | ||
| 96 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, pullup_buf, sizeof(pullup_buf), MCP23017_I2C_TIMEOUT); | ||
| 97 | uprintf("after transmit2 %i\n", mcp23017_status); | ||
| 98 | } | ||
| 99 | return mcp23017_status; | ||
| 100 | } | ||
| 101 | |||
| 102 | /* matrix state(1:on, 0:off) */ | ||
| 103 | static matrix_row_t matrix[MATRIX_ROWS]; // debounced values | ||
| 104 | |||
| 105 | static matrix_row_t read_cols(uint8_t row); | ||
| 106 | static void unselect_row(uint8_t row); | ||
| 107 | static void unselect_rows(void); | ||
| 108 | static void unselect_cols(void); | ||
| 109 | static void select_row(uint8_t row); | ||
| 110 | |||
| 111 | static uint8_t mcp23017_reset_loop; | ||
| 112 | |||
| 113 | static void init_mcu_pins(void) { | ||
| 114 | unselect_rows(); | ||
| 115 | unselect_cols(); | ||
| 116 | } | ||
| 117 | |||
| 118 | void matrix_init_custom(void) { | ||
| 119 | debug_enable = true; | ||
| 120 | debug_matrix = true; | ||
| 121 | dprint("matrix_init_custom\n"); | ||
| 122 | // initialize row and col | ||
| 123 | init_mcu_pins(); | ||
| 124 | mcp23017_status = init_mcp23017(); | ||
| 125 | |||
| 126 | // initialize matrix state: all keys off | ||
| 127 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 128 | matrix[i] = 0; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | // Reads and stores a row, returning | ||
| 133 | // whether a change occurred. | ||
| 134 | static inline bool store_matrix_row(matrix_row_t current_matrix[], uint8_t index) { | ||
| 135 | matrix_row_t temp = read_cols(index); | ||
| 136 | if (current_matrix[index] != temp) { | ||
| 137 | current_matrix[index] = temp; | ||
| 138 | return true; | ||
| 139 | } | ||
| 140 | return false; | ||
| 141 | } | ||
| 142 | |||
| 143 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { | ||
| 144 | if (mcp23017_status) { // if there was an error | ||
| 145 | if (++mcp23017_reset_loop == 0) { | ||
| 146 | // if (++mcp23017_reset_loop >= 1300) { | ||
| 147 | // since mcp23017_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans | ||
| 148 | // this will be approx bit more frequent than once per second | ||
| 149 | print("trying to reset mcp23017\n"); | ||
| 150 | mcp23017_status = init_mcp23017(); | ||
| 151 | if (mcp23017_status) { | ||
| 152 | print("right side not responding\n"); | ||
| 153 | } else { | ||
| 154 | print("right side attached\n"); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | bool changed = false; | ||
| 160 | for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) { | ||
| 161 | // select rows from left and right hands | ||
| 162 | uint8_t left_index = i; | ||
| 163 | uint8_t right_index = i + MATRIX_ROWS_PER_SIDE; | ||
| 164 | |||
| 165 | changed |= store_matrix_row(current_matrix, left_index); | ||
| 166 | changed |= store_matrix_row(current_matrix, right_index); | ||
| 167 | |||
| 168 | unselect_rows(); | ||
| 169 | } | ||
| 170 | |||
| 171 | return changed; | ||
| 172 | } | ||
| 173 | |||
| 174 | static matrix_row_t read_cols(uint8_t row) { | ||
| 175 | select_row(row); | ||
| 176 | if (row < MATRIX_ROWS_PER_SIDE) { | ||
| 177 | pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU; | ||
| 178 | matrix_row_t current_row_value = 0; | ||
| 179 | matrix_io_delay(); | ||
| 180 | // For each col... | ||
| 181 | for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) { | ||
| 182 | // Select the col pin to read (active low) | ||
| 183 | uint8_t pin_state = readPin(matrix_col_pins_mcu[col_index]); | ||
| 184 | |||
| 185 | // Populate the matrix row with the state of the col pin | ||
| 186 | current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); | ||
| 187 | } | ||
| 188 | unselect_row(row); | ||
| 189 | return current_row_value; | ||
| 190 | } else { | ||
| 191 | // we don't need a 30us delay anymore, because selecting a | ||
| 192 | // right-hand row requires more than 30us for i2c. | ||
| 193 | if (mcp23017_status) { // if there was an error | ||
| 194 | return 0; | ||
| 195 | } else { | ||
| 196 | uint8_t buf[] = {MCP23017_GPIOA}; | ||
| 197 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), MCP23017_I2C_TIMEOUT); | ||
| 198 | // We read all the pins on GPIOA. | ||
| 199 | // 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. | ||
| 200 | // 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. | ||
| 201 | // 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. | ||
| 202 | uint8_t data[] = {0}; | ||
| 203 | if (!mcp23017_status) { | ||
| 204 | mcp23017_status = i2c_receive(I2C_ADDR_READ, data, sizeof(data), MCP23017_I2C_TIMEOUT); | ||
| 205 | data[0] = ~(data[0]); | ||
| 206 | } | ||
| 207 | return data[0]; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | static void unselect_row(uint8_t row) { | ||
| 213 | pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU; | ||
| 214 | setPinInputHigh(matrix_row_pins_mcu[row]); | ||
| 215 | } | ||
| 216 | |||
| 217 | static void unselect_rows(void) { | ||
| 218 | // no need to unselect on mcp23017, because the select step sets all | ||
| 219 | // the other row bits high, and it's not changing to a different | ||
| 220 | // direction | ||
| 221 | |||
| 222 | // unselect rows on microcontroller | ||
| 223 | pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU; | ||
| 224 | for (int pin_index = 0; pin_index < MATRIX_ROWS_PER_SIDE; pin_index++) { | ||
| 225 | pin_t pin = matrix_row_pins_mcu[pin_index]; | ||
| 226 | setPinInputHigh(pin); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | static void unselect_cols(void) { | ||
| 230 | pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU; | ||
| 231 | for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) { | ||
| 232 | pin_t pin = matrix_col_pins_mcu[pin_index]; | ||
| 233 | setPinInputHigh(pin); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | static void select_row(uint8_t row) { | ||
| 238 | if (row < MATRIX_ROWS_PER_SIDE) { | ||
| 239 | // select on MCU | ||
| 240 | pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU; | ||
| 241 | pin_t pin = matrix_row_pins_mcu[row]; | ||
| 242 | setPinOutput(pin); | ||
| 243 | writePinLow(pin); | ||
| 244 | } else { | ||
| 245 | // select on mcp23017 | ||
| 246 | if (mcp23017_status) { // if there was an error | ||
| 247 | // do nothing | ||
| 248 | } else { | ||
| 249 | // 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. | ||
| 250 | // 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. | ||
| 251 | uint8_t buf[] = {MCP23017_GPIOB, 0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))}; | ||
| 252 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
