1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.
Dependents: MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more
Superseded by MaximInterface.
Diff: OneWire_Masters/DS248x/ds248x.cpp
- Revision:
- 32:bce180b544ed
- Parent:
- 27:d5aaefa252f1
- Child:
- 69:f915c4c59a69
--- a/OneWire_Masters/DS248x/ds248x.cpp Tue Mar 29 16:36:12 2016 -0500 +++ b/OneWire_Masters/DS248x/ds248x.cpp Wed Mar 30 16:50:29 2016 -0500 @@ -267,7 +267,7 @@ //********************************************************************* -OneWireMaster::CmdResult Ds248x::search_triplet(uint8_t search_direction, uint8_t & status) +OneWireMaster::CmdResult Ds248x::OWTriplet(SearchDirection & search_direction, std::uint8_t & sbr, std::uint8_t & tsb) { OneWireMaster::CmdResult result; @@ -315,7 +315,11 @@ } else { - status = read_data; + // check bit results in status byte + sbr = ((read_data & STATUS_SBR) == STATUS_SBR); + tsb = ((read_data & STATUS_TSB) == STATUS_TSB); + search_direction = ((read_data & STATUS_DIR) == STATUS_DIR) ? DIRECTION_WRITE_ONE : DIRECTION_WRITE_ZERO; + result = OneWireMaster::Success; } } @@ -398,7 +402,7 @@ //********************************************************************* -OneWireMaster::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit, OW_LEVEL after_level) +OneWireMaster::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit, OWLevel after_level) { OneWireMaster::CmdResult result; @@ -468,7 +472,7 @@ //********************************************************************* -OneWireMaster::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte, OW_LEVEL after_level) +OneWireMaster::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte, OWLevel after_level) { OneWireMaster::CmdResult result; @@ -528,7 +532,7 @@ //********************************************************************* -OneWireMaster::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte, OW_LEVEL after_level) +OneWireMaster::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte, OWLevel after_level) { OneWireMaster::CmdResult result; @@ -658,135 +662,7 @@ //********************************************************************* -OneWireMaster::CmdResult Ds248x::OWSearch(RomId & romId) -{ - 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; - - // if the last call was not the last one - if (!_last_device_flag) - { - // 1-Wire reset - OneWireMaster::CmdResult result = OWReset(); - if (result != OneWireMaster::Success) - { - // reset the search - _last_discrepancy = 0; - _last_device_flag = false; - _last_family_discrepancy = 0; - return result; - } - - // issue the search command - OneWireMaster::OWWriteByte(0xF0); - - // 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; - } - - // 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)) - { - last_zero = id_bit_number; - - // check for Last discrepancy in family - if (last_zero < 9) - _last_family_discrepancy = last_zero; - } - - // 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; - - // 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 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; - - // check for last device - if (_last_discrepancy == 0) - _last_device_flag = true; - - search_result = true; - } - } - - // 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 ? OneWireMaster::Success : OneWireMaster::OperationFailure; -} - - -//********************************************************************* -OneWireMaster::CmdResult Ds248x::OWSpeed(OW_SPEED new_speed) +OneWireMaster::CmdResult Ds248x::OWSetSpeed(OWSpeed new_speed) { // set the speed if (new_speed == SPEED_OVERDRIVE) @@ -804,7 +680,7 @@ //********************************************************************* -OneWireMaster::CmdResult Ds248x::OWLevel(OW_LEVEL new_level) +OneWireMaster::CmdResult Ds248x::OWSetLevel(OWLevel new_level) { OneWireMaster::CmdResult result;