From 60c5bd7b5e0e75f48d9731118b92a3add3342efd Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Wed, 8 Sep 2021 02:23:11 +0100 Subject: handwired/dactyl - Refactor use of legacy i2c implementation (#14344) --- keyboards/handwired/dactyl/matrix.c | 99 ++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 51 deletions(-) (limited to 'keyboards/handwired/dactyl/matrix.c') diff --git a/keyboards/handwired/dactyl/matrix.c b/keyboards/handwired/dactyl/matrix.c index 615621522d..a21cd08e14 100644 --- a/keyboards/handwired/dactyl/matrix.c +++ b/keyboards/handwired/dactyl/matrix.c @@ -25,7 +25,7 @@ along with this program. If not, see . #include "util.h" #include "matrix.h" #include "dactyl.h" -#include "i2cmaster.h" +#include "i2c_master.h" #include "timer.h" @@ -147,9 +147,6 @@ void init_expander(void) { #endif } - expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out; - expander_status = i2c_write(IODIRA); if (expander_status) goto out; - /* Pin direction and pull-up depends on both the diode direction and on whether the column register is GPIOA or GPIOB @@ -164,50 +161,65 @@ void init_expander(void) { #if (EXPANDER_COL_REGISTER == GPIOA) # if (DIODE_DIRECTION == COL2ROW) - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; - expander_status = i2c_write(0); if (expander_status) goto out; + uint8_t direction[2] = { + expander_input_pin_mask, + 0, + }; # elif (DIODE_DIRECTION == ROW2COL) - expander_status = i2c_write(0); if (expander_status) goto out; - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; + uint8_t direction[2] = { + 0, + expander_input_pin_mask, + }; # endif #elif (EXPANDER_COL_REGISTER == GPIOB) # if (DIODE_DIRECTION == COL2ROW) - expander_status = i2c_write(0); if (expander_status) goto out; - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; + uint8_t direction[2] = { + 0, + expander_input_pin_mask, + }; # elif (DIODE_DIRECTION == ROW2COL) - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; - expander_status = i2c_write(0); if (expander_status) goto out; + uint8_t direction[2] = { + expander_input_pin_mask, + 0, + }; # endif #endif - i2c_stop(); - // set pull-up // - unused : off : 0 // - input : on : 1 // - driving : off : 0 - expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out; - expander_status = i2c_write(GPPUA); if (expander_status) goto out; #if (EXPANDER_COL_REGISTER == GPIOA) # if (DIODE_DIRECTION == COL2ROW) - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; - expander_status = i2c_write(0); if (expander_status) goto out; + uint8_t pullup[2] = { + expander_input_pin_mask, + 0, + }; # elif (DIODE_DIRECTION == ROW2COL) - expander_status = i2c_write(0); if (expander_status) goto out; - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; + uint8_t pullup[2] = { + 0, + expander_input_pin_mask, + }; # endif #elif (EXPANDER_COL_REGISTER == GPIOB) # if (DIODE_DIRECTION == COL2ROW) - expander_status = i2c_write(0); if (expander_status) goto out; - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; + uint8_t pullup[2] = { + 0, + expander_input_pin_mask, + }; # elif (DIODE_DIRECTION == ROW2COL) - expander_status = i2c_write(expander_input_pin_mask); if (expander_status) goto out; - expander_status = i2c_write(0); if (expander_status) goto out; + uint8_t pullup[2] = { + expander_input_pin_mask, + 0, + }; # endif #endif -out: - i2c_stop(); + + expander_status = i2c_writeReg(I2C_ADDR, IODIRA, direction, 2, I2C_TIMEOUT); + if (expander_status) return; + + expander_status = i2c_writeReg(I2C_ADDR, GPPUA, pullup, 2, I2C_TIMEOUT); } uint8_t matrix_scan(void) @@ -337,14 +349,11 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Read columns from expander, unless it's in an error state if (! expander_status) { - expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out; - expander_status = i2c_write(EXPANDER_COL_REGISTER); if (expander_status) goto out; - expander_status = i2c_start(I2C_ADDR_READ); if (expander_status) goto out; - - current_matrix[current_row] |= (~i2c_readNak()) & expander_input_pin_mask; - - out: - i2c_stop(); + uint8_t state = 0; + expander_status = i2c_readReg(I2C_ADDR, EXPANDER_COL_REGISTER, &state, 1, I2C_TIMEOUT); + if (! expander_status) { + current_matrix[current_row] |= (~state) & expander_input_pin_mask; + } } // Read columns from onboard pins @@ -366,11 +375,8 @@ static void select_row(uint8_t row) { if (! expander_status) { // set active row low : 0 // set other rows hi-Z : 1 - expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out; - expander_status = i2c_write(EXPANDER_ROW_REGISTER); if (expander_status) goto out; - expander_status = i2c_write(0xFF & ~(1<