Update avr i2c to match new common i2c library
This commit is contained in:
@ -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<<TWINT)) && count > 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<<TWINT)) && count > 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<<TWINT) | (1<<TWEN) | (1<<TWSTO);
|
||||
}
|
||||
|
||||
uint8_t i2c_write(uint8_t data) {
|
||||
uint32_t count;
|
||||
|
||||
// load data into data register
|
||||
TWDR = data;
|
||||
// start transmission of data
|
||||
TWCR = (1<<TWINT) | (1<<TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & (1<<TWINT)) && count > 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<<TWINT) | (1<<TWEN);
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & (1<<TWINT)) && count > 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<<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;
|
||||
}
|
||||
|
||||
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;
|
||||
return i;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <board/i2c.h>
|
||||
#include <common/i2c.h>
|
||||
|
||||
uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
|
||||
return i2c_get(address, command, (uint8_t *)data, 2);
|
||||
|
Reference in New Issue
Block a user