@@ -142,74 +142,64 @@ size_t TwoWire::oldRequestFrom(uint8_t address, size_t size, bool sendStop)
142
142
rxLength = read ;
143
143
return read ;
144
144
}
145
+ /* @StickBreaker common handler for processing the queued commands
146
+ */
147
+ i2c_err_t TwoWire::processQueue (uint16_t * readCount){
148
+ last_error=i2cProcQueue (i2c);
149
+ rxIndex = 0 ;
150
+ rxLength = 0 ;
151
+ txQueued = 0 ; // the SendStop=true will restart all Queueing
152
+ rxQueued = 0 ;
153
+ *readCount = i2cQueueReadCount (i2c); // do I want to exclude all Local buffers from this?
154
+ // currently 17/NOV/2017 this count is of all bytes read from I2C, for this queue of commands.
155
+
156
+
157
+ // what about mix local buffers and Wire Buffers?
158
+ // Handled it by verifying location, only contiguous sections of
159
+ // rxBuffer are available for Wire.read(). Any local buffer requestfrom(id,*char..) is
160
+ // completed already, only the ones using Wire's buffers need to be considered
161
+
162
+ uint8_t *savePtr;
163
+ uint16_t len;
164
+ uint8_t idx=0 ;
165
+ while (I2C_ERROR_OK==i2cGetReadQueue (i2c,&savePtr,&len,&idx)){
166
+ if (savePtr==(rxBuffer+rxLength)){
167
+ rxLength = rxLength + len;
168
+ }
169
+ }
170
+ i2cFreeQueue (i2c);
171
+ return last_error;
172
+ }
173
+
174
+
145
175
146
176
/* @stickBreaker 11/2017 fix for ReSTART timeout, ISR
147
177
*/
148
178
size_t TwoWire::requestFrom (uint8_t address, size_t size, bool sendStop){
179
+ // use internal Wire rxBuffer, multiple requestFrom()'s may be pending, try to share rxBuffer
149
180
150
- uint16_t cnt = rxQueued; // currently queued reads
151
- if (cnt<I2C_BUFFER_LENGTH){
181
+ uint16_t cnt = rxQueued; // currently queued reads, next available position in rxBuffer
182
+ if (cnt<( I2C_BUFFER_LENGTH- 1 )){ // any room left in rxBuffer
152
183
if ((size+cnt)>I2C_BUFFER_LENGTH)
153
184
size = (I2C_BUFFER_LENGTH-cnt);
154
185
}
155
186
else { // no room to receive more!
156
- log_e (" no room %d" ,cnt);
187
+ log_e (" rxBuff overflow %d" ,cnt+size );
157
188
cnt = 0 ;
158
- rxIndex = 0 ;
159
- rxLength = 0 ;
160
- rxQueued = 0 ;
161
189
last_error = I2C_ERROR_MEMORY;
162
- i2cFreeQueue (i2c );
190
+ flush ( );
163
191
return cnt;
164
192
}
165
-
166
- last_error =i2cAddQueueRead (i2c,address,&rxBuffer[cnt],size,sendStop,NULL );
167
- if (last_error==I2C_ERROR_OK){ // successfully queued the read
168
- rxQueued += size;
169
- if (sendStop){ // now actually process the queued commands
170
- last_error=i2cProcQueue (i2c);
171
- rxIndex = 0 ;
172
- rxLength = i2cQueueReadCount (i2c);
173
- rxQueued = 0 ;
174
- txQueued = 0 ;
175
- cnt = rxLength;
176
- i2cFreeQueue (i2c);
177
- }
178
- else { // stop not received, so wait for I2C stop,
179
- last_error=I2C_ERROR_CONTINUE;
180
- cnt = 0 ;
181
- }
182
- }
183
- else {// only possible error is I2C_ERROR_MEMORY
184
- cnt = 0 ;
185
- }
186
- return cnt;
187
- }
193
+
194
+ return requestFrom (address, &rxBuffer[cnt],size,sendStop);
195
+ }
188
196
189
197
size_t TwoWire::requestFrom (uint8_t address, uint8_t * readBuff, size_t size, bool sendStop){
190
- size_t cnt=0 ;
198
+ uint16_t cnt=0 ;
191
199
last_error =i2cAddQueueRead (i2c,address,readBuff,size,sendStop,NULL );
192
200
if (last_error==I2C_ERROR_OK){ // successfully queued the read
193
201
if (sendStop){ // now actually process the queued commands
194
- last_error=i2cProcQueue (i2c);
195
- rxIndex = 0 ;
196
- rxLength = 0 ;
197
- txQueued = 0 ; // the SendStop=true will restart are Queueing
198
- rxQueued = 0 ;
199
- cnt = i2cQueueReadCount (i2c);
200
-
201
- // what about mix local buffers and Wire Buffers?
202
- // Handled it by verifying location, only contiguous sections of
203
- // rxBuffer are counted.
204
- uint8_t *savePtr;
205
- uint16_t len;
206
- uint8_t idx=0 ;
207
- while (I2C_ERROR_OK==i2cGetReadQueue (i2c,&savePtr,&len,&idx)){
208
- if (savePtr==(rxBuffer+rxLength)){
209
- rxLength = rxLength + len;
210
- }
211
- }
212
- i2cFreeQueue (i2c);
202
+ last_error = processQueue (&cnt);
213
203
}
214
204
else { // stop not received, so wait for I2C stop,
215
205
last_error=I2C_ERROR_CONTINUE;
@@ -229,24 +219,8 @@ last_error=i2cAddQueueWrite(i2c,address,buff,size,sendStop,NULL);
229
219
230
220
if (last_error==I2C_ERROR_OK){ // queued
231
221
if (sendStop){ // now actually process the queued commands, including READs
232
- last_error=i2cProcQueue (i2c);
233
- rxIndex = 0 ;
234
- rxLength = 0 ;
235
- txQueued = 0 ; // the SendStop=true will restart all Queueing
236
- rxQueued = 0 ;
237
-
238
- // what about mix local buffers and Wire Buffers?
239
- // Handled it by verifying location, only contiguous sections of
240
- // rxBuffer are counted.
241
- uint8_t *savePtr;
242
- uint16_t len;
243
- uint8_t idx=0 ;
244
- while (I2C_ERROR_OK==i2cGetReadQueue (i2c,&savePtr,&len,&idx)){
245
- if (savePtr==(rxBuffer+rxLength)){
246
- rxLength = rxLength + len;
247
- }
248
- }
249
- i2cFreeQueue (i2c);
222
+ uint16_t dummy;
223
+ last_error=processQueue (&dummy);
250
224
}
251
225
else { // stop not received, so wait for I2C stop,
252
226
last_error=I2C_ERROR_CONTINUE;
@@ -302,7 +276,7 @@ if(last_error==I2C_ERROR_CONTINUE){ // must have queued the Write
302
276
return cnt;
303
277
}
304
278
else {
305
- last_error = I2C_ERROR_MISSING_WRITE ;
279
+ last_error = I2C_ERROR_NO_BEGIN ;
306
280
return 0 ;
307
281
}
308
282
}
@@ -320,7 +294,7 @@ if(last_error==I2C_ERROR_CONTINUE){ // must have queued the write
320
294
return cnt;
321
295
}
322
296
else {
323
- last_error = I2C_ERROR_MISSING_WRITE ;
297
+ last_error = I2C_ERROR_NO_BEGIN ;
324
298
return 0 ;
325
299
}
326
300
}
@@ -335,9 +309,8 @@ if(transmitting==1){
335
309
336
310
if (last_error == I2C_ERROR_OK){
337
311
if (sendStop){
338
- last_error=i2cProcQueue (i2c);
339
- txQueued = 0 ;
340
- i2cFreeQueue (i2c);
312
+ uint16_t dummy;
313
+ last_error = processQueue (&dummy);
341
314
}
342
315
else { // queued because it had sendStop==false
343
316
// txlength is howmany bytes in txbufferhave been use
@@ -348,8 +321,7 @@ if(transmitting==1){
348
321
}
349
322
else {
350
323
last_error= I2C_ERROR_NO_BEGIN;
351
- txQueued = 0 ;
352
- i2cFreeQueue (i2c); // cleanup
324
+ flush ();
353
325
}
354
326
txIndex = 0 ;
355
327
txLength =0 ;
@@ -398,6 +370,9 @@ void TwoWire::beginTransmission(uint8_t address)
398
370
txAddress = address;
399
371
txIndex = txQueued; // allow multiple beginTransmission(),write(),endTransmission(false) until endTransmission(true)
400
372
txLength = txQueued;
373
+ if (txLength!=0 )
374
+ log_e (" multiple beginTransmissions starting at %d" ,txQueued);
375
+
401
376
}
402
377
403
378
void TwoWire::beginTransmission (int address)
0 commit comments