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_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp
- Revision:
- 34:11fffbe98ef9
- Parent:
- 33:a4c015046956
- Child:
- 49:36954b62f503
--- a/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Thu Mar 31 11:56:01 2016 -0500 +++ b/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Fri Apr 01 09:29:55 2016 -0500 @@ -27,17 +27,6 @@ #include "OneWire_Masters/OneWireMaster.h" #include "mbed.h" -// delay durations -#ifdef LOW_VOLTAGE -#define SHA_COMPUTATION_DELAY 4 -#define EEPROM_WRITE_DELAY 15 -#define SECRET_EEPROM_DELAY 200 -#else -#define SHA_COMPUTATION_DELAY 3 -#define EEPROM_WRITE_DELAY 10 -#define SECRET_EEPROM_DELAY 90 -#endif - // 1-Wire commands #define CMD_WRITE_MEMORY 0x55 #define CMD_READ_MEMORY 0xF0 @@ -52,25 +41,67 @@ #define CMD_PIO_READ 0xDD #define CMD_PIO_WRITE 0x96 -#define BLOCK_READ_PROTECT 0x80 -#define BLOCK_WRITE_PROTECT 0x40 -#define BLOCK_EPROM_PROTECT 0x20 -#define BLOCK_WRITE_AUTH_PROTECT 0x10 +DS28E15_22_25::BlockProtection::BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, std::uint8_t blockNum) +{ + setReadProtection(readProtection); + setWriteProtection(writeProtection); + setEepromEmulation(eepromEmulation); + setAuthProtection(authProtection); + setBlockNum(blockNum); +} + +void DS28E15_22_25::BlockProtection::setBlockNum(std::uint8_t blockNum) +{ + m_status &= ~blockNumMask; + m_status |= (blockNum & blockNumMask); +} + +bool DS28E15_22_25::BlockProtection::noProtection() const +{ + return !readProtection() && !writeProtection() && !eepromEmulation() && !authProtection(); +} -#define PROT_BIT_AUTHWRITE 0x10 -#define PROT_BIT_EPROM 0x20 -#define PROT_BIT_WRITE 0x40 -#define PROT_BIT_READ 0x80 +void DS28E15_22_25::BlockProtection::setReadProtection(bool readProtection) +{ + if (readProtection) + m_status |= readProtectionMask; + else + m_status &= ~readProtectionMask; +} + +void DS28E15_22_25::BlockProtection::setWriteProtection(bool writeProtection) +{ + if (writeProtection) + m_status |= writeProtectionMask; + else + m_status &= ~writeProtectionMask; +} -DS28E15_22_25::DS28E15_22_25(OneWireMaster& OW_master) - : m_OW_master(OW_master) +void DS28E15_22_25::BlockProtection::setEepromEmulation(bool eepromEmulation) +{ + if (eepromEmulation) + m_status |= eepromEmulationMask; + else + m_status &= ~eepromEmulationMask; +} + +void DS28E15_22_25::BlockProtection::setAuthProtection(bool authProtection) +{ + if (authProtection) + m_status |= authProtectionMask; + else + m_status &= ~authProtectionMask; +} + +DS28E15_22_25::DS28E15_22_25(OneWireMaster& OW_master, bool lowVoltage) + : lowVoltage(lowVoltage), m_OW_master(OW_master) { std::memset(manId, 0x00, manId.length); } -DS28E15_22_25::DevicePages DS28E15_22_25::devicePages() +DS28E15_22_25::MemoryPages DS28E15_22_25::memoryPages() { - DevicePages pages; + MemoryPages pages; switch (romId.familyCode()) { @@ -94,9 +125,9 @@ return pages; } -DS28E15_22_25::DeviceBlocks DS28E15_22_25::deviceBlocks() +DS28E15_22_25::ProtectionBlocks DS28E15_22_25::protectionBlocks() { - DeviceBlocks blocks; + ProtectionBlocks blocks; switch (romId.familyCode()) { @@ -133,14 +164,14 @@ // Returns: true - protection written // false - Failed to set protection // -OneWireSlave::CmdResult DS28E15_22_25::WriteAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, std::uint8_t newProtection, std::uint8_t oldProtection, bool contflag) +OneWireSlave::CmdResult DS28E15_22_25::writeAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, const BlockProtection & newProtection, const BlockProtection & oldProtection, bool contflag) { std::uint8_t buf[256], cs; int cnt = 0; Mac mac; buf[cnt++] = CMD_WRITE_AUTH_PROTECT; - buf[cnt++] = newProtection; + buf[cnt++] = newProtection.status(); // Send command m_OW_master.OWWriteBlock(&buf[0], 2); @@ -152,7 +183,7 @@ m_OW_master.OWReadBytePower(buf[cnt++]); // now wait for the MAC computation. - wait_ms(SHA_COMPUTATION_DELAY); + wait_ms(shaComputationDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -162,7 +193,7 @@ return CommunicationError; ISha256MacCoprocessor::CmdResult result; - result = CalculateProtectionWriteMAC256(MacCoproc, newProtection, oldProtection, romId, manId, mac); + result = calculateProtectionWriteMac(MacCoproc, newProtection, oldProtection, romId, manId, mac); if (result != ISha256MacCoprocessor::Success) return OperationFailure; cnt = 0; @@ -186,8 +217,8 @@ // DATASHEET_CORRECTION - last bit in release is a read-zero so don't check echo of write byte m_OW_master.OWWriteBytePower(0xAA); - // now wait for the MAC computation. - wait_ms(EEPROM_WRITE_DELAY); + // now wait for the programming. + wait_ms(eepromWriteDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -212,7 +243,7 @@ // Returns: true - protection written // false - Failed to set protection // -OneWireSlave::CmdResult DS28E15_22_25::WriteBlockProtection(std::uint8_t protection, bool continuing) +OneWireSlave::CmdResult DS28E15_22_25::writeBlockProtection(const BlockProtection & protection, bool continuing) { std::uint8_t buf[256], cs; int cnt = 0; @@ -224,7 +255,7 @@ } // compute parameter byte - buf[cnt++] = protection; + buf[cnt++] = protection.status(); m_OW_master.OWWriteBlock(&buf[0], cnt); @@ -243,8 +274,8 @@ // sent release m_OW_master.OWWriteBytePower(0xAA); - // now wait for programming - wait_ms(EEPROM_WRITE_DELAY); + // now wait for the programming. + wait_ms(eepromWriteDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -261,12 +292,12 @@ -OneWireSlave::CmdResult DS28E15_22_25::ReadBlockProtectionStatus(unsigned int blockNum, std::uint8_t & status) +OneWireSlave::CmdResult DS28E15_22_25::readBlockProtection(unsigned int blockNum, BlockProtection & protection) { - unsigned char buf; - CmdResult result = ReadStatus(false, false, blockNum, &buf); + std::uint8_t buf; + CmdResult result = readStatus(false, false, blockNum, &buf); if (result == Success) - status = buf; + protection.setStatus(buf); return result; } @@ -285,9 +316,9 @@ // Returns: true - status read // false - Failed to read status // -OneWireSlave::CmdResult DS28E15_22_25::ReadStatus(bool personality, bool allpages, unsigned int pageNum, unsigned char *rdbuf) const +OneWireSlave::CmdResult DS28E15_22_25::readStatus(bool personality, bool allpages, unsigned int pageNum, std::uint8_t * rdbuf) const { - unsigned char buf[256]; + std::uint8_t buf[256]; int cnt, offset, rdnum; cnt = 0; @@ -350,7 +381,7 @@ -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateAuthMAC256(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, const Scratchpad & challenge, const Page & pageData, const RomId & romId, const ManId & manId, bool anon, Mac & mac) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::calculateAuthMac(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, const Scratchpad & challenge, const Page & pageData, const RomId & romId, const ManId & manId, bool anon, Mac & mac) { ISha256MacCoprocessor::AuthMacData authMacData; @@ -371,34 +402,6 @@ } //-------------------------------------------------------------------------- -// Verify provided MAC and page data. Optionally do -// annonymous mode (anon != 0). -// -// Parameters -// page_num - page number to read 0 - 16 -// challange - 32 byte buffer containing the challenge -// page_data - 32 byte buffer to contain the data read -// manid - 2 byte buffer containing the manufacturer ID (general device: 00h,00h) -// mac - 32 byte buffer of mac read -// anon - Flag to indicate Annonymous mode -// -// Returns: true - page read has correct MAC -// false - Failed to read page or incorrect MAC -// -ISha256MacCoprocessor::CmdResult DS28E15_22_25::AuthVerify(const ISha256MacCoprocessor & MacCoproc, unsigned int page_num, const Scratchpad & challenge, const Page & pageData, const RomId & romId, const ManId & manId, bool anon, const Mac & mac) -{ - Mac calc_mac; - ISha256MacCoprocessor::CmdResult result; - result = CalculateAuthMAC256(MacCoproc, page_num, challenge, pageData, romId, manId, anon, calc_mac); - if (result == ISha256MacCoprocessor::Success) - { - if (mac != calc_mac) - result = ISha256MacCoprocessor::OperationFailure; - } - return result; -} - -//-------------------------------------------------------------------------- // Do Compute Page MAC command and return MAC. Optionally do // annonymous mode (anon != 0). // @@ -412,7 +415,7 @@ // Returns: true - page read has correct MAC // false - Failed to read page or incorrect MAC // -OneWireSlave::CmdResult DS28E15_22_25::ComputeReadPageMAC(unsigned int page_num, bool anon, Mac & mac) const +OneWireSlave::CmdResult DS28E15_22_25::computeReadPageMAC(unsigned int page_num, bool anon, Mac & mac) const { std::uint8_t buf[256],cs; int cnt = 0; @@ -430,7 +433,7 @@ m_OW_master.OWReadBytePower(buf[cnt++]); // now wait for the MAC computation. - wait_ms(SHA_COMPUTATION_DELAY * 2); + wait_ms(shaComputationDelayMs * 2); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -468,7 +471,7 @@ // Return: true - load complete // false - error during load, device not present // -OneWireSlave::CmdResult DS28E15_22_25::ComputeSecret(unsigned int page_num, bool lock) +OneWireSlave::CmdResult DS28E15_22_25::computeSecret(unsigned int page_num, bool lock) { std::uint8_t buf[256], cs; int cnt = 0; @@ -490,8 +493,8 @@ // send release and strong pull-up m_OW_master.OWWriteBytePower(0xAA); - // now wait for the MAC computation. - wait_ms(SHA_COMPUTATION_DELAY * 2 + SECRET_EEPROM_DELAY); + // now wait for the MAC computations and secret programming. + wait_ms(shaComputationDelayMs * 2 + secretEepromWriteDelayMs()); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -514,7 +517,7 @@ // Return: true - select complete // false - error during select, device not present // -OneWireSlave::CmdResult DS28E15_22_25::WriteScratchpad(const Scratchpad & data) const +OneWireSlave::CmdResult DS28E15_22_25::writeScratchpad(const Scratchpad & data) const { std::uint8_t buf[256]; int cnt = 0, offset; @@ -566,7 +569,7 @@ // Return: true - load complete // false - error during load, device not present // -OneWireSlave::CmdResult DS28E15_22_25::LoadSecret(bool lock) +OneWireSlave::CmdResult DS28E15_22_25::loadSecret(bool lock) { std::uint8_t buf[256], cs; int cnt = 0; @@ -588,8 +591,8 @@ // send release and strong pull-up m_OW_master.OWWriteBytePower(0xAA); - // now wait for the MAC computation. - wait_ms(SECRET_EEPROM_DELAY); + // now wait for the secret programming. + wait_ms(secretEepromWriteDelayMs()); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -674,7 +677,7 @@ // Returns: true - block written // false - Failed to write block (no presence or invalid CRC16) // -OneWireSlave::CmdResult DS28E15_22_25::writeAuthSegmentMAC(unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Mac & mac, bool continuing) +OneWireSlave::CmdResult DS28E15_22_25::writeAuthSegmentMac(unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Mac & mac, bool continuing) { std::uint8_t buf[256], cs; int cnt, i, offset; @@ -711,8 +714,8 @@ // read the last CRC and enable power m_OW_master.OWReadBytePower(buf[cnt++]); - // now wait for the MAC computation. - wait_ms(SHA_COMPUTATION_DELAY); + // now wait for the MAC computation. + wait_ms(shaComputationDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -762,8 +765,8 @@ // send release and strong pull-up m_OW_master.OWWriteBytePower(0xAA); - // now wait for the MAC computation. - wait_ms(EEPROM_WRITE_DELAY); + // now wait for the programming. + wait_ms(eepromWriteDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -792,7 +795,7 @@ // Returns: true - mac calculated // false - Failed to calculate // -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateSegmentWriteMAC256(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Segment & oldData, const RomId & romId, const ManId & manId, Mac & mac) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::calculateSegmentWriteMac(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Segment & oldData, const RomId & romId, const ManId & manId, Mac & mac) { ISha256MacCoprocessor::WriteMacData MT; @@ -816,7 +819,7 @@ -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateProtectionWriteMAC256(const ISha256MacCoprocessor & MacCoproc, std::uint8_t newProtection, std::uint8_t oldProtection, const RomId & romId, const ManId & manId, Mac & mac) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::calculateProtectionWriteMac(const ISha256MacCoprocessor & MacCoproc, const BlockProtection & newProtection, const BlockProtection & oldProtection, const RomId & romId, const ManId & manId, Mac & mac) { ISha256MacCoprocessor::WriteMacData MT; @@ -825,21 +828,21 @@ // instert block and page MT[11] = 0; - MT[10] = newProtection & 0x0F; + MT[10] = newProtection.blockNum(); MT[9] = manId[0]; MT[8] = manId[1]; // old data - MT[12] = (oldProtection & PROT_BIT_AUTHWRITE) ? 0x01 : 0x00; - MT[13] = (oldProtection & PROT_BIT_EPROM) ? 0x01 : 0x00; - MT[14] = (oldProtection & PROT_BIT_WRITE) ? 0x01 : 0x00; - MT[15] = (oldProtection & PROT_BIT_READ) ? 0x01 : 0x00; + MT[12] = oldProtection.authProtection() ? 0x01 : 0x00; + MT[13] = oldProtection.eepromEmulation() ? 0x01 : 0x00; + MT[14] = oldProtection.writeProtection() ? 0x01 : 0x00; + MT[15] = oldProtection.readProtection() ? 0x01 : 0x00; // new data - MT[16] = (newProtection & PROT_BIT_AUTHWRITE) ? 0x01 : 0x00; - MT[17] = (newProtection & PROT_BIT_EPROM) ? 0x01 : 0x00; - MT[18] = (newProtection & PROT_BIT_WRITE) ? 0x01 : 0x00; - MT[19] = (newProtection & PROT_BIT_READ) ? 0x01 : 0x00; + MT[16] = newProtection.authProtection() ? 0x01 : 0x00; + MT[17] = newProtection.eepromEmulation() ? 0x01 : 0x00; + MT[18] = newProtection.writeProtection() ? 0x01 : 0x00; + MT[19] = newProtection.readProtection() ? 0x01 : 0x00; // compute the mac return MacCoproc.computeWriteMac(MT, mac); @@ -901,7 +904,7 @@ m_OW_master.OWReadBytePower(buf[cnt++]); // now wait for the MAC computation. - wait_ms(SHA_COMPUTATION_DELAY); + wait_ms(shaComputationDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -931,7 +934,7 @@ // compute the mac ISha256MacCoprocessor::CmdResult result; Mac mac; - result = CalculateSegmentWriteMAC256(MacCoproc, pageNum, segmentNum, newData, oldData, romId, manId, mac); + result = calculateSegmentWriteMac(MacCoproc, pageNum, segmentNum, newData, oldData, romId, manId, mac); if (result != ISha256MacCoprocessor::Success) return OperationFailure; @@ -958,8 +961,8 @@ // send release and strong pull-up m_OW_master.OWWriteBytePower(0xAA); - // now wait for the MAC computation. - wait_ms(EEPROM_WRITE_DELAY); + // now wait for the programming. + wait_ms(eepromWriteDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -1061,8 +1064,8 @@ // send release and strong pull-up m_OW_master.OWWriteBytePower(0xAA); - // now wait for the MAC computation. - wait_ms(EEPROM_WRITE_DELAY); + // now wait for the programming. + wait_ms(eepromWriteDelayMs); // disable strong pullup m_OW_master.OWSetLevel(OneWireMaster::LEVEL_NORMAL); @@ -1091,7 +1094,7 @@ // Returns: true if compute successful // false failed to do compute // -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateNextSecret(ISha256MacCoprocessor & MacCoproc, const Page & binding, const Scratchpad & partial, const RomId & romId, const ManId & manId, unsigned int pageNum) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::calculateNextSecret(ISha256MacCoprocessor & MacCoproc, const Page & binding, const Scratchpad & partial, const RomId & romId, const ManId & manId, unsigned int pageNum) { ISha256MacCoprocessor::SlaveSecretData slaveSecretData;