- TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
+ TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
+
+ #ifdef I2C_TIMEOUT
+ uint16_t timeout_timer = timer_read();
+ while( !(TWCR & (1<<TWINT)) ) {
+ if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
+ return 2; // should make these codes standard
+ }
+ }
+ #else
+ // wait for end of transmission
+ while( !(TWCR & (1<<TWINT)) );
+ #endif
+