Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: OneWire_Masters/DS2465/DS2465.cpp
- Revision:
- 26:a361e3f42ba5
- Parent:
- 24:8942d8478d68
- Child:
- 27:d5aaefa252f1
diff -r bdb1c5a53b58 -r a361e3f42ba5 OneWire_Masters/DS2465/DS2465.cpp --- a/OneWire_Masters/DS2465/DS2465.cpp Tue Mar 22 15:18:00 2016 -0500 +++ b/OneWire_Masters/DS2465/DS2465.cpp Wed Mar 23 15:25:40 2016 -0500 @@ -262,11 +262,34 @@ // OneWireMaster::CmdResult DS2465::ConfigureAPU(bool apu_enable) { - // clear power down bit in the global config state - m_curConfig.cAPU = 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; +} + - // write the new config - return WriteConfig(m_curConfig); + +OneWireMaster::CmdResult DS2465::ConfigureSPU(bool spu_enable) +{ + OneWireMaster::CmdResult result; + if (m_curConfig.cSPU != spu_enable) + { + m_curConfig.cSPU = spu_enable; + result = WriteConfig(m_curConfig); + } + else + { + result = OneWireMaster::Success; + } + return result; } //-------------------------------------------------------------------------- @@ -275,125 +298,24 @@ // Returns: true successful // false failure during communication // -OneWireMaster::CmdResult DS2465::OWPowerUp(void) -{ - OneWireMaster::CmdResult rt; - - // clear power down bit in the global config state - m_curConfig.cPDN = false; - - // write the new config - rt = WriteConfig(m_curConfig); - - // delay 2ms to allow units to power up - wait_ms(2); - - return rt; -} - -//-------------------------------------------------------------------------- -// Power down 1-Wire using extended function -// -// Returns: true successful -// false program voltage not available -// -OneWireMaster::CmdResult DS2465::OWPowerDown(void) -{ - // set power down bit in the global config state - m_curConfig.cPDN = true; - - // write the new config - return WriteConfig(m_curConfig); -} - -//-------------------------------------------------------------------------- -// 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. -// -// '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 -// -OneWireMaster::CmdResult DS2465::OWReadBitPower(uint8_t applyPowerResponse) +OneWireMaster::CmdResult DS2465::ConfigurePowerDown(bool pdn_enable) { OneWireMaster::CmdResult result; - uint8_t rdbit; - - // set strong pull-up enable - m_curConfig.cSPU = true; - - // write the new config - result = WriteConfig(m_curConfig); - if (result != OneWireMaster::Success) - return result; - - // perform read bit - result = OWReadBit(rdbit); - if (result != OneWireMaster::Success) - return result; - - // check if response was correct, if not then turn off strong pull-up - if (rdbit != applyPowerResponse) + if (m_curConfig.cPDN != pdn_enable) { - OWLevel(LEVEL_NORMAL); - return OneWireMaster::OperationFailure; + m_curConfig.cPDN = pdn_enable; + result = WriteConfig(m_curConfig); + if (!pdn_enable) + wait_ms(2); // Delay 2ms to allow units to power up } - - return OneWireMaster::Success; -} - -//-------------------------------------------------------------------------- -// Read 8 bits of communication from the 1-Wire Net. After the -// 8 bits are read then change the level of the 1-Wire net. -// -// Returns: 8 bits read from 1-Wire Net -// -OneWireMaster::CmdResult DS2465::OWReadBytePower(uint8_t & recvbyte) -{ - OneWireMaster::CmdResult result; - - // set strong pull-up enable - m_curConfig.cSPU = true; - - // write the new config - result = WriteConfig(m_curConfig); - if (result != OneWireMaster::Success) - return result; - - // do the read byte - result = OWReadByte(recvbyte); + else + { + result = OneWireMaster::Success; + } return result; } //-------------------------------------------------------------------------- -// Send 8 bits of communication to the 1-Wire Net and verify that the -// 8 bits read from the 1-Wire Net is the same (write operation). -// The parameter 'sendbyte' least significant 8 bits are used. After the -// 8 bits are sent change the level of the 1-Wire net. -// -// 'sendbyte' - 8 bits to send (least significant bit) -// -OneWireMaster::CmdResult DS2465::OWWriteBytePower(uint8_t sendbyte) -{ - OneWireMaster::CmdResult result; - - // set strong pull-up enable - m_curConfig.cSPU = true; - - // write the new config - result = WriteConfig(m_curConfig); - if (result != OneWireMaster::Success) - return result; - - // perform write byte - return OWWriteByte(sendbyte); -} - -//-------------------------------------------------------------------------- // Set the 1-Wire Net line level pull-up to normal. The DS2465 does only // allows enabling strong pull-up on a bit or byte event. Consequently this // function only allows the MODE_STANDARD argument. To enable strong pull-up @@ -508,7 +430,7 @@ } // issue the search command - OWWriteByte(0xF0); + OneWireMaster::OWWriteByte(0xF0); // loop to do the search do @@ -677,33 +599,12 @@ } //-------------------------------------------------------------------------- -// The 'OWBlock' transfers a block of data to and from the -// 1-Wire Net. The result is returned in the same buffer. -// -// 'tran_buf' - pointer to a block of unsigned -// chars of length 'tran_len' that will be sent -// to the 1-Wire Net -// 'tran_len' - length in bytes to transfer -// -OneWireMaster::CmdResult DS2465::OWBlock(uint8_t *tran_buf, uint8_t tran_len) -{ - OneWireMaster::CmdResult result; - for (uint8_t i = 0; i < tran_len; i++) - { - result = OWTouchByte(tran_buf[i]); - if (result != OneWireMaster::Success) - break; - } - return result; -} - -//-------------------------------------------------------------------------- // Send 8 bits of read communication to the 1-Wire Net and return the // result 8 bits read from the 1-Wire Net. // // Returns: 8 bits read from 1-Wire Net // -OneWireMaster::CmdResult DS2465::OWReadByte(uint8_t & recvbyte) +OneWireMaster::CmdResult DS2465::OWReadByte(uint8_t & recvbyte, OW_LEVEL after_level) { OneWireMaster::CmdResult result; uint8_t buf; @@ -716,6 +617,10 @@ // // [] indicates from slave // DD data read + + result = ConfigureSPU(after_level == LEVEL_STRONG); + if (result != OneWireMaster::Success) + return result; buf = CMD_1WRB; result = WriteMemory(ADDR_CMD_REG, &buf, 1); @@ -742,7 +647,7 @@ // Returns: true: bytes written and echo was the same // false: echo was not the same // -OneWireMaster::CmdResult DS2465::OWWriteByte(uint8_t sendbyte) +OneWireMaster::CmdResult DS2465::OWWriteByte(uint8_t sendbyte, OW_LEVEL 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 @@ -752,6 +657,11 @@ // DD data to write OneWireMaster::CmdResult result; + + result = ConfigureSPU(after_level == LEVEL_STRONG); + if (result != OneWireMaster::Success) + return result; + uint8_t command[2] = { CMD_1WWB, sendbyte }; result = WriteMemory(ADDR_CMD_REG, command, 2); @@ -772,7 +682,7 @@ // Returns: 0: 0 bit read from sendbit // 1: 1 bit read from sendbit // -OneWireMaster::CmdResult DS2465::OWTouchBit(uint8_t & sendrecvbit) +OneWireMaster::CmdResult DS2465::OWTouchBit(uint8_t & sendrecvbit, OW_LEVEL 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 @@ -782,6 +692,11 @@ // BB indicates byte containing bit value in msbit OneWireMaster::CmdResult result; + + result = ConfigureSPU(after_level == LEVEL_STRONG); + if (result != OneWireMaster::Success) + return result; + uint8_t command[2] = { CMD_1WSB, (sendrecvbit ? 0x80 : 0x00) }; uint8_t status;