Skip to content

Commit

Permalink
Fix I2C Error during wakeup, add retry logic
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
tyeth committed Aug 21, 2023
1 parent 8f8be1a commit c14e66a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Adafruit_AM2320.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,25 @@ uint32_t Adafruit_AM2320::readRegister32(uint8_t reg) {
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};

// wake up
i2c_dev->write(buffer, 1);
delay(10); // wait 10 ms
bool writtenBytes = i2c_dev->write(buffer, 1);
delay(10); // wait 10 ms to wake up
if(!writtenBytes){
delay(90); // wait remainder of 100ms for wakeup before retrying
i2c_dev->write(buffer, 1);
delay(10); // wait 10 ms to wake up
}

// send a command to read register
buffer[0] = AM2320_CMD_READREG;
buffer[1] = reg;
buffer[2] = 4; // 4 bytes
i2c_dev->write(buffer, 3);
writtenBytes = i2c_dev->write(buffer, 3);
delay(2); // wait 2 ms
if(!writtenBytes){
//try once more...
writtenBytes = i2c_dev->write(buffer, 3);
delay(2); // wait 2 ms
}

// 2 bytes preamble, 4 bytes data, 2 bytes CRC
i2c_dev->read(buffer, 8);
Expand Down

0 comments on commit c14e66a

Please sign in to comment.