Mercurial > ~darius > hgwebdir.cgi > avr-lib
annotate 1wire.c @ 18:0876867347de
Remove CVS tags
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 31 Jan 2015 23:26:21 +1030 |
parents | d8002c716678 |
children | b5e4591b6570 |
rev | line source |
---|---|
0 | 1 /* |
2 * Various 1 wire routines | |
3 * Search routine is copied from the Dallas owpd library with mods | |
4 * available from here http://www.ibutton.com/software/1wire/wirekit.html | |
5 * | |
6 * Copyright (c) 2004 | |
7 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. | |
8 * | |
9 * Redistribution and use in source and binary forms, with or without | |
10 * modification, are permitted provided that the following conditions | |
11 * are met: | |
12 * 1. Redistributions of source code must retain the above copyright | |
13 * notice, this list of conditions and the following disclaimer. | |
14 * 2. Redistributions in binary form must reproduce the above copyright | |
15 * notice, this list of conditions and the following disclaimer in the | |
16 * documentation and/or other materials provided with the distribution. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE | |
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
28 * SUCH DAMAGE. | |
29 */ | |
30 | |
31 /* | |
32 * No user servicable parts inside | |
33 * | |
34 * Modify 1wire-config.h | |
35 */ | |
36 | |
37 #include <stdio.h> | |
13
6f8f7b87d2f1
- Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
38 #include <stdint.h> |
6f8f7b87d2f1
- Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
39 #include "1wire-config.h" |
0 | 40 #include "1wire.h" |
41 | |
42 static uint8_t OW_LastDevice = 0; | |
43 static uint8_t OW_LastDiscrepancy = 0; | |
44 static uint8_t OW_LastFamilyDiscrepancy = 0; | |
45 | |
46 const PROGMEM char *OWProgROM_Status[] = { | |
47 "OK", | |
48 "no HW support", | |
49 "Invalid params", | |
50 "module missing/broken" | |
51 }; | |
52 | |
53 /*----------------------------------------------------------------------------- | |
54 * Configure the IO port as we need | |
55 */ | |
56 void | |
57 OWInit(void) { | |
58 OWBUSINIT(); | |
59 OWSETBUSHIGH(); | |
60 } | |
61 | |
62 /*----------------------------------------------------------------------------- | |
63 * Generate a 1-Wire reset, return 0 if presence pulse was found, 1 if it | |
64 * wasn't, or 2 if the line appears to be being held low. | |
65 * | |
66 * (NOTE: Does not handle alarm presence from DS2404/DS1994) | |
67 */ | |
68 uint8_t | |
69 OWTouchReset(void) { | |
14
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
70 uint8_t i; |
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
71 |
0 | 72 OWDELAY_G; |
73 | |
74 /* Check the bus isn't being held low (ie it's broken) Do it after | |
75 * the delay so we guarantee we don't see a slave from a previous | |
76 * comms attempt | |
77 */ | |
14
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
78 #if 1 |
0 | 79 OWSETREAD(); |
80 if(OWREADBUS() == 0) | |
81 return 2; | |
14
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
82 #endif |
0 | 83 |
84 OWSETBUSLOW(); | |
85 OWDELAY_H; | |
86 OWSETBUSHIGH(); | |
87 OWDELAY_I; | |
88 | |
89 OWSETREAD(); | |
14
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
90 i = OWREADBUS(); |
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
91 |
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
92 OWDELAY_J; |
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
93 return(i); |
0 | 94 } |
95 | |
96 /*----------------------------------------------------------------------------- | |
97 * Send a 1-wire write bit. | |
98 */ | |
99 void | |
100 OWWriteBit(uint8_t bit) { | |
101 OWDELAY_I; | |
102 | |
103 if (bit) { | |
104 OWSETBUSLOW(); | |
105 OWDELAY_A; | |
106 OWSETBUSHIGH(); | |
107 OWDELAY_B; | |
108 } else { | |
109 OWSETBUSLOW(); | |
110 OWDELAY_C; | |
111 OWSETBUSHIGH(); | |
112 OWDELAY_D; | |
113 } | |
114 } | |
115 | |
116 /*----------------------------------------------------------------------------- | |
117 * Read a bit from the 1-wire bus and return it. | |
118 */ | |
119 uint8_t | |
120 OWReadBit(void) { | |
14
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
121 uint8_t i; |
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
122 |
0 | 123 OWDELAY_I; |
124 | |
125 OWSETBUSLOW(); | |
126 OWDELAY_A; | |
127 OWSETBUSHIGH(); | |
128 OWDELAY_E; | |
129 OWSETREAD(); | |
14
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
130 i = OWREADBUS(); |
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
131 OWDELAY_F; |
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
132 return(i); |
0 | 133 } |
134 | |
135 /*----------------------------------------------------------------------------- | |
136 * Write a byte to the 1-wire bus | |
137 */ | |
138 void | |
139 OWWriteByte(uint8_t data) { | |
140 uint8_t i; | |
141 | |
142 /* Send LSB first */ | |
143 for (i = 0; i < 8; i++) { | |
144 OWWriteBit(data & 0x01); | |
145 data >>= 1; | |
146 } | |
147 } | |
148 | |
149 /*----------------------------------------------------------------------------- | |
150 * Read a byte from the 1-wire bus | |
151 */ | |
152 uint8_t | |
153 OWReadByte(void) { | |
154 int i, result = 0; | |
155 | |
156 for (i = 0; i < 8; i++) { | |
157 result >>= 1; | |
158 if (OWReadBit()) | |
159 result |= 0x80; | |
160 } | |
161 return(result); | |
162 } | |
163 | |
164 /*----------------------------------------------------------------------------- | |
165 * Write a 1-wire data byte and return the sampled result. | |
166 */ | |
167 uint8_t | |
168 OWTouchByte(uint8_t data) { | |
169 uint8_t i, result = 0; | |
170 | |
171 for (i = 0; i < 8; i++) { | |
172 result >>= 1; | |
173 | |
174 /* If sending a 1 then read a bit, otherwise write a 0 */ | |
175 if (data & 0x01) { | |
176 if (OWReadBit()) | |
177 result |= 0x80; | |
178 } else | |
179 OWWriteBit(0); | |
180 | |
181 data >>= 1; | |
182 } | |
183 | |
184 return(result); | |
185 } | |
186 | |
187 /*----------------------------------------------------------------------------- | |
188 * Write a block of bytes to the 1-wire bus and return the sampled result in | |
189 * the same buffer | |
190 */ | |
191 void | |
192 OWBlock(uint8_t *data, int len) { | |
193 int i; | |
194 | |
195 for (i = 0; i < len; i++) | |
196 data[i] = OWTouchByte(data[i]); | |
197 } | |
198 | |
199 | |
200 /*----------------------------------------------------------------------------- | |
201 * Send a 1 wire command to a device, or all if no ROM ID provided | |
202 */ | |
203 void | |
204 OWSendCmd(uint8_t *ROM, uint8_t cmd) { | |
205 uint8_t i; | |
206 | |
207 OWTouchReset(); | |
208 | |
209 if (ROM == NULL) | |
210 OWWriteByte(OW_SKIP_ROM_CMD); | |
211 else { | |
212 OWWriteByte(OW_MATCH_ROM_CMD); | |
213 for (i = 0; i < 8; i++) | |
214 OWWriteByte(ROM[i]); | |
215 } | |
216 OWWriteByte(cmd); | |
217 } | |
218 | |
219 /*----------------------------------------------------------------------------- | |
220 * Search algorithm from App note 187 (and 162) | |
221 * | |
222 * OWFirst/OWNext return.. | |
223 * 1 when something is found, | |
224 * 0 no more modules | |
225 * -1 if no presence pulse, | |
226 * -2 if bad CRC, | |
227 * -3 if bad wiring. | |
228 */ | |
229 uint8_t | |
230 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { | |
231 /* Reset state */ | |
232 OW_LastDiscrepancy = 0; | |
233 OW_LastDevice = 0; | |
234 OW_LastFamilyDiscrepancy = 0; | |
235 | |
236 /* Go looking */ | |
237 return (OWNext(ROM, do_reset, alarm_only)); | |
238 } | |
239 | |
240 /* Returns 1 when something is found, 0 if nothing left */ | |
241 uint8_t | |
242 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { | |
243 uint8_t bit_test, search_direction, bit_number; | |
244 uint8_t last_zero, rom_byte_number, rom_byte_mask; | |
245 uint8_t lastcrc8, crcaccum; | |
246 int8_t next_result; | |
247 | |
248 /* Init for search */ | |
249 bit_number = 1; | |
250 last_zero = 0; | |
251 rom_byte_number = 0; | |
252 rom_byte_mask = 1; | |
253 next_result = OW_NOMODULES; | |
254 lastcrc8 = 0; | |
255 crcaccum = 0; | |
256 | |
257 /* if the last call was not the last one */ | |
258 if (!OW_LastDevice) { | |
259 /* check if reset first is requested */ | |
260 if (do_reset) { | |
261 /* reset the 1-wire | |
262 * if there are no parts on 1-wire, return 0 */ | |
263 OWPUTSP(PSTR("Resetting\r\n")); | |
264 switch (OWTouchReset()) { | |
265 case 0: | |
14
d8002c716678
- Actually wait after reading like the spec says.
Daniel O'Connor <darius@dons.net.au>
parents:
13
diff
changeset
|
266 OWPUTSP(PSTR("Found device(s)\r\n")); |
0 | 267 break; |
268 | |
269 case 1: | |
270 /* reset the search */ | |
271 OW_LastDiscrepancy = 0; | |
272 OW_LastFamilyDiscrepancy = 0; | |
273 OWPUTSP(PSTR("No devices on bus\r\n")); | |
274 return OW_NOPRESENCE; | |
275 break; | |
276 | |
277 case 2: | |
278 /* reset the search */ | |
279 OW_LastDiscrepancy = 0; | |
280 OW_LastFamilyDiscrepancy = 0; | |
281 OWPUTSP(PSTR("Bus appears to be being held low\r\n")); | |
282 return OW_BADWIRE; | |
283 break; | |
284 | |
285 } | |
286 } | |
287 | |
288 /* If finding alarming devices issue a different command */ | |
289 if (alarm_only) | |
290 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ | |
291 else | |
292 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ | |
293 | |
294 /* pause before beginning the search */ | |
295 OWDELAY_I; | |
296 OWDELAY_I; | |
297 OWDELAY_I; | |
298 | |
299 /* loop to do the search */ | |
300 do { | |
301 /* read a bit and its compliment */ | |
302 bit_test = OWReadBit() << 1; | |
303 bit_test |= OWReadBit(); | |
304 | |
305 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test); | |
306 | |
307 /* check for no devices on 1-wire */ | |
308 if (bit_test == 3) { | |
309 OWPRINTFP(PSTR("bit_test = %d\r\n"), bit_test); | |
310 return(OW_BADWIRE); | |
311 } | |
312 else { | |
313 /* all devices coupled have 0 or 1 */ | |
314 if (bit_test > 0) | |
315 search_direction = !(bit_test & 0x01); /* bit write value for search */ | |
316 else { | |
317 /* if this discrepancy is before the Last Discrepancy | |
318 * on a previous OWNext then pick the same as last time */ | |
319 if (bit_number < OW_LastDiscrepancy) | |
320 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0); | |
321 else | |
322 /* if equal to last pick 1, if not then pick 0 */ | |
323 search_direction = (bit_number == OW_LastDiscrepancy); | |
324 | |
325 /* if 0 was picked then record its position in LastZero */ | |
326 if (search_direction == 0) { | |
327 last_zero = bit_number; | |
328 | |
329 /* check for Last discrepancy in family */ | |
330 if (last_zero < 9) | |
331 OW_LastFamilyDiscrepancy = last_zero; | |
332 } | |
333 } | |
334 | |
335 /* set or clear the bit in the ROM byte rom_byte_number | |
336 * with mask rom_byte_mask */ | |
337 if (search_direction == 1) | |
338 ROM[rom_byte_number] |= rom_byte_mask; | |
339 else | |
340 ROM[rom_byte_number] &= ~rom_byte_mask; | |
341 | |
342 /* serial number search direction write bit */ | |
343 OWWriteBit(search_direction); | |
344 | |
345 /* increment the byte counter bit_number | |
346 * and shift the mask rom_byte_mask */ | |
347 bit_number++; | |
348 rom_byte_mask <<= 1; | |
349 | |
350 /* if the mask is 0 then go to new ROM byte rom_byte_number | |
351 * and reset mask */ | |
352 if (rom_byte_mask == 0) { | |
353 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */ | |
354 lastcrc8 = crcaccum; | |
355 | |
356 rom_byte_number++; | |
357 rom_byte_mask = 1; | |
358 } | |
359 } | |
360 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ | |
361 | |
362 /* if the search was successful then */ | |
363 if (!(bit_number < 65) || lastcrc8) { | |
364 if (lastcrc8) { | |
365 OWPRINTFP(PSTR("Bad CRC (%d)\r\n"), lastcrc8); | |
366 next_result = OW_BADCRC; | |
367 } else { | |
368 /* search successful so set LastDiscrepancy,LastDevice,next_result */ | |
369 OW_LastDiscrepancy = last_zero; | |
370 OW_LastDevice = (OW_LastDiscrepancy == 0); | |
371 OWPRINTFP(PSTR("Last device = %d\r\n"), OW_LastDevice); | |
372 next_result = OW_FOUND; | |
373 } | |
374 } | |
375 } | |
376 | |
377 /* if no device found then reset counters so next 'next' will be | |
378 * like a first */ | |
379 if (next_result != OW_FOUND || ROM[0] == 0) { | |
380 OW_LastDiscrepancy = 0; | |
381 OW_LastDevice = 0; | |
382 OW_LastFamilyDiscrepancy = 0; | |
383 } | |
384 | |
385 if (next_result == OW_FOUND && ROM[0] == 0x00) | |
386 next_result = OW_BADWIRE; | |
387 | |
388 return next_result; | |
389 | |
390 } | |
391 | |
392 uint8_t PROGMEM dscrc_table[] = { | |
393 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, | |
394 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, | |
395 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, | |
396 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, | |
397 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, | |
398 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, | |
399 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, | |
400 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, | |
401 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205, | |
402 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80, | |
403 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238, | |
404 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, | |
405 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, | |
406 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, | |
407 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, | |
408 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 | |
409 }; | |
410 | |
411 /*----------------------------------------------------------------------------- | |
412 * Update *crc based on the value of x | |
413 */ | |
414 void | |
415 OWCRC(uint8_t x, uint8_t *crc) { | |
416 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]); | |
417 } | |
418 | |
419 /*----------------------------------------------------------------------------- | |
420 * Program a DS2502's memory | |
421 * | |
422 * Arguments | |
423 * ROM - ROM ID (or NULL to send SKIP_ROM) | |
424 * start - Start address (bytes) | |
425 * len - Length of data to write | |
426 * data - Data to write | |
427 * exact - If true, only accept exact matches for programming, | |
428 * otherwise only ensure the bits we requested were | |
429 * programmed [to 0] | |
430 * status - If true program status rather than memory | |
431 * | |
432 * Returns.. | |
433 * 0 if all is OK | |
434 * 1 if the programming is not possible | |
435 * 2 if the parameters were invalid | |
436 * 3 if the DS2502 didn't respond appropriately (also happens if the | |
437 * module doesn't exist) | |
438 */ | |
13
6f8f7b87d2f1
- Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
439 #if defined(OWSETVPPON) && defined(OWSETVPPOFF) |
0 | 440 uint8_t |
441 OWProgROM(uint8_t *ROM, uint8_t start, uint8_t len, uint8_t *data, uint8_t exact, uint8_t status) { | |
442 uint8_t crc, i, tmp; | |
443 | |
444 /* Stupid programmer detection */ | |
445 if (status) { | |
446 if (start + len > 3) | |
447 return(2); | |
448 } else { | |
449 if (start + len > 127) | |
450 return(2); | |
451 } | |
452 | |
453 if (len < 1) | |
454 return(2); | |
455 | |
456 OWDELAY_I; | |
457 if (OWTouchReset() != 0) { | |
458 cons_putsP(PSTR("No presence pulse\r\n")); | |
459 return(3); | |
460 } | |
461 | |
462 crc = 0; | |
463 | |
464 /* Send the command */ | |
465 if (status) { | |
466 OWSendCmd(ROM, OW_WRITE_STATUS); | |
467 OWCRC(OW_WRITE_STATUS, &crc); | |
468 } else { | |
469 OWSendCmd(ROM, OW_WRITE_MEMORY); | |
470 OWCRC(OW_WRITE_MEMORY, &crc); | |
471 } | |
472 | |
473 /* And the start address | |
474 * (2 bytes even though one would do) | |
475 */ | |
476 OWWriteByte(start); | |
477 OWCRC(start, &crc); | |
478 | |
479 OWWriteByte(0x00); | |
480 OWCRC(0x00, &crc); | |
481 | |
482 for (i = 0; i < len; i++) { | |
483 cons_putsP(PSTR("Programming ")); | |
484 cons_puts_hex(data[i]); | |
485 cons_putsP(PSTR(" to ")); | |
486 cons_puts_hex(start + i); | |
487 cons_putsP(PSTR("\r\n")); | |
488 | |
489 OWWriteByte(data[i]); | |
490 OWCRC(data[i], &crc); | |
491 | |
492 tmp = OWReadByte(); | |
493 | |
494 if (crc != tmp) { | |
495 cons_putsP(PSTR("CRC mismatch ")); | |
496 cons_puts_hex(crc); | |
497 cons_putsP(PSTR(" vs ")); | |
498 cons_puts_hex(tmp); | |
499 cons_putsP(PSTR("\r\n")); | |
500 | |
501 OWTouchReset(); | |
502 return(3); | |
503 } | |
504 | |
505 OWSETVPPON(); | |
506 OWDELAY_H; | |
507 OWSETVPPOFF(); | |
508 | |
509 tmp = OWReadByte(); | |
510 | |
511 /* Check the bits we turned off are off */ | |
512 /* | |
513 for (i = 0; i < 8; i++) | |
514 if (!(data[i] & 1 << i) && (tmp & 1 << i)) | |
515 return(-3); | |
516 */ | |
517 if ((!data[i] & tmp) != 0) { | |
518 cons_putsP(PSTR("Readback mismatch ")); | |
519 cons_puts_hex(data[i]); | |
520 cons_putsP(PSTR(" vs ")); | |
521 cons_puts_hex(data[i]); | |
522 cons_putsP(PSTR("\r\n")); | |
523 | |
524 OWTouchReset(); | |
525 return(3); | |
526 } | |
527 | |
528 /* The DS2502 loads it's CRC register with the address of the | |
529 * next byte */ | |
530 crc = 0; | |
531 OWCRC(start + i + 1, &crc); | |
532 } | |
533 | |
534 return(0); | |
13
6f8f7b87d2f1
- Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
535 } |
0 | 536 #else |
13
6f8f7b87d2f1
- Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
537 uint8_t |
6f8f7b87d2f1
- Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
538 OWProgROM(uint8_t *ROM __attribute((unused)), uint8_t start __attribute((unused)), uint8_t len __attribute((unused)), uint8_t *data __attribute((unused)), uint8_t exact __attribute((unused)), uint8_t status __attribute((unused))) { |
0 | 539 return(1); |
13
6f8f7b87d2f1
- Rename 1wire config example to note it's for AVR.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
540 } |
0 | 541 #endif |
542 | |
543 /* | |
544 * OWGetTemp | |
545 * | |
546 * Get the temperature from a 1wire bus module | |
547 * | |
548 * Returns temperature in hundredths of a degree or OW_TEMP_xxx on | |
549 * error. | |
550 */ | |
551 int16_t | |
552 OWGetTemp(uint8_t *ROM) { | |
553 int8_t i; | |
554 uint8_t crc, buf[9]; | |
555 int16_t temp; | |
556 int16_t tfrac; | |
557 | |
558 if (ROM[0] != OW_FAMILY_TEMP) | |
559 return OW_TEMP_WRONG_FAM; | |
560 | |
561 OWSendCmd(ROM, OW_CONVERTT_CMD); | |
562 | |
563 i = 0; | |
564 | |
565 /* Wait for the conversion */ | |
566 while (OWReadBit() == 0) | |
567 i = 1; | |
568 | |
569 /* Check that we talked to a module and it did something */ | |
570 if (i == 0) { | |
571 return OW_TEMP_NO_ROM; | |
572 } | |
573 | |
574 OWSendCmd(ROM, OW_RD_SCR_CMD); | |
575 crc = 0; | |
576 for (i = 0; i < 8; i++) { | |
577 buf[i] = OWReadByte(); | |
578 OWCRC(buf[i], &crc); | |
579 } | |
580 buf[i] = OWReadByte(); | |
581 if (crc != buf[8]) | |
582 return OW_TEMP_CRC_ERR; | |
583 temp = buf[0]; | |
584 if (buf[1] & 0x80) | |
585 temp -= 256; | |
586 | |
587 /* Chop off 0.5 degree bit */ | |
588 temp >>= 1; | |
589 | |
590 /* Calulate the fractional remainder */ | |
591 tfrac = buf[7] - buf[6]; | |
592 | |
593 /* Work in 100'th of degreess to save on floats */ | |
594 tfrac *= (int16_t)100; | |
595 | |
596 /* Divide by count */ | |
597 tfrac /= buf[7]; | |
598 | |
599 /* Subtract 0.25 deg from temp */ | |
600 tfrac += 75; | |
601 if (tfrac < 100) | |
602 temp--; | |
603 else | |
604 tfrac -= 100; | |
605 | |
606 i = temp; | |
607 temp *= 100; | |
608 temp += tfrac; | |
609 | |
610 return(temp); | |
611 } | |
612 | |
613 /* | |
614 * OWTempStatusStr | |
615 * | |
616 * Return a string for each OW_TEMP_xxx error code | |
617 * | |
618 * shrt = 1 returns short strings | |
619 * | |
620 */ | |
621 const char * | |
622 OWTempStatusStr(int16_t val, uint8_t shrt) { | |
623 if (val > OW_TEMP_BADVAL) { | |
624 if (shrt) | |
625 return PSTR("OK"); | |
626 else | |
627 return PSTR("OK"); | |
628 } | |
629 | |
630 switch (val) { | |
631 case OW_TEMP_WRONG_FAM: | |
632 if (shrt) | |
633 return PSTR("WrFam"); | |
634 else | |
635 return PSTR("Wrong family"); | |
636 break; | |
637 case OW_TEMP_CRC_ERR: | |
638 if (shrt) | |
639 return PSTR("CRCErr"); | |
640 else | |
641 return PSTR("CRC Error"); | |
642 break; | |
643 case OW_TEMP_NO_ROM: | |
644 if (shrt) | |
645 return PSTR("NoROM"); | |
646 else | |
647 return PSTR("ROM did not reply"); | |
648 break; | |
649 default: | |
650 if (shrt) | |
651 return PSTR("???"); | |
652 else | |
653 return PSTR("Unknown error code"); | |
654 break; | |
655 } | |
656 } |