adds immediate i2c return, fixes ez matrix code

This commit is contained in:
Jack Humbert 2018-06-23 14:18:47 -04:00
parent 6380f83190
commit 7a44ad83fc
5 changed files with 35 additions and 31 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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