Mercurial > ~darius > hgwebdir.cgi > avr
changeset 10:eb1faf51968e
- Add some useful return values to search functions.
- Remove some unecessary trailing \'s in macros
- Replace some inline assembly I missed last time with C/macros.
author | darius |
---|---|
date | Mon, 12 Jul 2004 23:59:00 +0930 |
parents | 7ed10c59ba06 |
children | ccc39c9f320b |
files | 1wire-delay.h 1wire.c 1wire.h testavr.c |
diffstat | 4 files changed, 75 insertions(+), 103 deletions(-) [+] |
line wrap: on
line diff
--- a/1wire-delay.h Mon Jul 12 17:51:20 2004 +0930 +++ b/1wire-delay.h Mon Jul 12 23:59:00 2004 +0930 @@ -41,7 +41,7 @@ "L_%=: nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") /* 64 usec */ #define DELAY_B asm volatile( \ @@ -53,7 +53,7 @@ "nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") /* 60 usec */ #define DELAY_C asm volatile( \ @@ -65,7 +65,7 @@ "nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") /* 10 usec */ #define DELAY_D asm volatile( \ @@ -73,7 +73,7 @@ "L_%=: nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") /* 9 usec */ #define DELAY_E asm volatile( \ @@ -81,7 +81,7 @@ "L_%=: nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") /* 55 usec */ #define DELAY_F asm volatile( \ @@ -101,7 +101,7 @@ "nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") /* 0 usec */ #define DELAY_G @@ -124,7 +124,7 @@ "nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") /* 70 usec */ #define DELAY_I asm volatile( \ @@ -136,7 +136,7 @@ "nop\n\t" \ "dec r21\n\t" \ "brne L_%=\n\t" \ - ::: "r21"); + ::: "r21") #else #error No 1 wire delay routines for selected clock speed #endif
--- a/1wire.c Mon Jul 12 17:51:20 2004 +0930 +++ b/1wire.c Mon Jul 12 23:59:00 2004 +0930 @@ -47,25 +47,7 @@ static void OWdelay(void) { - asm volatile ( - "ldi r21, 50\n\t" - "_OWdelay:\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "dec r21\n\t" - "brne _OWdelay\n\t" - ::: "r21"); + DELAY_I; } /*----------------------------------------------------------------------------- @@ -75,62 +57,14 @@ */ int OWTouchReset(void) { - uint8_t result; + DELAY_G; + OWIREOUTPORT &= ~(_BV(OWIREOUTPIN)); + OWIREDDR |= _BV(OWIREOUTPIN); + DELAY_H; + OWIREDDR &= ~(_BV(OWIREOUTPIN)); + DELAY_I; - asm volatile ( - /* Delay G (0 usec) */ - "\n\t" - /* Drive bus low */ - "cbi %[out], %[opin]\n\t" - "sbi %[ddr], %[opin]\n\t" - /* Delay H (480 usec) */ - "ldi r21, 120\n\t" - "loopH:\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "dec r21\n\t" - "brne loopH\n\t" - /* Release bus */ - "cbi %[ddr], %[opin]\n\t" - /* Delay I (70 usec) */ - "ldi r21, 35\n\t" - "loopI:\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "dec r21\n\t" - "brne loopI\n\t" - /* Sample for presense */ - "ldi %[result], 0\n\t" - "sbi %[ddr], 1\n\t" - "sbi %[out], 1\n\t" - /* 1 == no presence */ - "sbic %[in], %[ipin]\n\t" - "ldi %[result], 1\n\t" - "cbi %[out], 1\n\t" - - : [result] "=r" (result) /* Outputs */ - : [out] "I" (_SFR_IO_ADDR(OWIREOUTPORT)), /* Inputs */ - [ddr] "I" (_SFR_IO_ADDR(OWIREDDR)), - [opin] "I" (OWIREOUTPIN), - [in] "I" (_SFR_IO_ADDR(OWIREINPORT)), - [ipin] "I" (OWIREINPIN) - : "r21"); /* Clobbers */ - - return(result); + return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0); } /*----------------------------------------------------------------------------- @@ -257,7 +191,12 @@ /*----------------------------------------------------------------------------- * Search algorithm from App note 187 (and 162) * - * Returns 1 when something is found, 0 if nothing present + * OWFirst/OWNext return.. + * 1 when something is found, + * 0 no more modules + * -1 if no presense pulse, + * -2 if bad CRC, + * -3 if bad wiring. */ int OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { @@ -274,9 +213,9 @@ int OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) { uint8_t bit_test, search_direction, bit_number; - uint8_t last_zero, rom_byte_number, next_result; - uint8_t rom_byte_mask; + uint8_t last_zero, rom_byte_number, rom_byte_mask; uint8_t lastcrc8, crcaccum; + int next_result; char errstr[30]; /* Init for search */ @@ -284,7 +223,7 @@ last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; - next_result = 0; + next_result = OW_NOMODULES; lastcrc8 = 0; crcaccum = 0; @@ -304,7 +243,7 @@ #if OW_DEBUG uart_putsP(PSTR("No devices on bus\n\r")); #endif - return 0; + return OW_NOPRESENCE; } } @@ -332,9 +271,11 @@ /* check for no devices on 1-wire */ if (bit_test == 3) { +#if OW_DEBUG sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); uart_puts(errstr); - break; +#endif + return(OW_BADWIRE); } else { /* all devices coupled have 0 or 1 */ @@ -391,7 +332,7 @@ if (lastcrc8) { sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); uart_puts(errstr); - next_result = 0; + next_result = OW_BADCRC; } else { /* search successful so set LastDiscrepancy,LastDevice,next_result */ OW_LastDiscrepancy = last_zero; @@ -400,20 +341,22 @@ sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); uart_puts(errstr); #endif - next_result = 1; + next_result = OW_FOUND; } } } /* if no device found then reset counters so next 'next' will be * like a first */ - if (!next_result || !ROM[0]) { + if (next_result != OW_FOUND || ROM[0] == 0) { OW_LastDiscrepancy = 0; OW_LastDevice = 0; OW_LastFamilyDiscrepancy = 0; - next_result = 0; } + if (next_result == OW_FOUND && ROM[0] == 0x00) + next_result = OW_BADWIRE; + return next_result; }
--- a/1wire.h Mon Jul 12 17:51:20 2004 +0930 +++ b/1wire.h Mon Jul 12 23:59:00 2004 +0930 @@ -47,6 +47,13 @@ #define OWIREDDR DDRC #define OWIREINPIN OWIREOUTPIN +/* Return codes for OWFirst()/OWNext() */ +#define OW_BADWIRE -3 +#define OW_BADCRC -2 +#define OW_NOPRESENCE -1 +#define OW_NOMODULES 0 +#define OW_FOUND 1 + /* General 1 wire commands */ #define OW_OVRDRV_SKIP_CMD 0x3c #define OW_SEARCH_ALRM_CMD 0xec
--- a/testavr.c Mon Jul 12 17:51:20 2004 +0930 +++ b/testavr.c Mon Jul 12 23:59:00 2004 +0930 @@ -174,9 +174,9 @@ uart_putsP(PSTR("Resetting... ")); if (OWTouchReset() == 1) - uart_putsP(PSTR("No presense\n\r")); + uart_putsP(PSTR("No presense pulse found\n\r")); else - uart_putsP(PSTR("Presense\n\r")); + uart_putsP(PSTR("Presense pulse found\n\r")); } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'e') { if (OWReadBit()) uart_putsP(PSTR("Read a 1\n\r")); @@ -291,15 +291,34 @@ uart_puts(cmdbuf); } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') { memset(ROM, 0, 8); - if (OWTouchReset()) { - uart_putsP(PSTR("No devices on bus\n\r")); - break; - } - if (OWFirst(ROM, 1, 0) == 0) { - uart_putsP(PSTR("No module found\n\r")); - break; - } + + i = OWFirst(ROM, 1, 0); do { + switch (i) { + case OW_BADWIRE: + uart_putsP(PSTR("Presense pulse, but no module found, bad module/cabling?\n\r")); + break; + + case OW_NOPRESENCE: + uart_putsP(PSTR("No presense pulse found\n\r")); + break; + + case OW_BADCRC: + uart_putsP(PSTR("Bad CRC\n\r")); + break; + + case OW_NOMODULES: + case OW_FOUND: + break; + + default: + uart_putsP(PSTR("Unknown error from 1 wire library\n\r")); + break; + } + + if (i != OW_FOUND) + break; + sprintf_P(cmdbuf, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"), ROM[0], ROM[1], @@ -310,7 +329,10 @@ ROM[6], ROM[7]); uart_puts(cmdbuf); - } while (OWNext(ROM, 1, 0)); + + i = OWNext(ROM, 1, 0); + } while (1); + #if 0 } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e' && cmdbuf[2] == 'd') {