diff 1wire.c @ 8:f9a085a0ba93

Change the 1 wire routines to mostly C with assembly delay routines for ease of portability.
author darius
date Mon, 12 Jul 2004 17:50:42 +0930
parents ffeab3c04e83
children eb1faf51968e
line wrap: on
line diff
--- a/1wire.c	Mon Jul 12 16:49:21 2004 +0930
+++ b/1wire.c	Mon Jul 12 17:50:42 2004 +0930
@@ -2,6 +2,8 @@
  * Various 1 wire routines
  * 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.
  *
@@ -32,6 +34,7 @@
 #include <avr/pgmspace.h>
 
 #include "1wire.h"
+#include "1wire-delay.h"
 
 void		uart_putsP(const char *addr);
 void		uart_puts(const char *addr);
@@ -137,61 +140,17 @@
 OWWriteBit(int bit) {
     OWdelay();
     if (bit) {
-	asm volatile (
-	    /* Drive bus low */
-	    "cbi %[out], %[pin]\n\t"
-	    "sbi %[ddr], %[pin]\n\t"
-	    /* Delay A (6 usec) */
-	    "ldi r21, 1\n\t"
-	    "loopA:\n\t"
-	    "nop\n\t"
-	    "dec r21\n\t"
-	    "brne loopA\n\t"
-	    /* Release bus */
-	    "cbi %[ddr], %[pin]\n\t"
-	    /* Delay B (64 usec) */
-	    "ldi r21, 32\n\t"
-	    "loopB:\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "dec r21\n\t"
-	    "brne loopB\n\t"
-	    :						/* Outputs */
-	    : [out] "I" (_SFR_IO_ADDR(OWIREOUTPORT)),	/* Inputs */
-	      [ddr] "I" (_SFR_IO_ADDR(OWIREDDR)),
-	      [pin] "I" (OWIREOUTPIN)
-	    : "r21");					/* Clobbers */
+	OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
+	OWIREDDR |= _BV(OWIREOUTPIN);
+	DELAY_A;
+	OWIREDDR &= ~(_BV(OWIREOUTPIN));
+	DELAY_B;
     } else {
-	asm volatile (
-	    /* Drive bus low */
-	    "cbi %[out], %[pin]\n\t"
-	    "sbi %[ddr], %[pin]\n\t"
-	    /* Delay C (60 usec) */
-	    "ldi r21, 30\n\t"
-	    "loopC:\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "nop\n\t"
-	    "dec r21\n\t"
-	    "brne loopC\n\t"
-	    /* Release bus */
-	    "cbi %[ddr], %[pin]\n\t"
-	    /* Delay D (10 usec) */
-	    "ldi r21, 9\n\t"
-	    "loopD:\n\t"
-	    "nop\n\t"
-	    "dec r21\n\t"
-	    "brne loopD\n\t"
-	    :						/* Outputs */
-	    : [out] "I" (_SFR_IO_ADDR(OWIREOUTPORT)),	/* Inputs */
-	      [ddr] "I" (_SFR_IO_ADDR(OWIREDDR)),
-	      [pin] "I" (OWIREOUTPIN)
-	    : "r21");					/* Clobbers */
+	OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
+	OWIREDDR |= _BV(OWIREOUTPIN);
+	DELAY_C;
+	OWIREDDR &= ~(_BV(OWIREOUTPIN));
+	DELAY_D;
     }
 }
 
@@ -200,60 +159,14 @@
  */
 int
 OWReadBit(void) {
-    uint8_t	result;
-
     OWdelay();
-    
-    asm volatile (
-	/* Drive bus low */
-	"cbi %[out], %[opin]\n\t"
-	"sbi %[ddr], %[opin]\n\t"
-	/* Delay A (6 usec) */
-	"ldi r21, 1\n\t"
-	"loopA1:\n\t"
-	"dec r21\n\t"
-	"brne loopA1\n\t"
-	/* Release bus */
-	"cbi %[ddr], %[opin]\n\t"
-	/* Delay E (9 usec) */
-	"ldi r21, 8\n\t"
-	"loopE:\n\t"
-	"nop\n\t"
-	"dec r21\n\t"
-	"brne loopE\n\t"
-	/* Sample */
-	"ldi %[res], 0\n\t"
-	"sbic %[in], %[ipin]\n\t"
-	"ldi %[res], 1\n\t"
 
-	/* Delay F (55 usec) */
-	"ldi r21, 27\n\t"
-	"loopF:\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"nop\n\t"
-	"dec r21\n\t"
-	"brne loopF\n\t"
-
-	: [res]  "=r" (result) 				/* Outputs */
-	: [out]  "I" (_SFR_IO_ADDR(OWIREOUTPORT)),	/* Inputs */
-	  [ddr]  "I" (_SFR_IO_ADDR(OWIREDDR)),
-	  [opin] "I" (OWIREOUTPIN),
-	  [in]   "I" (_SFR_IO_ADDR(OWIREINPORT)),
-	  [ipin] "I" (OWIREINPIN)	
-	: "r21");					/* Clobbers */
-
-    return(result);
+    OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
+    OWIREDDR |= _BV(OWIREOUTPIN);
+    DELAY_A;
+    OWIREDDR &= ~(_BV(OWIREOUTPIN));
+    DELAY_E;
+    return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0);
 }
 
 /*-----------------------------------------------------------------------------