A helper class for I2C

Dependents:   MPU9150

Files at this revision

API Documentation at this revision

Comitter:
ethanharstad
Date:
Mon Jun 09 21:17:12 2014 +0000
Parent:
1:e1b13a0c1987
Commit message:
Fix writeBytes

Changed in this revision

I2CHelper.cpp Show annotated file Show diff for this revision Revisions of this file
I2CHelper.h Show annotated file Show diff for this revision Revisions of this file
diff -r e1b13a0c1987 -r 51de41e0e0c9 I2CHelper.cpp
--- a/I2CHelper.cpp	Sun Jun 08 00:05:20 2014 +0000
+++ b/I2CHelper.cpp	Mon Jun 09 21:17:12 2014 +0000
@@ -5,6 +5,10 @@
 
 I2CHelper::I2CHelper(PinName sda, PinName scl) : i2c_(sda, scl) {}
 
+void I2CHelper::setFrequency(int hz) {
+    i2c_.frequency(hz);
+}
+
 
 /* Read Functions */
 bool I2CHelper::readBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, uint8_t *data) {
@@ -17,8 +21,8 @@
 
 bool I2CHelper::readBits(const uint8_t devAddr, const uint8_t regAddr, const uint8_t startBit, const uint8_t length, uint8_t *data) {
     uint8_t buf;
-    i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
-    i2c_.read((int)(devAddr << 1), (char *)&buf, 1);
+    int status = i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
+    status = i2c_.read((int)(devAddr << 1), (char *)&buf, 1);
     buf >>= (startBit - length + 1); 
     buf &= ((1 << length) - 1);
     *data = buf;
@@ -34,6 +38,15 @@
     return i2c_.read((int)(devAddr << 1), (char *)data, (int)length);
 }
 
+bool I2CHelper::readWord(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data) {
+    return readWords(devAddr, regAddr, data, 1);
+}
+
+bool I2CHelper::readWords(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data, const uint8_t length) {
+    i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
+    return i2c_.read((int)(devAddr << 1), (char *)data, length * 2);
+}
+
 /* Write Functions */
 bool I2CHelper::writeBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, const uint8_t data) {
     uint8_t buf;
@@ -62,12 +75,19 @@
 }
 
 bool I2CHelper::writeBytes(const uint8_t devAddr, const uint8_t regAddr, const uint8_t *data, const uint8_t length) {
-    i2c_.start();
+    /*i2c_.start();
     i2c_.write((int)(devAddr << 1));
     i2c_.write((int)regAddr);
     for(int i = 0; i < length; i++) {
         i2c_.write(data[i]);
     }
-    i2c_.stop();
+    i2c_.stop();*/
+    char *buf = (char *)malloc(length + 1);
+    buf[0] = regAddr;
+    for(int i = 0; i < length; i++) {
+        buf[i + 1] = data[i];
+    }
+    i2c_.write((int)(devAddr << 1), buf, length + 1);
+    free(buf);
     return true;
 }
\ No newline at end of file
diff -r e1b13a0c1987 -r 51de41e0e0c9 I2CHelper.h
--- a/I2CHelper.h	Sun Jun 08 00:05:20 2014 +0000
+++ b/I2CHelper.h	Mon Jun 09 21:17:12 2014 +0000
@@ -8,10 +8,14 @@
     I2CHelper();
     I2CHelper(PinName sda, PinName scl);
     
+    void setFrequency(int hz);
+    
     bool readBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, uint8_t *data);
     bool readBits(const uint8_t devAddr, const uint8_t regAddr, const uint8_t startBit, const uint8_t length, uint8_t *data);
     bool readByte(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data);
     bool readBytes(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data, const uint8_t length);
+    bool readWord(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data);
+    bool readWords(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data, const uint8_t length);
     
     bool writeBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, const uint8_t data);
     bool writeBits(const uint8_t devAddr, const uint8_t regAddr, const uint8_t startBit, const uint8_t length, const uint8_t data);