A helper class for I2C

Dependents:   MPU9150

Committer:
ethanharstad
Date:
Mon Jun 09 21:17:12 2014 +0000
Revision:
2:51de41e0e0c9
Parent:
1:e1b13a0c1987
Fix writeBytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ethanharstad 0:e0f604f504c4 1 #include "I2CHelper.h"
ethanharstad 0:e0f604f504c4 2
ethanharstad 0:e0f604f504c4 3 /* Constructors */
ethanharstad 0:e0f604f504c4 4 I2CHelper::I2CHelper() : i2c_(I2C_SDA, I2C_SCL) {}
ethanharstad 0:e0f604f504c4 5
ethanharstad 0:e0f604f504c4 6 I2CHelper::I2CHelper(PinName sda, PinName scl) : i2c_(sda, scl) {}
ethanharstad 0:e0f604f504c4 7
ethanharstad 2:51de41e0e0c9 8 void I2CHelper::setFrequency(int hz) {
ethanharstad 2:51de41e0e0c9 9 i2c_.frequency(hz);
ethanharstad 2:51de41e0e0c9 10 }
ethanharstad 2:51de41e0e0c9 11
ethanharstad 0:e0f604f504c4 12
ethanharstad 0:e0f604f504c4 13 /* Read Functions */
ethanharstad 0:e0f604f504c4 14 bool I2CHelper::readBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, uint8_t *data) {
ethanharstad 1:e1b13a0c1987 15 uint8_t buf;
ethanharstad 1:e1b13a0c1987 16 i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
ethanharstad 1:e1b13a0c1987 17 i2c_.read((int)(devAddr << 1), (char *)&buf, 1);
ethanharstad 1:e1b13a0c1987 18 *data = buf & (1 << bit);
ethanharstad 1:e1b13a0c1987 19 return true;
ethanharstad 0:e0f604f504c4 20 }
ethanharstad 0:e0f604f504c4 21
ethanharstad 0:e0f604f504c4 22 bool I2CHelper::readBits(const uint8_t devAddr, const uint8_t regAddr, const uint8_t startBit, const uint8_t length, uint8_t *data) {
ethanharstad 1:e1b13a0c1987 23 uint8_t buf;
ethanharstad 2:51de41e0e0c9 24 int status = i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
ethanharstad 2:51de41e0e0c9 25 status = i2c_.read((int)(devAddr << 1), (char *)&buf, 1);
ethanharstad 1:e1b13a0c1987 26 buf >>= (startBit - length + 1);
ethanharstad 1:e1b13a0c1987 27 buf &= ((1 << length) - 1);
ethanharstad 1:e1b13a0c1987 28 *data = buf;
ethanharstad 1:e1b13a0c1987 29 return true;
ethanharstad 0:e0f604f504c4 30 }
ethanharstad 0:e0f604f504c4 31
ethanharstad 0:e0f604f504c4 32 bool I2CHelper::readByte(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data) {
ethanharstad 0:e0f604f504c4 33 return readBytes(devAddr, regAddr, data, 1);
ethanharstad 0:e0f604f504c4 34 }
ethanharstad 0:e0f604f504c4 35
ethanharstad 0:e0f604f504c4 36 bool I2CHelper::readBytes(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data, const uint8_t length) {
ethanharstad 1:e1b13a0c1987 37 i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
ethanharstad 1:e1b13a0c1987 38 return i2c_.read((int)(devAddr << 1), (char *)data, (int)length);
ethanharstad 0:e0f604f504c4 39 }
ethanharstad 0:e0f604f504c4 40
ethanharstad 2:51de41e0e0c9 41 bool I2CHelper::readWord(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data) {
ethanharstad 2:51de41e0e0c9 42 return readWords(devAddr, regAddr, data, 1);
ethanharstad 2:51de41e0e0c9 43 }
ethanharstad 2:51de41e0e0c9 44
ethanharstad 2:51de41e0e0c9 45 bool I2CHelper::readWords(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data, const uint8_t length) {
ethanharstad 2:51de41e0e0c9 46 i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
ethanharstad 2:51de41e0e0c9 47 return i2c_.read((int)(devAddr << 1), (char *)data, length * 2);
ethanharstad 2:51de41e0e0c9 48 }
ethanharstad 2:51de41e0e0c9 49
ethanharstad 0:e0f604f504c4 50 /* Write Functions */
ethanharstad 0:e0f604f504c4 51 bool I2CHelper::writeBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, const uint8_t data) {
ethanharstad 1:e1b13a0c1987 52 uint8_t buf;
ethanharstad 1:e1b13a0c1987 53 i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
ethanharstad 1:e1b13a0c1987 54 i2c_.read((int)(devAddr << 1), (char *)&buf, 1, true);
ethanharstad 1:e1b13a0c1987 55 buf = (data != 0) ? (buf | (1 << bit)) : (buf & ~(1 << bit));
ethanharstad 1:e1b13a0c1987 56 i2c_.write((int)(devAddr << 1), (char *)&buf, 1);
ethanharstad 1:e1b13a0c1987 57 return true;
ethanharstad 0:e0f604f504c4 58 }
ethanharstad 0:e0f604f504c4 59
ethanharstad 0:e0f604f504c4 60 bool I2CHelper::writeBits(const uint8_t devAddr, const uint8_t regAddr, const uint8_t startBit, const uint8_t length, const uint8_t data) {
ethanharstad 1:e1b13a0c1987 61 uint8_t buf;
ethanharstad 1:e1b13a0c1987 62 i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
ethanharstad 1:e1b13a0c1987 63 i2c_.read((int)(devAddr << 1), (char *)&buf, 1, true);
ethanharstad 1:e1b13a0c1987 64 uint8_t dat = data << (startBit - length + 1);
ethanharstad 1:e1b13a0c1987 65 uint8_t mask = ((1 << length) - 1) << (startBit - length + 1);
ethanharstad 1:e1b13a0c1987 66 dat &= mask;
ethanharstad 1:e1b13a0c1987 67 buf &= ~mask;
ethanharstad 1:e1b13a0c1987 68 buf |= dat;
ethanharstad 1:e1b13a0c1987 69 i2c_.write((int)(devAddr << 1), (char *)&buf, 1);
ethanharstad 1:e1b13a0c1987 70 return true;
ethanharstad 0:e0f604f504c4 71 }
ethanharstad 0:e0f604f504c4 72
ethanharstad 0:e0f604f504c4 73 bool I2CHelper::writeByte(const uint8_t devAddr, const uint8_t regAddr, const uint8_t data) {
ethanharstad 0:e0f604f504c4 74 return writeBytes(devAddr, regAddr, &data, 1);
ethanharstad 0:e0f604f504c4 75 }
ethanharstad 0:e0f604f504c4 76
ethanharstad 0:e0f604f504c4 77 bool I2CHelper::writeBytes(const uint8_t devAddr, const uint8_t regAddr, const uint8_t *data, const uint8_t length) {
ethanharstad 2:51de41e0e0c9 78 /*i2c_.start();
ethanharstad 1:e1b13a0c1987 79 i2c_.write((int)(devAddr << 1));
ethanharstad 1:e1b13a0c1987 80 i2c_.write((int)regAddr);
ethanharstad 1:e1b13a0c1987 81 for(int i = 0; i < length; i++) {
ethanharstad 1:e1b13a0c1987 82 i2c_.write(data[i]);
ethanharstad 1:e1b13a0c1987 83 }
ethanharstad 2:51de41e0e0c9 84 i2c_.stop();*/
ethanharstad 2:51de41e0e0c9 85 char *buf = (char *)malloc(length + 1);
ethanharstad 2:51de41e0e0c9 86 buf[0] = regAddr;
ethanharstad 2:51de41e0e0c9 87 for(int i = 0; i < length; i++) {
ethanharstad 2:51de41e0e0c9 88 buf[i + 1] = data[i];
ethanharstad 2:51de41e0e0c9 89 }
ethanharstad 2:51de41e0e0c9 90 i2c_.write((int)(devAddr << 1), buf, length + 1);
ethanharstad 2:51de41e0e0c9 91 free(buf);
ethanharstad 1:e1b13a0c1987 92 return true;
ethanharstad 0:e0f604f504c4 93 }