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