@@ -434,12 +434,12 @@ KX13X_STATUS_t QwiicKX13xCore::readRegister(uint8_t *dataPointer, uint8_t reg)
434
434
i2cResult = _i2cPort->endTransmission (false );
435
435
if ( i2cResult != 0 )
436
436
return KX13X_I2C_ERROR; // Error: Sensor did not ack
437
- _i2cPort->requestFrom (static_cast <uint8_t >(_deviceAddress), static_cast <uint8_t >(1 ));
438
- *dataPointer = _i2cPort-> read ();
439
- i2cResult = _i2cPort->endTransmission ();
440
- if ( i2cResult != 0 )
441
- return KX13X_I2C_ERROR; // Error: Sensor did not ack
442
- return KX13X_SUCCESS;
437
+ i2cResult = _i2cPort->requestFrom (static_cast <uint8_t >(_deviceAddress), static_cast <uint8_t >(1 )); // returns number of bytes
438
+ if ( i2cResult != 0 ) {
439
+ *dataPointer = _i2cPort->read ();
440
+ return KX13X_SUCCESS;
441
+ }
442
+ return KX13X_I2C_ERROR; // Error: Sensor did not ack
443
443
}
444
444
}
445
445
@@ -474,14 +474,12 @@ KX13X_STATUS_t QwiicKX13xCore::readMultipleRegisters(uint8_t reg, uint8_t dataBu
474
474
if ( i2cResult != 0 )
475
475
return KX13X_I2C_ERROR; // Error: Sensor did not ack
476
476
477
- _i2cPort->requestFrom (static_cast <uint8_t >(_deviceAddress), numBytes, false );
477
+ i2cResult = _i2cPort->requestFrom (static_cast <uint8_t >(_deviceAddress), numBytes, false );
478
+ if ( i2cResult == 0 )
479
+ return KX13X_I2C_ERROR;
478
480
for (size_t i = 0 ; i < numBytes; i++) {
479
481
dataBuffer[i] = _i2cPort->read ();
480
482
}
481
-
482
- i2cResult = _i2cPort->endTransmission ();
483
- if ( i2cResult != 0 )
484
- return KX13X_I2C_ERROR; // Error: Sensor did not ack
485
483
return KX13X_SUCCESS;
486
484
}
487
485
}
@@ -507,19 +505,16 @@ KX13X_STATUS_t QwiicKX13xCore::overBufLenI2CRead(uint8_t reg, uint8_t dataBuffer
507
505
else
508
506
resizedRead = numBytes;
509
507
510
- _i2cPort->requestFrom (static_cast <uint8_t >(_deviceAddress), resizedRead, false ); // false = repeated start
508
+ i2cResult = _i2cPort->requestFrom (static_cast <uint8_t >(_deviceAddress), resizedRead, false ); // false = repeated start
509
+ if ( i2cResult == 0 )
510
+ return KX13X_I2C_ERROR;
511
511
for (size_t i = 0 ; i < resizedRead; i++) {
512
512
dataBuffer[arrayPlaceHolder] = _i2cPort->read ();
513
513
arrayPlaceHolder++;
514
514
}
515
515
numBytes = numBytes - MAX_BUFFER_LENGTH; // end condition
516
516
}
517
-
518
- i2cResult = _i2cPort->endTransmission ();
519
- if ( i2cResult != 0 )
520
- return KX13X_I2C_ERROR; // Error: Sensor did not ack
521
- else
522
- return KX13X_SUCCESS;
517
+ return KX13X_SUCCESS;
523
518
}
524
519
525
520
// Writes the given value to the given register, using the provided mask and
0 commit comments