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/DS2465/DS2465.cpp
- Revision:
- 32:bce180b544ed
- Parent:
- 27:d5aaefa252f1
- Child:
- 33:a4c015046956
diff -r 7c684e49fa8f -r bce180b544ed OneWire_Masters/DS2465/DS2465.cpp --- a/OneWire_Masters/DS2465/DS2465.cpp Tue Mar 29 16:36:12 2016 -0500 +++ b/OneWire_Masters/DS2465/DS2465.cpp Wed Mar 30 16:50:29 2016 -0500 @@ -72,7 +72,7 @@ -DS2465::DS2465(I2C & I2C_interface, unsigned char I2C_address) +DS2465::DS2465(I2C & I2C_interface, std::uint8_t I2C_address) : m_I2C_interface(I2C_interface), m_I2C_address(I2C_address) { @@ -212,8 +212,8 @@ return (result == OneWireMaster::Success ? ISha256MacCoprocessor::Success : ISha256MacCoprocessor::OperationFailure); } -ISha256MacCoprocessor::CmdResult DS2465::Compute_SSecret(const unsigned char (&devicePage)[devicePage_len], const unsigned char (&deviceScratchpad)[deviceScratchpad_len], - const unsigned char (&SSecret_data)[SSecret_data_len]) +ISha256MacCoprocessor::CmdResult DS2465::Compute_SSecret(const std::uint8_t (&devicePage)[devicePage_len], const std::uint8_t (&deviceScratchpad)[deviceScratchpad_len], + const std::uint8_t (&SSecret_data)[SSecret_data_len]) { OneWireMaster::CmdResult result; int addr = ADDR_SPAD; @@ -254,61 +254,16 @@ return WriteMemory(ADDR_CMD_REG, command, 2); } -//-------------------------------------------------------------------------- -// APU enable or disable -// -// 'readflag' - 1 if reading current configuration -// 'apu_enable' - 1 to enable -// -// Returns: true if write successful, or return configuration value if reading -// -OneWireMaster::CmdResult DS2465::ConfigureAPU(bool apu_enable) -{ - OneWireMaster::CmdResult result; - if (m_curConfig.cAPU != apu_enable) - { - m_curConfig.cAPU = apu_enable; - result = WriteConfig(m_curConfig); - } - else - { - result = OneWireMaster::Success; - } - return result; -} -OneWireMaster::CmdResult DS2465::ConfigureSPU(bool spu_enable) +OneWireMaster::CmdResult DS2465::ConfigureLevel(OWLevel level) { OneWireMaster::CmdResult result; - if (m_curConfig.cSPU != spu_enable) - { - m_curConfig.cSPU = spu_enable; - result = WriteConfig(m_curConfig); - } - else + if (m_curConfig.cSPU != (level == LEVEL_STRONG)) { - result = OneWireMaster::Success; - } - return result; -} - -//-------------------------------------------------------------------------- -// Power up 1-Wire using extended function -// -// Returns: true successful -// false failure during communication -// -OneWireMaster::CmdResult DS2465::ConfigurePowerDown(bool pdn_enable) -{ - OneWireMaster::CmdResult result; - if (m_curConfig.cPDN != pdn_enable) - { - m_curConfig.cPDN = pdn_enable; - result = WriteConfig(m_curConfig); - if (!pdn_enable) - wait_ms(2); // Delay 2ms to allow units to power up + m_curConfig.cSPU = (level == LEVEL_STRONG); + result = WriteConfig(m_curConfig); } else { @@ -328,21 +283,12 @@ // // Returns: current 1-Wire Net level // -OneWireMaster::CmdResult DS2465::OWLevel(OW_LEVEL new_level) +OneWireMaster::CmdResult DS2465::OWSetLevel(OWLevel new_level) { - // function only will turn back to non-strong pull-up - if (new_level != LEVEL_NORMAL) + if (new_level == LEVEL_STRONG) return OneWireMaster::OperationFailure; - // Requested level is already set - if (!m_curConfig.cSPU) - return OneWireMaster::Success; - - // clear the strong pull-up bit in the global config state - m_curConfig.cSPU = false; - - // write the new config - return WriteConfig(m_curConfig); + return ConfigureLevel(new_level); } //-------------------------------------------------------------------------- @@ -354,7 +300,7 @@ // // Returns: current 1-Wire Net speed // -OneWireMaster::CmdResult DS2465::OWSpeed(OW_SPEED new_speed) +OneWireMaster::CmdResult DS2465::OWSetSpeed(OWSpeed new_speed) { // Requested speed is already set if (m_curConfig.c1WS == (new_speed == SPEED_OVERDRIVE)) @@ -375,7 +321,7 @@ // // Returns � The DS2465 status byte result from the triplet command // -OneWireMaster::CmdResult DS2465::Triplet(Direction search_direction, std::uint8_t & status) +OneWireMaster::CmdResult DS2465::OWTriplet(SearchDirection & search_direction, std::uint8_t & sbr, std::uint8_t & tsb) { // 1-Wire Triplet (Case B) // S AD,0 [A] 1WT [A] SS [A] Sr AD,1 [A] [Status] A [Status] A\ P @@ -388,153 +334,21 @@ std::uint8_t command[2] = { CMD_1WT, ((search_direction == DIRECTION_WRITE_ONE) ? 0x80 : 0x00) }; result = WriteMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) + { + std::uint8_t status; result = PollBusy(&status); + if (result == OneWireMaster::Success) + { + // check bit results in status byte + sbr = ((status & STATUS_SBR) == STATUS_SBR); + tsb = ((status & STATUS_TSB) == STATUS_TSB); + search_direction = ((status & STATUS_DIR) == STATUS_DIR) ? DIRECTION_WRITE_ONE : DIRECTION_WRITE_ZERO; + } + } return result; } //-------------------------------------------------------------------------- -// The 'OWSearch' 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. -// -// Returns: true (1) : when a 1-Wire device was found and it's -// Serial Number placed in the global ROM -// 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. -// -OneWireMaster::CmdResult DS2465::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; - 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 - 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; -} - -//-------------------------------------------------------------------------- // The 'OWReadBlock' receives a block of data from the // 1-Wire Net. The destination is the mac buffer (rx_mac=1) or // the scratchpad (rx_mac=0). The result is buffer is returned. @@ -614,7 +428,7 @@ // // Returns: 8 bits read from 1-Wire Net // -OneWireMaster::CmdResult DS2465::OWReadByte(std::uint8_t & recvbyte, OW_LEVEL after_level) +OneWireMaster::CmdResult DS2465::OWReadByte(std::uint8_t & recvbyte, OWLevel after_level) { OneWireMaster::CmdResult result; std::uint8_t buf; @@ -628,7 +442,7 @@ // [] indicates from slave // DD data read - result = ConfigureSPU(after_level == LEVEL_STRONG); + result = ConfigureLevel(after_level); if (result != OneWireMaster::Success) return result; @@ -657,7 +471,7 @@ // Returns: true: bytes written and echo was the same // false: echo was not the same // -OneWireMaster::CmdResult DS2465::OWWriteByte(std::uint8_t sendbyte, OW_LEVEL after_level) +OneWireMaster::CmdResult DS2465::OWWriteByte(std::uint8_t sendbyte, OWLevel after_level) { // 1-Wire Write Byte (Case B) // S AD,0 [A] ADDR_CMD_REG [A] 1WWB [A] DD [A] Sr AD,1 [A] [Status] A [Status] A\ P @@ -668,7 +482,7 @@ OneWireMaster::CmdResult result; - result = ConfigureSPU(after_level == LEVEL_STRONG); + result = ConfigureLevel(after_level); if (result != OneWireMaster::Success) return result; @@ -692,7 +506,7 @@ // Returns: 0: 0 bit read from sendbit // 1: 1 bit read from sendbit // -OneWireMaster::CmdResult DS2465::OWTouchBit(std::uint8_t & sendrecvbit, OW_LEVEL after_level) +OneWireMaster::CmdResult DS2465::OWTouchBit(std::uint8_t & sendrecvbit, OWLevel after_level) { // 1-Wire bit (Case B) // S AD,0 [A] ADDR_CMD_REG [A] 1WSB [A] BB [A] Sr AD,1 [A] [Status] A [Status] A\ P @@ -703,7 +517,7 @@ OneWireMaster::CmdResult result; - result = ConfigureSPU(after_level == LEVEL_STRONG); + result = ConfigureLevel(after_level); if (result != OneWireMaster::Success) return result; @@ -744,12 +558,12 @@ // DD memory data to write m_I2C_interface.start(); - if (m_I2C_interface.write((unsigned char)(m_I2C_address | I2C_WRITE)) != I2C_WRITE_OK) + if (m_I2C_interface.write((m_I2C_address | I2C_WRITE)) != I2C_WRITE_OK) { m_I2C_interface.stop(); return OneWireMaster::CommunicationWriteError; } - if (m_I2C_interface.write((unsigned char)addr) != I2C_WRITE_OK) + if (m_I2C_interface.write(addr) != I2C_WRITE_OK) { m_I2C_interface.stop(); return OneWireMaster::CommunicationWriteError; @@ -794,12 +608,12 @@ m_I2C_interface.start(); if (!skip_set_pointer) { - if (m_I2C_interface.write((unsigned char)(m_I2C_address | I2C_WRITE)) != I2C_WRITE_OK) + if (m_I2C_interface.write((m_I2C_address | I2C_WRITE)) != I2C_WRITE_OK) { m_I2C_interface.stop(); return OneWireMaster::CommunicationWriteError; } - if (m_I2C_interface.write((unsigned char)addr) != I2C_WRITE_OK) + if (m_I2C_interface.write(addr) != I2C_WRITE_OK) { m_I2C_interface.stop(); return OneWireMaster::CommunicationWriteError; @@ -807,7 +621,7 @@ m_I2C_interface.start(); } - if (m_I2C_interface.write((unsigned char)(m_I2C_address | I2C_READ)) != I2C_WRITE_OK) + if (m_I2C_interface.write((m_I2C_address | I2C_READ)) != I2C_WRITE_OK) { m_I2C_interface.stop(); return OneWireMaster::CommunicationWriteError; @@ -852,6 +666,13 @@ +DS2465::Config DS2465::CurrentConfig() const +{ + return m_curConfig; +} + + + OneWireMaster::CmdResult DS2465::PollBusy(std::uint8_t * pStatus) {