annotate onewire.c @ 14:c17f2e4a7a8b

merge
author Matt Johnston <matt@ucc.asn.au>
date Wed, 12 Jun 2013 23:49:27 +0800
parents 8705acff2494
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 Access Dallas 1-Wire Devices with ATMEL AVRs
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 Author of the initial code: Peter Dannegger (danni(at)specs.de)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 modified by Martin Thomas (mthomas(at)rhrk.uni-kl.de)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 9/2004 - use of delay.h, optional bus configuration at runtime
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 10/2009 - additional delay in ow_bit_io for recovery
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 5/2010 - timing modifcations, additonal config-values and comments,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 use of atomic.h macros, internal pull-up support
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 7/2010 - added method to skip recovery time after last bit transfered
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 via ow_command_skip_last_recovery
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 */
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 #include <avr/io.h>
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 #include <util/delay.h>
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #include <util/atomic.h>
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #include "onewire.h"
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 #ifdef OW_ONE_BUS
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 #define OW_GET_IN() ( OW_IN & (1<<OW_PIN))
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 #define OW_OUT_LOW() ( OW_OUT &= (~(1 << OW_PIN)) )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 #define OW_OUT_HIGH() ( OW_OUT |= (1 << OW_PIN) )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #define OW_DIR_IN() ( OW_DDR &= (~(1 << OW_PIN )) )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #define OW_DIR_OUT() ( OW_DDR |= (1 << OW_PIN) )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #else
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 /* set bus-config with ow_set_bus() */
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 uint8_t OW_PIN_MASK;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 volatile uint8_t* OW_IN;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 volatile uint8_t* OW_OUT;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 volatile uint8_t* OW_DDR;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #define OW_GET_IN() ( *OW_IN & OW_PIN_MASK )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #define OW_OUT_LOW() ( *OW_OUT &= (uint8_t) ~OW_PIN_MASK )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #define OW_OUT_HIGH() ( *OW_OUT |= (uint8_t) OW_PIN_MASK )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 #define OW_DIR_IN() ( *OW_DDR &= (uint8_t) ~OW_PIN_MASK )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 #define OW_DIR_OUT() ( *OW_DDR |= (uint8_t) OW_PIN_MASK )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 void ow_set_bus(volatile uint8_t* in,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 volatile uint8_t* out,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 volatile uint8_t* ddr,
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 uint8_t pin)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 OW_DDR=ddr;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 OW_OUT=out;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 OW_IN=in;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 OW_PIN_MASK = (1 << pin);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 ow_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 uint8_t ow_input_pin_state()
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 return OW_GET_IN();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 void ow_parasite_enable(void)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 OW_OUT_HIGH();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 OW_DIR_OUT();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 void ow_parasite_disable(void)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 OW_DIR_IN();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #if (!OW_USE_INTERNAL_PULLUP)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 OW_OUT_LOW();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 uint8_t ow_reset(void)
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 uint8_t err;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 OW_OUT_LOW();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 OW_DIR_OUT(); // pull OW-Pin low for 480us
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 _delay_us(480);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 // set Pin as input - wait for clients to pull low
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 OW_DIR_IN(); // input
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #if OW_USE_INTERNAL_PULLUP
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 OW_OUT_HIGH();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 _delay_us(64); // was 66
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 err = OW_GET_IN(); // no presence detect
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 // if err!=0: nobody pulled to low, still high
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 // after a delay the clients should release the line
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 // and input-pin gets back to high by pull-up-resistor
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 _delay_us(480 - 64); // was 480-66
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 if( OW_GET_IN() == 0 ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 err = 1; // short circuit, expected low but got high
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 return err;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 /* Timing issue when using runtime-bus-selection (!OW_ONE_BUS):
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 The master should sample at the end of the 15-slot after initiating
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 the read-time-slot. The variable bus-settings need more
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 cycles than the constant ones so the delays had to be shortened
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 to achive a 15uS overall delay
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 Setting/clearing a bit in I/O Register needs 1 cyle in OW_ONE_BUS
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 but around 14 cyles in configureable bus (us-Delay is 4 cyles per uS) */
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 static uint8_t ow_bit_io_intern( uint8_t b, uint8_t with_parasite_enable )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #if OW_USE_INTERNAL_PULLUP
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 OW_OUT_LOW();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 OW_DIR_OUT(); // drive bus low
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 _delay_us(2); // T_INT > 1usec accoding to timing-diagramm
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 if ( b ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 OW_DIR_IN(); // to write "1" release bus, resistor pulls high
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #if OW_USE_INTERNAL_PULLUP
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 OW_OUT_HIGH();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 // "Output data from the DS18B20 is valid for 15usec after the falling
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 // edge that initiated the read time slot. Therefore, the master must
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 // release the bus and then sample the bus state within 15ussec from
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 // the start of the slot."
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 _delay_us(15-2-OW_CONF_DELAYOFFSET);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 if( OW_GET_IN() == 0 ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 b = 0; // sample at end of read-timeslot
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 _delay_us(60-15-2+OW_CONF_DELAYOFFSET);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 #if OW_USE_INTERNAL_PULLUP
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 OW_OUT_HIGH();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 #endif
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 OW_DIR_IN();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 if ( with_parasite_enable ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 ow_parasite_enable();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 } /* ATOMIC_BLOCK */
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 _delay_us(OW_RECOVERY_TIME); // may be increased for longer wires
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 return b;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 uint8_t ow_bit_io( uint8_t b )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 return ow_bit_io_intern( b & 1, 0 );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 uint8_t ow_byte_wr( uint8_t b )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 uint8_t i = 8, j;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 do {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 j = ow_bit_io( b & 1 );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 b >>= 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 if( j ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 b |= 0x80;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 } while( --i );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 return b;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 uint8_t ow_byte_wr_with_parasite_enable( uint8_t b )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 uint8_t i = 8, j;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 do {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 if ( i != 1 ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 j = ow_bit_io_intern( b & 1, 0 );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 } else {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 j = ow_bit_io_intern( b & 1, 1 );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 b >>= 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 if( j ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 b |= 0x80;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 } while( --i );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 return b;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 uint8_t ow_byte_rd( void )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 // read by sending only "1"s, so bus gets released
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 // after the init low-pulse in every slot
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 return ow_byte_wr( 0xFF );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 uint8_t ow_rom_search( uint8_t diff, uint8_t *id )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 uint8_t i, j, next_diff;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 uint8_t b;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 if( ow_reset() ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 return OW_PRESENCE_ERR; // error, no device found <--- early exit!
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 ow_byte_wr( OW_SEARCH_ROM ); // ROM search command
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 next_diff = OW_LAST_DEVICE; // unchanged on last device
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 i = OW_ROMCODE_SIZE * 8; // 8 bytes
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 do {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 j = 8; // 8 bits
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 do {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 b = ow_bit_io( 1 ); // read bit
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 if( ow_bit_io( 1 ) ) { // read complement bit
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 if( b ) { // 0b11
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 return OW_DATA_ERR; // data error <--- early exit!
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 else {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 if( !b ) { // 0b00 = 2 devices
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 if( diff > i || ((*id & 1) && diff != i) ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 b = 1; // now 1
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 next_diff = i; // next pass 0
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 ow_bit_io( b ); // write bit
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 *id >>= 1;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 if( b ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 *id |= 0x80; // store bit
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 i--;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 } while( --j );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 id++; // next byte
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 } while( i );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 return next_diff; // to continue search
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 static void ow_command_intern( uint8_t command, uint8_t *id, uint8_t with_parasite_enable )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 uint8_t i;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 ow_reset();
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 if( id ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 ow_byte_wr( OW_MATCH_ROM ); // to a single device
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 i = OW_ROMCODE_SIZE;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 do {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 ow_byte_wr( *id );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 id++;
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 } while( --i );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 else {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 ow_byte_wr( OW_SKIP_ROM ); // to all devices
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 if ( with_parasite_enable ) {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 ow_byte_wr_with_parasite_enable( command );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 } else {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 ow_byte_wr( command );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 void ow_command( uint8_t command, uint8_t *id )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 ow_command_intern( command, id, 0);
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 void ow_command_with_parasite_enable( uint8_t command, uint8_t *id )
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 {
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 ow_command_intern( command, id, 1 );
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 }
8705acff2494 lots of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287