//-------------------------------------------------------------------------- // Send 1 bit of communication to the 1-Wire Net and verify that the // response matches the 'applyPowerResponse' bit and apply power delivery // to the 1-Wire net. Note that some implementations may apply the power // first and then turn it off if the response is incorrect. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'applyPowerResponse' - 1 bit response to check, if correct then start // power delivery // // Returns: TRUE: bit written and response correct, strong pullup now on // FALSE: response incorrect // SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse) { if(owTouchBit(portnum,0x01) != applyPowerResponse) return FALSE; if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5) return FALSE; return TRUE; }
//-------------------------------------------------------------------------- // Send 8 bits of communication to the 1-Wire Net and return the // result 8 bits read from the 1-Wire Net. The parameter 'sendbyte' // least significant 8 bits are used and the least significant 8 bits // of the result is the return byte. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'sendbyte' - 8 bits to send (least significant byte) // // Returns: 8 bytes read from sendbyte // SMALLINT owTouchByte(int portnum, SMALLINT sendbyte) { int i; SMALLINT dat = 0; for (i=0; i<8; i++) { dat |= owTouchBit(portnum,sendbyte & 0x1) << i; sendbyte >>= 1; } return dat; }
//-------------------------------------------------------------------------- // Send 8 bits of communication to the 1-Wire Net and return the // result 8 bits read from the 1-Wire Net. The parameter 'sendbyte' // least significant 8 bits are used and the least significant 8 bits // of the result is the return byte. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'sendbyte' - 8 bits to send (least significant byte) // // Returns: 8 bytes read from sendbyte // SMALLINT owTouchByte(int portnum, SMALLINT sendbyte) { int i; unsigned result = 0u; for (i = 0; i < 8; i++){ if (owTouchBit(portnum,sendbyte & 1) != 0) result |= (1u << i); sendbyte >>= 1; } return (SMALLINT)result; }
//-------------------------------------------------------------------------- // The 'owNext' function does a general search. This function // continues from the previos search state. The search state // can be reset by using the 'owFirst' function. // This function contains one parameter 'alarm_only'. // When 'alarm_only' is TRUE (1) the find alarm command // 0xEC is sent instead of the normal search command 0xF0. // Using the find alarm command 0xEC will limit the search to only // 1-Wire devices that are in an 'alarm' state. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not // perform reset before search. // 'alarm_only' - TRUE (1) the find alarm command 0xEC is // sent instead of the normal search command 0xF0 // // Returns: TRUE (1) : when a 1-Wire device was found and it's // Serial Number placed in the global SerialNum[portnum] // FALSE (0): when no new device was found. Either the // last search was the last device or there // are no devices on the 1-Wire Net. // SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only) { uchar bit_test, search_direction, bit_number; uchar last_zero, serial_byte_number, next_result; uchar serial_byte_mask; uchar lastcrc8; // initialize for search bit_number = 1; last_zero = 0; serial_byte_number = 0; serial_byte_mask = 1; next_result = 0; setcrc8(portnum,0); // if the last call was not the last one if (!LastDevice[portnum]) { // check if reset first is requested if (do_reset) { // reset the 1-wire // if there are no parts on 1-wire, return FALSE if (!owTouchReset(portnum)) { // printf("owTouchReset failed\r\n"); // reset the search LastDiscrepancy[portnum] = 0; LastFamilyDiscrepancy[portnum] = 0; OWERROR(OWERROR_NO_DEVICES_ON_NET); return FALSE; } } // If finding alarming devices issue a different command if (alarm_only) owWriteByte(portnum,0xEC); // issue the alarming search command else owWriteByte(portnum,0xF0); // issue the search command //pause before beginning the search //usDelay(100); // loop to do the search do { // read a bit and its compliment bit_test = owTouchBit(portnum,1) << 1; bit_test |= owTouchBit(portnum,1); // check for no devices on 1-wire if (bit_test == 3) break; 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 if before the Last Discrepancy // on a previous next then pick the same as last time if (bit_number < LastDiscrepancy[portnum]) search_direction = ((SerialNum[portnum][serial_byte_number] & serial_byte_mask) > 0); else // if equal to last pick 1, if not then pick 0 search_direction = (bit_number == LastDiscrepancy[portnum]); // 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) LastFamilyDiscrepancy[portnum] = last_zero; } } // set or clear the bit in the SerialNum[portnum] byte serial_byte_number // with mask serial_byte_mask if (search_direction == 1) SerialNum[portnum][serial_byte_number] |= serial_byte_mask; else SerialNum[portnum][serial_byte_number] &= ~serial_byte_mask; // serial number search direction write bit owTouchBit(portnum,search_direction); // increment the byte counter bit_number // and shift the mask serial_byte_mask bit_number++; serial_byte_mask <<= 1; // if the mask is 0 then go to new SerialNum[portnum] byte serial_byte_number // and reset mask if (serial_byte_mask == 0) { // The below has been added to accomidate the valid CRC with the // possible changing serial number values of the DS28E04. if (((SerialNum[portnum][0] & 0x7F) == 0x1C) && (serial_byte_number == 1)) lastcrc8 = docrc8(portnum,0x7F); else lastcrc8 = docrc8(portnum,SerialNum[portnum][serial_byte_number]); // accumulate the CRC serial_byte_number++; serial_byte_mask = 1; } } } while(serial_byte_number < 8); // loop until through all SerialNum[portnum] bytes 0-7 // if the search was successful then if (!((bit_number < 65) || lastcrc8)) { // search successful so set LastDiscrepancy[portnum],LastDevice[portnum],next_result LastDiscrepancy[portnum] = last_zero; LastDevice[portnum] = (LastDiscrepancy[portnum] == 0); next_result = TRUE; } } // if no device found then reset counters so next 'next' will be // like a first if (!next_result || !SerialNum[portnum][0]) { LastDiscrepancy[portnum] = 0; LastDevice[portnum] = FALSE; LastFamilyDiscrepancy[portnum] = 0; next_result = FALSE; } return next_result; }
//-------------------------------------------------------------------------- // The 'owNext' function does a general search. This function // continues from the previos search state. The search state // can be reset by using the 'owFirst' function. // This function contains one parameter 'alarm_only'. // When 'alarm_only' is TRUE (1) the find alarm command // 0xEC is sent instead of the normal search command 0xF0. // Using the find alarm command 0xEC will limit the search to only // 1-Wire devices that are in an 'alarm' state. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not // perform reset before search. // 'alarm_only' - TRUE (1) the find alarm command 0xEC is // sent instead of the normal search command 0xF0 // // Returns: TRUE (1) : when a 1-Wire device was found and it's // Serial Number placed in the global SerialNum[portnum] // FALSE (0): when no new device was found. Either the // last search was the last device or there // are no devices on the 1-Wire Net. // BYTE owNext(BYTE portnum, BYTE do_reset, BYTE alarm_only) { BYTE bit_test, search_direction, bit_number; BYTE last_zero, serial_byte_number, next_result; BYTE serial_byte_mask; BYTE lastcrc8; owSetCurrentPort(portnum); // initialize for search bit_number = 1; last_zero = 0; serial_byte_number = 0; serial_byte_mask = 1; next_result = 0; setcrc8(portnum, 0); // if the last call was not the last one if(!owNetCurrent.LastDevice) { // check if reset first is requested if(do_reset) { // reset the 1-wire // if there are no parts on 1-wire, return FALSE if(!owTouchReset(owCurrentPortnum)) { // reset the search owNetCurrent.LastDiscrepancy = 0; owNetCurrent.LastFamilyDiscrepancy = 0; OWERROR(OWERROR_NO_DEVICES_ON_NET); return FALSE; } } // If finding alarming devices issue a different command if(alarm_only) { owWriteByte(owCurrentPortnum, 0xEC); // issue the alarming search command } else { owWriteByte(owCurrentPortnum, 0xF0); // issue the search command } //pause before beginning the search //usDelay(100); // loop to do the search do { // read a bit and its compliment bit_test = owTouchBit(owCurrentPortnum, 1) << 1; bit_test |= owTouchBit(owCurrentPortnum, 1); // check for no devices on 1-wire if(bit_test == 3) { break; } // 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 if before the Last Discrepancy // on a previous next then pick the same as last time if(bit_number < owNetCurrent.LastDiscrepancy) { search_direction = ((owNetCurrent.SerialNum[serial_byte_number] & serial_byte_mask) > 0); } else { // if equal to last pick 1, if not then pick 0 search_direction = (bit_number == owNetCurrent.LastDiscrepancy); } // 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) { owNetCurrent.LastFamilyDiscrepancy = last_zero; } } } // set or clear the bit in the SerialNum byte serial_byte_number // with mask serial_byte_mask if(search_direction == 1) { owNetCurrent.SerialNum[serial_byte_number] |= serial_byte_mask; } else { owNetCurrent.SerialNum[serial_byte_number] &= ~serial_byte_mask; } // serial number search direction write bit owTouchBit(owCurrentPortnum, search_direction); // increment the byte counter bit_number // and shift the mask serial_byte_mask bit_number++; serial_byte_mask <<= 1; // if the mask is 0 then go to new SerialNum byte serial_byte_number // and reset mask if(serial_byte_mask == 0) { lastcrc8 = docrc8(owCurrentPortnum, owNetCurrent.SerialNum[serial_byte_number]); // accumulate the CRC serial_byte_number++; serial_byte_mask = 1; } } while(serial_byte_number < 8); // loop until through all SerialNum bytes 0-7 // if the search was successful then if(!((bit_number < 65) || lastcrc8)) { // search successful so set LastDiscrepancy, LastDevice, next_result owNetCurrent.LastDiscrepancy = last_zero; if(last_zero == 0) { owNetCurrent.LastDevice = TRUE; } else { owNetCurrent.LastDevice = FALSE; } // owNetCurrent.LastDevice = (last_zero == 0); next_result = TRUE; } } // if no device found then reset counters so next 'next' will be // like a first if(!next_result || !owNetCurrent.SerialNum[0]) { owNetCurrent.LastDiscrepancy = 0; owNetCurrent.LastDevice = FALSE; owNetCurrent.LastFamilyDiscrepancy = 0; next_result = FALSE; } return next_result; }