jm6wud

Committer:
jm6wud
Date:
Tue Oct 13 15:06:29 2020 +0000
Revision:
2:0f8c991d7d50
Parent:
1:8a94254a671b
jm6wud

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jm6wud 1:8a94254a671b 1 // ported from arduino library: https://github.com/jrowberg/i2cdevlib
jm6wud 1:8a94254a671b 2 // written by szymon gaertig (email: szymon@gaertig.com.pl, website: szymongaertig.pl)
jm6wud 1:8a94254a671b 3 // Changelog:
jm6wud 1:8a94254a671b 4 // 2013-01-08 - first release
jm6wud 1:8a94254a671b 5
jm6wud 1:8a94254a671b 6 #include "I2Cdev.h"
jm6wud 1:8a94254a671b 7
jm6wud 1:8a94254a671b 8 #define useDebugSerial
jm6wud 1:8a94254a671b 9
jm6wud 1:8a94254a671b 10 I2Cdev::I2Cdev(): debugSerial(USBTX, USBRX), i2c(I2C_SDA,I2C_SCL)
jm6wud 1:8a94254a671b 11 {
jm6wud 1:8a94254a671b 12
jm6wud 1:8a94254a671b 13 }
jm6wud 1:8a94254a671b 14
jm6wud 1:8a94254a671b 15 I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): debugSerial(USBTX, USBRX), i2c(i2cSda,i2cScl)
jm6wud 1:8a94254a671b 16 {
jm6wud 1:8a94254a671b 17
jm6wud 1:8a94254a671b 18 }
jm6wud 1:8a94254a671b 19
jm6wud 1:8a94254a671b 20 /** Read a single bit from an 8-bit device register.
jm6wud 1:8a94254a671b 21 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 22 * @param regAddr Register regAddr to read from
jm6wud 1:8a94254a671b 23 * @param bitNum Bit position to read (0-7)
jm6wud 1:8a94254a671b 24 * @param data Container for single bit value
jm6wud 1:8a94254a671b 25 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jm6wud 1:8a94254a671b 26 * @return Status of read operation (true = success)
jm6wud 1:8a94254a671b 27 */
jm6wud 1:8a94254a671b 28 int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) {
jm6wud 1:8a94254a671b 29 uint8_t b;
jm6wud 1:8a94254a671b 30 uint8_t count = readByte(devAddr, regAddr, &b, timeout);
jm6wud 1:8a94254a671b 31 *data = b & (1 << bitNum);
jm6wud 1:8a94254a671b 32 return count;
jm6wud 1:8a94254a671b 33 }
jm6wud 1:8a94254a671b 34
jm6wud 1:8a94254a671b 35 /** Read a single bit from a 16-bit device register.
jm6wud 1:8a94254a671b 36 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 37 * @param regAddr Register regAddr to read from
jm6wud 1:8a94254a671b 38 * @param bitNum Bit position to read (0-15)
jm6wud 1:8a94254a671b 39 * @param data Container for single bit value
jm6wud 1:8a94254a671b 40 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jm6wud 1:8a94254a671b 41 * @return Status of read operation (true = success)
jm6wud 1:8a94254a671b 42 */
jm6wud 1:8a94254a671b 43 int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) {
jm6wud 1:8a94254a671b 44 uint16_t b;
jm6wud 1:8a94254a671b 45 uint8_t count = readWord(devAddr, regAddr, &b, timeout);
jm6wud 1:8a94254a671b 46 *data = b & (1 << bitNum);
jm6wud 1:8a94254a671b 47 return count;
jm6wud 1:8a94254a671b 48 }
jm6wud 1:8a94254a671b 49
jm6wud 1:8a94254a671b 50 /** Read multiple bits from an 8-bit device register.
jm6wud 1:8a94254a671b 51 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 52 * @param regAddr Register regAddr to read from
jm6wud 1:8a94254a671b 53 * @param bitStart First bit position to read (0-7)
jm6wud 1:8a94254a671b 54 * @param length Number of bits to read (not more than 8)
jm6wud 1:8a94254a671b 55 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
jm6wud 1:8a94254a671b 56 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jm6wud 1:8a94254a671b 57 * @return Status of read operation (true = success)
jm6wud 1:8a94254a671b 58 */
jm6wud 1:8a94254a671b 59 int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) {
jm6wud 1:8a94254a671b 60 // 01101001 read byte
jm6wud 1:8a94254a671b 61 // 76543210 bit numbers
jm6wud 1:8a94254a671b 62 // xxx args: bitStart=4, length=3
jm6wud 1:8a94254a671b 63 // 010 masked
jm6wud 1:8a94254a671b 64 // -> 010 shifted
jm6wud 1:8a94254a671b 65 uint8_t count, b;
jm6wud 1:8a94254a671b 66 if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) {
jm6wud 1:8a94254a671b 67 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jm6wud 1:8a94254a671b 68 b &= mask;
jm6wud 1:8a94254a671b 69 b >>= (bitStart - length + 1);
jm6wud 1:8a94254a671b 70 *data = b;
jm6wud 1:8a94254a671b 71 }
jm6wud 1:8a94254a671b 72 return count;
jm6wud 1:8a94254a671b 73 }
jm6wud 1:8a94254a671b 74
jm6wud 1:8a94254a671b 75 /** Read multiple bits from a 16-bit device register.
jm6wud 1:8a94254a671b 76 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 77 * @param regAddr Register regAddr to read from
jm6wud 1:8a94254a671b 78 * @param bitStart First bit position to read (0-15)
jm6wud 1:8a94254a671b 79 * @param length Number of bits to read (not more than 16)
jm6wud 1:8a94254a671b 80 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
jm6wud 1:8a94254a671b 81 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jm6wud 1:8a94254a671b 82 * @return Status of read operation (1 = success, 0 = failure, -1 = timeout)
jm6wud 1:8a94254a671b 83 */
jm6wud 1:8a94254a671b 84 int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) {
jm6wud 1:8a94254a671b 85 // 1101011001101001 read byte
jm6wud 1:8a94254a671b 86 // fedcba9876543210 bit numbers
jm6wud 1:8a94254a671b 87 // xxx args: bitStart=12, length=3
jm6wud 1:8a94254a671b 88 // 010 masked
jm6wud 1:8a94254a671b 89 // -> 010 shifted
jm6wud 1:8a94254a671b 90 uint8_t count;
jm6wud 1:8a94254a671b 91 uint16_t w;
jm6wud 1:8a94254a671b 92 if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) {
jm6wud 1:8a94254a671b 93 uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jm6wud 1:8a94254a671b 94 w &= mask;
jm6wud 1:8a94254a671b 95 w >>= (bitStart - length + 1);
jm6wud 1:8a94254a671b 96 *data = w;
jm6wud 1:8a94254a671b 97 }
jm6wud 1:8a94254a671b 98 return count;
jm6wud 1:8a94254a671b 99 }
jm6wud 1:8a94254a671b 100 /** Read single byte from an 8-bit device register.
jm6wud 1:8a94254a671b 101 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 102 * @param regAddr Register regAddr to read from
jm6wud 1:8a94254a671b 103 * @param data Container for byte value read from device
jm6wud 1:8a94254a671b 104 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jm6wud 1:8a94254a671b 105 * @return Status of read operation (true = success)
jm6wud 1:8a94254a671b 106 */
jm6wud 1:8a94254a671b 107 int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) {
jm6wud 1:8a94254a671b 108 return readBytes(devAddr, regAddr, 1, data, timeout);
jm6wud 1:8a94254a671b 109 }
jm6wud 1:8a94254a671b 110
jm6wud 1:8a94254a671b 111 /** Read single word from a 16-bit device register.
jm6wud 1:8a94254a671b 112 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 113 * @param regAddr Register regAddr to read from
jm6wud 1:8a94254a671b 114 * @param data Container for word value read from device
jm6wud 1:8a94254a671b 115 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jm6wud 1:8a94254a671b 116 * @return Status of read operation (true = success)
jm6wud 1:8a94254a671b 117 */
jm6wud 1:8a94254a671b 118 int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) {
jm6wud 1:8a94254a671b 119 return readWords(devAddr, regAddr, 1, data, timeout);
jm6wud 1:8a94254a671b 120 }
jm6wud 1:8a94254a671b 121
jm6wud 1:8a94254a671b 122 /** Read multiple bytes from an 8-bit device register.
jm6wud 1:8a94254a671b 123 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 124 * @param regAddr First register regAddr to read from
jm6wud 1:8a94254a671b 125 * @param length Number of bytes to read
jm6wud 1:8a94254a671b 126 * @param data Buffer to store read data in
jm6wud 1:8a94254a671b 127 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jm6wud 1:8a94254a671b 128 * @return Number of bytes read (-1 indicates failure)
jm6wud 1:8a94254a671b 129 */
jm6wud 1:8a94254a671b 130 int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout)
jm6wud 1:8a94254a671b 131 {
jm6wud 1:8a94254a671b 132 char command[1];
jm6wud 1:8a94254a671b 133 command[0] = regAddr;
jm6wud 1:8a94254a671b 134 char *redData = (char*)malloc(length);
jm6wud 1:8a94254a671b 135 i2c.write(devAddr<<1, command, 1, true);
jm6wud 1:8a94254a671b 136 i2c.read(devAddr<<1, redData, length);
jm6wud 1:8a94254a671b 137 for(int i =0; i < length; i++) {
jm6wud 1:8a94254a671b 138 data[i] = redData[i];
jm6wud 1:8a94254a671b 139 }
jm6wud 1:8a94254a671b 140 free (redData);
jm6wud 1:8a94254a671b 141 return length;
jm6wud 1:8a94254a671b 142 }
jm6wud 1:8a94254a671b 143
jm6wud 1:8a94254a671b 144 int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout)
jm6wud 1:8a94254a671b 145 {
jm6wud 1:8a94254a671b 146 return 0;
jm6wud 1:8a94254a671b 147 }
jm6wud 1:8a94254a671b 148
jm6wud 1:8a94254a671b 149 /** write a single bit in an 8-bit device register.
jm6wud 1:8a94254a671b 150 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 151 * @param regAddr Register regAddr to write to
jm6wud 1:8a94254a671b 152 * @param bitNum Bit position to write (0-7)
jm6wud 1:8a94254a671b 153 * @param value New bit value to write
jm6wud 1:8a94254a671b 154 * @return Status of operation (true = success)
jm6wud 1:8a94254a671b 155 */
jm6wud 1:8a94254a671b 156 bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) {
jm6wud 1:8a94254a671b 157 uint8_t b;
jm6wud 1:8a94254a671b 158 readByte(devAddr, regAddr, &b);
jm6wud 1:8a94254a671b 159 b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
jm6wud 1:8a94254a671b 160 return writeByte(devAddr, regAddr, b);
jm6wud 1:8a94254a671b 161 }
jm6wud 1:8a94254a671b 162
jm6wud 1:8a94254a671b 163 /** write a single bit in a 16-bit device register.
jm6wud 1:8a94254a671b 164 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 165 * @param regAddr Register regAddr to write to
jm6wud 1:8a94254a671b 166 * @param bitNum Bit position to write (0-15)
jm6wud 1:8a94254a671b 167 * @param value New bit value to write
jm6wud 1:8a94254a671b 168 * @return Status of operation (true = success)
jm6wud 1:8a94254a671b 169 */
jm6wud 1:8a94254a671b 170 bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) {
jm6wud 1:8a94254a671b 171 uint16_t w;
jm6wud 1:8a94254a671b 172 readWord(devAddr, regAddr, &w);
jm6wud 1:8a94254a671b 173 w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum));
jm6wud 1:8a94254a671b 174 return writeWord(devAddr, regAddr, w);
jm6wud 1:8a94254a671b 175 }
jm6wud 1:8a94254a671b 176
jm6wud 1:8a94254a671b 177 /** Write multiple bits in an 8-bit device register.
jm6wud 1:8a94254a671b 178 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 179 * @param regAddr Register regAddr to write to
jm6wud 1:8a94254a671b 180 * @param bitStart First bit position to write (0-7)
jm6wud 1:8a94254a671b 181 * @param length Number of bits to write (not more than 8)
jm6wud 1:8a94254a671b 182 * @param data Right-aligned value to write
jm6wud 1:8a94254a671b 183 * @return Status of operation (true = success)
jm6wud 1:8a94254a671b 184 */
jm6wud 1:8a94254a671b 185 bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) {
jm6wud 1:8a94254a671b 186 // 010 value to write
jm6wud 1:8a94254a671b 187 // 76543210 bit numbers
jm6wud 1:8a94254a671b 188 // xxx args: bitStart=4, length=3
jm6wud 1:8a94254a671b 189 // 00011100 mask byte
jm6wud 1:8a94254a671b 190 // 10101111 original value (sample)
jm6wud 1:8a94254a671b 191 // 10100011 original & ~mask
jm6wud 1:8a94254a671b 192 // 10101011 masked | value
jm6wud 1:8a94254a671b 193 uint8_t b;
jm6wud 1:8a94254a671b 194 if (readByte(devAddr, regAddr, &b) != 0) {
jm6wud 1:8a94254a671b 195 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jm6wud 1:8a94254a671b 196 data <<= (bitStart - length + 1); // shift data into correct position
jm6wud 1:8a94254a671b 197 data &= mask; // zero all non-important bits in data
jm6wud 1:8a94254a671b 198 b &= ~(mask); // zero all important bits in existing byte
jm6wud 1:8a94254a671b 199 b |= data; // combine data with existing byte
jm6wud 1:8a94254a671b 200 return writeByte(devAddr, regAddr, b);
jm6wud 1:8a94254a671b 201 } else {
jm6wud 1:8a94254a671b 202 return false;
jm6wud 1:8a94254a671b 203 }
jm6wud 1:8a94254a671b 204 }
jm6wud 1:8a94254a671b 205
jm6wud 1:8a94254a671b 206 /** Write multiple bits in a 16-bit device register.
jm6wud 1:8a94254a671b 207 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 208 * @param regAddr Register regAddr to write to
jm6wud 1:8a94254a671b 209 * @param bitStart First bit position to write (0-15)
jm6wud 1:8a94254a671b 210 * @param length Number of bits to write (not more than 16)
jm6wud 1:8a94254a671b 211 * @param data Right-aligned value to write
jm6wud 1:8a94254a671b 212 * @return Status of operation (true = success)
jm6wud 1:8a94254a671b 213 */
jm6wud 1:8a94254a671b 214 bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) {
jm6wud 1:8a94254a671b 215 // 010 value to write
jm6wud 1:8a94254a671b 216 // fedcba9876543210 bit numbers
jm6wud 1:8a94254a671b 217 // xxx args: bitStart=12, length=3
jm6wud 1:8a94254a671b 218 // 0001110000000000 mask byte
jm6wud 1:8a94254a671b 219 // 1010111110010110 original value (sample)
jm6wud 1:8a94254a671b 220 // 1010001110010110 original & ~mask
jm6wud 1:8a94254a671b 221 // 1010101110010110 masked | value
jm6wud 1:8a94254a671b 222 uint16_t w;
jm6wud 1:8a94254a671b 223 if (readWord(devAddr, regAddr, &w) != 0) {
jm6wud 1:8a94254a671b 224 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jm6wud 1:8a94254a671b 225 data <<= (bitStart - length + 1); // shift data into correct position
jm6wud 1:8a94254a671b 226 data &= mask; // zero all non-important bits in data
jm6wud 1:8a94254a671b 227 w &= ~(mask); // zero all important bits in existing word
jm6wud 1:8a94254a671b 228 w |= data; // combine data with existing word
jm6wud 1:8a94254a671b 229 return writeWord(devAddr, regAddr, w);
jm6wud 1:8a94254a671b 230 } else {
jm6wud 1:8a94254a671b 231 return false;
jm6wud 1:8a94254a671b 232 }
jm6wud 1:8a94254a671b 233 }
jm6wud 1:8a94254a671b 234
jm6wud 1:8a94254a671b 235 /** Write single byte to an 8-bit device register.
jm6wud 1:8a94254a671b 236 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 237 * @param regAddr Register address to write to
jm6wud 1:8a94254a671b 238 * @param data New byte value to write
jm6wud 1:8a94254a671b 239 * @return Status of operation (true = success)
jm6wud 1:8a94254a671b 240 */
jm6wud 1:8a94254a671b 241 bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) {
jm6wud 1:8a94254a671b 242 return writeBytes(devAddr, regAddr, 1, &data);
jm6wud 1:8a94254a671b 243 }
jm6wud 1:8a94254a671b 244
jm6wud 1:8a94254a671b 245 /** Write single word to a 16-bit device register.
jm6wud 1:8a94254a671b 246 * @param devAddr I2C slave device address
jm6wud 1:8a94254a671b 247 * @param regAddr Register address to write to
jm6wud 1:8a94254a671b 248 * @param data New word value to write
jm6wud 1:8a94254a671b 249 * @return Status of operation (true = success)
jm6wud 1:8a94254a671b 250 */
jm6wud 1:8a94254a671b 251 bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) {
jm6wud 1:8a94254a671b 252 return writeWords(devAddr, regAddr, 1, &data);
jm6wud 1:8a94254a671b 253 }
jm6wud 1:8a94254a671b 254
jm6wud 1:8a94254a671b 255 bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
jm6wud 1:8a94254a671b 256 {
jm6wud 1:8a94254a671b 257 i2c.start();
jm6wud 1:8a94254a671b 258 i2c.write(devAddr<<1);
jm6wud 1:8a94254a671b 259 i2c.write(regAddr);
jm6wud 1:8a94254a671b 260 for(int i = 0; i < length; i++) {
jm6wud 1:8a94254a671b 261 i2c.write(data[i]);
jm6wud 1:8a94254a671b 262 }
jm6wud 1:8a94254a671b 263 i2c.stop();
jm6wud 1:8a94254a671b 264 return true;
jm6wud 1:8a94254a671b 265 }
jm6wud 1:8a94254a671b 266
jm6wud 1:8a94254a671b 267 bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data)
jm6wud 1:8a94254a671b 268 {
jm6wud 1:8a94254a671b 269 return true;
jm6wud 1:8a94254a671b 270 }
jm6wud 1:8a94254a671b 271
jm6wud 1:8a94254a671b 272 uint16_t I2Cdev::readTimeout(void)
jm6wud 1:8a94254a671b 273 {
jm6wud 1:8a94254a671b 274 return 0;
jm6wud 1:8a94254a671b 275 }