diff options
-rwxr-xr-x | drivers/avr/i2c_master.c | 12 | ||||
-rwxr-xr-x | drivers/avr/i2c_master.h | 3 | ||||
-rw-r--r-- | keyboards/ergodox_ez/ergodox_ez.c | 31 | ||||
-rw-r--r-- | keyboards/ergodox_ez/ergodox_ez.h | 1 | ||||
-rw-r--r-- | keyboards/ergodox_ez/matrix.c | 19 |
5 files changed, 35 insertions, 31 deletions
diff --git a/drivers/avr/i2c_master.c b/drivers/avr/i2c_master.c index 30ea760c9..f25e354b8 100755 --- a/drivers/avr/i2c_master.c +++ b/drivers/avr/i2c_master.c @@ -28,7 +28,7 @@ i2c_status_t i2c_start(uint8_t address, uint16_t timeout) uint16_t timeout_timer = timer_read(); while( !(TWCR & (1<<TWINT)) ) { - if (timeout && ((timer_read() - timeout_timer) > timeout)) { + if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) { return I2C_STATUS_TIMEOUT; } } @@ -43,7 +43,7 @@ i2c_status_t i2c_start(uint8_t address, uint16_t timeout) timeout_timer = timer_read(); while( !(TWCR & (1<<TWINT)) ) { - if (timeout && ((timer_read() - timeout_timer) > timeout)) { + if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) { return I2C_STATUS_TIMEOUT; } } @@ -64,7 +64,7 @@ i2c_status_t i2c_write(uint8_t data, uint16_t timeout) uint16_t timeout_timer = timer_read(); while( !(TWCR & (1<<TWINT)) ) { - if (timeout && ((timer_read() - timeout_timer) > timeout)) { + if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) { return I2C_STATUS_TIMEOUT; } } @@ -82,7 +82,7 @@ int16_t i2c_read_ack(uint16_t timeout) uint16_t timeout_timer = timer_read(); while( !(TWCR & (1<<TWINT)) ) { - if (timeout && ((timer_read() - timeout_timer) > timeout)) { + if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) { return I2C_STATUS_TIMEOUT; } } @@ -99,7 +99,7 @@ int16_t i2c_read_nack(uint16_t timeout) uint16_t timeout_timer = timer_read(); while( !(TWCR & (1<<TWINT)) ) { - if (timeout && ((timer_read() - timeout_timer) > timeout)) { + if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) { return I2C_STATUS_TIMEOUT; } } @@ -210,7 +210,7 @@ i2c_status_t i2c_stop(uint16_t timeout) uint16_t timeout_timer = timer_read(); while(TWCR & (1<<TWSTO)) { - if (timeout && ((timer_read() - timeout_timer) > timeout)) { + if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) { return I2C_STATUS_TIMEOUT; } } diff --git a/drivers/avr/i2c_master.h b/drivers/avr/i2c_master.h index 0806d76aa..cf93680be 100755 --- a/drivers/avr/i2c_master.h +++ b/drivers/avr/i2c_master.h @@ -14,6 +14,9 @@ typedef int16_t i2c_status_t; #define I2C_STATUS_ERROR (-1) #define I2C_STATUS_TIMEOUT (-2) +#define I2C_TIMEOUT_IMMEDIATE (0) +#define I2C_TIMEOUT_INFINITE (0xFFFF) + void i2c_init(void); i2c_status_t i2c_start(uint8_t address, uint16_t timeout); i2c_status_t i2c_write(uint8_t data, uint16_t timeout); diff --git a/keyboards/ergodox_ez/ergodox_ez.c b/keyboards/ergodox_ez/ergodox_ez.c index 65671b38f..61f910711 100644 --- a/keyboards/ergodox_ez/ergodox_ez.c +++ b/keyboards/ergodox_ez/ergodox_ez.c @@ -22,7 +22,6 @@ extern inline void ergodox_right_led_set(uint8_t led, uint8_t n); extern inline void ergodox_led_all_set(uint8_t n); - bool i2c_initialized = 0; i2c_status_t mcp23018_status = 0x20; @@ -125,23 +124,23 @@ uint8_t init_mcp23018(void) { // - unused : input : 1 // - input : input : 1 // - driving : output : 0 - mcp23018_status = i2c_start(I2C_ADDR_WRITE, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(IODIRA, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(0b00000000, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(0b00111111, 0); if (mcp23018_status) goto out; - i2c_stop(0); + mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(IODIRA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b00111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + i2c_stop(ERGODOX_EZ_I2C_TIMEOUT); // set pull-up // - unused : on : 1 // - input : on : 1 // - driving : off : 0 - mcp23018_status = i2c_start(I2C_ADDR_WRITE, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(GPPUA, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(0b00000000, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(0b00111111, 0); if (mcp23018_status) goto out; + mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(GPPUA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b00111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; out: - i2c_stop(0); + i2c_stop(ERGODOX_EZ_I2C_TIMEOUT); #ifdef LEFT_LEDS if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update(); @@ -165,22 +164,22 @@ uint8_t ergodox_left_leds_update(void) { // - unused : hi-Z : 1 // - input : hi-Z : 1 // - driving : hi-Z : 1 - mcp23018_status = i2c_start(I2C_ADDR_WRITE, 0); + mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(OLATA, 0); + mcp23018_status = i2c_write(OLATA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; mcp23018_status = i2c_write(0b11111111 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT), - 0); + ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; mcp23018_status = i2c_write(0b11111111 & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT) & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT), - 0); + ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; out: - i2c_stop(0); + i2c_stop(ERGODOX_EZ_I2C_TIMEOUT); return mcp23018_status; } #endif diff --git a/keyboards/ergodox_ez/ergodox_ez.h b/keyboards/ergodox_ez/ergodox_ez.h index 9086db2fb..641030495 100644 --- a/keyboards/ergodox_ez/ergodox_ez.h +++ b/keyboards/ergodox_ez/ergodox_ez.h @@ -24,6 +24,7 @@ #define OLATB 0x15 extern i2c_status_t mcp23018_status; +#define ERGODOX_EZ_I2C_TIMEOUT 100 void init_ergodox(void); void ergodox_blink_all_leds(void); diff --git a/keyboards/ergodox_ez/matrix.c b/keyboards/ergodox_ez/matrix.c index d8b708a16..2e95c83b6 100644 --- a/keyboards/ergodox_ez/matrix.c +++ b/keyboards/ergodox_ez/matrix.c @@ -295,13 +295,14 @@ static matrix_row_t read_cols(uint8_t row) return 0; } else { uint8_t data = 0; - mcp23018_status = i2c_start(I2C_ADDR_WRITE, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(GPIOB, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_start(I2C_ADDR_READ, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_read_nack(0); if (mcp23018_status < 0) goto out; + mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out; data = ~((uint8_t)mcp23018_status); + mcp23018_status = I2C_STATUS_SUCCESS; out: - i2c_stop(0); + i2c_stop(ERGODOX_EZ_I2C_TIMEOUT); return data; } } else { @@ -350,11 +351,11 @@ 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, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(GPIOA, 0); if (mcp23018_status) goto out; - mcp23018_status = i2c_write(0xFF & ~(1<<row), 0); if (mcp23018_status) goto out; + mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out; out: - i2c_stop(0); + i2c_stop(ERGODOX_EZ_I2C_TIMEOUT); } } else { // select on teensy |