@@ -142,74 +142,64 @@ size_t TwoWire::oldRequestFrom(uint8_t address, size_t size, bool sendStop)
142142 rxLength = read;
143143 return read;
144144}
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+
145175
146176/* @stickBreaker 11/2017 fix for ReSTART timeout, ISR
147177*/
148178size_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
149180
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
152183 if ((size+cnt)>I2C_BUFFER_LENGTH)
153184 size = (I2C_BUFFER_LENGTH-cnt);
154185 }
155186 else { // no room to receive more!
156- log_e (" no room %d" ,cnt);
187+ log_e (" rxBuff overflow %d" ,cnt+size );
157188 cnt = 0 ;
158- rxIndex = 0 ;
159- rxLength = 0 ;
160- rxQueued = 0 ;
161189 last_error = I2C_ERROR_MEMORY;
162- i2cFreeQueue (i2c );
190+ flush ( );
163191 return cnt;
164192 }
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+ }
188196
189197size_t TwoWire::requestFrom (uint8_t address, uint8_t * readBuff, size_t size, bool sendStop){
190- size_t cnt=0 ;
198+ uint16_t cnt=0 ;
191199 last_error =i2cAddQueueRead (i2c,address,readBuff,size,sendStop,NULL );
192200 if (last_error==I2C_ERROR_OK){ // successfully queued the read
193201 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);
213203 }
214204 else { // stop not received, so wait for I2C stop,
215205 last_error=I2C_ERROR_CONTINUE;
@@ -229,24 +219,8 @@ last_error=i2cAddQueueWrite(i2c,address,buff,size,sendStop,NULL);
229219
230220if (last_error==I2C_ERROR_OK){ // queued
231221 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);
250224 }
251225 else { // stop not received, so wait for I2C stop,
252226 last_error=I2C_ERROR_CONTINUE;
@@ -302,7 +276,7 @@ if(last_error==I2C_ERROR_CONTINUE){ // must have queued the Write
302276 return cnt;
303277 }
304278else {
305- last_error = I2C_ERROR_MISSING_WRITE ;
279+ last_error = I2C_ERROR_NO_BEGIN ;
306280 return 0 ;
307281 }
308282}
@@ -320,7 +294,7 @@ if(last_error==I2C_ERROR_CONTINUE){ // must have queued the write
320294 return cnt;
321295 }
322296else {
323- last_error = I2C_ERROR_MISSING_WRITE ;
297+ last_error = I2C_ERROR_NO_BEGIN ;
324298 return 0 ;
325299 }
326300}
@@ -335,9 +309,8 @@ if(transmitting==1){
335309
336310 if (last_error == I2C_ERROR_OK){
337311 if (sendStop){
338- last_error=i2cProcQueue (i2c);
339- txQueued = 0 ;
340- i2cFreeQueue (i2c);
312+ uint16_t dummy;
313+ last_error = processQueue (&dummy);
341314 }
342315 else { // queued because it had sendStop==false
343316 // txlength is howmany bytes in txbufferhave been use
@@ -348,8 +321,7 @@ if(transmitting==1){
348321 }
349322else {
350323 last_error= I2C_ERROR_NO_BEGIN;
351- txQueued = 0 ;
352- i2cFreeQueue (i2c); // cleanup
324+ flush ();
353325 }
354326txIndex = 0 ;
355327txLength =0 ;
@@ -398,6 +370,9 @@ void TwoWire::beginTransmission(uint8_t address)
398370 txAddress = address;
399371 txIndex = txQueued; // allow multiple beginTransmission(),write(),endTransmission(false) until endTransmission(true)
400372 txLength = txQueued;
373+ if (txLength!=0 )
374+ log_e (" multiple beginTransmissions starting at %d" ,txQueued);
375+
401376}
402377
403378void TwoWire::beginTransmission (int address)
0 commit comments