comparison 1wire.c @ 33:0aa6bf4b98ae

- Don't wrap individual debug statements in ifdef, use a conditionally defined macro instead. - Add OWProgROM routine, can't remember if I actually tested it tho :)
author darius
date Tue, 23 Oct 2007 10:51:35 +0930
parents b0cb873c0206
children 5898fba6593c
comparison
equal deleted inserted replaced
32:b0cb873c0206 33:0aa6bf4b98ae
41 #include <avr/pgmspace.h> 41 #include <avr/pgmspace.h>
42 #include <util/delay.h> 42 #include <util/delay.h>
43 #include "1wire.h" 43 #include "1wire.h"
44 #include "1wire-config.h" 44 #include "1wire-config.h"
45 45
46 #if OW_DEBUG
47 void uart_putsP(const char *addr);
48 void uart_puts(const char *addr);
49 void uart_getc();
50 char uart_putc(char c);
51 #endif
52
53 static uint8_t OW_LastDevice = 0; 46 static uint8_t OW_LastDevice = 0;
54 static uint8_t OW_LastDiscrepancy = 0; 47 static uint8_t OW_LastDiscrepancy = 0;
55 static uint8_t OW_LastFamilyDiscrepancy = 0; 48 static uint8_t OW_LastFamilyDiscrepancy = 0;
56 49
57 /*----------------------------------------------------------------------------- 50 /*-----------------------------------------------------------------------------
248 if (!OW_LastDevice) { 241 if (!OW_LastDevice) {
249 /* check if reset first is requested */ 242 /* check if reset first is requested */
250 if (do_reset) { 243 if (do_reset) {
251 /* reset the 1-wire 244 /* reset the 1-wire
252 * if there are no parts on 1-wire, return 0 */ 245 * if there are no parts on 1-wire, return 0 */
253 #if OW_DEBUG 246 OWPUTSP(PSTR("Resetting\n\r"));
254 uart_putsP(PSTR("Resetting\n\r"));
255 #endif
256 switch (OWTouchReset()) { 247 switch (OWTouchReset()) {
257 case 0: 248 case 0:
258 break; 249 break;
259 250
260 case 1: 251 case 1:
261 /* reset the search */ 252 /* reset the search */
262 OW_LastDiscrepancy = 0; 253 OW_LastDiscrepancy = 0;
263 OW_LastFamilyDiscrepancy = 0; 254 OW_LastFamilyDiscrepancy = 0;
264 #if OW_DEBUG 255 OWPUTSP(PSTR("No devices on bus\n\r"));
265 uart_putsP(PSTR("No devices on bus\n\r"));
266 #endif
267 return OW_NOPRESENCE; 256 return OW_NOPRESENCE;
268 break; 257 break;
269 258
270 case 2: 259 case 2:
271 /* reset the search */ 260 /* reset the search */
272 OW_LastDiscrepancy = 0; 261 OW_LastDiscrepancy = 0;
273 OW_LastFamilyDiscrepancy = 0; 262 OW_LastFamilyDiscrepancy = 0;
274 #if OW_DEBUG 263 OWPUTSP(PSTR("Bus appears to be being held low\n\r"));
275 uart_putsP(PSTR("Bus appears to be being held low\n\r"));
276 #endif
277 return OW_BADWIRE; 264 return OW_BADWIRE;
278 break; 265 break;
279 266
280 } 267 }
281 } 268 }
297 bit_test = OWReadBit() << 1; 284 bit_test = OWReadBit() << 1;
298 bit_test |= OWReadBit(); 285 bit_test |= OWReadBit();
299 286
300 #if OW_DEBUG 287 #if OW_DEBUG
301 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); 288 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
302 uart_puts(errstr); 289 OWPUTSP(errstr);
303 #endif 290 #endif
304 291
305 /* check for no devices on 1-wire */ 292 /* check for no devices on 1-wire */
306 if (bit_test == 3) { 293 if (bit_test == 3) {
307 #if OW_DEBUG 294 #if OW_DEBUG
308 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); 295 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
309 uart_puts(errstr); 296 OWPUTSP(errstr);
310 #endif 297 #endif
311 return(OW_BADWIRE); 298 return(OW_BADWIRE);
312 } 299 }
313 else { 300 else {
314 /* all devices coupled have 0 or 1 */ 301 /* all devices coupled have 0 or 1 */
363 /* if the search was successful then */ 350 /* if the search was successful then */
364 if (!(bit_number < 65) || lastcrc8) { 351 if (!(bit_number < 65) || lastcrc8) {
365 if (lastcrc8) { 352 if (lastcrc8) {
366 #if OW_DEBUG 353 #if OW_DEBUG
367 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); 354 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8);
368 uart_puts(errstr); 355 OWPUTSP(errstr);
369 #endif 356 #endif
370 next_result = OW_BADCRC; 357 next_result = OW_BADCRC;
371 } else { 358 } else {
372 /* search successful so set LastDiscrepancy,LastDevice,next_result */ 359 /* search successful so set LastDiscrepancy,LastDevice,next_result */
373 OW_LastDiscrepancy = last_zero; 360 OW_LastDiscrepancy = last_zero;
374 OW_LastDevice = (OW_LastDiscrepancy == 0); 361 OW_LastDevice = (OW_LastDiscrepancy == 0);
375 #if OW_DEBUG 362 #if OW_DEBUG
376 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); 363 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice);
377 uart_puts(errstr); 364 OWPUTSP(errstr);
378 #endif 365 #endif
379 next_result = OW_FOUND; 366 next_result = OW_FOUND;
380 } 367 }
381 } 368 }
382 } 369 }
413 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, 400 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
414 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, 401 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
415 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 402 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
416 }; 403 };
417 404
405 /*-----------------------------------------------------------------------------
406 * Update *crc based on the value of x
407 */
418 void 408 void
419 OWCRC(uint8_t x, uint8_t *crc) { 409 OWCRC(uint8_t x, uint8_t *crc) {
420 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]); 410 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]);
421 } 411 }
412
413 /*-----------------------------------------------------------------------------
414 * Program a DS2502's memory
415 *
416 * Arguments
417 * ROM - ROM ID (or NULL to send SKIP_ROM)
418 * start - Start address (bytes)
419 * len - Length of data to write
420 * data - Data to write
421 * exact - If true, only accept exact matches for programming,
422 * otherwise only ensure the bits we requested were
423 * programmed [to 0]
424 * status - If true program status rather than memory
425 *
426 * Returns..
427 * 0 if all is OK
428 * 1 if the programming was unsuccessful
429 * 2 if the parameters were invalid
430 * 3 if the DS2502 didn't respond appropriately (also happens if the
431 * module doesn't exist)
432 */
433 uint8_t
434 OWProgROM(uint8_t *ROM, uint8_t start, uint8_t len, uint8_t *data, uint8_t exact, uint8_t status) {
435 #if defined(OWSETVPPON) && defined(OWSETVPPOFF)
436 uint8_t crc, i, tmp;
437
438 /* Stupid programmer detection */
439 if (status) {
440 if (start + len > 3)
441 return(2);
442 } else {
443 if (start + len > 127)
444 return(2);
445 }
446
447 if (len < 1)
448 return(2);
449
450 OWDELAY_I;
451 if (OWTouchReset() != 0) {
452 uart_putsP(PSTR("No presence pulse\n\r"));
453 return(3);
454 }
455
456 crc = 0;
457
458 /* Send the command */
459 if (status) {
460 OWSendCmd(ROM, OW_WRITE_STATUS);
461 OWCRC(OW_WRITE_STATUS, &crc);
462 } else {
463 OWSendCmd(ROM, OW_WRITE_MEMORY);
464 OWCRC(OW_WRITE_MEMORY, &crc);
465 }
466
467 /* And the start address
468 * (2 bytes even though one would do)
469 */
470 OWWriteByte(start);
471 OWCRC(start, &crc);
472
473 OWWriteByte(0x00);
474 OWCRC(0x00, &crc);
475
476 for (i = 0; i < len; i++) {
477 uart_putsP(PSTR("Programming "));
478 uart_puts_hex(data[i]);
479 uart_putsP(PSTR(" to "));
480 uart_puts_hex(start + i);
481 uart_putsP(PSTR("\n\r"));
482
483 OWWriteByte(data[i]);
484 OWCRC(data[i], &crc);
485
486 tmp = OWReadByte();
487
488 if (crc != tmp) {
489 uart_putsP(PSTR("CRC mismatch "));
490 uart_puts_hex(crc);
491 uart_putsP(PSTR(" vs "));
492 uart_puts_hex(tmp);
493 uart_putsP(PSTR("\n\r"));
494
495 OWTouchReset();
496 return(3);
497 }
498
499 OWSETVPPON();
500 OWDELAY_H;
501 OWSETVPPOFF();
502
503 tmp = OWReadByte();
504
505 /* Check the bits we turned off are off */
506 /*
507 for (i = 0; i < 8; i++)
508 if (!(data[i] & 1 << i) && (tmp & 1 << i))
509 return(-3);
510 */
511 if ((!data[i] & tmp) != 0) {
512 uart_putsP(PSTR("Readback mismatch "));
513 uart_puts_hex(data[i]);
514 uart_putsP(PSTR(" vs "));
515 uart_puts_hex(data[i]);
516 uart_putsP(PSTR("\n\r"));
517
518 OWTouchReset();
519 return(3);
520 }
521
522 /* The DS2502 loads it's CRC register with the address of the
523 * next byte */
524 crc = 0;
525 OWCRC(start + i + 1, &crc);
526 }
527
528 return(0);
529 #else
530 return(1);
531 #endif
532 }
533