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