diff 1wire.c @ 32:b0cb873c0206

Isolate the bus frobbing parts and the delays into a separate header. This means the user only has to edit a single file to suit their situation and allows the code to work with active drive systems as well as passive pullups (ie 1 vs 2 IO pins)
author darius
date Sun, 23 Apr 2006 22:57:16 +0930
parents 02b67e09ca12
children 0aa6bf4b98ae
line wrap: on
line diff
--- a/1wire.c	Mon Apr 10 17:27:48 2006 +0930
+++ b/1wire.c	Sun Apr 23 22:57:16 2006 +0930
@@ -30,12 +30,18 @@
  * SUCH DAMAGE.
  */
 
+/* 
+ * No user servicable parts inside
+ *
+ * Modify 1wire-config.h
+ */
+
 #include <stdio.h>
 #include <avr/io.h>
 #include <avr/pgmspace.h>
 #include <util/delay.h>
 #include "1wire.h"
-#include "1wire-delay.h"
+#include "1wire-config.h"
 
 #if OW_DEBUG
 void		uart_putsP(const char *addr);
@@ -48,9 +54,13 @@
 static uint8_t OW_LastDiscrepancy = 0;
 static uint8_t OW_LastFamilyDiscrepancy = 0;
 
-static void
-OWdelay(void) {
-    DELAY_I;
+/*-----------------------------------------------------------------------------
+ * Configure the IO port as we need
+ */
+void
+OWInit(void) {
+    OWBUSINIT();
+    OWSETBUSHIGH();
 }
 
 /*-----------------------------------------------------------------------------
@@ -61,18 +71,21 @@
  */
 uint8_t
 OWTouchReset(void) {
-    DELAY_G;
+    OWDELAY_G;
 
-    if (!(OWIREINPORT & _BV(OWIREINPIN)))
+    /* Check the bus isn't being held low (ie it's broken) Do it after
+     * the delay so we guarantee we don't see a slave from a previous
+     * comms attempt
+     */
+    if(OWREADBUS() == 0)
 	return 2;
-    
-    OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
-    OWIREDDR |= _BV(OWIREOUTPIN);
-    DELAY_H;
-    OWIREDDR &= ~(_BV(OWIREOUTPIN));
-    DELAY_I;
 
-    return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0);
+    OWSETBUSLOW();
+    OWDELAY_H;
+    OWSETBUSHIGH();
+    OWDELAY_I;
+
+    return(OWREADBUS());
 }
 
 /*-----------------------------------------------------------------------------
@@ -80,19 +93,18 @@
  */
 void
 OWWriteBit(uint8_t bit) {
-    OWdelay();
+    OWDELAY_I;
+    
     if (bit) {
-	OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
-	OWIREDDR |= _BV(OWIREOUTPIN);
-	DELAY_A;
-	OWIREDDR &= ~(_BV(OWIREOUTPIN));
-	DELAY_B;
+	OWSETBUSLOW();
+	OWDELAY_A;
+	OWSETBUSHIGH();
+	OWDELAY_B;
     } else {
-	OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
-	OWIREDDR |= _BV(OWIREOUTPIN);
-	DELAY_C;
-	OWIREDDR &= ~(_BV(OWIREOUTPIN));
-	DELAY_D;
+	OWSETBUSLOW();
+	OWDELAY_C;
+	OWSETBUSHIGH();
+	OWDELAY_D;
     }
 }
 
@@ -101,14 +113,13 @@
  */
 uint8_t
 OWReadBit(void) {
-    OWdelay();
-
-    OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
-    OWIREDDR |= _BV(OWIREOUTPIN);
-    DELAY_A;
-    OWIREDDR &= ~(_BV(OWIREOUTPIN));
-    DELAY_E;
-    return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0);
+    OWDELAY_I;
+    
+    OWSETBUSLOW();
+    OWDELAY_A;
+    OWSETBUSHIGH();
+    OWDELAY_E;
+    return(OWREADBUS());
 }
 
 /*-----------------------------------------------------------------------------
@@ -192,7 +203,6 @@
 	for (i = 0; i < 8; i++)
 	    OWWriteByte(ROM[i]);
     }
-
     OWWriteByte(cmd);
 }
 
@@ -277,10 +287,10 @@
 	    OWWriteByte(OW_SEARCH_ROM_CMD);	/* issue the search command */
 
 	/* pause before beginning the search */
-	OWdelay();
-	OWdelay();
-	OWdelay();
-      
+	OWDELAY_I;
+	OWDELAY_I;
+	OWDELAY_I;
+	
 	/* loop to do the search */
 	do {
 	    /* read a bit and its compliment */