changeset 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 4e417d84365e
children 0aa6bf4b98ae
files 1wire-config.h 1wire-delay.h 1wire.c 1wire.h
diffstat 4 files changed, 128 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/1wire-config.h	Sun Apr 23 22:57:16 2006 +0930
@@ -0,0 +1,72 @@
+/*
+ * 1 wire header
+ *
+ * This is the user servicable stuff - how to do delays and how to
+ * frob the IO pins.
+ *
+ * $Id$
+ *
+ * Copyright (c) 2004
+ *      Daniel O'Connor <darius@dons.net.au>.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Alter these for your configuration
+ */
+
+/* Set DDR on the right pins */
+#define OWBUSINIT()		do {			\
+	                            DDRD |= _BV(4);	\
+				    DDRD &= ~_BV(3);	\
+                                } while (0)
+
+/* Read the 1-wire bus, non-inverting logic */
+#define OWREADBUS()		(PIND & _BV(3) ? 1 : 0)
+
+/* Set the 1-wire bus to 0
+ * Turns a transistor on to pull it low
+ */
+#define OWSETBUSLOW()		PORTD |= _BV(4)
+
+/* Set the 1-wire bus to 1
+ * Turn the transistor off to let the pullup do its job
+ */
+#define OWSETBUSHIGH()		PORTD &= ~_BV(4)
+
+/* _delay_us can only do a delay of 768/clock_freq */
+#if F_CPU > 16000000
+#error F_CPU > 16MHz, delays need adjusting
+#endif
+
+#define OWDELAY_A _delay_us(6)						/* 6 usec */
+#define OWDELAY_B do { _delay_us(48); _delay_us(16); } while (0)	/* 64 usec */
+#define OWDELAY_C do { _delay_us(48); _delay_us(12); } while (0)	/* 60 usec */
+#define OWDELAY_D _delay_us(10)						/* 10 usec */
+#define OWDELAY_E _delay_us(9)						/* 9 usec */
+#define OWDELAY_F do { _delay_us(55); } while (0)			/* 55 usec */
+#define OWDELAY_G							/* 0 usec */
+#define OWDELAY_H do { _delay_us(48); _delay_us(48);  _delay_us(48); 	\
+	_delay_us(48);	_delay_us(48);  _delay_us(48);  _delay_us(48);  \
+	_delay_us(48);_delay_us(48);  _delay_us(48); } while (0)	/* 480 usec */
+#define OWDELAY_I do { _delay_us(48); _delay_us(22); } while (0)	/* 70 usec */
--- a/1wire-delay.h	Mon Apr 10 17:27:48 2006 +0930
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-/*
- * Delay routines for the 1 wire bus
- * Search routine is copied from the Dallas owpd library with mods.
- *
- * $Id$
- *
- * Copyright (c) 2004
- *      Daniel O'Connor <darius@dons.net.au>.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#define DELAY_A _delay_us(6)						/* 6 usec */
-#define DELAY_B _delay_us(30); _delay_us(30); _delay_us(4)		/* 64 usec */
-#define DELAY_C _delay_us(30); _delay_us(30)				/* 60 usec */
-#define DELAY_D _delay_us(10)						/* 10 usec */
-#define DELAY_E _delay_us(9)						/* 9 usec */
-#define DELAY_F _delay_us(25); _delay_us(30);				/* 55 usec */
-#define DELAY_G								/* 0 usec */
-#define DELAY_H _delay_us(30); _delay_us(30);  _delay_us(30);  _delay_us(30); \
-     _delay_us(30);  _delay_us(30);  _delay_us(30);  _delay_us(30); \
-     _delay_us(30);  _delay_us(30);  _delay_us(30);  _delay_us(30); \
-    _delay_us(30);  _delay_us(30);  _delay_us(30);  _delay_us(30)	/* 480 usec */
-#define DELAY_I _delay_us(30); _delay_us(30);_delay_us(10)		/* 70 usec */
--- 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 */
--- a/1wire.h	Mon Apr 10 17:27:48 2006 +0930
+++ b/1wire.h	Sun Apr 23 22:57:16 2006 +0930
@@ -1,5 +1,5 @@
 /*
- * 1 wire header code
+ * 1 wire header which defines functions and constants
  *
  * $Id$
  *
@@ -28,6 +28,7 @@
  * SUCH DAMAGE.
  */
 
+void		OWInit(void);
 uint8_t		OWTouchReset(void);
 void		OWWriteBit(uint8_t bit);
 uint8_t		OWReadBit(void);
@@ -40,13 +41,6 @@
 void		OWCRC(uint8_t x, uint8_t *crc);
 void		OWSendCmd(uint8_t *ROM, uint8_t cmd);
 
-#define OWIREOUTPORT		PORTD
-#define OWIREOUTPIN		2
-
-#define OWIREINPORT		PIND
-#define OWIREDDR		DDRD
-#define OWIREINPIN		OWIREOUTPIN
-
 /* Return codes for OWFirst()/OWNext() */
 #define OW_BADWIRE		-3
 #define OW_BADCRC		-2
@@ -70,6 +64,13 @@
 #define OW_RECALL_CMD		0xb8
 #define OW_RD_PSU_CMD		0xb4
 
+/* DS2502 commands */
+#define OW_READ_MEMORY		0xf0
+#define OW_READ_STATUS		0xaa
+#define OW_GEN_CRC		0xc3
+#define OW_WRITE_MEMORY		0x0f
+#define OW_WRITE_STATUS		0x55
+
 /* Family codes */
 #define OW_FAMILY_ROM		0x09
 #define OW_FAMILY_TEMP		0x10