A helper class for I2C
I2CHelper.cpp@1:e1b13a0c1987, 2014-06-08 (annotated)
- Committer:
- ethanharstad
- Date:
- Sun Jun 08 00:05:20 2014 +0000
- Revision:
- 1:e1b13a0c1987
- Parent:
- 0:e0f604f504c4
- Child:
- 2:51de41e0e0c9
Untested implementation
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:e0f604f504c4 | 8 | |
ethanharstad | 0:e0f604f504c4 | 9 | /* Read Functions */ |
ethanharstad | 0:e0f604f504c4 | 10 | bool I2CHelper::readBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, uint8_t *data) { |
ethanharstad | 1:e1b13a0c1987 | 11 | uint8_t buf; |
ethanharstad | 1:e1b13a0c1987 | 12 | i2c_.write((int)(devAddr << 1), (char *)®Addr, 1, true); |
ethanharstad | 1:e1b13a0c1987 | 13 | i2c_.read((int)(devAddr << 1), (char *)&buf, 1); |
ethanharstad | 1:e1b13a0c1987 | 14 | *data = buf & (1 << bit); |
ethanharstad | 1:e1b13a0c1987 | 15 | return true; |
ethanharstad | 0:e0f604f504c4 | 16 | } |
ethanharstad | 0:e0f604f504c4 | 17 | |
ethanharstad | 0:e0f604f504c4 | 18 | 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 | 19 | uint8_t buf; |
ethanharstad | 1:e1b13a0c1987 | 20 | i2c_.write((int)(devAddr << 1), (char *)®Addr, 1, true); |
ethanharstad | 1:e1b13a0c1987 | 21 | i2c_.read((int)(devAddr << 1), (char *)&buf, 1); |
ethanharstad | 1:e1b13a0c1987 | 22 | buf >>= (startBit - length + 1); |
ethanharstad | 1:e1b13a0c1987 | 23 | buf &= ((1 << length) - 1); |
ethanharstad | 1:e1b13a0c1987 | 24 | *data = buf; |
ethanharstad | 1:e1b13a0c1987 | 25 | return true; |
ethanharstad | 0:e0f604f504c4 | 26 | } |
ethanharstad | 0:e0f604f504c4 | 27 | |
ethanharstad | 0:e0f604f504c4 | 28 | bool I2CHelper::readByte(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data) { |
ethanharstad | 0:e0f604f504c4 | 29 | return readBytes(devAddr, regAddr, data, 1); |
ethanharstad | 0:e0f604f504c4 | 30 | } |
ethanharstad | 0:e0f604f504c4 | 31 | |
ethanharstad | 0:e0f604f504c4 | 32 | bool I2CHelper::readBytes(const uint8_t devAddr, const uint8_t regAddr, uint8_t *data, const uint8_t length) { |
ethanharstad | 1:e1b13a0c1987 | 33 | i2c_.write((int)(devAddr << 1), (char *)®Addr, 1, true); |
ethanharstad | 1:e1b13a0c1987 | 34 | return i2c_.read((int)(devAddr << 1), (char *)data, (int)length); |
ethanharstad | 0:e0f604f504c4 | 35 | } |
ethanharstad | 0:e0f604f504c4 | 36 | |
ethanharstad | 0:e0f604f504c4 | 37 | /* Write Functions */ |
ethanharstad | 0:e0f604f504c4 | 38 | bool I2CHelper::writeBit(const uint8_t devAddr, const uint8_t regAddr, const uint8_t bit, const uint8_t data) { |
ethanharstad | 1:e1b13a0c1987 | 39 | uint8_t buf; |
ethanharstad | 1:e1b13a0c1987 | 40 | i2c_.write((int)(devAddr << 1), (char *)®Addr, 1, true); |
ethanharstad | 1:e1b13a0c1987 | 41 | i2c_.read((int)(devAddr << 1), (char *)&buf, 1, true); |
ethanharstad | 1:e1b13a0c1987 | 42 | buf = (data != 0) ? (buf | (1 << bit)) : (buf & ~(1 << bit)); |
ethanharstad | 1:e1b13a0c1987 | 43 | i2c_.write((int)(devAddr << 1), (char *)&buf, 1); |
ethanharstad | 1:e1b13a0c1987 | 44 | return true; |
ethanharstad | 0:e0f604f504c4 | 45 | } |
ethanharstad | 0:e0f604f504c4 | 46 | |
ethanharstad | 0:e0f604f504c4 | 47 | 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 | 48 | uint8_t buf; |
ethanharstad | 1:e1b13a0c1987 | 49 | i2c_.write((int)(devAddr << 1), (char *)®Addr, 1, true); |
ethanharstad | 1:e1b13a0c1987 | 50 | i2c_.read((int)(devAddr << 1), (char *)&buf, 1, true); |
ethanharstad | 1:e1b13a0c1987 | 51 | uint8_t dat = data << (startBit - length + 1); |
ethanharstad | 1:e1b13a0c1987 | 52 | uint8_t mask = ((1 << length) - 1) << (startBit - length + 1); |
ethanharstad | 1:e1b13a0c1987 | 53 | dat &= mask; |
ethanharstad | 1:e1b13a0c1987 | 54 | buf &= ~mask; |
ethanharstad | 1:e1b13a0c1987 | 55 | buf |= dat; |
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::writeByte(const uint8_t devAddr, const uint8_t regAddr, const uint8_t data) { |
ethanharstad | 0:e0f604f504c4 | 61 | return writeBytes(devAddr, regAddr, &data, 1); |
ethanharstad | 0:e0f604f504c4 | 62 | } |
ethanharstad | 0:e0f604f504c4 | 63 | |
ethanharstad | 0:e0f604f504c4 | 64 | bool I2CHelper::writeBytes(const uint8_t devAddr, const uint8_t regAddr, const uint8_t *data, const uint8_t length) { |
ethanharstad | 1:e1b13a0c1987 | 65 | i2c_.start(); |
ethanharstad | 1:e1b13a0c1987 | 66 | i2c_.write((int)(devAddr << 1)); |
ethanharstad | 1:e1b13a0c1987 | 67 | i2c_.write((int)regAddr); |
ethanharstad | 1:e1b13a0c1987 | 68 | for(int i = 0; i < length; i++) { |
ethanharstad | 1:e1b13a0c1987 | 69 | i2c_.write(data[i]); |
ethanharstad | 1:e1b13a0c1987 | 70 | } |
ethanharstad | 1:e1b13a0c1987 | 71 | i2c_.stop(); |
ethanharstad | 1:e1b13a0c1987 | 72 | return true; |
ethanharstad | 0:e0f604f504c4 | 73 | } |