mpu6050 nadafa

Dependencies:   mbed

Committer:
khaledelmadawi
Date:
Tue Jun 23 14:47:52 2015 +0000
Revision:
0:8fe8d6dd7cd6
mpu nadafa

Who changed what in which revision?

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