Skip to content

Commit dd6a854

Browse files
Replace clock stretch repeated code w/inline loop
There were multiple places where the code was waiting for a slave to finish stretching the clock. Factor them out to an *inline* function to reduce code smell.
1 parent f67ddf5 commit dd6a854

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

cores/esp8266/core_esp8266_si2c.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ class Twi
116116
{
117117
return (GPI & (1 << twi_scl)) != 0;
118118
}
119+
// Handle the case where a slave needs to stretch the clock with a time-limited busy wait
120+
inline void WAIT_CLOCK_STRETCH()
121+
{
122+
for (unsigned int t = 0; !SCL_READ() && (t < twi_clockStretchLimit); t++) {
123+
/* noop */
124+
}
125+
}
119126

120127
public:
121128
void setClock(unsigned int freq);
@@ -145,6 +152,7 @@ static Twi twi;
145152
#define TWI_CLOCK_STRETCH_MULTIPLIER 6
146153
#endif
147154

155+
148156
void Twi::setClock(unsigned int freq)
149157
{
150158
preferred_si2c_clock = freq;
@@ -272,12 +280,11 @@ bool Twi::write_start(void)
272280

273281
bool Twi::write_stop(void)
274282
{
275-
uint32_t i = 0;
276283
SCL_LOW();
277284
SDA_LOW();
278285
busywait(twi_dcount);
279286
SCL_HIGH();
280-
while (!SCL_READ() && (i++) < twi_clockStretchLimit); // Clock stretching
287+
WAIT_CLOCK_STRETCH();
281288
busywait(twi_dcount);
282289
SDA_HIGH();
283290
busywait(twi_dcount);
@@ -286,7 +293,6 @@ bool Twi::write_stop(void)
286293

287294
bool Twi::write_bit(bool bit)
288295
{
289-
uint32_t i = 0;
290296
SCL_LOW();
291297
if (bit)
292298
{
@@ -298,19 +304,18 @@ bool Twi::write_bit(bool bit)
298304
}
299305
busywait(twi_dcount + 1);
300306
SCL_HIGH();
301-
while (!SCL_READ() && (i++) < twi_clockStretchLimit);// Clock stretching
307+
WAIT_CLOCK_STRETCH();
302308
busywait(twi_dcount);
303309
return true;
304310
}
305311

306312
bool Twi::read_bit(void)
307313
{
308-
uint32_t i = 0;
309314
SCL_LOW();
310315
SDA_HIGH();
311316
busywait(twi_dcount + 2);
312317
SCL_HIGH();
313-
while (!SCL_READ() && (i++) < twi_clockStretchLimit);// Clock stretching
318+
WAIT_CLOCK_STRETCH();
314319
bool bit = SDA_READ();
315320
busywait(twi_dcount);
316321
return bit;
@@ -375,7 +380,7 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned
375380
SCL_LOW();
376381
busywait(twi_dcount);
377382
SCL_HIGH();
378-
unsigned int t = 0; while (!SCL_READ() && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit
383+
WAIT_CLOCK_STRETCH();
379384
busywait(twi_dcount);
380385
}
381386
return 0;
@@ -411,7 +416,7 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
411416
SCL_LOW();
412417
busywait(twi_dcount);
413418
SCL_HIGH();
414-
unsigned int t = 0; while (!SCL_READ() && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit
419+
WAIT_CLOCK_STRETCH();
415420
busywait(twi_dcount);
416421
}
417422
return 0;

0 commit comments

Comments
 (0)