Mercurial > ~darius > hgwebdir.cgi > avr
annotate 1wire.c @ 38:0a148f362097
Use \r\n instead of \n\r (keeps pexpect happy)
author | darius |
---|---|
date | Fri, 23 Nov 2007 12:01:54 +1030 |
parents | 0aa6bf4b98ae |
children | 5898fba6593c |
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 |
46 static uint8_t OW_LastDevice = 0; | |
47 static uint8_t OW_LastDiscrepancy = 0; | |
48 static uint8_t OW_LastFamilyDiscrepancy = 0; | |
49 | |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
50 /*----------------------------------------------------------------------------- |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
51 * 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
|
52 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
53 void |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
54 OWInit(void) { |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
55 OWBUSINIT(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
56 OWSETBUSHIGH(); |
0 | 57 } |
58 | |
59 /*----------------------------------------------------------------------------- | |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
60 * 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
|
61 * 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
|
62 * |
0 | 63 * (NOTE: Does not handle alarm presence from DS2404/DS1994) |
64 */ | |
12 | 65 uint8_t |
0 | 66 OWTouchReset(void) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
67 OWDELAY_G; |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
68 |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
69 /* 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
|
70 * 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
|
71 * comms attempt |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
72 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
73 if(OWREADBUS() == 0) |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
74 return 2; |
0 | 75 |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
76 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
77 OWDELAY_H; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
78 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
79 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
80 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
81 return(OWREADBUS()); |
0 | 82 } |
83 | |
84 /*----------------------------------------------------------------------------- | |
85 * Send a 1-wire write bit. | |
86 */ | |
87 void | |
12 | 88 OWWriteBit(uint8_t bit) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
89 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
90 |
0 | 91 if (bit) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
92 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
93 OWDELAY_A; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
94 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
95 OWDELAY_B; |
0 | 96 } else { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
97 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
98 OWDELAY_C; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
99 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
100 OWDELAY_D; |
0 | 101 } |
102 } | |
103 | |
104 /*----------------------------------------------------------------------------- | |
105 * Read a bit from the 1-wire bus and return it. | |
106 */ | |
12 | 107 uint8_t |
0 | 108 OWReadBit(void) { |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
109 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
110 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
111 OWSETBUSLOW(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
112 OWDELAY_A; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
113 OWSETBUSHIGH(); |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
114 OWDELAY_E; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
115 return(OWREADBUS()); |
0 | 116 } |
117 | |
118 /*----------------------------------------------------------------------------- | |
119 * Write a byte to the 1-wire bus | |
120 */ | |
121 void | |
122 OWWriteByte(uint8_t data) { | |
12 | 123 uint8_t i; |
0 | 124 |
125 /* Send LSB first */ | |
126 for (i = 0; i < 8; i++) { | |
127 OWWriteBit(data & 0x01); | |
128 data >>= 1; | |
129 } | |
130 } | |
131 | |
132 /*----------------------------------------------------------------------------- | |
133 * Read a byte from the 1-wire bus | |
134 */ | |
12 | 135 uint8_t |
0 | 136 OWReadByte(void) { |
137 int i, result = 0; | |
138 | |
139 for (i = 0; i < 8; i++) { | |
140 result >>= 1; | |
141 if (OWReadBit()) | |
142 result |= 0x80; | |
143 } | |
144 return(result); | |
145 } | |
146 | |
147 /*----------------------------------------------------------------------------- | |
148 * Write a 1-wire data byte and return the sampled result. | |
149 */ | |
12 | 150 uint8_t |
0 | 151 OWTouchByte(uint8_t data) { |
12 | 152 uint8_t i, result = 0; |
0 | 153 |
154 for (i = 0; i < 8; i++) { | |
155 result >>= 1; | |
156 | |
157 /* If sending a 1 then read a bit, otherwise write a 0 */ | |
158 if (data & 0x01) { | |
159 if (OWReadBit()) | |
160 result |= 0x80; | |
161 } else | |
162 OWWriteBit(0); | |
163 | |
164 data >>= 1; | |
165 } | |
166 | |
167 return(result); | |
168 } | |
169 | |
170 /*----------------------------------------------------------------------------- | |
171 * Write a block of bytes to the 1-wire bus and return the sampled result in | |
172 * the same buffer | |
173 */ | |
174 void | |
175 OWBlock(uint8_t *data, int len) { | |
176 int i; | |
177 | |
178 for (i = 0; i < len; i++) | |
179 data[i] = OWTouchByte(data[i]); | |
180 } | |
181 | |
182 | |
183 /*----------------------------------------------------------------------------- | |
184 * Send a 1 wire command to a device, or all if no ROM ID provided | |
185 */ | |
186 void | |
187 OWSendCmd(uint8_t *ROM, uint8_t cmd) { | |
12 | 188 uint8_t i; |
0 | 189 |
190 OWTouchReset(); | |
191 | |
192 if (ROM == NULL) | |
193 OWWriteByte(OW_SKIP_ROM_CMD); | |
194 else { | |
195 OWWriteByte(OW_MATCH_ROM_CMD); | |
196 for (i = 0; i < 8; i++) | |
197 OWWriteByte(ROM[i]); | |
198 } | |
199 OWWriteByte(cmd); | |
200 } | |
201 | |
202 /*----------------------------------------------------------------------------- | |
203 * Search algorithm from App note 187 (and 162) | |
204 * | |
10 | 205 * OWFirst/OWNext return.. |
206 * 1 when something is found, | |
207 * 0 no more modules | |
16 | 208 * -1 if no presence pulse, |
10 | 209 * -2 if bad CRC, |
210 * -3 if bad wiring. | |
0 | 211 */ |
12 | 212 uint8_t |
0 | 213 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
214 /* Reset state */ | |
215 OW_LastDiscrepancy = 0; | |
216 OW_LastDevice = 0; | |
217 OW_LastFamilyDiscrepancy = 0; | |
218 | |
219 /* Go looking */ | |
220 return (OWNext(ROM, do_reset, alarm_only)); | |
221 } | |
222 | |
223 /* Returns 1 when something is found, 0 if nothing left */ | |
12 | 224 uint8_t |
0 | 225 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { |
226 uint8_t bit_test, search_direction, bit_number; | |
10 | 227 uint8_t last_zero, rom_byte_number, rom_byte_mask; |
0 | 228 uint8_t lastcrc8, crcaccum; |
12 | 229 int8_t next_result; |
0 | 230 |
231 /* Init for search */ | |
21 | 232 bit_number = 1; |
233 last_zero = 0; | |
234 rom_byte_number = 0; | |
235 rom_byte_mask = 1; | |
236 next_result = OW_NOMODULES; | |
237 lastcrc8 = 0; | |
238 crcaccum = 0; | |
0 | 239 |
21 | 240 /* if the last call was not the last one */ |
241 if (!OW_LastDevice) { | |
242 /* check if reset first is requested */ | |
243 if (do_reset) { | |
244 /* reset the 1-wire | |
245 * if there are no parts on 1-wire, return 0 */ | |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
246 OWPUTSP(PSTR("Resetting\n\r")); |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
247 switch (OWTouchReset()) { |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
248 case 0: |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
249 break; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
250 |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
251 case 1: |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
252 /* reset the search */ |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
253 OW_LastDiscrepancy = 0; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
254 OW_LastFamilyDiscrepancy = 0; |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
255 OWPUTSP(PSTR("No devices on bus\n\r")); |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
256 return OW_NOPRESENCE; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
257 break; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
258 |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
259 case 2: |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
260 /* reset the search */ |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
261 OW_LastDiscrepancy = 0; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
262 OW_LastFamilyDiscrepancy = 0; |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
263 OWPUTSP(PSTR("Bus appears to be being held low\n\r")); |
22
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
264 return OW_BADWIRE; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
265 break; |
bd792ebf813d
Report the bus being held low back to the caller from OWFirst/Next.
darius
parents:
21
diff
changeset
|
266 |
21 | 267 } |
268 } | |
0 | 269 |
21 | 270 /* If finding alarming devices issue a different command */ |
271 if (alarm_only) | |
272 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ | |
273 else | |
274 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ | |
0 | 275 |
21 | 276 /* pause before beginning the search */ |
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
277 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
278 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
279 OWDELAY_I; |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
29
diff
changeset
|
280 |
21 | 281 /* loop to do the search */ |
282 do { | |
283 /* read a bit and its compliment */ | |
284 bit_test = OWReadBit() << 1; | |
285 bit_test |= OWReadBit(); | |
0 | 286 |
287 #if OW_DEBUG | |
21 | 288 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
289 OWPUTSP(errstr); |
0 | 290 #endif |
291 | |
21 | 292 /* check for no devices on 1-wire */ |
293 if (bit_test == 3) { | |
10 | 294 #if OW_DEBUG |
21 | 295 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
296 OWPUTSP(errstr); |
10 | 297 #endif |
21 | 298 return(OW_BADWIRE); |
299 } | |
300 else { | |
301 /* all devices coupled have 0 or 1 */ | |
302 if (bit_test > 0) | |
303 search_direction = !(bit_test & 0x01); /* bit write value for search */ | |
304 else { | |
305 /* if this discrepancy is before the Last Discrepancy | |
306 * on a previous OWNext then pick the same as last time */ | |
307 if (bit_number < OW_LastDiscrepancy) | |
308 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0); | |
309 else | |
310 /* if equal to last pick 1, if not then pick 0 */ | |
311 search_direction = (bit_number == OW_LastDiscrepancy); | |
0 | 312 |
21 | 313 /* if 0 was picked then record its position in LastZero */ |
314 if (search_direction == 0) { | |
315 last_zero = bit_number; | |
0 | 316 |
21 | 317 /* check for Last discrepancy in family */ |
318 if (last_zero < 9) | |
319 OW_LastFamilyDiscrepancy = last_zero; | |
320 } | |
321 } | |
0 | 322 |
21 | 323 /* set or clear the bit in the ROM byte rom_byte_number |
324 * with mask rom_byte_mask */ | |
325 if (search_direction == 1) | |
326 ROM[rom_byte_number] |= rom_byte_mask; | |
327 else | |
328 ROM[rom_byte_number] &= ~rom_byte_mask; | |
0 | 329 |
21 | 330 /* serial number search direction write bit */ |
331 OWWriteBit(search_direction); | |
0 | 332 |
21 | 333 /* increment the byte counter bit_number |
334 * and shift the mask rom_byte_mask */ | |
335 bit_number++; | |
336 rom_byte_mask <<= 1; | |
0 | 337 |
21 | 338 /* if the mask is 0 then go to new ROM byte rom_byte_number |
339 * and reset mask */ | |
340 if (rom_byte_mask == 0) { | |
341 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */ | |
342 lastcrc8 = crcaccum; | |
0 | 343 |
21 | 344 rom_byte_number++; |
345 rom_byte_mask = 1; | |
346 } | |
347 } | |
348 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ | |
0 | 349 |
21 | 350 /* if the search was successful then */ |
351 if (!(bit_number < 65) || lastcrc8) { | |
352 if (lastcrc8) { | |
12 | 353 #if OW_DEBUG |
21 | 354 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
355 OWPUTSP(errstr); |
12 | 356 #endif |
21 | 357 next_result = OW_BADCRC; |
358 } else { | |
359 /* search successful so set LastDiscrepancy,LastDevice,next_result */ | |
360 OW_LastDiscrepancy = last_zero; | |
361 OW_LastDevice = (OW_LastDiscrepancy == 0); | |
0 | 362 #if OW_DEBUG |
21 | 363 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
364 OWPUTSP(errstr); |
0 | 365 #endif |
21 | 366 next_result = OW_FOUND; |
367 } | |
368 } | |
369 } | |
0 | 370 |
21 | 371 /* if no device found then reset counters so next 'next' will be |
372 * like a first */ | |
373 if (next_result != OW_FOUND || ROM[0] == 0) { | |
374 OW_LastDiscrepancy = 0; | |
375 OW_LastDevice = 0; | |
376 OW_LastFamilyDiscrepancy = 0; | |
377 } | |
0 | 378 |
21 | 379 if (next_result == OW_FOUND && ROM[0] == 0x00) |
380 next_result = OW_BADWIRE; | |
10 | 381 |
21 | 382 return next_result; |
0 | 383 |
384 } | |
385 | |
386 uint8_t PROGMEM dscrc_table[] = { | |
387 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, | |
388 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, | |
389 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, | |
390 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, | |
391 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, | |
392 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, | |
393 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, | |
394 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, | |
395 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205, | |
396 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80, | |
397 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238, | |
398 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, | |
399 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, | |
400 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, | |
401 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, | |
402 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 | |
403 }; | |
404 | |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
405 /*----------------------------------------------------------------------------- |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
406 * Update *crc based on the value of x |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
407 */ |
0 | 408 void |
409 OWCRC(uint8_t x, uint8_t *crc) { | |
410 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]); | |
411 } | |
33
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
412 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
413 /*----------------------------------------------------------------------------- |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
414 * Program a DS2502's memory |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
415 * |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
416 * Arguments |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
417 * ROM - ROM ID (or NULL to send SKIP_ROM) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
418 * start - Start address (bytes) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
419 * len - Length of data to write |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
420 * data - Data to write |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
421 * exact - If true, only accept exact matches for programming, |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
422 * otherwise only ensure the bits we requested were |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
423 * programmed [to 0] |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
424 * status - If true program status rather than memory |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
425 * |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
426 * Returns.. |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
427 * 0 if all is OK |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
428 * 1 if the programming was unsuccessful |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
429 * 2 if the parameters were invalid |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
430 * 3 if the DS2502 didn't respond appropriately (also happens if the |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
431 * module doesn't exist) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
432 */ |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
433 uint8_t |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
434 OWProgROM(uint8_t *ROM, uint8_t start, uint8_t len, uint8_t *data, uint8_t exact, uint8_t status) { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
435 #if defined(OWSETVPPON) && defined(OWSETVPPOFF) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
436 uint8_t crc, i, tmp; |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
437 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
438 /* Stupid programmer detection */ |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
439 if (status) { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
440 if (start + len > 3) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
441 return(2); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
442 } else { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
443 if (start + len > 127) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
444 return(2); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
445 } |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
446 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
447 if (len < 1) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
448 return(2); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
449 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
450 OWDELAY_I; |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
451 if (OWTouchReset() != 0) { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
452 uart_putsP(PSTR("No presence pulse\n\r")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
453 return(3); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
454 } |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
455 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
456 crc = 0; |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
457 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
458 /* Send the command */ |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
459 if (status) { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
460 OWSendCmd(ROM, OW_WRITE_STATUS); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
461 OWCRC(OW_WRITE_STATUS, &crc); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
462 } else { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
463 OWSendCmd(ROM, OW_WRITE_MEMORY); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
464 OWCRC(OW_WRITE_MEMORY, &crc); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
465 } |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
466 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
467 /* And the start address |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
468 * (2 bytes even though one would do) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
469 */ |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
470 OWWriteByte(start); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
471 OWCRC(start, &crc); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
472 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
473 OWWriteByte(0x00); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
474 OWCRC(0x00, &crc); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
475 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
476 for (i = 0; i < len; i++) { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
477 uart_putsP(PSTR("Programming ")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
478 uart_puts_hex(data[i]); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
479 uart_putsP(PSTR(" to ")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
480 uart_puts_hex(start + i); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
481 uart_putsP(PSTR("\n\r")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
482 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
483 OWWriteByte(data[i]); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
484 OWCRC(data[i], &crc); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
485 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
486 tmp = OWReadByte(); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
487 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
488 if (crc != tmp) { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
489 uart_putsP(PSTR("CRC mismatch ")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
490 uart_puts_hex(crc); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
491 uart_putsP(PSTR(" vs ")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
492 uart_puts_hex(tmp); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
493 uart_putsP(PSTR("\n\r")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
494 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
495 OWTouchReset(); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
496 return(3); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
497 } |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
498 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
499 OWSETVPPON(); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
500 OWDELAY_H; |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
501 OWSETVPPOFF(); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
502 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
503 tmp = OWReadByte(); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
504 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
505 /* Check the bits we turned off are off */ |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
506 /* |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
507 for (i = 0; i < 8; i++) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
508 if (!(data[i] & 1 << i) && (tmp & 1 << i)) |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
509 return(-3); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
510 */ |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
511 if ((!data[i] & tmp) != 0) { |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
512 uart_putsP(PSTR("Readback mismatch ")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
513 uart_puts_hex(data[i]); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
514 uart_putsP(PSTR(" vs ")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
515 uart_puts_hex(data[i]); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
516 uart_putsP(PSTR("\n\r")); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
517 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
518 OWTouchReset(); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
519 return(3); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
520 } |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
521 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
522 /* The DS2502 loads it's CRC register with the address of the |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
523 * next byte */ |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
524 crc = 0; |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
525 OWCRC(start + i + 1, &crc); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
526 } |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
527 |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
528 return(0); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
529 #else |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
530 return(1); |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
531 #endif |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
532 } |
0aa6bf4b98ae
- Don't wrap individual debug statements in ifdef, use a conditionally
darius
parents:
32
diff
changeset
|
533 |