Browse Source

TWIBus patch for proper use of Wire.requestFrom

Scott Lahteine 8 years ago
parent
commit
2b226bc5c2
2 changed files with 14 additions and 24 deletions
  1. 11
    13
      Marlin/twibus.cpp
  2. 3
    11
      Marlin/twibus.h

+ 11
- 13
Marlin/twibus.cpp View File

@@ -25,7 +25,6 @@
25 25
 #if ENABLED(EXPERIMENTAL_I2CBUS)
26 26
 
27 27
 #include "twibus.h"
28
-
29 28
 #include <Wire.h>
30 29
 
31 30
 TWIBus::TWIBus() {
@@ -121,18 +120,12 @@ bool TWIBus::request(const uint8_t bytes) {
121 120
   #endif
122 121
 
123 122
   // requestFrom() is a blocking function
124
-  Wire.requestFrom(this->addr, bytes);
125
-
126
-  // Wait for all bytes to arrive
127
-  millis_t t = millis() + this->timeout;
128
-  while (Wire.available() < bytes)
129
-    if (ELAPSED(millis(), t)) {
130
-      #if ENABLED(DEBUG_TWIBUS)
131
-        SERIAL_ECHO_START;
132
-        SERIAL_ECHOLNPGM("i2c timeout");
133
-      #endif
134
-      return false;
135
-    }
123
+  if (Wire.requestFrom(this->addr, bytes) == 0) {
124
+    #if ENABLED(DEBUG_TWIBUS)
125
+      debug("request fail", this->addr);
126
+    #endif
127
+    return false;
128
+  }
136 129
 
137 130
   return true;
138 131
 }
@@ -151,6 +144,11 @@ uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
151 144
   uint8_t count = 0;
152 145
   while (count < bytes && Wire.available())
153 146
     dst[count++] = Wire.read();
147
+
148
+  #if ENABLED(DEBUG_TWIBUS)
149
+    debug(PSTR("capture"), count);
150
+  #endif
151
+
154 152
   return count;
155 153
 }
156 154
 

+ 3
- 11
Marlin/twibus.h View File

@@ -55,14 +55,6 @@ typedef void (*twiRequestFunc_t)();
55 55
 class TWIBus {
56 56
   private:
57 57
     /**
58
-     * @brief Timeout value in milliseconds
59
-     * @details Maximum amount of time (ms) to wait for a reply.
60
-     *          Useful if something goes wrong on the bus and the
61
-     *          SDA/SCL lines are held up by another device.
62
-     */
63
-    const int timeout = 5;
64
-
65
-    /**
66 58
      * @brief Number of bytes on buffer
67 59
      * @description Number of bytes in the buffer waiting to be flushed to the bus
68 60
      */
@@ -165,11 +157,11 @@ class TWIBus {
165 157
     /**
166 158
      * @brief Request data from the slave device and wait.
167 159
      * @details Request a number of bytes from a slave device.
168
-     *          Wait for the data to arrive until the timeout
169
-     *          interval expires. Return true on success.
160
+     *          Wait for the data to arrive, and return true
161
+     *          on success.
170 162
      *
171 163
      * @param bytes the number of bytes to request
172
-     * @return status of the request: true=success, false=timeout
164
+     * @return status of the request: true=success, false=fail
173 165
      */
174 166
     bool request(const uint8_t bytes);
175 167
 

Loading…
Cancel
Save