Merge pull request #4718 from thinkyhead/rc_twibus_patch_20160827

TWIBus patch for proper use of Wire.requestFrom
master
Scott Lahteine 8 years ago committed by GitHub
commit fae92e3f3c

@ -25,7 +25,6 @@
#if ENABLED(EXPERIMENTAL_I2CBUS) #if ENABLED(EXPERIMENTAL_I2CBUS)
#include "twibus.h" #include "twibus.h"
#include <Wire.h> #include <Wire.h>
TWIBus::TWIBus() { TWIBus::TWIBus() {
@ -121,15 +120,9 @@ bool TWIBus::request(const uint8_t bytes) {
#endif #endif
// requestFrom() is a blocking function // requestFrom() is a blocking function
Wire.requestFrom(this->addr, bytes); if (Wire.requestFrom(this->addr, bytes) == 0) {
// Wait for all bytes to arrive
millis_t t = millis() + this->timeout;
while (Wire.available() < bytes)
if (ELAPSED(millis(), t)) {
#if ENABLED(DEBUG_TWIBUS) #if ENABLED(DEBUG_TWIBUS)
SERIAL_ECHO_START; debug("request fail", this->addr);
SERIAL_ECHOLNPGM("i2c timeout");
#endif #endif
return false; return false;
} }
@ -151,6 +144,11 @@ uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
uint8_t count = 0; uint8_t count = 0;
while (count < bytes && Wire.available()) while (count < bytes && Wire.available())
dst[count++] = Wire.read(); dst[count++] = Wire.read();
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("capture"), count);
#endif
return count; return count;
} }

@ -54,14 +54,6 @@ typedef void (*twiRequestFunc_t)();
*/ */
class TWIBus { class TWIBus {
private: private:
/**
* @brief Timeout value in milliseconds
* @details Maximum amount of time (ms) to wait for a reply.
* Useful if something goes wrong on the bus and the
* SDA/SCL lines are held up by another device.
*/
const int timeout = 5;
/** /**
* @brief Number of bytes on buffer * @brief Number of bytes on buffer
* @description Number of bytes in the buffer waiting to be flushed to the bus * @description Number of bytes in the buffer waiting to be flushed to the bus
@ -165,11 +157,11 @@ class TWIBus {
/** /**
* @brief Request data from the slave device and wait. * @brief Request data from the slave device and wait.
* @details Request a number of bytes from a slave device. * @details Request a number of bytes from a slave device.
* Wait for the data to arrive until the timeout * Wait for the data to arrive, and return true
* interval expires. Return true on success. * on success.
* *
* @param bytes the number of bytes to request * @param bytes the number of bytes to request
* @return status of the request: true=success, false=timeout * @return status of the request: true=success, false=fail
*/ */
bool request(const uint8_t bytes); bool request(const uint8_t bytes);

Loading…
Cancel
Save