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
 #if ENABLED(EXPERIMENTAL_I2CBUS)
25
 #if ENABLED(EXPERIMENTAL_I2CBUS)
26
 
26
 
27
 #include "twibus.h"
27
 #include "twibus.h"
28
-
29
 #include <Wire.h>
28
 #include <Wire.h>
30
 
29
 
31
 TWIBus::TWIBus() {
30
 TWIBus::TWIBus() {
121
   #endif
120
   #endif
122
 
121
 
123
   // requestFrom() is a blocking function
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
   return true;
130
   return true;
138
 }
131
 }
151
   uint8_t count = 0;
144
   uint8_t count = 0;
152
   while (count < bytes && Wire.available())
145
   while (count < bytes && Wire.available())
153
     dst[count++] = Wire.read();
146
     dst[count++] = Wire.read();
147
+
148
+  #if ENABLED(DEBUG_TWIBUS)
149
+    debug(PSTR("capture"), count);
150
+  #endif
151
+
154
   return count;
152
   return count;
155
 }
153
 }
156
 
154
 

+ 3
- 11
Marlin/twibus.h View File

55
 class TWIBus {
55
 class TWIBus {
56
   private:
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
      * @brief Number of bytes on buffer
58
      * @brief Number of bytes on buffer
67
      * @description Number of bytes in the buffer waiting to be flushed to the bus
59
      * @description Number of bytes in the buffer waiting to be flushed to the bus
68
      */
60
      */
165
     /**
157
     /**
166
      * @brief Request data from the slave device and wait.
158
      * @brief Request data from the slave device and wait.
167
      * @details Request a number of bytes from a slave device.
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
      * @param bytes the number of bytes to request
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
     bool request(const uint8_t bytes);
166
     bool request(const uint8_t bytes);
175
 
167
 

Loading…
Cancel
Save