From caf13833252ff0fa869d225a118779b3504ab51e Mon Sep 17 00:00:00 2001 From: Gabriel Smith Date: Tue, 3 Mar 2015 16:26:19 +0000 Subject: [PATCH] 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 Signed-off-by: Brendan Le Foll --- api/mraa/i2c.h | 11 +++++++++++ api/mraa/i2c.hpp | 13 +++++++++++++ src/i2c/i2c.c | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/api/mraa/i2c.h b/api/mraa/i2c.h index 9ba9779..11ae1c3 100644 --- a/api/mraa/i2c.h +++ b/api/mraa/i2c.h @@ -116,6 +116,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 * diff --git a/api/mraa/i2c.hpp b/api/mraa/i2c.hpp index a87902a..28db518 100644 --- a/api/mraa/i2c.hpp +++ b/api/mraa/i2c.hpp @@ -133,6 +133,19 @@ class I2c { } /** + * 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 * * @param data The byte to send on the bus diff --git a/src/i2c/i2c.c b/src/i2c/i2c.c index 5d87eac..48590f9 100644 --- a/src/i2c/i2c.c +++ b/src/i2c/i2c.c @@ -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) { -- 2.7.4