X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/4bd44139b1d120e73c6b8863578b0ef6996f7368..d03d6513d0d24cf63225c8d3dfa07675d9107f40:/LUFA/Drivers/Peripheral/TWI.c?ds=inline diff --git a/LUFA/Drivers/Peripheral/TWI.c b/LUFA/Drivers/Peripheral/TWI.c index 6028bbae9..9e0c20dc0 100644 --- a/LUFA/Drivers/Peripheral/TWI.c +++ b/LUFA/Drivers/Peripheral/TWI.c @@ -7,39 +7,58 @@ #include "TWI.h" -bool TWI_StartTransmission(uint8_t SlaveAddress) +bool TWI_StartTransmission(uint8_t SlaveAddress, uint8_t TimeoutMS) { for (;;) { - uint8_t IterationsRemaining = 50; - bool BusCaptured = false; + bool BusCaptured = false; + uint16_t TimeoutRemaining; - while (IterationsRemaining-- && !BusCaptured) + TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN)); + + TimeoutRemaining = (TimeoutMS * 100); + while (TimeoutRemaining-- && !(BusCaptured)) { - TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN)); - while (!(TWCR & (1 << TWINT))); - - switch (TWSR & TW_STATUS_MASK) + if (TWCR & (1 << TWINT)) { - case TW_START: - case TW_REP_START: - BusCaptured = true; - break; - case TW_MT_ARB_LOST: - continue; - default: - return false; + switch (TWSR & TW_STATUS_MASK) + { + case TW_START: + case TW_REP_START: + BusCaptured = true; + break; + case TW_MT_ARB_LOST: + TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN)); + continue; + default: + TWCR = (1 << TWEN); + return false; + } } + + _delay_us(10); } if (!(BusCaptured)) - return false; - + { + TWCR = (1 << TWEN); + return false; + } + TWDR = SlaveAddress; TWCR = ((1 << TWINT) | (1 << TWEN)); - while (!(TWCR & (1 << TWINT))); - GPIOR0 = (TWSR & TW_STATUS_MASK); + TimeoutRemaining = (TimeoutMS * 100); + while (TimeoutRemaining--) + { + if (TWCR & (1 << TWINT)) + break; + + _delay_us(10); + } + + if (!(TimeoutRemaining)) + return false; switch (TWSR & TW_STATUS_MASK) { @@ -47,8 +66,8 @@ bool TWI_StartTransmission(uint8_t SlaveAddress) case TW_MR_SLA_ACK: return true; default: - TWI_StopTransmission(); - break; - } + TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN)); + return false; + } } }