i2c: Add functions for bulk read from register
authorGabriel Smith <ga29smith@gmail.com>
Tue, 3 Mar 2015 16:26:19 +0000 (16:26 +0000)
committerBrendan Le Foll <brendan.le.foll@intel.com>
Tue, 3 Mar 2015 16:26:57 +0000 (16:26 +0000)
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>
api/mraa/i2c.h
api/mraa/i2c.hpp
src/i2c/i2c.c

index 9ba9779..11ae1c3 100644 (file)
@@ -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
  *
index a87902a..28db518 100644 (file)
@@ -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
index 5d87eac..48590f9 100644 (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)
 {