A helper class for I2C

Dependents:   MPU9150

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CHelper.cpp Source File

I2CHelper.cpp

00001 #include "I2CHelper.h"
00002 
00003 /* Constructors */
00004 I2CHelper::I2CHelper() : i2c_(I2C_SDA, I2C_SCL) {}
00005 
00006 I2CHelper::I2CHelper(PinName sda, PinName scl) : i2c_(sda, scl) {}
00007 
00008 void I2CHelper::setFrequency(int hz) {
00009     i2c_.frequency(hz);
00010 }
00011 
00012 
00013 /* Read Functions */
00014 bool I2CHelper::readBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, uint8_t *data) {
00015     uint8_t buf;
00016     i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
00017     i2c_.read((int)(devAddr << 1), (char *)&buf, 1);
00018     *data = buf & (1 << bit);
00019     return true;
00020 }
00021 
00022 bool I2CHelper::readBits(const uint8_t devAddr, const uint8_t regAddr, const uint8_t startBit, const uint8_t length, uint8_t *data) {
00023     uint8_t buf;
00024     int status = i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
00025     status = i2c_.read((int)(devAddr << 1), (char *)&buf, 1);
00026     buf >>= (startBit - length + 1); 
00027     buf &= ((1 << length) - 1);
00028     *data = buf;
00029     return true;
00030 }
00031 
00032 bool I2CHelper::readByte(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data) {
00033     return readBytes(devAddr, regAddr, data, 1);
00034 }
00035 
00036 bool I2CHelper::readBytes(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data, const uint8_t length) {
00037     i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
00038     return i2c_.read((int)(devAddr << 1), (char *)data, (int)length);
00039 }
00040 
00041 bool I2CHelper::readWord(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data) {
00042     return readWords(devAddr, regAddr, data, 1);
00043 }
00044 
00045 bool I2CHelper::readWords(const uint8_t devAddr, const uint8_t regAddr, uint16_t *data, const uint8_t length) {
00046     i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
00047     return i2c_.read((int)(devAddr << 1), (char *)data, length * 2);
00048 }
00049 
00050 /* Write Functions */
00051 bool I2CHelper::writeBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, const uint8_t data) {
00052     uint8_t buf;
00053     i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
00054     i2c_.read((int)(devAddr << 1), (char *)&buf, 1, true);
00055     buf = (data != 0) ? (buf | (1 << bit)) : (buf & ~(1 << bit));
00056     i2c_.write((int)(devAddr << 1), (char *)&buf, 1);
00057     return true;
00058 }
00059 
00060 bool I2CHelper::writeBits(const uint8_t devAddr, const uint8_t regAddr, const uint8_t startBit, const uint8_t length, const uint8_t data) {
00061     uint8_t buf;
00062     i2c_.write((int)(devAddr << 1), (char *)&regAddr, 1, true);
00063     i2c_.read((int)(devAddr << 1), (char *)&buf, 1, true);
00064     uint8_t dat = data << (startBit - length + 1);
00065     uint8_t mask = ((1 << length) - 1) << (startBit - length + 1);
00066     dat &= mask;
00067     buf &= ~mask;
00068     buf |= dat;
00069     i2c_.write((int)(devAddr << 1), (char *)&buf, 1);
00070     return true;
00071 }
00072 
00073 bool I2CHelper::writeByte(const uint8_t devAddr, const uint8_t regAddr, const uint8_t data) {
00074     return writeBytes(devAddr, regAddr, &data, 1);
00075 }
00076 
00077 bool I2CHelper::writeBytes(const uint8_t devAddr, const uint8_t regAddr, const uint8_t *data, const uint8_t length) {
00078     /*i2c_.start();
00079     i2c_.write((int)(devAddr << 1));
00080     i2c_.write((int)regAddr);
00081     for(int i = 0; i < length; i++) {
00082         i2c_.write(data[i]);
00083     }
00084     i2c_.stop();*/
00085     char *buf = (char *)malloc(length + 1);
00086     buf[0] = regAddr;
00087     for(int i = 0; i < length; i++) {
00088         buf[i + 1] = data[i];
00089     }
00090     i2c_.write((int)(devAddr << 1), buf, length + 1);
00091     free(buf);
00092     return true;
00093 }