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