Skip to content

Commit

Permalink
Add i2c_read_register function.
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpulvis committed Oct 11, 2015
1 parent 633ed7b commit ebad4e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/avrm/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ void i2c_write(byte data){
return;
}

int i2c_read_register(byte addr, byte rgstr, byte *buf, size_t len)
{
i2c_start(addr, I2C_WRITE);
i2c_write(rgstr);
i2c_start(addr, I2C_READ);
for (int i = 0; i < len; i++)
buf[i] = i2c_read_ack();

// TODO: Errors?
return 0;
}

byte i2c_read_ack(void){
// start TWI module and acknowledge data after reception
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
Expand Down
3 changes: 3 additions & 0 deletions lib/avrm/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ byte i2c_start(byte address, byte config);
// i2c_write
void i2c_write(byte data);

// i2c_read_address
int i2c_read_register(byte address, byte register, byte *buffer, size_t length);

// i2c_read_ack
byte i2c_read_ack(void);

Expand Down

0 comments on commit ebad4e1

Please sign in to comment.