Skip to content

Commit 36821cf

Browse files
committed
all while loops are now protected by timeouts
1 parent 68fe5f1 commit 36821cf

File tree

1 file changed

+11
-1
lines changed
  • libraries/Wire/src/utility

1 file changed

+11
-1
lines changed

libraries/Wire/src/utility/twi.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
189189
// up. Also, don't enable the START interrupt. There may be one pending from the
190190
// repeated start that we sent ourselves, and that would really confuse things.
191191
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
192+
startMicros = micros();
192193
do {
193194
TWDR = twi_slarw;
195+
if((twi_timeout_us > 0ul) && (micros() - startMicros > twi_timeout_us)) {
196+
twi_handleTimeout();
197+
return 0;
198+
}
194199
} while(TWCR & _BV(TWWC));
195200
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
196201
}
@@ -281,8 +286,13 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
281286
// up. Also, don't enable the START interrupt. There may be one pending from the
282287
// repeated start that we sent outselves, and that would really confuse things.
283288
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
289+
startMicros = micros();
284290
do {
285-
TWDR = twi_slarw;
291+
TWDR = twi_slarw;
292+
if((twi_timeout_us > 0ul) && (micros() - startMicros > twi_timeout_us)) {
293+
twi_handleTimeout();
294+
return 4;
295+
}
286296
} while(TWCR & _BV(TWWC));
287297
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
288298
}

0 commit comments

Comments
 (0)