Segue

Fork of MPU6050 by Simon Garfieldsg

Committer:
Gaetios
Date:
Mon Oct 02 16:42:34 2017 +0000
Revision:
2:85734ad35573
foi alterado para utilizar 4 acelerometros

Who changed what in which revision?

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