ported from https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050. Please refer this examples https://developer.mbed.org/users/syundo0730/code/MPU6050_Example/ to run this library in mbed.

Dependencies:   ArduinoSerial I2Cdev

Dependents:   MPU6050_Example

Fork of MPU6050 + MPU9150 by Andreas Kodewitz

Committer:
syundo0730
Date:
Sun Jan 31 09:12:19 2016 +0000
Revision:
7:d5845b617139
trace changes in arduino project

Who changed what in which revision?

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