dsafeadf

Dependents:   DMP_tidy_working

Fork of MPU6050-DMP-Ian by Ian Hua

Committer:
majik
Date:
Tue May 27 09:21:09 2014 +0000
Revision:
14:3f0bc381e1dd
Parent:
7:4619a083f289
tempory code

Who changed what in which revision?

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