diff ds1307.c @ 8:119688bb743f

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.
author darius@dons.net.au
date Fri, 01 May 2009 15:21:31 +0930
parents 3da232f97e81
children b5e4591b6570
line wrap: on
line diff
--- 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