MCPcopy Create free account
hub / github.com/SmingHub/Sming / readWords

Method readWords

Sming/Libraries/I2Cdev/I2Cdev.cpp:328–470  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

326 * @return Number of words read (-1 indicates failure)
327 */
328int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout) {
329 #ifdef I2CDEV_SERIAL_DEBUG
330 Serial.print("I2C (0x");
331 Serial.print(devAddr, HEX);
332 Serial.print(") reading ");
333 Serial.print(length, DEC);
334 Serial.print(" words from 0x");
335 Serial.print(regAddr, HEX);
336 Serial.print("...");
337 #endif
338
339 int8_t count = 0;
340 uint32_t t1 = millis();
341
342 #if (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE)
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 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(uint8_t(length * 2), BUFFER_LENGTH)) {
351 Wire.beginTransmission(devAddr);
352 Wire.send(regAddr);
353 Wire.endTransmission();
354 Wire.beginTransmission(devAddr);
355 Wire.requestFrom(devAddr, (uint8_t)(length * 2)); // length=words, this wants bytes
356
357 bool msb = true; // starts with MSB, then LSB
358 for (; Wire.available() && count < length && (timeout == 0 || millis() - t1 < timeout);) {
359 if (msb) {
360 // first byte is bits 15-8 (MSb=15)
361 data[count] = Wire.receive() << 8;
362 } else {
363 // second byte is bits 7-0 (LSb=0)
364 data[count] |= Wire.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 Wire.endTransmission();
375 }
376 #elif (ARDUINO == 100)
377 // Arduino v1.0.0, Wire library
378 // Adds standardized write() and read() stream methods instead of send() and receive()
379
380 // I2C/TWI subsystem uses internal buffer that breaks with large data requests
381 // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
382 // smaller chunks instead of all at once
383 for (uint8_t k = 0; k < length * 2; k += min(uint8_t(length * 2), BUFFER_LENGTH)) {
384 Wire.beginTransmission(devAddr);
385 Wire.write(regAddr);

Callers

nothing calls this directly

Calls 12

millisFunction · 0.85
minFunction · 0.85
receiveMethod · 0.80
printlnMethod · 0.80
printMethod · 0.45
beginTransmissionMethod · 0.45
sendMethod · 0.45
endTransmissionMethod · 0.45
requestFromMethod · 0.45
availableMethod · 0.45
writeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected