@@ -197,38 +197,39 @@ static bool twi_write_start(void) {
197
197
return true ;
198
198
}
199
199
200
+ static void ICACHE_RAM_ATTR twi_wait_clockStretchLimit () {
201
+ uint32_t t=0 ; while (SCL_READ ()==0 && (t++)<twi_clockStretchLimit); // twi_clockStretchLimit
202
+ }
203
+
200
204
static bool twi_write_stop (void ){
201
- uint32_t i = 0 ;
202
205
SCL_LOW ();
203
206
SDA_LOW ();
204
207
twi_delay (twi_dcount);
205
208
SCL_HIGH ();
206
- while ( SCL_READ () == 0 && (i++) < twi_clockStretchLimit); // Clock stretching
209
+ twi_wait_clockStretchLimit ();
207
210
twi_delay (twi_dcount);
208
211
SDA_HIGH ();
209
212
twi_delay (twi_dcount);
210
213
return true ;
211
214
}
212
215
213
216
static bool twi_write_bit (bool bit) {
214
- uint32_t i = 0 ;
215
217
SCL_LOW ();
216
218
if (bit) SDA_HIGH ();
217
219
else SDA_LOW ();
218
220
twi_delay (twi_dcount+1 );
219
221
SCL_HIGH ();
220
- while ( SCL_READ () == 0 && (i++) < twi_clockStretchLimit); // Clock stretching
222
+ twi_wait_clockStretchLimit ();
221
223
twi_delay (twi_dcount);
222
224
return true ;
223
225
}
224
226
225
227
static bool twi_read_bit (void ) {
226
- uint32_t i = 0 ;
227
228
SCL_LOW ();
228
229
SDA_HIGH ();
229
230
twi_delay (twi_dcount+2 );
230
231
SCL_HIGH ();
231
- while ( SCL_READ () == 0 && (i++) < twi_clockStretchLimit); // Clock stretching
232
+ twi_wait_clockStretchLimit ();
232
233
bool bit = SDA_READ ();
233
234
twi_delay (twi_dcount);
234
235
return bit;
@@ -251,6 +252,13 @@ static unsigned char twi_read_byte(bool nack) {
251
252
return byte;
252
253
}
253
254
255
+ // Generate a clock "valey" (at the end of a segment, just before a repeated start)
256
+ void twi_scl_valey ( void ) {
257
+ SCL_LOW ();
258
+ twi_delay (twi_dcount);
259
+ SCL_HIGH ();
260
+ }
261
+
254
262
unsigned char twi_writeTo (unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){
255
263
unsigned int i;
256
264
if (!twi_write_start ()) return 4 ;// line busy
@@ -265,12 +273,15 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i
265
273
}
266
274
}
267
275
if (sendStop) twi_write_stop ();
276
+ else {
277
+ twi_scl_valey ();
278
+ twi_wait_clockStretchLimit ();
279
+ // TD-er: Also twi_delay here?
280
+ // twi_delay(twi_dcount);
281
+ }
268
282
i = 0 ;
269
283
while (SDA_READ () == 0 && (i++) < 10 ){
270
- SCL_LOW ();
271
- twi_delay (twi_dcount);
272
- SCL_HIGH ();
273
- unsigned int t=0 ; while (SCL_READ ()==0 && (t++)<twi_clockStretchLimit); // twi_clockStretchLimit
284
+ twi_scl_valey ();
274
285
twi_delay (twi_dcount);
275
286
}
276
287
return 0 ;
@@ -286,12 +297,15 @@ unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned i
286
297
for (i=0 ; i<(len-1 ); i++) buf[i] = twi_read_byte (false );
287
298
buf[len-1 ] = twi_read_byte (true );
288
299
if (sendStop) twi_write_stop ();
300
+ else {
301
+ twi_scl_valey ();
302
+ twi_wait_clockStretchLimit ();
303
+ // TD-er: Also twi_delay here?
304
+ // twi_delay(twi_dcount);
305
+ }
289
306
i = 0 ;
290
307
while (SDA_READ () == 0 && (i++) < 10 ){
291
- SCL_LOW ();
292
- twi_delay (twi_dcount);
293
- SCL_HIGH ();
294
- unsigned int t=0 ; while (SCL_READ ()==0 && (t++)<twi_clockStretchLimit); // twi_clockStretchLimit
308
+ twi_scl_valey ();
295
309
twi_delay (twi_dcount);
296
310
}
297
311
return 0 ;
0 commit comments