From 6e8067208e8655ef1cdc327e7e47ecc2733a3357 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Tue, 26 Apr 2016 21:47:42 +0200 Subject: [PATCH] i2c.c: simplify error handling. If all error conditions are handled the same, there's not much point to use distinct code for each of them. Also, handle collisions like the other error conditions. This saves a nice 52 bytes of program memory. Program: 24404 bytes Data: 1543 bytes EEPROM: 32 bytes --- i2c.c | 98 ++++++++++++++++++++++++----------------------------------- 1 file changed, 39 insertions(+), 59 deletions(-) diff --git a/i2c.c b/i2c.c index 139815d..2e35cae 100644 --- a/i2c.c +++ b/i2c.c @@ -65,9 +65,7 @@ // Transmission not interrupted. #define I2C_NOINTERRUPTED 0b01111111 -#define I2C_ERROR_BUS_FAIL 0b00000001 -#define I2C_ERROR_NACK 0b00000010 -#define I2C_ERROR_NO_ANSWER 0b00010000 +#define I2C_ERROR 0b00000001 #define I2C_ERROR_LOW_PRIO 0b00100000 @@ -214,9 +212,7 @@ void i2c_write(uint8_t data, uint8_t last_byte) { } // Recover from error conditions by draining the buffer. - if ((i2c_state & I2C_ERROR_BUS_FAIL) || - (i2c_state & I2C_ERROR_NO_ANSWER) || - (i2c_state & I2C_ERROR_NACK)) { + if (i2c_state & I2C_ERROR) { while (buf_canread(send)) { buf_pop(send, TWDR); } @@ -278,21 +274,10 @@ ISR(TWI_vect) { #endif switch (status) { - case TW_BUS_ERROR: - // A hardware error was detected. - #ifdef TWI_INTERRUPT_DEBUG - serial_writechar('1'); - #endif - i2c_state |= I2C_ERROR_BUS_FAIL; - // Let i2c_write() continue. - i2c_should_end = 0; - // Send stop condition. - TWCR = (1<