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