#include <util/twi.h>
#include "i2c_master.h"
+#include "timer.h"
#define F_SCL 400000UL // SCL frequency
#define Prescaler 1
TWCR = 0;
// transmit START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
+
+ #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
// check if the start condition was successfully transmitted
if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return 1; }
TWDR = address;
// start transmission of address
TWCR = (1<<TWINT) | (1<<TWEN);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
+
+ #ifdef I2C_TIMEOUT
+ 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
// check if the device has acknowledged the READ / WRITE mode
uint8_t twst = TW_STATUS & 0xF8;
TWDR = data;
// start transmission of data
TWCR = (1<<TWINT) | (1<<TWEN);
+
+ #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)) );
+ while( !(TWCR & (1<<TWINT)) );
+ #endif
if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
// start TWI module and acknowledge data after reception
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
+
+ #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
+
// return received data from TWDR
return TWDR;
}
// start receiving without acknowledging reception
TWCR = (1<<TWINT) | (1<<TWEN);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
+
+ #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
+
// return received data from TWDR
return TWDR;
}
return 0;
}
-void i2c_stop(void)
+uint8_t i2c_stop(void)
{
// transmit STOP condition
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
- // wait until stop condition is executed and bus released
- while(TWCR & (1<<TWSTO));
+
+ #ifdef I2C_TIMEOUT
+ uint16_t timeout_timer = timer_read();
+ while(TWCR & (1<<TWSTO)) {
+ if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
+ return 2; // should make these codes standard
+ }
+ }
+ #else
+ // wait for end of transmission
+ while(TWCR & (1<<TWSTO));
+ #endif
+
+ return 0;
}