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