Mercurial > ~darius > hgwebdir.cgi > avr
annotate 1wire.c @ 13:2c03ec600dbc OWIRE_1_3
Reduce object code size as much as possible.
author | darius |
---|---|
date | Wed, 01 Sep 2004 17:46:15 +0930 |
parents | 4b141cc7cbd4 |
children | 026dc24d85e0 |
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 | |
33 #include <stdio.h> | |
34 #include <avr/io.h> | |
35 #include <avr/pgmspace.h> | |
36 | |
37 #include "1wire.h" | |
8
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
38 #include "1wire-delay.h" |
0 | 39 |
12 | 40 #if OW_DEBUG |
0 | 41 void uart_putsP(const char *addr); |
42 void uart_puts(const char *addr); | |
43 void uart_getc(); | |
44 int uart_putc(char c); | |
12 | 45 #endif |
0 | 46 |
47 static uint8_t OW_LastDevice = 0; | |
48 static uint8_t OW_LastDiscrepancy = 0; | |
49 static uint8_t OW_LastFamilyDiscrepancy = 0; | |
50 | |
51 static void | |
52 OWdelay(void) { | |
10 | 53 DELAY_I; |
0 | 54 } |
55 | |
56 /*----------------------------------------------------------------------------- | |
12 | 57 * Generate a 1-Wire reset, return 0 if presence pulse was found, |
58 * return 1 otherwise. | |
0 | 59 * (NOTE: Does not handle alarm presence from DS2404/DS1994) |
60 */ | |
12 | 61 uint8_t |
0 | 62 OWTouchReset(void) { |
10 | 63 DELAY_G; |
64 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); | |
65 OWIREDDR |= _BV(OWIREOUTPIN); | |
66 DELAY_H; | |
67 OWIREDDR &= ~(_BV(OWIREOUTPIN)); | |
68 DELAY_I; | |
0 | 69 |
10 | 70 return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0); |
0 | 71 } |
72 | |
73 /*----------------------------------------------------------------------------- | |
74 * Send a 1-wire write bit. | |
75 */ | |
76 void | |
12 | 77 OWWriteBit(uint8_t bit) { |
0 | 78 OWdelay(); |
79 if (bit) { | |
8
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
80 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
81 OWIREDDR |= _BV(OWIREOUTPIN); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
82 DELAY_A; |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
83 OWIREDDR &= ~(_BV(OWIREOUTPIN)); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
84 DELAY_B; |
0 | 85 } else { |
8
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
86 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
87 OWIREDDR |= _BV(OWIREOUTPIN); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
88 DELAY_C; |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
89 OWIREDDR &= ~(_BV(OWIREOUTPIN)); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
90 DELAY_D; |
0 | 91 } |
92 } | |
93 | |
94 /*----------------------------------------------------------------------------- | |
95 * Read a bit from the 1-wire bus and return it. | |
96 */ | |
12 | 97 uint8_t |
0 | 98 OWReadBit(void) { |
99 OWdelay(); | |
100 | |
8
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
101 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
102 OWIREDDR |= _BV(OWIREOUTPIN); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
103 DELAY_A; |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
104 OWIREDDR &= ~(_BV(OWIREOUTPIN)); |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
105 DELAY_E; |
f9a085a0ba93
Change the 1 wire routines to mostly C with assembly delay routines
darius
parents:
0
diff
changeset
|
106 return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0); |
0 | 107 } |
108 | |
109 /*----------------------------------------------------------------------------- | |
110 * Write a byte to the 1-wire bus | |
111 */ | |
112 void | |
113 OWWriteByte(uint8_t data) { | |
12 | 114 uint8_t i; |
0 | 115 |
116 /* Send LSB first */ | |
117 for (i = 0; i < 8; i++) { | |
118 OWWriteBit(data & 0x01); | |
119 data >>= 1; | |
120 } | |
121 } | |
122 | |
123 /*----------------------------------------------------------------------------- | |
124 * Read a byte from the 1-wire bus | |
125 */ | |
12 | 126 uint8_t |
0 | 127 OWReadByte(void) { |
128 int i, result = 0; | |
129 | |
130 for (i = 0; i < 8; i++) { | |
131 result >>= 1; | |
132 if (OWReadBit()) | |
133 result |= 0x80; | |
134 } | |
135 return(result); | |
136 } | |
137 | |
138 /*----------------------------------------------------------------------------- | |
139 * Write a 1-wire data byte and return the sampled result. | |
140 */ | |
12 | 141 uint8_t |
0 | 142 OWTouchByte(uint8_t data) { |
12 | 143 uint8_t i, result = 0; |
0 | 144 |
145 for (i = 0; i < 8; i++) { | |
146 result >>= 1; | |
147 | |
148 /* If sending a 1 then read a bit, otherwise write a 0 */ | |
149 if (data & 0x01) { | |
150 if (OWReadBit()) | |
151 result |= 0x80; | |
152 } else | |
153 OWWriteBit(0); | |
154 | |
155 data >>= 1; | |
156 } | |
157 | |
158 return(result); | |
159 } | |
160 | |
161 /*----------------------------------------------------------------------------- | |
162 * Write a block of bytes to the 1-wire bus and return the sampled result in | |
163 * the same buffer | |
164 */ | |
165 void | |
166 OWBlock(uint8_t *data, int len) { | |
167 int i; | |
168 | |
169 for (i = 0; i < len; i++) | |
170 data[i] = OWTouchByte(data[i]); | |
171 } | |
172 | |
173 | |
174 /*----------------------------------------------------------------------------- | |
175 * Send a 1 wire command to a device, or all if no ROM ID provided | |
176 */ | |
177 void | |
178 OWSendCmd(uint8_t *ROM, uint8_t cmd) { | |
12 | 179 uint8_t i; |
0 | 180 |
181 OWTouchReset(); | |
182 | |
183 if (ROM == NULL) | |
184 OWWriteByte(OW_SKIP_ROM_CMD); | |
185 else { | |
186 OWWriteByte(OW_MATCH_ROM_CMD); | |
187 for (i = 0; i < 8; i++) | |
188 OWWriteByte(ROM[i]); | |
189 } | |
190 | |
191 OWWriteByte(cmd); | |
192 } | |
193 | |
194 /*----------------------------------------------------------------------------- | |
195 * Search algorithm from App note 187 (and 162) | |
196 * | |
10 | 197 * OWFirst/OWNext return.. |
198 * 1 when something is found, | |
199 * 0 no more modules | |
200 * -1 if no presense pulse, | |
201 * -2 if bad CRC, | |
202 * -3 if bad wiring. | |
0 | 203 */ |
12 | 204 uint8_t |
0 | 205 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
206 /* Reset state */ | |
207 OW_LastDiscrepancy = 0; | |
208 OW_LastDevice = 0; | |
209 OW_LastFamilyDiscrepancy = 0; | |
210 | |
211 /* Go looking */ | |
212 return (OWNext(ROM, do_reset, alarm_only)); | |
213 } | |
214 | |
215 /* Returns 1 when something is found, 0 if nothing left */ | |
12 | 216 uint8_t |
0 | 217 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
218 uint8_t bit_test, search_direction, bit_number; | |
10 | 219 uint8_t last_zero, rom_byte_number, rom_byte_mask; |
0 | 220 uint8_t lastcrc8, crcaccum; |
12 | 221 int8_t next_result; |
0 | 222 |
223 /* Init for search */ | |
224 bit_number = 1; | |
225 last_zero = 0; | |
226 rom_byte_number = 0; | |
227 rom_byte_mask = 1; | |
10 | 228 next_result = OW_NOMODULES; |
0 | 229 lastcrc8 = 0; |
230 crcaccum = 0; | |
231 | |
232 /* if the last call was not the last one */ | |
233 if (!OW_LastDevice) { | |
234 /* check if reset first is requested */ | |
235 if (do_reset) { | |
236 /* reset the 1-wire | |
237 * if there are no parts on 1-wire, return 0 */ | |
238 #if OW_DEBUG | |
239 uart_putsP(PSTR("Resetting\n\r")); | |
240 #endif | |
241 if (OWTouchReset()) { | |
242 /* reset the search */ | |
243 OW_LastDiscrepancy = 0; | |
244 OW_LastFamilyDiscrepancy = 0; | |
245 #if OW_DEBUG | |
246 uart_putsP(PSTR("No devices on bus\n\r")); | |
247 #endif | |
10 | 248 return OW_NOPRESENCE; |
0 | 249 } |
250 } | |
251 | |
252 /* If finding alarming devices issue a different command */ | |
253 if (alarm_only) | |
254 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ | |
255 else | |
256 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ | |
257 | |
258 /* pause before beginning the search */ | |
259 OWdelay(); | |
260 OWdelay(); | |
261 OWdelay(); | |
262 | |
263 /* loop to do the search */ | |
264 do { | |
265 /* read a bit and its compliment */ | |
266 bit_test = OWReadBit() << 1; | |
267 bit_test |= OWReadBit(); | |
268 | |
269 #if OW_DEBUG | |
270 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); | |
271 uart_puts(errstr); | |
272 #endif | |
273 | |
274 /* check for no devices on 1-wire */ | |
275 if (bit_test == 3) { | |
10 | 276 #if OW_DEBUG |
0 | 277 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); |
278 uart_puts(errstr); | |
10 | 279 #endif |
280 return(OW_BADWIRE); | |
0 | 281 } |
282 else { | |
283 /* all devices coupled have 0 or 1 */ | |
284 if (bit_test > 0) | |
285 search_direction = !(bit_test & 0x01); /* bit write value for search */ | |
286 else { | |
287 /* if this discrepancy is before the Last Discrepancy | |
288 * on a previous OWNext then pick the same as last time */ | |
289 if (bit_number < OW_LastDiscrepancy) | |
290 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0); | |
291 else | |
292 /* if equal to last pick 1, if not then pick 0 */ | |
293 search_direction = (bit_number == OW_LastDiscrepancy); | |
294 | |
295 /* if 0 was picked then record its position in LastZero */ | |
296 if (search_direction == 0) { | |
297 last_zero = bit_number; | |
298 | |
299 /* check for Last discrepancy in family */ | |
300 if (last_zero < 9) | |
301 OW_LastFamilyDiscrepancy = last_zero; | |
302 } | |
303 } | |
304 | |
305 /* set or clear the bit in the ROM byte rom_byte_number | |
306 * with mask rom_byte_mask */ | |
307 if (search_direction == 1) | |
308 ROM[rom_byte_number] |= rom_byte_mask; | |
309 else | |
310 ROM[rom_byte_number] &= ~rom_byte_mask; | |
311 | |
312 /* serial number search direction write bit */ | |
313 OWWriteBit(search_direction); | |
314 | |
315 /* increment the byte counter bit_number | |
316 * and shift the mask rom_byte_mask */ | |
317 bit_number++; | |
318 rom_byte_mask <<= 1; | |
319 | |
320 /* if the mask is 0 then go to new ROM byte rom_byte_number | |
321 * and reset mask */ | |
322 if (rom_byte_mask == 0) { | |
323 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */ | |
324 lastcrc8 = crcaccum; | |
325 | |
326 rom_byte_number++; | |
327 rom_byte_mask = 1; | |
328 } | |
329 } | |
330 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ | |
331 | |
332 /* if the search was successful then */ | |
333 if (!(bit_number < 65) || lastcrc8) { | |
334 if (lastcrc8) { | |
12 | 335 #if OW_DEBUG |
0 | 336 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); |
337 uart_puts(errstr); | |
12 | 338 #endif |
10 | 339 next_result = OW_BADCRC; |
0 | 340 } else { |
341 /* search successful so set LastDiscrepancy,LastDevice,next_result */ | |
342 OW_LastDiscrepancy = last_zero; | |
343 OW_LastDevice = (OW_LastDiscrepancy == 0); | |
344 #if OW_DEBUG | |
345 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); | |
346 uart_puts(errstr); | |
347 #endif | |
10 | 348 next_result = OW_FOUND; |
0 | 349 } |
350 } | |
351 } | |
352 | |
353 /* if no device found then reset counters so next 'next' will be | |
354 * like a first */ | |
10 | 355 if (next_result != OW_FOUND || ROM[0] == 0) { |
0 | 356 OW_LastDiscrepancy = 0; |
357 OW_LastDevice = 0; | |
358 OW_LastFamilyDiscrepancy = 0; | |
359 } | |
360 | |
10 | 361 if (next_result == OW_FOUND && ROM[0] == 0x00) |
362 next_result = OW_BADWIRE; | |
363 | |
0 | 364 return next_result; |
365 | |
366 } | |
367 | |
368 uint8_t PROGMEM dscrc_table[] = { | |
369 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, | |
370 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, | |
371 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, | |
372 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, | |
373 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, | |
374 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, | |
375 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, | |
376 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, | |
377 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205, | |
378 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80, | |
379 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238, | |
380 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, | |
381 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, | |
382 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, | |
383 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, | |
384 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 | |
385 }; | |
386 | |
387 void | |
388 OWCRC(uint8_t x, uint8_t *crc) { | |
389 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]); | |
390 } |