diff options
| author | Pierre Chevalier <pierrechevalier83@gmail.com> | 2021-04-18 16:51:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-18 17:51:45 +0200 |
| commit | d0973e1cfb9f0a2643a30128ba119e76c6fe2f3c (patch) | |
| tree | ba66dc60ceee797b62b205e9c1c0da78a7e03df0 /keyboards/ferris/0_1/matrix.c | |
| parent | c32264d9b7382e86c7df75edfe3bc4154d714e25 (diff) | |
| download | qmk_firmware-d0973e1cfb9f0a2643a30128ba119e76c6fe2f3c.tar.gz qmk_firmware-d0973e1cfb9f0a2643a30128ba119e76c6fe2f3c.zip | |
Add ferris 0.2 (#12133)
Co-authored-by: Ryan <fauxpark@gmail.com>
Diffstat (limited to 'keyboards/ferris/0_1/matrix.c')
| -rw-r--r-- | keyboards/ferris/0_1/matrix.c | 77 |
1 files changed, 24 insertions, 53 deletions
diff --git a/keyboards/ferris/0_1/matrix.c b/keyboards/ferris/0_1/matrix.c index e13c35d35..0dfb150b6 100644 --- a/keyboards/ferris/0_1/matrix.c +++ b/keyboards/ferris/0_1/matrix.c | |||
| @@ -72,40 +72,25 @@ uint8_t init_mcp23017(void) { | |||
| 72 | // - unused : input : 1 | 72 | // - unused : input : 1 |
| 73 | // - input : input : 1 | 73 | // - input : input : 1 |
| 74 | // - driving : output : 0 | 74 | // - driving : output : 0 |
| 75 | mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); | ||
| 76 | if (mcp23017_status) goto out; | ||
| 77 | mcp23017_status = i2c_write(IODIRA, I2C_TIMEOUT); | ||
| 78 | if (mcp23017_status) goto out; | ||
| 79 | // This means: we will read all the bits on GPIOA | 75 | // This means: we will read all the bits on GPIOA |
| 80 | mcp23017_status = i2c_write(0b11111111, I2C_TIMEOUT); | ||
| 81 | if (mcp23017_status) goto out; | ||
| 82 | // This means: we will write to the pins 0-4 on GPIOB (in select_rows) | 76 | // This means: we will write to the pins 0-4 on GPIOB (in select_rows) |
| 83 | mcp23017_status = i2c_write(0b11110000, I2C_TIMEOUT); | 77 | uint8_t buf[] = {IODIRA, 0b11111111, 0b11110000}; |
| 84 | if (mcp23017_status) goto out; | 78 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT); |
| 85 | i2c_stop(); | 79 | if (!mcp23017_status) { |
| 86 | 80 | // set pull-up | |
| 87 | // set pull-up | 81 | // - unused : on : 1 |
| 88 | // - unused : on : 1 | 82 | // - input : on : 1 |
| 89 | // - input : on : 1 | 83 | // - driving : off : 0 |
| 90 | // - driving : off : 0 | 84 | // This means: we will read all the bits on GPIOA |
| 91 | mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); | 85 | // This means: we will write to the pins 0-4 on GPIOB (in select_rows) |
| 92 | if (mcp23017_status) goto out; | 86 | uint8_t pullup_buf[] = {GPPUA, 0b11111111, 0b11110000}; |
| 93 | mcp23017_status = i2c_write(GPPUA, I2C_TIMEOUT); | 87 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, pullup_buf, sizeof(pullup_buf), I2C_TIMEOUT); |
| 94 | if (mcp23017_status) goto out; | 88 | } |
| 95 | // This means: we will read all the bits on GPIOA | ||
| 96 | mcp23017_status = i2c_write(0b11111111, I2C_TIMEOUT); | ||
| 97 | if (mcp23017_status) goto out; | ||
| 98 | // This means: we will write to the pins 0-4 on GPIOB (in select_rows) | ||
| 99 | mcp23017_status = i2c_write(0b11110000, I2C_TIMEOUT); | ||
| 100 | if (mcp23017_status) goto out; | ||
| 101 | |||
| 102 | out: | ||
| 103 | i2c_stop(); | ||
| 104 | return mcp23017_status; | 89 | return mcp23017_status; |
| 105 | } | 90 | } |
| 106 | 91 | ||
| 107 | /* matrix state(1:on, 0:off) */ | 92 | /* matrix state(1:on, 0:off) */ |
| 108 | static matrix_row_t matrix[MATRIX_ROWS]; // debounced values | 93 | static matrix_row_t matrix[MATRIX_ROWS]; // debounced values |
| 109 | 94 | ||
| 110 | static matrix_row_t read_cols(uint8_t row); | 95 | static matrix_row_t read_cols(uint8_t row); |
| 111 | static void init_cols(void); | 96 | static void init_cols(void); |
| @@ -124,7 +109,7 @@ void matrix_init_custom(void) { | |||
| 124 | 109 | ||
| 125 | // initialize matrix state: all keys off | 110 | // initialize matrix state: all keys off |
| 126 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 111 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { |
| 127 | matrix[i] = 0; | 112 | matrix[i] = 0; |
| 128 | } | 113 | } |
| 129 | } | 114 | } |
| 130 | 115 | ||
| @@ -217,25 +202,18 @@ static matrix_row_t read_cols(uint8_t row) { | |||
| 217 | if (mcp23017_status) { // if there was an error | 202 | if (mcp23017_status) { // if there was an error |
| 218 | return 0; | 203 | return 0; |
| 219 | } else { | 204 | } else { |
| 220 | uint8_t data = 0; | 205 | uint8_t buf[] = {GPIOA}; |
| 221 | mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); | 206 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT); |
| 222 | if (mcp23017_status) goto out; | ||
| 223 | mcp23017_status = i2c_write(GPIOA, I2C_TIMEOUT); | ||
| 224 | if (mcp23017_status) goto out; | ||
| 225 | mcp23017_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); | ||
| 226 | if (mcp23017_status) goto out; | ||
| 227 | mcp23017_status = i2c_read_nack(I2C_TIMEOUT); | ||
| 228 | if (mcp23017_status < 0) goto out; | ||
| 229 | // We read all the pins on GPIOA. | 207 | // We read all the pins on GPIOA. |
| 230 | // 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. | 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. |
| 231 | // 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. | 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. |
| 232 | // 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. | 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. |
| 233 | data = ~((uint8_t)mcp23017_status); | 211 | uint8_t data[] = {0}; |
| 234 | mcp23017_status = I2C_STATUS_SUCCESS; | 212 | if (!mcp23017_status) { |
| 235 | out: | 213 | mcp23017_status = i2c_receive(I2C_ADDR_READ, data, sizeof(data), I2C_TIMEOUT); |
| 236 | i2c_stop(); | 214 | data[0] = ~(data[0]); |
| 237 | // return reverse_bits(data, MATRIX_COLS_PER_SIDE); | 215 | } |
| 238 | return data; | 216 | return data[0]; |
| 239 | } | 217 | } |
| 240 | } | 218 | } |
| 241 | } | 219 | } |
| @@ -266,17 +244,10 @@ static void select_row(uint8_t row) { | |||
| 266 | if (mcp23017_status) { // if there was an error | 244 | if (mcp23017_status) { // if there was an error |
| 267 | // do nothing | 245 | // do nothing |
| 268 | } else { | 246 | } else { |
| 269 | mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); | ||
| 270 | if (mcp23017_status) goto out; | ||
| 271 | mcp23017_status = i2c_write(GPIOB, I2C_TIMEOUT); | ||
| 272 | if (mcp23017_status) goto out; | ||
| 273 | // 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. | 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. |
| 274 | // 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. | 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. |
| 275 | mcp23017_status = i2c_write(0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE)), I2C_TIMEOUT); | 249 | uint8_t buf[] = {GPIOB, 0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))}; |
| 276 | 250 | mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT); | |
| 277 | if (mcp23017_status) goto out; | ||
| 278 | out: | ||
| 279 | i2c_stop(); | ||
| 280 | } | 251 | } |
| 281 | } | 252 | } |
| 282 | } | 253 | } |
