MPU9250

Dependents:   FreeIMU

Fork of MPU6050 by Yifei Teng

Committer:
tyftyftyf
Date:
Wed Apr 18 20:24:13 2018 +0000
Revision:
22:ec9725201195
Parent:
13:a74f2d622b54
change to I2C 1

Who changed what in which revision?

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