From 97502b61b071737a43322acd15b69b609aeb23b8 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 13 Nov 2019 19:26:04 -0700 Subject: [PATCH] Update avr i2c to match new common i2c library --- src/arch/avr/i2c.c | 87 +++++++++++++--------------- src/board/arduino/mega2560/battery.c | 2 +- 2 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/arch/avr/i2c.c b/src/arch/avr/i2c.c index e3255be..0ee788b 100644 --- a/src/arch/avr/i2c.c +++ b/src/arch/avr/i2c.c @@ -7,7 +7,7 @@ #define TIMEOUT (F_CPU/1000) -uint8_t i2c_start(uint8_t addr, bool read) { +int i2c_start(uint8_t addr, bool read) { uint32_t count; // reset TWI control register @@ -17,16 +17,10 @@ uint8_t i2c_start(uint8_t addr, bool read) { // wait for end of transmission count = TIMEOUT; while(!(TWCR & (1< 0) count -= 1; - if (count == 0) { - printf("i2c_start timed out waiting to send start to 0x%X\n", addr); - return 1; - } + if (count == 0) return -1; // check if the start condition was successfully transmitted - if((TWSR & 0xF8) != TW_START){ - printf("i2c_start failed to send start condition to 0x%X\n", addr); - return 1; - } + if((TWSR & 0xF8) != TW_START) return -1; // load slave addr into data register TWDR = ((addr << 1) | read); @@ -35,17 +29,11 @@ uint8_t i2c_start(uint8_t addr, bool read) { // wait for end of transmission count = TIMEOUT; while(!(TWCR & (1< 0) count -= 1; - if (count == 0) { - printf("i2c_start timed out waiting to send addr to 0x%X\n", addr); - return 1; - } + if (count == 0) return -1; // check if the device has acknowledged the READ / WRITE mode uint8_t twst = TW_STATUS & 0xF8; - if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) { - printf("i2c_start failed to receive ack from 0x%X\n", addr); - return 1; - } + if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) return -1; return 0; } @@ -55,39 +43,42 @@ void i2c_stop(void) { TWCR = (1< 0) count -= 1; - if (count == 0) { - printf("i2c_write timed out waiting to send 0x%X\n", data); - return 1; +int i2c_write(uint8_t * data, int length) { + int i; + for (i = 0; i < length; i++) { + // load data into data register + TWDR = data[i]; + // start transmission of data + TWCR = (1< 0) count -= 1; + // timed out + if (count == 0) return -1; + // failed to receive ack + if((TWSR & 0xF8) != TW_MT_DATA_ACK) return -1; } - if( (TWSR & 0xF8) != TW_MT_DATA_ACK ){ - printf("i2c_write failed to receive ack for 0x%X\n", data); - return 1; + return i; +} + +int i2c_read(uint8_t * data, int length) { + int i; + for (i = 0; i < length; i++) { + if ((i + 1) < length) { + // start TWI module and acknowledge data after reception + TWCR = (1< 0) count -= 1; + if (count == 0) return -1; + // return received data from TWDR + data[i] = TWDR; } - return 0; -} - -uint8_t i2c_read(bool ack) { - if (ack) { - // start TWI module and acknowledge data after reception - TWCR = (1< -#include +#include uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { return i2c_get(address, command, (uint8_t *)data, 2);