PCA9555
Dependents: Telliskivi2_2014 PowerManagementBoard_Rev_A_2017
Revision 8:8f59b7233e6c, committed 2013-11-03
- Comitter:
- Reiko
- Date:
- Sun Nov 03 11:43:05 2013 +0000
- Parent:
- 7:3b54389686ca
- Commit message:
- Added retry on read error
Changed in this revision
PCA9555.cpp | Show annotated file Show diff for this revision Revisions of this file |
PCA9555.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 3b54389686ca -r 8f59b7233e6c PCA9555.cpp --- a/PCA9555.cpp Thu Sep 19 07:06:54 2013 +0000 +++ b/PCA9555.cpp Sun Nov 03 11:43:05 2013 +0000 @@ -1,11 +1,13 @@ #include "PCA9555.h" PCA9555::PCA9555(PinName sda, PinName scl, PinName interrupPin, int address) - : _irqpin(interrupPin), _i2c(sda, scl) { + : _irqpin(interrupPin), _i2c(sda, scl), led3(LED3) { _address = address; _irqpin.rise(this, &PCA9555::callChange); _irqpin.fall(this, &PCA9555::callChange); _i2c.frequency(400000); + retryCount = 3; + currentRetryCount = retryCount; } void PCA9555::setDirection(int data) { @@ -19,13 +21,31 @@ } int PCA9555::read() { + bool isCorrect = false; char rx_array[2] = {0x00, 0x00}; char tx_array[1] = {0x00}; - _i2c.write(_address, tx_array, 1); - - _i2c.read(_address, rx_array, 2); - lastReadState = (rx_array[1] << 8) | rx_array[0]; + while (--currentRetryCount) { + tx_array[0] = 0x00; + _i2c.write(_address, tx_array, 1); + int success = _i2c.read(_address, rx_array, 2); + //_i2c.stop(); + if (success != 0) { + led3 = 1; + } else { + led3 = 0; + currentRetryCount = retryCount; + isCorrect = true; + break; + } + } + if (isCorrect) { + lastReadState = (rx_array[1] << 8) | rx_array[0]; + } + return lastReadState; +} + +int PCA9555::getLastRead() { return lastReadState; }
diff -r 3b54389686ca -r 8f59b7233e6c PCA9555.h --- a/PCA9555.h Thu Sep 19 07:06:54 2013 +0000 +++ b/PCA9555.h Sun Nov 03 11:43:05 2013 +0000 @@ -23,7 +23,7 @@ * @return The two bytes read */ int read(); - + int getLastRead(); /** Write to the IO pins * * @param data The 16 bits to write to the IO port @@ -73,7 +73,9 @@ int _address; unsigned int currentWriteState; unsigned int lastReadState; - + DigitalOut led3; + int retryCount; + int currentRetryCount; void callChange(void); };