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