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