Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: OneWire_Masters/DS2465/DS2465.cpp
- Revision:
- 27:d5aaefa252f1
- Parent:
- 26:a361e3f42ba5
- Child:
- 32:bce180b544ed
--- a/OneWire_Masters/DS2465/DS2465.cpp Wed Mar 23 15:25:40 2016 -0500 +++ b/OneWire_Masters/DS2465/DS2465.cpp Fri Mar 25 11:11:59 2016 -0500 @@ -1,4 +1,6 @@ #include "DS2465.hpp" +#include "RomId.hpp" +#include "mbed.h" #define I2C_WRITE 0 #define I2C_READ 1 @@ -97,7 +99,7 @@ // OneWireMaster::CmdResult DS2465::Compute_NextMasterSecret(bool swap, unsigned int pageNum, PageRegion region) { - uint8_t command[2] = { CMD_CNMS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; + std::uint8_t command[2] = { CMD_CNMS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; return WriteMemory(ADDR_CMD_REG, command, 2); } @@ -114,7 +116,7 @@ // OneWireMaster::CmdResult DS2465::Compute_WriteMAC(bool regwrite, bool swap, unsigned int pageNum, unsigned int segmentNum) const { - uint8_t command[2] = { CMD_CSWM, ((regwrite << 7) | (swap << 6) | (pageNum << 4) | segmentNum) }; + std::uint8_t command[2] = { CMD_CSWM, ((regwrite << 7) | (swap << 6) | (pageNum << 4) | segmentNum) }; return CWriteMemory(ADDR_CMD_REG, command, 2); } @@ -130,7 +132,7 @@ // OneWireMaster::CmdResult DS2465::Compute_AuthMAC(bool swap, unsigned int pageNum, PageRegion region) const { - uint8_t command[2] = { CMD_CSAM, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; + std::uint8_t command[2] = { CMD_CSAM, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; return CWriteMemory(ADDR_CMD_REG, command, 2); } @@ -146,7 +148,7 @@ // OneWireMaster::CmdResult DS2465::Compute_SSecret(bool swap, unsigned int pageNum, PageRegion region) { - uint8_t command[2] = { CMD_CSS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; + std::uint8_t command[2] = { CMD_CSS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; return WriteMemory(ADDR_CMD_REG, command, 2); } @@ -248,7 +250,7 @@ // OneWireMaster::CmdResult DS2465::CopyScratchpad(bool dest_secret, unsigned int pageNum, bool notFull, unsigned int segmentNum) { - uint8_t command[2] = { CMD_CPS, (dest_secret ? 0 : (0x80 | (pageNum << 4) | (notFull << 3) | segmentNum)) }; + std::uint8_t command[2] = { CMD_CPS, (dest_secret ? 0 : (0x80 | (pageNum << 4) | (notFull << 3) | segmentNum)) }; return WriteMemory(ADDR_CMD_REG, command, 2); } @@ -328,15 +330,19 @@ // OneWireMaster::CmdResult DS2465::OWLevel(OW_LEVEL new_level) { - // function only will turn back to non-strong pull-up - if (new_level != LEVEL_NORMAL) - return OneWireMaster::OperationFailure; + // function only will turn back to non-strong pull-up + if (new_level != LEVEL_NORMAL) + 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; + // clear the strong pull-up bit in the global config state + m_curConfig.cSPU = false; - // write the new config - return WriteConfig(m_curConfig); + // write the new config + return WriteConfig(m_curConfig); } //-------------------------------------------------------------------------- @@ -350,11 +356,15 @@ // OneWireMaster::CmdResult DS2465::OWSpeed(OW_SPEED new_speed) { - // set the speed - m_curConfig.c1WS = (new_speed == SPEED_OVERDRIVE); + // Requested speed is already set + if (m_curConfig.c1WS == (new_speed == SPEED_OVERDRIVE)) + return OneWireMaster::Success; + + // set the speed + m_curConfig.c1WS = (new_speed == SPEED_OVERDRIVE); - // write the new config - return WriteConfig(m_curConfig); + // write the new config + return WriteConfig(m_curConfig); } //-------------------------------------------------------------------------- @@ -365,7 +375,7 @@ // // Returns � The DS2465 status byte result from the triplet command // -OneWireMaster::CmdResult DS2465::Triplet(Direction search_direction, uint8_t & status) +OneWireMaster::CmdResult DS2465::Triplet(Direction search_direction, std::uint8_t & status) { // 1-Wire Triplet (Case B) // S AD,0 [A] 1WT [A] SS [A] Sr AD,1 [A] [Status] A [Status] A\ P @@ -375,7 +385,7 @@ // SS indicates byte containing search direction bit value in msbit OneWireMaster::CmdResult result; - uint8_t command[2] = { CMD_1WT, ((search_direction == DIRECTION_WRITE_ONE) ? 0x80 : 0x00) }; + 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) result = PollBusy(&status); @@ -533,7 +543,7 @@ // of length 'rx_len' from 1-Wire Net // 'rx_len' - length in bytes to read. Only valid numbers are 8,16,20,32; // -OneWireMaster::CmdResult DS2465::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len) +OneWireMaster::CmdResult DS2465::OWReadBlock(std::uint8_t *rx_buf, std::uint8_t rx_len) { // 1-Wire Receive Block (Case A) // S AD,0 [A] ADDR_CMD_REG [A] 1WRF [A] PR [A] P @@ -541,7 +551,7 @@ // PR indicates byte containing parameter OneWireMaster::CmdResult result; - uint8_t command[2] = { CMD_1WRF, rx_len }; + std::uint8_t command[2] = { CMD_1WRF, rx_len }; result = WriteMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) @@ -555,7 +565,7 @@ -OneWireMaster::CmdResult DS2465::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len) +OneWireMaster::CmdResult DS2465::OWWriteBlock(const std::uint8_t *tran_buf, std::uint8_t tran_len) { return OWWriteBlock(false, tran_buf, tran_len); } @@ -572,10 +582,10 @@ // to the 1-Wire Net // 'tran_len' - length in bytes to transfer. Only valid numbers are 8,16,20,32; // -OneWireMaster::CmdResult DS2465::OWWriteBlock(bool tx_mac, const uint8_t *tran_buf, uint8_t tran_len) +OneWireMaster::CmdResult DS2465::OWWriteBlock(bool tx_mac, const std::uint8_t *tran_buf, std::uint8_t tran_len) { OneWireMaster::CmdResult result; - uint8_t command[2] = { CMD_1WTB, (tx_mac ? 0xFF : tran_len) }; + std::uint8_t command[2] = { CMD_1WTB, (tx_mac ? 0xFF : tran_len) }; if (!tx_mac) { @@ -604,10 +614,10 @@ // // Returns: 8 bits read from 1-Wire Net // -OneWireMaster::CmdResult DS2465::OWReadByte(uint8_t & recvbyte, OW_LEVEL after_level) +OneWireMaster::CmdResult DS2465::OWReadByte(std::uint8_t & recvbyte, OW_LEVEL after_level) { OneWireMaster::CmdResult result; - uint8_t buf; + std::uint8_t buf; // 1-Wire Read Bytes (Case C) // S AD,0 [A] ADDR_CMD_REG [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A @@ -647,7 +657,7 @@ // Returns: true: bytes written and echo was the same // false: echo was not the same // -OneWireMaster::CmdResult DS2465::OWWriteByte(uint8_t sendbyte, OW_LEVEL after_level) +OneWireMaster::CmdResult DS2465::OWWriteByte(std::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 @@ -662,7 +672,7 @@ if (result != OneWireMaster::Success) return result; - uint8_t command[2] = { CMD_1WWB, sendbyte }; + std::uint8_t command[2] = { CMD_1WWB, sendbyte }; result = WriteMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) @@ -682,7 +692,7 @@ // Returns: 0: 0 bit read from sendbit // 1: 1 bit read from sendbit // -OneWireMaster::CmdResult DS2465::OWTouchBit(uint8_t & sendrecvbit, OW_LEVEL after_level) +OneWireMaster::CmdResult DS2465::OWTouchBit(std::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 @@ -697,8 +707,8 @@ if (result != OneWireMaster::Success) return result; - uint8_t command[2] = { CMD_1WSB, (sendrecvbit ? 0x80 : 0x00) }; - uint8_t status; + std::uint8_t command[2] = { CMD_1WSB, (sendrecvbit ? 0x80 : 0x00) }; + std::uint8_t status; result = WriteMemory(ADDR_CMD_REG, command, 2); @@ -721,7 +731,7 @@ // Returns: true write successful // false failure to complete write // -OneWireMaster::CmdResult DS2465::CWriteMemory(std::uint8_t addr, const std::uint8_t * buf, size_t bufLen) const +OneWireMaster::CmdResult DS2465::CWriteMemory(std::uint8_t addr, const std::uint8_t * buf, std::size_t bufLen) const { int i; @@ -769,7 +779,7 @@ // Returns: true read successful // false failure to complete read // -OneWireMaster::CmdResult DS2465::ReadMemory(std::uint8_t addr, std::uint8_t * buf, size_t bufLen, bool skip_set_pointer) const +OneWireMaster::CmdResult DS2465::ReadMemory(std::uint8_t addr, std::uint8_t * buf, std::size_t bufLen, bool skip_set_pointer) const { int i; @@ -843,12 +853,12 @@ -OneWireMaster::CmdResult DS2465::PollBusy(uint8_t * pStatus) +OneWireMaster::CmdResult DS2465::PollBusy(std::uint8_t * pStatus) { const unsigned int pollLimit = 200; OneWireMaster::CmdResult result; - uint8_t status; + std::uint8_t status; unsigned int pollCount = 0; // loop checking 1WB bit for completion of 1-Wire operation @@ -883,7 +893,7 @@ // [] indicates from slave OneWireMaster::CmdResult result; - uint8_t buf; + std::uint8_t buf; buf = CMD_1WRS; result = WriteMemory(ADDR_CMD_REG, &buf, 1); @@ -909,7 +919,7 @@ // SS status byte to read to verify state OneWireMaster::CmdResult result; - uint8_t buf; + std::uint8_t buf; buf = CMD_1WMR; result = WriteMemory(ADDR_CMD_REG, &buf, 1);