I2C driver cleanup (#21273)
* remove i2c_start and i2c_stop from i2c drivers * remove static i2c_address variable from chibios i2c driver
This commit is contained in:
@@ -14,7 +14,6 @@ void ds1307_set_time(uint8_t h, uint8_t m, uint8_t s) {
|
||||
void ds1307_get_time(uint8_t *h, uint8_t *m, uint8_t *s) {
|
||||
uint8_t data[3];
|
||||
i2c_read_register(DS1307_ADDR, 0, data, 3, 100);
|
||||
i2c_stop();
|
||||
*s = (data[0] & 0b1111) + ((data[0] & 0b1110000) >> 4) * 10;
|
||||
*m = (data[1] & 0b1111) + ((data[1] & 0b1110000) >> 4) * 10;
|
||||
*h = (data[2] & 0b1111) + ((data[2] & 0b0110000) >> 4) * 10;
|
||||
|
||||
@@ -83,23 +83,16 @@ uint8_t init_mcp23018(void) {
|
||||
// - unused : input : 1
|
||||
// - input : input : 1
|
||||
// - driving : output : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(IODIRA, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00111111, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
i2c_stop();
|
||||
uint8_t data[] = {0b00000000, 0b00111111};
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, sizeof(data), I2C_TIMEOUT);
|
||||
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPPUA, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00111111, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
if (!mcp23018_status) {
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPPUA, data, sizeof(data), I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
// SREG=sreg_prev;
|
||||
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
#include <util/delay.h>
|
||||
|
||||
// I2C aliases and register addresses (see "mcp23018.md" on tmk repository)
|
||||
#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
|
||||
#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
|
||||
#define I2C_ADDR (0b0100000<<1)
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
|
||||
@@ -224,15 +224,9 @@ static matrix_row_t read_cols(uint8_t row)
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
data = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
|
||||
data = ~((uint8_t)mcp23018_status);
|
||||
mcp23018_status = I2C_STATUS_SUCCESS;
|
||||
out:
|
||||
i2c_stop();
|
||||
return data;
|
||||
mcp23018_status = i2c_readReg(I2C_ADDR, GPIOB, &data, 1, I2C_TIMEOUT);
|
||||
|
||||
return ~data;
|
||||
}
|
||||
} else {
|
||||
// read from teensy
|
||||
@@ -263,11 +257,10 @@ static void unselect_rows(void)
|
||||
// do nothing
|
||||
} else {
|
||||
// set all rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write( 0xFF & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data;
|
||||
data = 0xFF & ~(0<<8);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
|
||||
|
||||
}
|
||||
|
||||
// unselect on teensy
|
||||
@@ -289,11 +282,8 @@ static void select_row(uint8_t row)
|
||||
} else {
|
||||
// set active row low : 0
|
||||
// set other rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write( 0xFF & ~(1<<row) & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data = 0xFF & ~(1<<row) & ~(0<<8);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
|
||||
}
|
||||
} else {
|
||||
// select on teensy
|
||||
|
||||
@@ -8,16 +8,6 @@
|
||||
// TODO: remove patch
|
||||
#ifdef PROTOCOL_CHIBIOS
|
||||
# pragma message("ChibiOS is currently 'best effort' and might not report accurate results")
|
||||
|
||||
i2c_status_t i2c_start_bodge(uint8_t address, uint16_t timeout) {
|
||||
i2c_start(address);
|
||||
|
||||
// except on ChibiOS where the only way is do do "something"
|
||||
uint8_t data = 0;
|
||||
return i2c_read_register(address, 0, &data, sizeof(data), TIMEOUT);
|
||||
}
|
||||
|
||||
# define i2c_start i2c_start_bodge
|
||||
#endif
|
||||
|
||||
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
|
||||
@@ -31,10 +21,9 @@ void do_scan(void) {
|
||||
|
||||
for (uint8_t address = 1; address < 127; address++) {
|
||||
// The i2c_scanner uses the return value of
|
||||
// i2c_start to see if a device did acknowledge to the address.
|
||||
i2c_status_t error = i2c_start(address << 1, TIMEOUT);
|
||||
// i2c_ping_address to see if a device did acknowledge to the address.
|
||||
i2c_status_t error = i2c_ping_address(address << 1, TIMEOUT);
|
||||
if (error == I2C_STATUS_SUCCESS) {
|
||||
i2c_stop();
|
||||
dprintf(" I2C device found at address 0x%02X\n", address);
|
||||
nDevices++;
|
||||
} else {
|
||||
|
||||
@@ -29,9 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#define I2C_TIMEOUT 100
|
||||
|
||||
#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
|
||||
#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
|
||||
#define I2C_ADDR (0b0100000<<1)
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
@@ -159,9 +157,6 @@ void init_expander(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(IODIRA, I2C_TIMEOUT); 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
|
||||
@@ -176,50 +171,27 @@ void init_expander(void) {
|
||||
|
||||
#if (EXPANDER_COL_REGISTER == GPIOA)
|
||||
# if (DIODE_DIRECTION == COL2ROW)
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
uint8_t data[] = { expander_input_pin_mask, 0};
|
||||
# elif (DIODE_DIRECTION == ROW2COL)
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
uint8_t data[] = { 0, expander_input_pin_mask};
|
||||
# endif
|
||||
#elif (EXPANDER_COL_REGISTER == GPIOB)
|
||||
# if (DIODE_DIRECTION == COL2ROW)
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
uint8_t data[] = { 0, expander_input_pin_mask};
|
||||
# elif (DIODE_DIRECTION == ROW2COL)
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
uint8_t data[] = { expander_input_pin_mask, 0};
|
||||
# endif
|
||||
#endif
|
||||
expander_status = i2c_writeReg(I2C_ADDR, IODIRA, data, sizeof(data), I2C_TIMEOUT);
|
||||
|
||||
i2c_stop();
|
||||
if (!expander_status) {
|
||||
// set pull-up
|
||||
// - unused : off : 0
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
expander_status = i2c_writeReg(I2C_ADDR, GPPUA, data, sizeof(data), I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
// set pull-up
|
||||
// - unused : off : 0
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(GPPUA, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
#if (EXPANDER_COL_REGISTER == GPIOA)
|
||||
# if (DIODE_DIRECTION == COL2ROW)
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
# elif (DIODE_DIRECTION == ROW2COL)
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
# endif
|
||||
#elif (EXPANDER_COL_REGISTER == GPIOB)
|
||||
# if (DIODE_DIRECTION == COL2ROW)
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
# elif (DIODE_DIRECTION == ROW2COL)
|
||||
expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
# endif
|
||||
#endif
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
@@ -332,14 +304,9 @@ 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, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(EXPANDER_COL_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
|
||||
current_matrix[current_row] |= (~i2c_read_nack(I2C_TIMEOUT)) & expander_input_pin_mask;
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data;
|
||||
i2c_readReg(I2C_ADDR, EXPANDER_COL_REGISTER, &data, 1, I2C_TIMEOUT);
|
||||
current_matrix[current_row] |= (~data) & expander_input_pin_mask;
|
||||
}
|
||||
|
||||
// Read columns from onboard pins
|
||||
@@ -361,11 +328,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, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (expander_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data = 0xFF & ~(1<<row);
|
||||
i2c_writeReg(I2C_ADDR, EXPANDER_ROW_REGISTER, &data, 1, I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
// select on teensy
|
||||
@@ -421,14 +385,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
return false;
|
||||
}
|
||||
|
||||
expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
|
||||
column_state = i2c_read_nack(I2C_TIMEOUT);
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
|
||||
i2c_writeReg(I2C_ADDR, EXPANDER_ROW_REGISTER, &column_state, 1, I2C_TIMEOUT);
|
||||
column_state = ~column_state;
|
||||
} else {
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
|
||||
|
||||
Reference in New Issue
Block a user