diff --git a/I2CManager.cpp b/I2CManager.cpp index 94c4baf..a3ea611 100644 --- a/I2CManager.cpp +++ b/I2CManager.cpp @@ -179,7 +179,7 @@ I2CManagerClass I2CManager = I2CManagerClass(); /*************************************************************************** * Block waiting for request block to complete, and return completion status. * Since such a loop could potentially last for ever if the RB status doesn't - * change, we set a high limit (0.1sec, 100ms) on the wait time and, if it + * change, we set a high limit (1sec, 1000ms) on the wait time and, if it * hasn't changed by that time we assume it's not going to, and just return * a timeout status. This means that CS will not lock up. ***************************************************************************/ @@ -187,8 +187,8 @@ uint8_t I2CRB::wait() { unsigned long waitStart = millis(); do { I2CManager.loop(); - // Rather than looping indefinitely, let's set a very high timeout (100ms). - if ((millis() - waitStart) > 100UL) { + // Rather than looping indefinitely, let's set a very high timeout (1s). + if ((millis() - waitStart) > 1000UL) { DIAG(F("I2C TIMEOUT I2C:x%x I2CRB:x%x"), i2cAddress, this); status = I2C_STATUS_TIMEOUT; // Note that, although the timeout is posted, the request may yet complete. diff --git a/I2CManager_AVR.h b/I2CManager_AVR.h index 310afa2..6492e00 100644 --- a/I2CManager_AVR.h +++ b/I2CManager_AVR.h @@ -62,7 +62,18 @@ * Set I2C clock speed register. ***************************************************************************/ void I2CManagerClass::I2C_setClock(unsigned long i2cClockSpeed) { - TWBR = ((F_CPU / i2cClockSpeed) - 16) / 2; + unsigned long temp = ((F_CPU / i2cClockSpeed) - 16) / 2; + for (uint8_t preScaler = 0; preScaler<=3; preScaler++) { + if (temp <= 255) { + TWBR = temp; + TWSR = (TWSR & 0xfc) | preScaler; + return; + } else + temp /= 4; + } + // Set slowest speed ~= 500 bits/sec + TWBR = 255; + TWSR |= 0x03; } /***************************************************************************