Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: OneWire_Masters/DS248x/ds248x.cpp
- Revision:
- 17:b646b1e3970b
- Parent:
- 15:f6cb0d906fb6
- Child:
- 21:00c94aeb533e
--- a/OneWire_Masters/DS248x/ds248x.cpp Wed Mar 16 19:10:19 2016 +0000 +++ b/OneWire_Masters/DS248x/ds248x.cpp Fri Mar 18 20:21:05 2016 +0000 @@ -1,16 +1,4 @@ /******************************************************************//** -* @file ds248x.cpp -* -* @author Justin Jordan -* -* @version 0.0.0 -* -* Started: 30JAN16 -* -* Updated: -* -* @brief Source file for Ds248x I2C to 1-wire master -*********************************************************************** * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a @@ -73,48 +61,39 @@ //********************************************************************* -bool Ds248x::OWInitMaster() +OneWireInterface::CmdResult Ds248x::OWInitMaster(void) { return(detect()); } //********************************************************************* -bool Ds248x::detect(void) +OneWireInterface::CmdResult Ds248x::detect(void) { - bool rtn_val = false; + OneWireInterface::CmdResult result; // reset the ds2484 ON selected address - if (!reset()) - { - rtn_val = false; - } - else + result = reset(); + if(result == OneWireInterface::Success) { // default configuration - _c1WS = false; - _cSPU = false; - _cPDN = false; - _cAPU = false; + _c1WS = 0; + _cSPU = 0; + _cPDN = 0; + _cAPU = 0; - // write the default configuration setup - if (!write_config(_c1WS | _cSPU | _cPDN | _cAPU)) - { - rtn_val = false; - } - else - { - rtn_val = true; - } + result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); } - return(rtn_val); + return result; } //********************************************************************* -bool Ds248x::reset(void) +OneWireInterface::CmdResult Ds248x::reset(void) { + OneWireInterface::CmdResult result; + char status; char packet[] = {CMD_DRST}; @@ -123,43 +102,74 @@ // [] indicates from slave // SS status byte to read to verify state - _p_i2c_bus->write(_w_adrs, packet, 1); - _p_i2c_bus->read(_r_adrs, &status, 1); + if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) + { + result = OneWireInterface::CommunicationWriteError; + } + else + { + if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + else + { + if((status & 0xF7) == 0x10) + { + result = OneWireInterface::Success; + } + else + { + result = OneWireInterface::OperationFailure; + } + } + } - return((status & 0xF7) == 0x10); + return result; } //********************************************************************* -bool Ds248x::write_config(uint8_t config) +OneWireInterface::CmdResult Ds248x::write_config(uint8_t config) { - bool rtn_val = false; + OneWireInterface::CmdResult result; + char read_config; char packet [] = {CMD_WCFG, (config | (~config << 4))}; - _p_i2c_bus->write(_w_adrs, packet, 2); - _p_i2c_bus->read(_r_adrs, &read_config, 1); - - // check for failure due to incorrect read back - if (config != read_config) + if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - // handle error - // ... - reset(); - rtn_val = false; + result = OneWireInterface::CommunicationWriteError; } else { - rtn_val = true; + if(_p_i2c_bus->read(_r_adrs, &read_config, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + else + { + // check for failure due to incorrect read back + if (config != read_config) + { + reset(); + result = OneWireInterface::OperationFailure; + } + else + { + result = OneWireInterface::Success; + } + } } - return(rtn_val); + return result; } //********************************************************************* -bool Ds248x::channel_select(uint8_t channel) +OneWireInterface::CmdResult Ds248x::channel_select(uint8_t channel) { + OneWireInterface::CmdResult result; char ch, ch_read, check; char packet [2]; @@ -186,18 +196,39 @@ packet[1] = ch; - _p_i2c_bus->write(_w_adrs, packet, 2); - _p_i2c_bus->read(_r_adrs, &check, 1); + if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) + { + result = OneWireInterface::CommunicationWriteError; + } + else + { + if(_p_i2c_bus->read(_r_adrs, &check, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + else + { + // check for failure due to incorrect read back of channel + if (check == ch_read) + { + result = OneWireInterface::Success; + } + else + { + result = OneWireInterface::OperationFailure; + } + } + } - // check for failure due to incorrect read back of channel - return (check == ch_read); + return result; } //********************************************************************* -bool Ds248x::adjust_timing(uint8_t param, uint8_t val) +OneWireInterface::CmdResult Ds248x::adjust_timing(uint8_t param, uint8_t val) { - bool rtn_val = false; + OneWireInterface::CmdResult result; + char read_port_config; char control_byte; @@ -205,34 +236,43 @@ char packet [] = {CMD_A1WP, control_byte}; - _p_i2c_bus->write(_w_adrs, packet, 2); - _p_i2c_bus->read(_r_adrs, &read_port_config, 1); - - // check for failure due to incorrect read back - if ((control_byte & 0x0F) != read_port_config) + if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) + { + result = OneWireInterface::CommunicationWriteError; + } + else { - // handle error - // ... - reset(); - - rtn_val = false; - } - else - { - rtn_val = true; + if(_p_i2c_bus->read(_r_adrs, &read_port_config, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + else + { + // check for failure due to incorrect read back + if ((control_byte & 0x0F) != read_port_config) + { + result = OneWireInterface::OperationFailure; + reset(); + } + else + { + result = OneWireInterface::Success; + } + } } - - return(rtn_val); + + return result; } //********************************************************************* -uint8_t Ds248x::search_triplet(uint8_t search_direction) +OneWireInterface::CmdResult Ds248x::search_triplet(uint8_t search_direction, uint8_t & status) { - uint8_t rtn_val = 0; + OneWireInterface::CmdResult result; + uint8_t poll_count = 0; - char status; char packet [] = {CMD_1WT, search_direction ? 0x80 : 0x00}; + char read_data; // 1-Wire Triplet (Case B) // S AD,0 [A] 1WT [A] SS [A] Sr AD,1 [A] [Status] A [Status] A\ P @@ -241,37 +281,52 @@ // [] indicates from slave // SS indicates byte containing search direction bit value in msbit - _p_i2c_bus->write(_w_adrs, packet, 2); - - // loop checking 1WB bit for completion of 1-Wire operation - // abort if poll limit reached - do + if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - _p_i2c_bus->read(_r_adrs, &status, 1); - } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); - - // check for failure due to poll limit reached - if (poll_count >= POLL_LIMIT) - { - // handle error - // ... - reset(); - rtn_val = false; + result = OneWireInterface::CommunicationWriteError; } else { - rtn_val = status; + // loop checking 1WB bit for completion of 1-Wire operation + // abort if poll limit reached + + //dummy write for loop + result = OneWireInterface::Success; //so far + do + { + if(_p_i2c_bus->read(_r_adrs, &read_data, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + } + while ((read_data & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + + if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + { + // check for failure due to poll limit reached + if(poll_count >= POLL_LIMIT) + { + // handle error + // ... + reset(); + result = OneWireInterface::TimeoutError; + } + } + else + { + status = read_data; + result = OneWireInterface::Success; + } } - - return(rtn_val); + + return result; } //********************************************************************* -bool Ds248x::OWReset() +OneWireInterface::CmdResult Ds248x::OWReset(void) { - bool rtn_val = false; + OneWireInterface::CmdResult result; uint8_t poll_count = 0; char status; @@ -283,58 +338,72 @@ // Repeat until 1WB bit has changed to 0 // [] indicates from slave - _p_i2c_bus->write(_w_adrs, packet, 1); - - // loop checking 1WB bit for completion of 1-Wire operation - // abort if poll limit reached - do + if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) { - _p_i2c_bus->read(_r_adrs, &status, 1); - } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); - - // check for failure due to poll limit reached - if (poll_count >= POLL_LIMIT) - { - // handle error - // ... - reset(); - rtn_val = false; + result = OneWireInterface::CommunicationWriteError; } else { - // check for short condition - if (status & STATUS_SD) + // loop checking 1WB bit for completion of 1-Wire operation + // abort if poll limit reached + //dummy write for loop + result = OneWireInterface::Success; //so far + do { - _short_detected = true; + if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + } + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + + if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + { + // check for failure due to poll limit reached + if(poll_count >= POLL_LIMIT) + { + // handle error + // ... + reset(); + result = OneWireInterface::TimeoutError; + } } else { - _short_detected = false; - } - - // check for presence detect - if (status & STATUS_PPD) - { - rtn_val = true; - } - else - { - rtn_val = false; + // check for short condition + if (status & STATUS_SD) + { + _short_detected = true; + } + else + { + _short_detected = false; + } + + // check for presence detect + if (status & STATUS_PPD) + { + result = OneWireInterface::Success; + } + else + { + result = OneWireInterface::OperationFailure; + } } } - - return(rtn_val); + + return result; } //********************************************************************* -uint8_t Ds248x::OWTouchBit(uint8_t sendbit) +OneWireInterface::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit) { - uint8_t rtn_val; + OneWireInterface::CmdResult result; + uint8_t poll_count = 0; char status; - char packet[] = {CMD_1WSB, sendbit ? 0x80 : 0x00}; + char packet[] = {CMD_1WSB, sendrecvbit ? 0x80 : 0x00}; // 1-Wire bit (Case B) // S AD,0 [A] 1WSB [A] BB [A] Sr AD,1 [A] [Status] A [Status] A\ P @@ -343,45 +412,60 @@ // [] indicates from slave // BB indicates byte containing bit value in msbit - _p_i2c_bus->write(_w_adrs, packet, 2); - - // loop checking 1WB bit for completion of 1-Wire operation - // abort if poll limit reached - do + if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - _p_i2c_bus->read(_r_adrs, &status, 1); - } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); - - // check for failure due to poll limit reached - if (poll_count >= POLL_LIMIT) - { - // handle error - // ... - reset(); - rtn_val = 0; + result = OneWireInterface::CommunicationWriteError; } else { - // return bit state - if (status & STATUS_SBR) + // loop checking 1WB bit for completion of 1-Wire operation + // abort if poll limit reached + //dummy write for loop + result = OneWireInterface::Success; //so far + do { - rtn_val = 1; + if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + } + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + + if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + { + // check for failure due to poll limit reached + if (poll_count >= POLL_LIMIT) + { + // handle error + // ... + reset(); + result = OneWireInterface::TimeoutError; + } } else { - rtn_val = 0; + // return bit state through out param + if (status & STATUS_SBR) + { + sendrecvbit = 1; + + } + else + { + sendrecvbit = 0; + } + result = OneWireInterface::Success; } } - - return(rtn_val); + + return result; } //********************************************************************* -bool Ds248x::OWWriteByte(uint8_t sendbyte) +OneWireInterface::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte) { - bool rtn_val = false; + OneWireInterface::CmdResult result; uint8_t poll_count = 0; char status; @@ -394,37 +478,50 @@ // [] indicates from slave // DD data to write - _p_i2c_bus->write(_w_adrs, packet, 2); - - // loop checking 1WB bit for completion of 1-Wire operation - // abort if poll limit reached - do + if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - _p_i2c_bus->read(_r_adrs, &status, 1); - } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); - - // check for failure due to poll limit reached - if (poll_count >= POLL_LIMIT) - { - // handle error - // ... - reset(); - rtn_val = false; + result = OneWireInterface::CommunicationWriteError; } else { - rtn_val = true; + // loop checking 1WB bit for completion of 1-Wire operation + // abort if poll limit reached + //dummy write for loop + result = OneWireInterface::Success; //so far + do + { + if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + } + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + + if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + { + // check for failure due to poll limit reached + if (poll_count >= POLL_LIMIT) + { + // handle error + // ... + reset(); + result = OneWireInterface::TimeoutError; + } + } + else + { + result = OneWireInterface::Success; + } } - return(rtn_val); + return result; } //********************************************************************* -uint8_t Ds248x::OWReadByte(void) +OneWireInterface::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte) { - uint8_t rtn_val; + OneWireInterface::CmdResult result; uint8_t poll_count = 0; char data, status; @@ -439,166 +536,231 @@ // [] indicates from slave // DD data read - _p_i2c_bus->write(_w_adrs, packet, 1); - - // loop checking 1WB bit for completion of 1-Wire operation - // abort if poll limit reached - do + if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) { - _p_i2c_bus->read(_r_adrs, &status, 1); - } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); - - // check for failure due to poll limit reached - if (poll_count >= POLL_LIMIT) - { - // handle error - // ... - reset(); - rtn_val = 0; + result = OneWireInterface::CommunicationWriteError; } else { - packet[0] = CMD_SRP; - packet[1] = 0xE1; - - _p_i2c_bus->write(_w_adrs, packet, 2); - _p_i2c_bus->read(_r_adrs, &data, 1); - - rtn_val = data; + // loop checking 1WB bit for completion of 1-Wire operation + // abort if poll limit reached + //dummy write for loop + result = OneWireInterface::Success; //so far + do + { + if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + } + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + + if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + { + // check for failure due to poll limit reached + if (poll_count >= POLL_LIMIT) + { + // handle error + // ... + reset(); + result = OneWireInterface::TimeoutError; + } + } + else + { + packet[0] = CMD_SRP; + packet[1] = 0xE1; + + if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) + { + result = OneWireInterface::CommunicationWriteError; + } + else + { + if(_p_i2c_bus->read(_r_adrs, &data, 1) != I2C_READ_OK) + { + result = OneWireInterface::CommunicationReadError; + } + else + { + recvbyte = data; + result = OneWireInterface::Success; + } + } + } } - return(rtn_val); + return result; +} + + +//********************************************************************* +OneWireInterface::CmdResult Ds248x::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len) +{ + OneWireInterface::CmdResult result; + + for(uint8_t idx = 0; idx < tran_len; idx++) + { + result = OWWriteByte(tran_buf[idx]); + if(result != OneWireInterface::Success) + { + break; + } + } + + return result; +} + + +//********************************************************************* +OneWireInterface::CmdResult Ds248x::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len) +{ + OneWireInterface::CmdResult result; + + for(uint8_t idx = 0; idx < rx_len; idx++) + { + //OwReadByte() uses pass by reference + result = OWReadByte(rx_buf[idx]); + if(result != OneWireInterface::Success) + { + break; + } + } + + return result; } //********************************************************************* -bool Ds248x::OWSearch(void) +OneWireInterface::CmdResult Ds248x::OWSearch(RomId & romId) { - uint8_t id_bit_number; - uint8_t last_zero, rom_byte_number, search_result; - uint8_t id_bit, cmp_id_bit; - uint8_t rom_byte_mask, search_direction, status; + int id_bit_number; + int last_zero, rom_byte_number; + int id_bit, cmp_id_bit; + unsigned char rom_byte_mask, status; + bool search_result; + unsigned char crc8 = 0; + SEARCH_DIRECTION search_direction; + + // initialize for search + id_bit_number = 1; + last_zero = 0; + rom_byte_number = 0; + rom_byte_mask = 1; + search_result = false; - // initialize for search - id_bit_number = 1; - last_zero = 0; - rom_byte_number = 0; - rom_byte_mask = 1; - search_result = false; - _crc8 = 0; + // if the last call was not the last one + if (!_last_device_flag) + { + // 1-Wire reset + OneWireInterface::CmdResult result = OWReset(); + if (result != OneWireInterface::Success) + { + // reset the search + _last_discrepancy = 0; + _last_device_flag = false; + _last_family_discrepancy = 0; + return result; + } + + // issue the search command + OWWriteByte(0xF0); - // if the last call was not the last one - if (!_last_device_flag) - { - // 1-Wire reset - if (!OWReset()) - { - // reset the search - _last_discrepancy = 0; - _last_device_flag = false; - _last_family_discrepancy = 0; - return false; - } - - // issue the search command - OWWriteByte(SEARCH_ROM); + // loop to do the search + do + { + // if this discrepancy if before the Last Discrepancy + // on a previous next then pick the same as last time + if (id_bit_number < _last_discrepancy) + { + if ((romId[rom_byte_number] & rom_byte_mask) > 0) + search_direction = DIRECTION_WRITE_ONE; + else + search_direction = DIRECTION_WRITE_ZERO; + } + else + { + // if equal to last pick 1, if not then pick 0 + if (id_bit_number == _last_discrepancy) + search_direction = DIRECTION_WRITE_ONE; + else + search_direction = DIRECTION_WRITE_ZERO; + } - // loop to do the search - do - { - // if this discrepancy if before the Last Discrepancy - // on a previous next then pick the same as last time - if (id_bit_number < _last_discrepancy) + // Peform a triple operation on the DS2465 which will perform 2 read bits and 1 write bit + search_triplet(search_direction, status); + + // check bit results in status byte + id_bit = ((status & STATUS_SBR) == STATUS_SBR); + cmp_id_bit = ((status & STATUS_TSB) == STATUS_TSB); + search_direction = ((status & STATUS_DIR) == STATUS_DIR) ? DIRECTION_WRITE_ONE : DIRECTION_WRITE_ZERO; + + // check for no devices on 1-wire + if ((id_bit) && (cmp_id_bit)) + break; + else + { + if ((!id_bit) && (!cmp_id_bit) && (search_direction == DIRECTION_WRITE_ZERO)) { - if ((_rom_number[rom_byte_number] & rom_byte_mask) > 0) - search_direction = 1; - else - search_direction = 0; - } - else - { - // if equal to last pick 1, if not then pick 0 - if (id_bit_number == _last_discrepancy) - search_direction = 1; - else - search_direction = 0; + last_zero = id_bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + _last_family_discrepancy = last_zero; } - // Perform a triple operation on the ds2484 which will perform 2 read bits and 1 write bit - status = search_triplet(search_direction); - - // check bit results in status byte - id_bit = ((status & STATUS_SBR) == STATUS_SBR); - cmp_id_bit = ((status & STATUS_TSB) == STATUS_TSB); - search_direction = ((status & STATUS_DIR) == STATUS_DIR) ? (unsigned char)1 : (unsigned char)0; - - // check for no devices on 1-Wire - if ((id_bit) && (cmp_id_bit)) - break; - else - { - if ((!id_bit) && (!cmp_id_bit) && (search_direction == 0)) - { - last_zero = id_bit_number; + // set or clear the bit in the ROM byte rom_byte_number + // with mask rom_byte_mask + if (search_direction == DIRECTION_WRITE_ONE) + romId[rom_byte_number] |= rom_byte_mask; + else + romId[rom_byte_number] &= (unsigned char)~rom_byte_mask; - // check for Last discrepancy in family - if (last_zero < 9) - _last_family_discrepancy = last_zero; - } + // increment the byte counter id_bit_number + // and shift the mask rom_byte_mask + id_bit_number++; + rom_byte_mask <<= 1; - // set or clear the bit in the ROM byte rom_byte_number - // with mask rom_byte_mask - if (search_direction == 1) - _rom_number[rom_byte_number] |= rom_byte_mask; - else - _rom_number[rom_byte_number] &= (unsigned char)~rom_byte_mask; - - // increment the byte counter id_bit_number - // and shift the mask rom_byte_mask - id_bit_number++; - rom_byte_mask <<= 1; + // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask + if (rom_byte_mask == 0) + { + crc8 = romId.calculateCRC8(crc8, romId[rom_byte_number]); // accumulate the CRC + rom_byte_number++; + rom_byte_mask = 1; + } + } + } + while(rom_byte_number < RomId::byteLen); // loop until through all ROM bytes 0-7 - // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask - if (rom_byte_mask == 0) - { - _crc8 = OWCalc_crc8(_rom_number[rom_byte_number], _crc8); // accumulate the CRC - 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 (!((id_bit_number <= (RomId::byteLen * 8)) || (crc8 != 0))) + { + // search successful so set m_last_discrepancy,m_last_device_flag,search_result + _last_discrepancy = last_zero; - // if the search was successful then - if (!((id_bit_number < 65) || (_crc8 != 0))) - { - // search successful so set LastDiscrepancy,LastDeviceFlag,search_result - _last_discrepancy = last_zero; + // check for last device + if (_last_discrepancy == 0) + _last_device_flag = true; - // check for last device - if (_last_discrepancy == 0) - _last_device_flag = true; - - search_result = true; - } - } + search_result = true; + } + } - // if no device found then reset counters so next 'search' will be like a first - if (!search_result || (_rom_number[0] == 0)) - { - _last_discrepancy = 0; - _last_device_flag = false; - _last_family_discrepancy = 0; - search_result = false; - } + // if no device found then reset counters so next 'search' will be like a first + if (!search_result || (romId.familyCode() == 0)) + { + _last_discrepancy = 0; + _last_device_flag = false; + _last_family_discrepancy = 0; + search_result = false; + } - return search_result; + return search_result ? OneWireInterface::Success : OneWireInterface::OperationFailure; } //********************************************************************* -uint8_t Ds248x::OWSpeed(OW_SPEED new_speed) +OneWireInterface::CmdResult Ds248x::OWSpeed(OW_SPEED new_speed) { // set the speed if (new_speed == SPEED_OVERDRIVE) @@ -607,94 +769,83 @@ } else { - _c1WS = false; + _c1WS = 0; } - // write the new config - write_config(_c1WS | _cSPU | _cPDN | _cAPU); - - return(new_speed); + // write the new config, and return result of op + return write_config(_c1WS | _cSPU | _cPDN | _cAPU); } //********************************************************************* -uint8_t Ds248x::OWLevel(OW_LEVEL new_level) +OneWireInterface::CmdResult Ds248x::OWLevel(OW_LEVEL new_level) { - uint8_t rtn_val; + OneWireInterface::CmdResult result; // function only will turn back to non-strong pull-up if (new_level != LEVEL_NORMAL) { - rtn_val = LEVEL_STRONG; + result = OneWireInterface::OperationFailure; } else { // clear the strong pull-up bit in the global config state - _cSPU = false; - // write the new config - write_config(_c1WS | _cSPU | _cPDN | _cAPU); - rtn_val = LEVEL_NORMAL; + _cSPU = 0; + + result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); } - return(rtn_val); + return result; } //********************************************************************* -bool Ds248x::OWWriteBytePower(uint8_t sendbyte) +OneWireInterface::CmdResult Ds248x::OWWriteBytePower(uint8_t sendbyte) { - bool rtn_val = false; + OneWireInterface::CmdResult result; // set strong pull-up enable _cSPU = CONFIG_SPU; // write the new config - if (!write_config(_c1WS | _cSPU | _cPDN | _cAPU)) - { - rtn_val = false; - } - else + result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); + if (result == OneWireInterface::Success) { // perform write byte - OWWriteByte(sendbyte); - rtn_val = true; + result = OWWriteByte(sendbyte); } - return(rtn_val); + return result; } //********************************************************************* -bool Ds248x::OWReadBitPower(uint8_t applyPowerResponse) +OneWireInterface::CmdResult Ds248x::OWReadBitPower(uint8_t applyPowerResponse) { - bool rtn_val = false; - + OneWireInterface::CmdResult result; uint8_t rdbit; // set strong pull-up enable _cSPU = CONFIG_SPU; // write the new config - if (!write_config(_c1WS | _cSPU | _cPDN | _cAPU)) - { - rtn_val = false; - } - else + result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); + if (result == OneWireInterface::Success) { // perform read bit - rdbit = OWReadBit(); - - // check if response was correct, if not then turn off strong pull-up - if (rdbit != applyPowerResponse) + result = OWReadBit(rdbit); + if(result == OneWireInterface::Success) { - OWLevel(LEVEL_NORMAL); - rtn_val = false; + // check if response was correct, if not then turn off strong pull-up + if (rdbit != applyPowerResponse) + { + OWLevel(LEVEL_NORMAL); + result = OneWireInterface::OperationFailure; + } } - - rtn_val = true; } - return(rtn_val); + return result; }