Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: OneWire_Masters/DS2465/DS2465.cpp
- Revision:
- 34:11fffbe98ef9
- Parent:
- 33:a4c015046956
- Child:
- 35:5d23395628f6
--- a/OneWire_Masters/DS2465/DS2465.cpp Thu Mar 31 11:56:01 2016 -0500 +++ b/OneWire_Masters/DS2465/DS2465.cpp Fri Apr 01 09:29:55 2016 -0500 @@ -35,11 +35,6 @@ #define STATUS_TSB 0x40 #define STATUS_DIR 0x80 -// delays (if not polling for complete) -#define EEPROM_WRITE_DELAY 30 -#define LOAD_SECRET_DELAY 90 -#define SHA_COMPUTATION_DELAY 5 - static const int I2C_WRITE_OK = 0; @@ -83,7 +78,7 @@ OneWireMaster::CmdResult DS2465::OWInitMaster() { - return Detect(); + return detect(); } @@ -100,7 +95,7 @@ OneWireMaster::CmdResult DS2465::computeNextMasterSecret(bool swap, unsigned int pageNum, PageRegion region) { std::uint8_t command[2] = { CMD_CNMS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; - return WriteMemory(ADDR_CMD_REG, command, 2); + return writeMemory(ADDR_CMD_REG, command, 2); } //-------------------------------------------------------------------------- @@ -117,7 +112,7 @@ OneWireMaster::CmdResult DS2465::computeWriteMac(bool regwrite, bool swap, unsigned int pageNum, unsigned int segmentNum) const { std::uint8_t command[2] = { CMD_CSWM, ((regwrite << 7) | (swap << 6) | (pageNum << 4) | segmentNum) }; - return CWriteMemory(ADDR_CMD_REG, command, 2); + return cWriteMemory(ADDR_CMD_REG, command, 2); } //-------------------------------------------------------------------------- @@ -133,7 +128,7 @@ OneWireMaster::CmdResult DS2465::computeAuthMac(bool swap, unsigned int pageNum, PageRegion region) const { std::uint8_t command[2] = { CMD_CSAM, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; - return CWriteMemory(ADDR_CMD_REG, command, 2); + return cWriteMemory(ADDR_CMD_REG, command, 2); } //-------------------------------------------------------------------------- @@ -149,7 +144,7 @@ OneWireMaster::CmdResult DS2465::computeSlaveSecret(bool swap, unsigned int pageNum, PageRegion region) { std::uint8_t command[2] = { CMD_CSS, (swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) }; - return WriteMemory(ADDR_CMD_REG, command, 2); + return writeMemory(ADDR_CMD_REG, command, 2); } @@ -158,11 +153,11 @@ ISha256MacCoprocessor::CmdResult DS2465::setMasterSecret(const Secret & secret) { OneWireMaster::CmdResult result; - result = WriteMemory(ADDR_SPAD, secret, secret.length); + result = writeMemory(ADDR_SPAD, secret, secret.length); if (result == OneWireMaster::Success) - result = CopyScratchpad(1, 0, 1, 0); + result = copyScratchpad(1, 0, 1, 0); if (result == OneWireMaster::Success) - wait_ms(8 * EEPROM_WRITE_DELAY); + wait_ms(8 * eepromWriteDelayMs); return (result == OneWireMaster::Success ? ISha256MacCoprocessor::Success : ISha256MacCoprocessor::OperationFailure); } @@ -170,15 +165,15 @@ { OneWireMaster::CmdResult result; // Write input data to scratchpad - result = WriteScratchpad(writeMacData, writeMacData.length); + result = writeScratchpad(writeMacData, writeMacData.length); // Compute MAC if (result == OneWireMaster::Success) result = computeWriteMac(false, false, 0, 0); if (result == OneWireMaster::Success) { - wait_ms(SHA_COMPUTATION_DELAY); + wait_ms(shaComputationDelayMs); // Read MAC from register - result = ReadMemory(ADDR_MAC_READ, mac, mac.length, true); + result = readMemory(ADDR_MAC_READ, mac, mac.length, true); } return (result == OneWireMaster::Success ? ISha256MacCoprocessor::Success : ISha256MacCoprocessor::OperationFailure); } @@ -188,25 +183,25 @@ OneWireMaster::CmdResult result; int addr = ADDR_SPAD; // Write input data to scratchpad - result = CWriteMemory(addr, devicePage, devicePage.length); + result = cWriteMemory(addr, devicePage, devicePage.length); if (result == OneWireMaster::Success) { addr += devicePage.length; - result = CWriteMemory(addr, challenge, challenge.length); + result = cWriteMemory(addr, challenge, challenge.length); } if (result == OneWireMaster::Success) { addr += challenge.length; - result = CWriteMemory(addr, authMacData, authMacData.length); + result = cWriteMemory(addr, authMacData, authMacData.length); } // Compute MAC if (result == OneWireMaster::Success) result = computeAuthMac(false, 0, REGION_FULL_PAGE); if (result == OneWireMaster::Success) { - wait_ms(SHA_COMPUTATION_DELAY * 2); + wait_ms(shaComputationDelayMs * 2); // Read MAC from register - result = ReadMemory(ADDR_MAC_READ, mac, mac.length, true); + result = readMemory(ADDR_MAC_READ, mac, mac.length, true); } return (result == OneWireMaster::Success ? ISha256MacCoprocessor::Success : ISha256MacCoprocessor::OperationFailure); } @@ -216,52 +211,52 @@ OneWireMaster::CmdResult result; int addr = ADDR_SPAD; // Write input data to scratchpad - result = WriteMemory(addr, devicePage, devicePage.length); + result = writeMemory(addr, devicePage, devicePage.length); if (result == OneWireMaster::Success) { addr += devicePage.length; - result = WriteMemory(addr, deviceScratchpad, deviceScratchpad.length); + result = writeMemory(addr, deviceScratchpad, deviceScratchpad.length); } if (result == OneWireMaster::Success) { addr += deviceScratchpad.length; - result = WriteMemory(addr, slaveSecretData, slaveSecretData.length); + result = writeMemory(addr, slaveSecretData, slaveSecretData.length); } // Compute secret if (result == OneWireMaster::Success) result = computeSlaveSecret(false, 0, REGION_FULL_PAGE); if (result == OneWireMaster::Success) - wait_ms(SHA_COMPUTATION_DELAY * 2); + wait_ms(shaComputationDelayMs * 2); return (result == OneWireMaster::Success ? ISha256MacCoprocessor::Success : ISha256MacCoprocessor::OperationFailure); } //-------------------------------------------------------------------------- // Copy Scratchpad on DS2465 to either secret or memory page // -// 'dest_secret' - 1 if destination is secret, 0 if memory page -// 'page' - page number if dest_secret=0 +// 'destSecret' - 1 if destination is secret, 0 if memory page +// 'page' - page number if destSecret=0 // 'notfull' - 0 if only 4 byte segment, 1 if writing to full page, // 'seg' - Segment number if full=0. // // Returns: true write successful // false failure to complete read // -OneWireMaster::CmdResult DS2465::CopyScratchpad(bool dest_secret, unsigned int pageNum, bool notFull, unsigned int segmentNum) +OneWireMaster::CmdResult DS2465::copyScratchpad(bool destSecret, unsigned int pageNum, bool notFull, unsigned int segmentNum) { - std::uint8_t command[2] = { CMD_CPS, (dest_secret ? 0 : (0x80 | (pageNum << 4) | (notFull << 3) | segmentNum)) }; - return WriteMemory(ADDR_CMD_REG, command, 2); + std::uint8_t command[2] = { CMD_CPS, (destSecret ? 0 : (0x80 | (pageNum << 4) | (notFull << 3) | segmentNum)) }; + return writeMemory(ADDR_CMD_REG, command, 2); } -OneWireMaster::CmdResult DS2465::ConfigureLevel(OWLevel level) +OneWireMaster::CmdResult DS2465::configureLevel(OWLevel level) { OneWireMaster::CmdResult result; if (m_curConfig.cSPU != (level == LEVEL_STRONG)) { m_curConfig.cSPU = (level == LEVEL_STRONG); - result = WriteConfig(m_curConfig); + result = writeConfig(m_curConfig, true); } else { @@ -286,7 +281,7 @@ if (new_level == LEVEL_STRONG) return OneWireMaster::OperationFailure; - return ConfigureLevel(new_level); + return configureLevel(new_level); } //-------------------------------------------------------------------------- @@ -308,7 +303,7 @@ m_curConfig.c1WS = (new_speed == SPEED_OVERDRIVE); // write the new config - return WriteConfig(m_curConfig); + return writeConfig(m_curConfig, true); } //-------------------------------------------------------------------------- @@ -330,11 +325,11 @@ OneWireMaster::CmdResult result; std::uint8_t command[2] = { CMD_1WT, ((search_direction == DIRECTION_WRITE_ONE) ? 0x80 : 0x00) }; - result = WriteMemory(ADDR_CMD_REG, command, 2); + result = writeMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) { std::uint8_t status; - result = PollBusy(&status); + result = pollBusy(&status); if (result == OneWireMaster::Success) { // check bit results in status byte @@ -365,11 +360,11 @@ OneWireMaster::CmdResult result; std::uint8_t command[2] = { CMD_1WRF, rx_len }; - result = WriteMemory(ADDR_CMD_REG, command, 2); + result = writeMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) - result = PollBusy(); + result = pollBusy(); if (result == OneWireMaster::Success) - result = ReadMemory(ADDR_SPAD, rx_buf, rx_len, false); + result = readMemory(ADDR_SPAD, rx_buf, rx_len, false); return result; } @@ -402,7 +397,7 @@ if (!tx_mac) { // prefill scratchpad with required data - result = WriteMemory(ADDR_SPAD, tran_buf, tran_len); + result = writeMemory(ADDR_SPAD, tran_buf, tran_len); if (result != OneWireMaster::Success) return result; } @@ -412,10 +407,10 @@ // [] indicates from slave // PR indicates byte containing parameter - result = WriteMemory(ADDR_CMD_REG, command, 2); + result = writeMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) - result = PollBusy(); + result = pollBusy(); return result; } @@ -440,18 +435,18 @@ // [] indicates from slave // DD data read - result = ConfigureLevel(after_level); + result = configureLevel(after_level); if (result != OneWireMaster::Success) return result; buf = CMD_1WRB; - result = WriteMemory(ADDR_CMD_REG, &buf, 1); + result = writeMemory(ADDR_CMD_REG, &buf, 1); if (result == OneWireMaster::Success) - result = PollBusy(); + result = pollBusy(); if (result == OneWireMaster::Success) - result = ReadMemory(ADDR_DATA_REG, &buf, 1); + result = readMemory(ADDR_DATA_REG, &buf, 1); if (result == OneWireMaster::Success) recvbyte = buf; @@ -480,15 +475,15 @@ OneWireMaster::CmdResult result; - result = ConfigureLevel(after_level); + result = configureLevel(after_level); if (result != OneWireMaster::Success) return result; std::uint8_t command[2] = { CMD_1WWB, sendbyte }; - result = WriteMemory(ADDR_CMD_REG, command, 2); + result = writeMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) - result = PollBusy(); + result = pollBusy(); return result; } @@ -515,17 +510,17 @@ OneWireMaster::CmdResult result; - result = ConfigureLevel(after_level); + result = configureLevel(after_level); if (result != OneWireMaster::Success) return result; std::uint8_t command[2] = { CMD_1WSB, (sendrecvbit ? 0x80 : 0x00) }; std::uint8_t status; - result = WriteMemory(ADDR_CMD_REG, command, 2); + result = writeMemory(ADDR_CMD_REG, command, 2); if (result == OneWireMaster::Success) - result = PollBusy(&status); + result = pollBusy(&status); if (result == OneWireMaster::Success) sendrecvbit = (status & STATUS_SBR); @@ -543,7 +538,7 @@ // Returns: true write successful // false failure to complete write // -OneWireMaster::CmdResult DS2465::CWriteMemory(std::uint8_t addr, const std::uint8_t * buf, std::size_t bufLen) const +OneWireMaster::CmdResult DS2465::cWriteMemory(std::uint8_t addr, const std::uint8_t * buf, std::size_t bufLen) const { int i; @@ -586,12 +581,12 @@ // 'addr' - address to start reading // 'buf' - buffer to hold memory read // 'len' - length to read -// 'skip_set_pointer' - flag to indicate to skip setting address pointer +// 'skipSetPointer' - flag to indicate to skip setting address pointer // // Returns: true read successful // false failure to complete read // -OneWireMaster::CmdResult DS2465::ReadMemory(std::uint8_t addr, std::uint8_t * buf, std::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 skipSetPointer) const { int i; @@ -604,7 +599,7 @@ // DD memory data read m_I2C_interface.start(); - if (!skip_set_pointer) + if (!skipSetPointer) { if (m_I2C_interface.write((m_I2C_address | I2C_WRITE)) != I2C_WRITE_OK) { @@ -642,21 +637,24 @@ // Returns: true: config written and response correct // false: response incorrect // -OneWireMaster::CmdResult DS2465::WriteConfig(const Config & config) +OneWireMaster::CmdResult DS2465::writeConfig(const Config & config, bool verify) { std::uint8_t configBuf; OneWireMaster::CmdResult result; configBuf = config.writeByte(); - result = WriteMemory(ADDR_WCFG_REG, &configBuf, 1); - if (result == OneWireMaster::Success) + result = writeMemory(ADDR_WCFG_REG, &configBuf, 1); + if (verify) { - result = ReadMemory(ADDR_WCFG_REG, &configBuf, 1); - } - if (result == OneWireMaster::Success) - { + if (result == OneWireMaster::Success) + { + result = readMemory(ADDR_WCFG_REG, &configBuf, 1); + } + if (result == OneWireMaster::Success) + { if (configBuf != config.readByte()) - result = OneWireMaster::OperationFailure; + result = OneWireMaster::OperationFailure; + } } return result; @@ -664,7 +662,7 @@ -DS2465::Config DS2465::CurrentConfig() const +DS2465::Config DS2465::currentConfig() const { return m_curConfig; } @@ -672,7 +670,7 @@ -OneWireMaster::CmdResult DS2465::PollBusy(std::uint8_t * pStatus) +OneWireMaster::CmdResult DS2465::pollBusy(std::uint8_t * pStatus) { const unsigned int pollLimit = 200; @@ -685,7 +683,7 @@ do { - result = ReadMemory(ADDR_STATUS_REG, &status, 1, true); + result = readMemory(ADDR_STATUS_REG, &status, 1, true); if (result != OneWireMaster::Success) return result; if (pStatus != NULL) @@ -715,10 +713,10 @@ std::uint8_t buf; buf = CMD_1WRS; - result = WriteMemory(ADDR_CMD_REG, &buf, 1); + result = writeMemory(ADDR_CMD_REG, &buf, 1); if (result == OneWireMaster::Success) - result = PollBusy(&buf); + result = pollBusy(&buf); if (result == OneWireMaster::Success) { @@ -730,7 +728,7 @@ return result; } -OneWireMaster::CmdResult DS2465::Reset(void) +OneWireMaster::CmdResult DS2465::reset(void) { // Device Reset // S AD,0 [A] ADDR_CMD_REG [A] 1WMR [A] Sr AD,1 [A] [SS] A\ P @@ -741,10 +739,10 @@ std::uint8_t buf; buf = CMD_1WMR; - result = WriteMemory(ADDR_CMD_REG, &buf, 1); + result = writeMemory(ADDR_CMD_REG, &buf, 1); if (result == OneWireMaster::Success) - result = ReadMemory(ADDR_STATUS_REG, &buf, 1, true); + result = readMemory(ADDR_STATUS_REG, &buf, 1, true); if (result == OneWireMaster::Success) { @@ -758,12 +756,12 @@ return result; } -OneWireMaster::CmdResult DS2465::Detect(void) +OneWireMaster::CmdResult DS2465::detect() { OneWireMaster::CmdResult result; // reset DS2465 - result = Reset(); + result = reset(); if (result != OneWireMaster::Success) return result; @@ -771,6 +769,6 @@ m_curConfig.reset(); // write the default configuration setup - result = WriteConfig(m_curConfig); + result = writeConfig(m_curConfig, true); return result; }