Private
Public Access
2
0

i2c: Add functions for bulk read from register

Functions issue a write command for the register to read from and then a read
command without a stop signal in between

Signed-off-by: Gabriel Smith <ga29smith@gmail.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Gabriel Smith
2015-03-03 16:26:19 +00:00
committed by Brendan Le Foll
parent f854a2b410
commit caf1383325
3 changed files with 45 additions and 0 deletions

View File

@@ -115,6 +115,17 @@ uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command);
*/
uint16_t mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command);
/**
* Bulk read from i2c context, starting from designated register
*
* @param dev The i2c context
* @param command The register
* @param data pointer to the byte array to read data in to
* @param length max number of bytes to read
* @return The length in bytes passed to the function or 0
*/
int mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length);
/**
* Write length bytes to the bus, the first byte in the array is the
* command/register to write

View File

@@ -132,6 +132,19 @@ class I2c {
return mraa_i2c_read_word_data(m_i2c, reg);
}
/**
* Read length bytes from the bus into *data pointer starting from
* an i2c register
*
* @param reg Register to read from
* @param data pointer to the byte array to read data in to
* @param length max number of bytes to read
* @return length passed to the function or 0
*/
int readBytesReg(uint8_t reg, uint8_t* data, int length) {
return mraa_i2c_read_bytes_data(m_i2c, reg, data, length);
}
/**
* Write a byte on the bus
*

View File

@@ -200,6 +200,27 @@ mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
return 0xFFFF & d.word;
}
int
mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
{
struct i2c_rdwr_ioctl_data d;
struct i2c_msg m[2];
m[0].addr = dev->addr;
m[0].flags = I2C_M_RD;
m[0].len = 1;
m[0].buf = &command;
m[1].addr = dev->addr;
m[1].flags = 0x00;
m[1].len = length;
m[1].buf = data;
d.msgs = m;
d.nmsgs = 2;
return ioctl(dev->fh, I2C_RDWR, &d) < 0 ? -1 : length;
}
mraa_result_t
mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
{