# HG changeset patch # User darius@dons.net.au # Date 1241157091 -34200 # Node ID 119688bb743ff96b45c93812dabf82194593685e # Parent d111f64eb619b823adb011c74a6b13158bfa8af5 Don't spin forever waiting for the TWI hardware to do something. I _think_ this will help the case where I find the micro is hanging but I haven't seen an error from it yet. diff -r d111f64eb619 -r 119688bb743f ds1307.c --- a/ds1307.c Mon Apr 20 14:05:20 2009 +0800 +++ b/ds1307.c Fri May 01 15:21:31 2009 +0930 @@ -38,6 +38,16 @@ // #define TWDEBUG +/* Helper code to wait for the TWCR to do something */ +#define WAITFORTWINT() do { \ + uint8_t i; \ + for (i = 0; i < 255 && (TWCR & _BV(TWINT)) == 0; i++) \ + _delay_ms(1); \ + if (i == 255) \ + return(IIC_NORESP); \ + } while (0) + + /* * ds1307_init * @@ -87,8 +97,8 @@ TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* Spin waiting for START to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); + switch (twst = TW_STATUS) { case TW_REP_START: /* OK but shouldn't happen */ case TW_START: @@ -110,8 +120,7 @@ TWCR = _BV(TWINT) | _BV(TWEN); /* Spin waiting for a response to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); #ifdef TWDEBUG printf_P(PSTR("Sent SLA+W\r\n")); @@ -139,8 +148,8 @@ TWCR = _BV(TWINT) | _BV(TWEN); /* Spin waiting for a response to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); + #ifdef TWDEBUG printf_P(PSTR("Sent address\r\n")); #endif @@ -164,8 +173,9 @@ /* Master receive cycle */ TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); - while ((TWCR & _BV(TWINT)) == 0) /* wait for transmission */ - ; + + /* wait for transmission */ + WAITFORTWINT(); #ifdef TWDEBUG printf_P(PSTR("Sent START\r\n")); @@ -189,8 +199,8 @@ TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */ /* Spin waiting for a response to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); + #ifdef TWDEBUG printf_P(PSTR("Sent SLA+R\r\n")); #endif @@ -220,8 +230,8 @@ twcr = _BV(TWINT) | _BV(TWEN); TWCR = twcr; /* clear int to start transmission */ /* Spin waiting for a response to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); + #ifdef TWDEBUG printf_P(PSTR("Data request done\r\n")); #endif @@ -272,8 +282,8 @@ TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* Spin waiting for START to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); + switch (twst = TW_STATUS) { case TW_REP_START: /* OK but shouldn't happen */ case TW_START: @@ -296,8 +306,7 @@ TWCR = _BV(TWINT) | _BV(TWEN); /* Spin waiting for a response to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); #ifdef TWDEBUG printf_P(PSTR("Sent SLA+W\r\n")); @@ -325,8 +334,8 @@ TWCR = _BV(TWINT) | _BV(TWEN); /* Spin waiting for a response to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); + #ifdef TWDEBUG printf_P(PSTR("Sent address\r\n")); #endif @@ -354,8 +363,8 @@ TWCR = _BV(TWINT) | _BV(TWEN); /* Spin waiting for a response to be generated */ - while ((TWCR & _BV(TWINT)) == 0) - ; + WAITFORTWINT(); + #ifdef TWDEBUG printf_P(PSTR("Data sent\r\n")); #endif diff -r d111f64eb619 -r 119688bb743f ds1307.h --- a/ds1307.h Mon Apr 20 14:05:20 2009 +0800 +++ b/ds1307.h Fri May 01 15:21:31 2009 +0930 @@ -30,6 +30,7 @@ #define IIC_FAILARB -2 #define IIC_SLNAK -3 #define IIC_NOREPLY -4 +#define IIC_NORESP -5 #define IIC_UNKNOWN -99 #define DS1307_ADR 0xd0 // DS1307's TWI address