Read multiple words from a 16-bit device register. * @param devAddr I2C slave device address * @param regAddr First register regAddr to read from * @param length Number of words to read * @param data Buffer to store read data in * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Number of words read (-1
| 324 | * @return Number of words read (-1 indicates failure) |
| 325 | */ |
| 326 | int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout, void *wireObj) { |
| 327 | #ifdef I2CDEV_SERIAL_DEBUG |
| 328 | Serial.print("I2C (0x"); |
| 329 | Serial.print(devAddr, HEX); |
| 330 | Serial.print(") reading "); |
| 331 | Serial.print(length, DEC); |
| 332 | Serial.print(" words from 0x"); |
| 333 | Serial.print(regAddr, HEX); |
| 334 | Serial.print("..."); |
| 335 | #endif |
| 336 | |
| 337 | int8_t count = 0; |
| 338 | uint32_t t1 = millis(); |
| 339 | |
| 340 | #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE |
| 341 | TwoWire *useWire = &Wire; |
| 342 | if (wireObj) useWire = (TwoWire *)wireObj; |
| 343 | |
| 344 | #if (ARDUINO < 100) |
| 345 | // Arduino v00xx (before v1.0), Wire library |
| 346 | |
| 347 | // I2C/TWI subsystem uses internal buffer that breaks with large data requests |
| 348 | // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in |
| 349 | // smaller chunks instead of all at once |
| 350 | for (uint8_t k = 0; k < length * 2; k += min(length * 2, I2CDEVLIB_WIRE_BUFFER_LENGTH)) { |
| 351 | useWire->beginTransmission(devAddr); |
| 352 | useWire->send(regAddr); |
| 353 | useWire->endTransmission(); |
| 354 | useWire->beginTransmission(devAddr); |
| 355 | useWire->requestFrom(devAddr, (uint8_t)(length * 2)); // length=words, this wants bytes |
| 356 | |
| 357 | bool msb = true; // starts with MSB, then LSB |
| 358 | for (; useWire->available() && count < length && (timeout == 0 || millis() - t1 < timeout);) { |
| 359 | if (msb) { |
| 360 | // first byte is bits 15-8 (MSb=15) |
| 361 | data[count] = useWire->receive() << 8; |
| 362 | } else { |
| 363 | // second byte is bits 7-0 (LSb=0) |
| 364 | data[count] |= useWire->receive(); |
| 365 | #ifdef I2CDEV_SERIAL_DEBUG |
| 366 | Serial.print(data[count], HEX); |
| 367 | if (count + 1 < length) Serial.print(" "); |
| 368 | #endif |
| 369 | count++; |
| 370 | } |
| 371 | msb = !msb; |
| 372 | } |
| 373 | } |
| 374 | #elif (ARDUINO == 100) |
| 375 | // Arduino v1.0.0, Wire library |
| 376 | // Adds standardized write() and read() stream methods instead of send() and receive() |
| 377 | |
| 378 | // I2C/TWI subsystem uses internal buffer that breaks with large data requests |
| 379 | // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in |
| 380 | // smaller chunks instead of all at once |
| 381 | for (uint8_t k = 0; k < length * 2; k += min(length * 2, I2CDEVLIB_WIRE_BUFFER_LENGTH)) { |
| 382 | useWire->beginTransmission(devAddr); |
| 383 | useWire->write(regAddr); |
nothing calls this directly
no test coverage detected