I2C devices lib, modified for mbed

Dependents:   ezSBC_MPU9250

Committer:
JojoS
Date:
Mon Apr 24 16:21:21 2017 +0000
Revision:
0:c3b288708216
modified for mbed;

Who changed what in which revision?

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