Add retry support to I2CInterface
Enhance the I2CInterface class hierarchy to support retrying failed I2C
operations.
Add an optional parameter to i2c::create() to specify the maximum number
of retries to perform for an I2C operation. Default to 0 retries so
that existing code will not be affected.
Testing:
* Tested each modified function
* When I2C operation succeeds
* When I2C operation fails
* When no retries are done
* When retries are done
* When retried operation succeeds
* When retried operation fails
* See complete test plan at
https://gist.github.com/smccarney/8d203a40d9984402ac495dc3d689c12d
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I2b0e782e5caaafa9908ef5625687377b959b38ff
diff --git a/tools/i2c/i2c.hpp b/tools/i2c/i2c.hpp
index 738547c..23acdbc 100644
--- a/tools/i2c/i2c.hpp
+++ b/tools/i2c/i2c.hpp
@@ -19,11 +19,13 @@
* @param[in] busId - The i2c bus ID
* @param[in] devAddr - The device address of the I2C device
* @param[in] initialState - Initial state of the I2CDevice object
+ * @param[in] maxRetries - Maximum number of times to retry an I2C operation
*/
explicit I2CDevice(uint8_t busId, uint8_t devAddr,
- InitialState initialState = InitialState::OPEN) :
+ InitialState initialState = InitialState::OPEN,
+ int maxRetries = 0) :
busId(busId),
- devAddr(devAddr)
+ devAddr(devAddr), maxRetries(maxRetries)
{
busStr = "/dev/i2c-" + std::to_string(busId);
if (initialState == InitialState::OPEN)
@@ -44,6 +46,9 @@
/** @brief The i2c device address in the bus */
uint8_t devAddr;
+ /** @brief Maximum number of times to retry an I2C operation */
+ int maxRetries = 0;
+
/** @brief The file descriptor of the opened i2c device */
int fd = INVALID_FD;
@@ -164,12 +169,14 @@
* @param[in] busId - The i2c bus ID
* @param[in] devAddr - The device address of the i2c
* @param[in] initialState - Initial state of the I2CInterface object
+ * @param[in] maxRetries - Maximum number of times to retry an I2C operation
*
* @return The unique_ptr holding the I2CInterface
*/
static std::unique_ptr<I2CInterface>
create(uint8_t busId, uint8_t devAddr,
- InitialState initialState = InitialState::OPEN);
+ InitialState initialState = InitialState::OPEN,
+ int maxRetries = 0);
};
} // namespace i2c