Update avr i2c to match new common i2c library

This commit is contained in:
Jeremy Soller
2019-11-13 19:26:04 -07:00
parent 7401a540e8
commit 97502b61b0
2 changed files with 40 additions and 49 deletions

View File

@ -7,7 +7,7 @@
#define TIMEOUT (F_CPU/1000) #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; uint32_t count;
// reset TWI control register // reset TWI control register
@ -17,16 +17,10 @@ uint8_t i2c_start(uint8_t addr, bool read) {
// wait for end of transmission // wait for end of transmission
count = TIMEOUT; count = TIMEOUT;
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1; while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
if (count == 0) { if (count == 0) return -1;
printf("i2c_start timed out waiting to send start to 0x%X\n", addr);
return 1;
}
// check if the start condition was successfully transmitted // check if the start condition was successfully transmitted
if((TWSR & 0xF8) != TW_START){ if((TWSR & 0xF8) != TW_START) return -1;
printf("i2c_start failed to send start condition to 0x%X\n", addr);
return 1;
}
// load slave addr into data register // load slave addr into data register
TWDR = ((addr << 1) | read); TWDR = ((addr << 1) | read);
@ -35,17 +29,11 @@ uint8_t i2c_start(uint8_t addr, bool read) {
// wait for end of transmission // wait for end of transmission
count = TIMEOUT; count = TIMEOUT;
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1; while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
if (count == 0) { if (count == 0) return -1;
printf("i2c_start timed out waiting to send addr to 0x%X\n", addr);
return 1;
}
// check if the device has acknowledged the READ / WRITE mode // check if the device has acknowledged the READ / WRITE mode
uint8_t twst = TW_STATUS & 0xF8; uint8_t twst = TW_STATUS & 0xF8;
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) { if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) return -1;
printf("i2c_start failed to receive ack from 0x%X\n", addr);
return 1;
}
return 0; return 0;
} }
@ -55,39 +43,42 @@ void i2c_stop(void) {
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
} }
uint8_t i2c_write(uint8_t data) { int i2c_write(uint8_t * data, int length) {
uint32_t count; int i;
for (i = 0; i < length; i++) {
// load data into data register // load data into data register
TWDR = data; TWDR = data[i];
// start transmission of data // start transmission of data
TWCR = (1<<TWINT) | (1<<TWEN); TWCR = (1<<TWINT) | (1<<TWEN);
// wait for end of transmission // wait for end of transmission
count = TIMEOUT; uint32_t count = TIMEOUT;
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1; while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
if (count == 0) { // timed out
printf("i2c_write timed out waiting to send 0x%X\n", data); if (count == 0) return -1;
return 1; // failed to receive ack
if((TWSR & 0xF8) != TW_MT_DATA_ACK) return -1;
} }
if( (TWSR & 0xF8) != TW_MT_DATA_ACK ){ return i;
printf("i2c_write failed to receive ack for 0x%X\n", data); }
return 1;
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<<TWINT) | (1<<TWEN) | (1<<TWEA);
} else {
// start receiving without acknowledging reception
TWCR = (1<<TWINT) | (1<<TWEN);
}
// wait for end of transmission
uint32_t count = TIMEOUT;
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
if (count == 0) return -1;
// return received data from TWDR
data[i] = TWDR;
} }
return 0; return i;
}
uint8_t i2c_read(bool ack) {
if (ack) {
// start TWI module and acknowledge data after reception
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
} else {
// start receiving without acknowledging reception
TWCR = (1<<TWINT) | (1<<TWEN);
}
// wait for end of transmission
while( !(TWCR & (1<<TWINT)) );
// return received data from TWDR
return TWDR;
} }

View File

@ -1,6 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <board/i2c.h> #include <common/i2c.h>
uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
return i2c_get(address, command, (uint8_t *)data, 2); return i2c_get(address, command, (uint8_t *)data, 2);