Mercurial > ~darius > hgwebdir.cgi > avr
diff 1wire.c @ 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 | f9a085a0ba93 |
children | 4b141cc7cbd4 |
line wrap: on
line diff
--- 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; }