From 2b226bc5c2d1edf8de854e132fd8c8b7e5cf266a Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 27 Aug 2016 19:00:45 -0500 Subject: [PATCH] TWIBus patch for proper use of Wire.requestFrom --- Marlin/twibus.cpp | 24 +++++++++++------------- Marlin/twibus.h | 14 +++----------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/Marlin/twibus.cpp b/Marlin/twibus.cpp index 11f205713..0352e1b9c 100644 --- a/Marlin/twibus.cpp +++ b/Marlin/twibus.cpp @@ -25,7 +25,6 @@ #if ENABLED(EXPERIMENTAL_I2CBUS) #include "twibus.h" - #include TWIBus::TWIBus() { @@ -121,18 +120,12 @@ bool TWIBus::request(const uint8_t bytes) { #endif // requestFrom() is a blocking function - Wire.requestFrom(this->addr, bytes); - - // Wait for all bytes to arrive - millis_t t = millis() + this->timeout; - while (Wire.available() < bytes) - if (ELAPSED(millis(), t)) { - #if ENABLED(DEBUG_TWIBUS) - SERIAL_ECHO_START; - SERIAL_ECHOLNPGM("i2c timeout"); - #endif - return false; - } + if (Wire.requestFrom(this->addr, bytes) == 0) { + #if ENABLED(DEBUG_TWIBUS) + debug("request fail", this->addr); + #endif + return false; + } return true; } @@ -151,6 +144,11 @@ uint8_t TWIBus::capture(char *dst, const uint8_t bytes) { uint8_t count = 0; while (count < bytes && Wire.available()) dst[count++] = Wire.read(); + + #if ENABLED(DEBUG_TWIBUS) + debug(PSTR("capture"), count); + #endif + return count; } diff --git a/Marlin/twibus.h b/Marlin/twibus.h index d578a6d1f..bd2d8a5d4 100644 --- a/Marlin/twibus.h +++ b/Marlin/twibus.h @@ -54,14 +54,6 @@ typedef void (*twiRequestFunc_t)(); */ class TWIBus { 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 * @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. * @details Request a number of bytes from a slave device. - * Wait for the data to arrive until the timeout - * interval expires. Return true on success. + * Wait for the data to arrive, and return true + * on success. * * @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);