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