Mercurial > ~darius > hgwebdir.cgi > avr
annotate 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 |
rev | line source |
---|---|
0 | 1 /* |
2 * Various 1 wire routines | |
12 | 3 * Search routine is copied from the Dallas owpd library with mods |
4 * available from here http://www.ibutton.com/software/1wire/wirekit.html | |
0 | 5 * |
8
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
6 * $Id$ |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
7 * |
0 | 8 * Copyright (c) 2004 |
9 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. | |
10 * | |
11 * Redistribution and use in source and binary forms, with or without | |
12 * modification, are permitted provided that the following conditions | |
13 * are met: | |
14 * 1. Redistributions of source code must retain the above copyright | |
15 * notice, this list of conditions and the following disclaimer. | |
16 * 2. Redistributions in binary form must reproduce the above copyright | |
17 * notice, this list of conditions and the following disclaimer in the | |
18 * documentation and/or other materials provided with the distribution. | |
19 * | |
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE | |
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 * SUCH DAMAGE. | |
31 */ | |
32 | |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
33 /* |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
34 * No user servicable parts inside |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
35 * |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
36 * Modify 1wire-config.h |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
37 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
38 |
0 | 39 #include <stdio.h> |
40 #include <avr/io.h> | |
41 #include <avr/pgmspace.h> | |
21 | 42 #include <util/delay.h> |
0 | 43 #include "1wire.h" |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
44 #include "1wire-config.h" |
0 | 45 |
12 | 46 #if OW_DEBUG |
0 | 47 void uart_putsP(const char *addr); |
48 void uart_puts(const char *addr); | |
49 void uart_getc(); | |
21 | 50 char uart_putc(char c); |
12 | 51 #endif |
0 | 52 |
53 static uint8_t OW_LastDevice = 0; | |
54 static uint8_t OW_LastDiscrepancy = 0; | |
55 static uint8_t OW_LastFamilyDiscrepancy = 0; | |
56 | |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
57 /*----------------------------------------------------------------------------- |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
58 * Configure the IO port as we need |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
59 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
60 void |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
61 OWInit(void) { |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
62 OWBUSINIT(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
63 OWSETBUSHIGH(); |
0 | 64 } |
65 | |
66 /*----------------------------------------------------------------------------- | |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
67 * Generate a 1-Wire reset, return 0 if presence pulse was found, 1 if it |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
68 * wasn't, or 2 if the line appears to be being held low. |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
69 * |
0 | 70 * (NOTE: Does not handle alarm presence from DS2404/DS1994) |
71 */ | |
12 | 72 uint8_t |
0 | 73 OWTouchReset(void) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
74 OWDELAY_G; |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
75 |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
76 /* Check the bus isn't being held low (ie it's broken) Do it after |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
77 * the delay so we guarantee we don't see a slave from a previous |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
78 * comms attempt |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
79 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
80 if(OWREADBUS() == 0) |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
81 return 2; |
0 | 82 |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
83 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
84 OWDELAY_H; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
85 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
86 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
87 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
88 return(OWREADBUS()); |
0 | 89 } |
90 | |
91 /*----------------------------------------------------------------------------- | |
92 * Send a 1-wire write bit. | |
93 */ | |
94 void | |
12 | 95 OWWriteBit(uint8_t bit) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
96 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
97 |
0 | 98 if (bit) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
99 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
100 OWDELAY_A; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
101 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
102 OWDELAY_B; |
0 | 103 } else { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
104 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
105 OWDELAY_C; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
106 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
107 OWDELAY_D; |
0 | 108 } |
109 } | |
110 | |
111 /*----------------------------------------------------------------------------- | |
112 * Read a bit from the 1-wire bus and return it. | |
113 */ | |
12 | 114 uint8_t |
0 | 115 OWReadBit(void) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
116 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
117 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
118 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
119 OWDELAY_A; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
120 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
121 OWDELAY_E; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
122 return(OWREADBUS()); |
0 | 123 } |
124 | |
125 /*----------------------------------------------------------------------------- | |
126 * Write a byte to the 1-wire bus | |
127 */ | |
128 void | |
129 OWWriteByte(uint8_t data) { | |
12 | 130 uint8_t i; |
0 | 131 |
132 /* Send LSB first */ | |
133 for (i = 0; i < 8; i++) { | |
134 OWWriteBit(data & 0x01); | |
135 data >>= 1; | |
136 } | |
137 } | |
138 | |
139 /*----------------------------------------------------------------------------- | |
140 * Read a byte from the 1-wire bus | |
141 */ | |
12 | 142 uint8_t |
0 | 143 OWReadByte(void) { |
144 int i, result = 0; | |
145 | |
146 for (i = 0; i < 8; i++) { | |
147 result >>= 1; | |
148 if (OWReadBit()) | |
149 result |= 0x80; | |
150 } | |
151 return(result); | |
152 } | |
153 | |
154 /*----------------------------------------------------------------------------- | |
155 * Write a 1-wire data byte and return the sampled result. | |
156 */ | |
12 | 157 uint8_t |
0 | 158 OWTouchByte(uint8_t data) { |
12 | 159 uint8_t i, result = 0; |
0 | 160 |
161 for (i = 0; i < 8; i++) { | |
162 result >>= 1; | |
163 | |
164 /* If sending a 1 then read a bit, otherwise write a 0 */ | |
165 if (data & 0x01) { | |
166 if (OWReadBit()) | |
167 result |= 0x80; | |
168 } else | |
169 OWWriteBit(0); | |
170 | |
171 data >>= 1; | |
172 } | |
173 | |
174 return(result); | |
175 } | |
176 | |
177 /*----------------------------------------------------------------------------- | |
178 * Write a block of bytes to the 1-wire bus and return the sampled result in | |
179 * the same buffer | |
180 */ | |
181 void | |
182 OWBlock(uint8_t *data, int len) { | |
183 int i; | |
184 | |
185 for (i = 0; i < len; i++) | |
186 data[i] = OWTouchByte(data[i]); | |
187 } | |
188 | |
189 | |
190 /*----------------------------------------------------------------------------- | |
191 * Send a 1 wire command to a device, or all if no ROM ID provided | |
192 */ | |
193 void | |
194 OWSendCmd(uint8_t *ROM, uint8_t cmd) { | |
12 | 195 uint8_t i; |
0 | 196 |
197 OWTouchReset(); | |
198 | |
199 if (ROM == NULL) | |
200 OWWriteByte(OW_SKIP_ROM_CMD); | |
201 else { | |
202 OWWriteByte(OW_MATCH_ROM_CMD); | |
203 for (i = 0; i < 8; i++) | |
204 OWWriteByte(ROM[i]); | |
205 } | |
206 OWWriteByte(cmd); | |
207 } | |
208 | |
209 /*----------------------------------------------------------------------------- | |
210 * Search algorithm from App note 187 (and 162) | |
211 * | |
10 | 212 * OWFirst/OWNext return.. |
213 * 1 when something is found, | |
214 * 0 no more modules | |
16 | 215 * -1 if no presence pulse, |
10 | 216 * -2 if bad CRC, |
217 * -3 if bad wiring. | |
0 | 218 */ |
12 | 219 uint8_t |
0 | 220 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
221 /* Reset state */ | |
222 OW_LastDiscrepancy = 0; | |
223 OW_LastDevice = 0; | |
224 OW_LastFamilyDiscrepancy = 0; | |
225 | |
226 /* Go looking */ | |
227 return (OWNext(ROM, do_reset, alarm_only)); | |
228 } | |
229 | |
230 /* Returns 1 when something is found, 0 if nothing left */ | |
12 | 231 uint8_t |
0 | 232 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
233 uint8_t bit_test, search_direction, bit_number; | |
10 | 234 uint8_t last_zero, rom_byte_number, rom_byte_mask; |
0 | 235 uint8_t lastcrc8, crcaccum; |
12 | 236 int8_t next_result; |
0 | 237 |
238 /* Init for search */ | |
21 | 239 bit_number = 1; |
240 last_zero = 0; | |
241 rom_byte_number = 0; | |
242 rom_byte_mask = 1; | |
243 next_result = OW_NOMODULES; | |
244 lastcrc8 = 0; | |
245 crcaccum = 0; | |
0 | 246 |
21 | 247 /* if the last call was not the last one */ |
248 if (!OW_LastDevice) { | |
249 /* check if reset first is requested */ | |
250 if (do_reset) { | |
251 /* reset the 1-wire | |
252 * if there are no parts on 1-wire, return 0 */ | |
0 | 253 #if OW_DEBUG |
21 | 254 uart_putsP(PSTR("Resetting\n\r")); |
0 | 255 #endif |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
256 switch (OWTouchReset()) { |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
257 case 0: |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
258 break; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
259 |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
260 case 1: |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
261 /* reset the search */ |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
262 OW_LastDiscrepancy = 0; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
263 OW_LastFamilyDiscrepancy = 0; |
0 | 264 #if OW_DEBUG |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
265 uart_putsP(PSTR("No devices on bus\n\r")); |
0 | 266 #endif |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
267 return OW_NOPRESENCE; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
268 break; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
269 |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
270 case 2: |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
271 /* reset the search */ |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
272 OW_LastDiscrepancy = 0; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
273 OW_LastFamilyDiscrepancy = 0; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
274 #if OW_DEBUG |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
275 uart_putsP(PSTR("Bus appears to be being held low\n\r")); |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
276 #endif |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
277 return OW_BADWIRE; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
278 break; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
279 |
21 | 280 } |
281 } | |
0 | 282 |
21 | 283 /* If finding alarming devices issue a different command */ |
284 if (alarm_only) | |
285 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ | |
286 else | |
287 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ | |
0 | 288 |
21 | 289 /* pause before beginning the search */ |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
290 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
291 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
292 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
293 |
21 | 294 /* loop to do the search */ |
295 do { | |
296 /* read a bit and its compliment */ | |
297 bit_test = OWReadBit() << 1; | |
298 bit_test |= OWReadBit(); | |
0 | 299 |
300 #if OW_DEBUG | |
21 | 301 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); |
302 uart_puts(errstr); | |
0 | 303 #endif |
304 | |
21 | 305 /* check for no devices on 1-wire */ |
306 if (bit_test == 3) { | |
10 | 307 #if OW_DEBUG |
21 | 308 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); |
309 uart_puts(errstr); | |
10 | 310 #endif |
21 | 311 return(OW_BADWIRE); |
312 } | |
313 else { | |
314 /* all devices coupled have 0 or 1 */ | |
315 if (bit_test > 0) | |
316 search_direction = !(bit_test & 0x01); /* bit write value for search */ | |
317 else { | |
318 /* if this discrepancy is before the Last Discrepancy | |
319 * on a previous OWNext then pick the same as last time */ | |
320 if (bit_number < OW_LastDiscrepancy) | |
321 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0); | |
322 else | |
323 /* if equal to last pick 1, if not then pick 0 */ | |
324 search_direction = (bit_number == OW_LastDiscrepancy); | |
0 | 325 |
21 | 326 /* if 0 was picked then record its position in LastZero */ |
327 if (search_direction == 0) { | |
328 last_zero = bit_number; | |
0 | 329 |
21 | 330 /* check for Last discrepancy in family */ |
331 if (last_zero < 9) | |
332 OW_LastFamilyDiscrepancy = last_zero; | |
333 } | |
334 } | |
0 | 335 |
21 | 336 /* set or clear the bit in the ROM byte rom_byte_number |
337 * with mask rom_byte_mask */ | |
338 if (search_direction == 1) | |
339 ROM[rom_byte_number] |= rom_byte_mask; | |
340 else | |
341 ROM[rom_byte_number] &= ~rom_byte_mask; | |
0 | 342 |
21 | 343 /* serial number search direction write bit */ |
344 OWWriteBit(search_direction); | |
0 | 345 |
21 | 346 /* increment the byte counter bit_number |
347 * and shift the mask rom_byte_mask */ | |
348 bit_number++; | |
349 rom_byte_mask <<= 1; | |
0 | 350 |
21 | 351 /* if the mask is 0 then go to new ROM byte rom_byte_number |
352 * and reset mask */ | |
353 if (rom_byte_mask == 0) { | |
354 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */ | |
355 lastcrc8 = crcaccum; | |
0 | 356 |
21 | 357 rom_byte_number++; |
358 rom_byte_mask = 1; | |
359 } | |
360 } | |
361 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ | |
0 | 362 |
21 | 363 /* if the search was successful then */ |
364 if (!(bit_number < 65) || lastcrc8) { | |
365 if (lastcrc8) { | |
12 | 366 #if OW_DEBUG |
21 | 367 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); |
368 uart_puts(errstr); | |
12 | 369 #endif |
21 | 370 next_result = OW_BADCRC; |
371 } else { | |
372 /* search successful so set LastDiscrepancy,LastDevice,next_result */ | |
373 OW_LastDiscrepancy = last_zero; | |
374 OW_LastDevice = (OW_LastDiscrepancy == 0); | |
0 | 375 #if OW_DEBUG |
21 | 376 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); |
377 uart_puts(errstr); | |
0 | 378 #endif |
21 | 379 next_result = OW_FOUND; |
380 } | |
381 } | |
382 } | |
0 | 383 |
21 | 384 /* if no device found then reset counters so next 'next' will be |
385 * like a first */ | |
386 if (next_result != OW_FOUND || ROM[0] == 0) { | |
387 OW_LastDiscrepancy = 0; | |
388 OW_LastDevice = 0; | |
389 OW_LastFamilyDiscrepancy = 0; | |
390 } | |
0 | 391 |
21 | 392 if (next_result == OW_FOUND && ROM[0] == 0x00) |
393 next_result = OW_BADWIRE; | |
10 | 394 |
21 | 395 return next_result; |
0 | 396 |
397 } | |
398 | |
399 uint8_t PROGMEM dscrc_table[] = { | |
400 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, | |
401 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, | |
402 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, | |
403 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, | |
404 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, | |
405 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, | |
406 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, | |
407 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, | |
408 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205, | |
409 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80, | |
410 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238, | |
411 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, | |
412 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, | |
413 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, | |
414 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, | |
415 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 | |
416 }; | |
417 | |
418 void | |
419 OWCRC(uint8_t x, uint8_t *crc) { | |
420 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]); | |
421 } |