Skip to content

Commit 02dcc6e

Browse files
sandeepmistrycmaglie
authored andcommitted
check TWWC (Write Collision Flag) bit after setting TWDR
as suggested by @earlyprogrammer in #2173, to ensure TWDR value is set if there is a write collision
1 parent 48bcef5 commit 02dcc6e

File tree

1 file changed

+6
-2
lines changed
  • libraries/Wire/utility

1 file changed

+6
-2
lines changed

libraries/Wire/utility/twi.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
167167
// up. Also, don't enable the START interrupt. There may be one pending from the
168168
// repeated start that we sent outselves, and that would really confuse things.
169169
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
170-
TWDR = twi_slarw;
170+
do {
171+
TWDR = twi_slarw;
172+
} while(TWCR & _BV(TWWC));
171173
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
172174
}
173175
else
@@ -247,7 +249,9 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
247249
// up. Also, don't enable the START interrupt. There may be one pending from the
248250
// repeated start that we sent outselves, and that would really confuse things.
249251
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
250-
TWDR = twi_slarw;
252+
do {
253+
TWDR = twi_slarw;
254+
} while(TWCR & _BV(TWWC));
251255
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
252256
}
253257
else

0 commit comments

Comments
 (0)