Skip to content

Commit

Permalink
adds immediate i2c return, fixes ez matrix code
Browse files Browse the repository at this point in the history
  • Loading branch information
jackhumbert committed Jun 23, 2018
1 parent 6380f83 commit 7a44ad8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
12 changes: 6 additions & 6 deletions drivers/avr/i2c_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions drivers/avr/i2c_master.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 15 additions & 16 deletions keyboards/ergodox_ez/ergodox_ez.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions keyboards/ergodox_ez/ergodox_ez.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 10 additions & 9 deletions keyboards/ergodox_ez/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7a44ad8

Please sign in to comment.