Mercurial > ~darius > hgwebdir.cgi > avr
changeset 21:e82d15fa9a1a
Re-format the code.
author | darius |
---|---|
date | Mon, 12 Dec 2005 23:01:00 +1030 |
parents | 59c7fcf10ea0 |
children | bd792ebf813d |
files | 1wire.c |
diffstat | 1 files changed, 115 insertions(+), 114 deletions(-) [+] |
line wrap: on
line diff
--- a/1wire.c Mon Dec 12 22:57:46 2005 +1030 +++ b/1wire.c Mon Dec 12 23:01:00 2005 +1030 @@ -33,7 +33,7 @@ #include <stdio.h> #include <avr/io.h> #include <avr/pgmspace.h> - +#include <util/delay.h> #include "1wire.h" #include "1wire-delay.h" @@ -41,7 +41,7 @@ void uart_putsP(const char *addr); void uart_puts(const char *addr); void uart_getc(); -int uart_putc(char c); +char uart_putc(char c); #endif static uint8_t OW_LastDevice = 0; @@ -56,6 +56,7 @@ /*----------------------------------------------------------------------------- * Generate a 1-Wire reset, return 0 if presence pulse was found, * return 1 otherwise. + * * (NOTE: Does not handle alarm presence from DS2404/DS1994) */ uint8_t @@ -221,147 +222,147 @@ int8_t next_result; /* Init for search */ - bit_number = 1; - last_zero = 0; - rom_byte_number = 0; - rom_byte_mask = 1; - next_result = OW_NOMODULES; - lastcrc8 = 0; - crcaccum = 0; + bit_number = 1; + last_zero = 0; + rom_byte_number = 0; + rom_byte_mask = 1; + next_result = OW_NOMODULES; + lastcrc8 = 0; + crcaccum = 0; - /* if the last call was not the last one */ - if (!OW_LastDevice) { - /* check if reset first is requested */ - if (do_reset) { - /* reset the 1-wire - * if there are no parts on 1-wire, return 0 */ + /* if the last call was not the last one */ + if (!OW_LastDevice) { + /* check if reset first is requested */ + if (do_reset) { + /* reset the 1-wire + * if there are no parts on 1-wire, return 0 */ #if OW_DEBUG - uart_putsP(PSTR("Resetting\n\r")); + uart_putsP(PSTR("Resetting\n\r")); #endif - if (OWTouchReset()) { - /* reset the search */ - OW_LastDiscrepancy = 0; - OW_LastFamilyDiscrepancy = 0; + if (OWTouchReset()) { + /* reset the search */ + OW_LastDiscrepancy = 0; + OW_LastFamilyDiscrepancy = 0; #if OW_DEBUG - uart_putsP(PSTR("No devices on bus\n\r")); + uart_putsP(PSTR("No devices on bus\n\r")); #endif - return OW_NOPRESENCE; - } - } + return OW_NOPRESENCE; + } + } - /* If finding alarming devices issue a different command */ - if (alarm_only) - OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ - else - OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ + /* If finding alarming devices issue a different command */ + if (alarm_only) + OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ + else + OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ - /* pause before beginning the search */ - OWdelay(); - OWdelay(); - OWdelay(); + /* pause before beginning the search */ + OWdelay(); + OWdelay(); + OWdelay(); - /* loop to do the search */ - do { - /* read a bit and its compliment */ - bit_test = OWReadBit() << 1; - bit_test |= OWReadBit(); + /* loop to do the search */ + do { + /* read a bit and its compliment */ + bit_test = OWReadBit() << 1; + bit_test |= OWReadBit(); #if OW_DEBUG - sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); - uart_puts(errstr); + sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); + uart_puts(errstr); #endif - /* check for no devices on 1-wire */ - if (bit_test == 3) { + /* 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); + sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); + uart_puts(errstr); #endif - return(OW_BADWIRE); - } - else { - /* all devices coupled have 0 or 1 */ - if (bit_test > 0) - search_direction = !(bit_test & 0x01); /* bit write value for search */ - else { - /* if this discrepancy is before the Last Discrepancy - * on a previous OWNext then pick the same as last time */ - if (bit_number < OW_LastDiscrepancy) - search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0); - else - /* if equal to last pick 1, if not then pick 0 */ - search_direction = (bit_number == OW_LastDiscrepancy); + return(OW_BADWIRE); + } + else { + /* all devices coupled have 0 or 1 */ + if (bit_test > 0) + search_direction = !(bit_test & 0x01); /* bit write value for search */ + else { + /* if this discrepancy is before the Last Discrepancy + * on a previous OWNext then pick the same as last time */ + if (bit_number < OW_LastDiscrepancy) + search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0); + else + /* if equal to last pick 1, if not then pick 0 */ + search_direction = (bit_number == OW_LastDiscrepancy); - /* if 0 was picked then record its position in LastZero */ - if (search_direction == 0) { - last_zero = bit_number; + /* if 0 was picked then record its position in LastZero */ + if (search_direction == 0) { + last_zero = bit_number; - /* check for Last discrepancy in family */ - if (last_zero < 9) - OW_LastFamilyDiscrepancy = last_zero; - } - } + /* check for Last discrepancy in family */ + if (last_zero < 9) + OW_LastFamilyDiscrepancy = last_zero; + } + } - /* set or clear the bit in the ROM byte rom_byte_number - * with mask rom_byte_mask */ - if (search_direction == 1) - ROM[rom_byte_number] |= rom_byte_mask; - else - ROM[rom_byte_number] &= ~rom_byte_mask; + /* set or clear the bit in the ROM byte rom_byte_number + * with mask rom_byte_mask */ + if (search_direction == 1) + ROM[rom_byte_number] |= rom_byte_mask; + else + ROM[rom_byte_number] &= ~rom_byte_mask; - /* serial number search direction write bit */ - OWWriteBit(search_direction); + /* serial number search direction write bit */ + OWWriteBit(search_direction); - /* increment the byte counter bit_number - * and shift the mask rom_byte_mask */ - bit_number++; - rom_byte_mask <<= 1; + /* increment the byte counter bit_number + * and shift the mask rom_byte_mask */ + bit_number++; + rom_byte_mask <<= 1; - /* if the mask is 0 then go to new ROM byte rom_byte_number - * and reset mask */ - if (rom_byte_mask == 0) { - OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */ - lastcrc8 = crcaccum; + /* if the mask is 0 then go to new ROM byte rom_byte_number + * and reset mask */ + if (rom_byte_mask == 0) { + OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */ + lastcrc8 = crcaccum; - rom_byte_number++; - rom_byte_mask = 1; - } - } - } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ + rom_byte_number++; + rom_byte_mask = 1; + } + } + } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ - /* if the search was successful then */ - if (!(bit_number < 65) || lastcrc8) { - if (lastcrc8) { + /* if the search was successful then */ + if (!(bit_number < 65) || lastcrc8) { + if (lastcrc8) { #if OW_DEBUG - sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); - uart_puts(errstr); + sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); + uart_puts(errstr); #endif - next_result = OW_BADCRC; - } else { - /* search successful so set LastDiscrepancy,LastDevice,next_result */ - OW_LastDiscrepancy = last_zero; - OW_LastDevice = (OW_LastDiscrepancy == 0); + next_result = OW_BADCRC; + } else { + /* search successful so set LastDiscrepancy,LastDevice,next_result */ + OW_LastDiscrepancy = last_zero; + OW_LastDevice = (OW_LastDiscrepancy == 0); #if OW_DEBUG - sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); - uart_puts(errstr); + sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); + uart_puts(errstr); #endif - next_result = OW_FOUND; - } - } - } + next_result = OW_FOUND; + } + } + } - /* if no device found then reset counters so next 'next' will be - * like a first */ - if (next_result != OW_FOUND || ROM[0] == 0) { - OW_LastDiscrepancy = 0; - OW_LastDevice = 0; - OW_LastFamilyDiscrepancy = 0; - } + /* if no device found then reset counters so next 'next' will be + * like a first */ + if (next_result != OW_FOUND || ROM[0] == 0) { + OW_LastDiscrepancy = 0; + OW_LastDevice = 0; + OW_LastFamilyDiscrepancy = 0; + } - if (next_result == OW_FOUND && ROM[0] == 0x00) - next_result = OW_BADWIRE; + if (next_result == OW_FOUND && ROM[0] == 0x00) + next_result = OW_BADWIRE; - return next_result; + return next_result; }