@@ -113,6 +113,12 @@ class Twi
113
113
return (GPI & (1 << twi_scl)) != 0 ;
114
114
}
115
115
116
+ // ICACHE_RAM_ATTR used to have a constant delay.
117
+ void ICACHE_RAM_ATTR twi_wait_clockStretchLimit (void );
118
+
119
+ // Generate a clock "valley" (at the end of a segment, just before a repeated start)
120
+ void twi_scl_valley (void );
121
+
116
122
public:
117
123
void setClock (unsigned int freq);
118
124
void setClockStretchLimit (uint32_t limit);
@@ -268,12 +274,11 @@ bool Twi::write_start(void)
268
274
269
275
bool Twi::write_stop (void )
270
276
{
271
- uint32_t i = 0 ;
272
277
SCL_LOW ();
273
278
SDA_LOW ();
274
279
delay (twi_dcount);
275
280
SCL_HIGH ();
276
- while ( SCL_READ () == 0 && (i++) < twi_clockStretchLimit); // Clock stretching
281
+ twi_wait_clockStretchLimit ();
277
282
delay (twi_dcount);
278
283
SDA_HIGH ();
279
284
delay (twi_dcount);
@@ -282,7 +287,6 @@ bool Twi::write_stop(void)
282
287
283
288
bool Twi::write_bit (bool bit)
284
289
{
285
- uint32_t i = 0 ;
286
290
SCL_LOW ();
287
291
if (bit)
288
292
{
@@ -294,19 +298,18 @@ bool Twi::write_bit(bool bit)
294
298
}
295
299
delay (twi_dcount + 1 );
296
300
SCL_HIGH ();
297
- while ( SCL_READ () == 0 && (i++) < twi_clockStretchLimit); // Clock stretching
301
+ twi_wait_clockStretchLimit ();
298
302
delay (twi_dcount);
299
303
return true ;
300
304
}
301
305
302
306
bool Twi::read_bit (void )
303
307
{
304
- uint32_t i = 0 ;
305
308
SCL_LOW ();
306
309
SDA_HIGH ();
307
310
delay (twi_dcount + 2 );
308
311
SCL_HIGH ();
309
- while ( SCL_READ () == 0 && (i++) < twi_clockStretchLimit); // Clock stretching
312
+ twi_wait_clockStretchLimit ();
310
313
bool bit = SDA_READ ();
311
314
delay (twi_dcount);
312
315
return bit;
@@ -364,14 +367,18 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned
364
367
if (sendStop)
365
368
{
366
369
write_stop ();
370
+ }
371
+ else
372
+ {
373
+ twi_scl_valley ();
374
+ twi_wait_clockStretchLimit ();
375
+ // TD-er: Also twi_delay here?
376
+ // delay(twi_dcount);
367
377
}
368
378
i = 0 ;
369
379
while (SDA_READ () == 0 && (i++) < 10 )
370
380
{
371
- SCL_LOW ();
372
- delay (twi_dcount);
373
- SCL_HIGH ();
374
- unsigned int t = 0 ; while (SCL_READ () == 0 && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit
381
+ twi_scl_valley ();
375
382
delay (twi_dcount);
376
383
}
377
384
return 0 ;
@@ -401,13 +408,17 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
401
408
{
402
409
write_stop ();
403
410
}
411
+ else
412
+ {
413
+ twi_scl_valley ();
414
+ twi_wait_clockStretchLimit ();
415
+ // TD-er: Also twi_delay here?
416
+ // delay(twi_dcount);
417
+ }
404
418
i = 0 ;
405
419
while (SDA_READ () == 0 && (i++) < 10 )
406
420
{
407
- SCL_LOW ();
408
- delay (twi_dcount);
409
- SCL_HIGH ();
410
- unsigned int t = 0 ; while (SCL_READ () == 0 && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit
421
+ twi_scl_valley ();
411
422
delay (twi_dcount);
412
423
}
413
424
return 0 ;
@@ -628,6 +639,19 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status)
628
639
}
629
640
}
630
641
642
+ void ICACHE_RAM_ATTR Twi::twi_wait_clockStretchLimit (void )
643
+ {
644
+ uint32_t t=0 ;
645
+ while (SCL_READ () == 0 && (t++) < twi_clockStretchLimit); // Clock stretching
646
+ }
647
+
648
+ void Twi::twi_scl_valley (void )
649
+ {
650
+ SCL_LOW ();
651
+ delay (twi_dcount);
652
+ SCL_HIGH ();
653
+ }
654
+
631
655
void ICACHE_RAM_ATTR Twi::onTimer (void *unused)
632
656
{
633
657
(void )unused;
0 commit comments