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