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:
- 50:e967f9befbd0
- Parent:
- 49:36954b62f503
- Child:
- 51:a65f031e997b
--- a/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Fri Apr 08 16:11:16 2016 -0500 +++ b/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Mon Apr 11 14:48:43 2016 -0500 @@ -152,7 +152,7 @@ return blocks; } -OneWireSlave::CmdResult DS28E15_22_25::writeAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, const BlockProtection & newProtection, const BlockProtection & oldProtection, bool contflag) +OneWireSlave::CmdResult DS28E15_22_25::writeAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, const BlockProtection & newProtection, const BlockProtection & oldProtection) { std::uint8_t buf[256], cs; int cnt = 0; @@ -220,16 +220,12 @@ return OperationFailure; } -OneWireSlave::CmdResult DS28E15_22_25::writeBlockProtection(const BlockProtection & protection, bool continuing) +OneWireSlave::CmdResult DS28E15_22_25::writeBlockProtection(const BlockProtection & protection) { std::uint8_t buf[256], cs; int cnt = 0; - // check if not continuing a previous block write - if (!continuing) - { - buf[cnt++] = CMD_WRITE_BLOCK_PROTECT; - } + buf[cnt++] = CMD_WRITE_BLOCK_PROTECT; // compute parameter byte buf[cnt++] = protection.statusByte(); @@ -244,10 +240,6 @@ if (OneWireMaster::calculateCRC16(buf, 0, cnt) != 0xB001) return CommunicationError; - // DATASHEET_CORRECTION, on continue need second release byte - if (continuing) - m_OW_master.OWWriteByte(0xAA); - // sent release m_OW_master.OWWriteBytePower(0xAA); @@ -275,66 +267,105 @@ return result; } -OneWireSlave::CmdResult DS28E15_22_25::readStatus(bool personality, bool allpages, unsigned int pageNum, std::uint8_t * rdbuf) const +template <DS28E15_22_25::ProtectionBlocks blocks> OneWireSlave::CmdResult DS28E15_22_25::readAllBlockProtection(BlockProtection (&protection)[blocks]) { - std::uint8_t buf[256]; - int cnt, offset, rdnum; + std::uint8_t buf[blocks]; + CmdResult result = readStatus(false, true, 0, buf); + if (result == Success) + { + for (std::size_t i = 0; i < blocks; i++) + protection[i].setStatusByte(buf[i]); + } + return result; +} - cnt = 0; - offset = 0; +OneWireSlave::CmdResult DS28E15_22_25::readAllBlockProtection(BlockProtection (&protection)[DS28E15_BLOCKS]) +{ + return readAllBlockProtection<DS28E15_BLOCKS>(protection); +} + +OneWireSlave::CmdResult DS28E15_22_25::readAllBlockProtection(BlockProtection (&protection)[DS28E25_BLOCKS]) +{ + return readAllBlockProtection<DS28E25_BLOCKS>(protection); +} - buf[cnt++] = CMD_READ_STATUS; - if (personality) - buf[cnt++] = 0xE0; - else if (!allpages) - buf[cnt++] = pageNum; - else - buf[cnt++] = 0; +OneWireSlave::CmdResult DS28E15_22_25::readPersonality(Personality & personality) +{ + return readStatus(true, false, 0, personality.bytes); +} + +OneWireSlave::CmdResult DS28E15_22_25::readStatus(bool personality, bool allpages, unsigned int blockNum, std::uint8_t * rdbuf) const +{ + const std::size_t crcLen = 4, ds28e22_25_pagesPerBlock = 2; + + std::uint8_t buf[256]; + std::size_t cnt = 0, offset = 0; - // send the command - m_OW_master.OWWriteBlock(&buf[0], 2); - - offset = cnt + 2; + buf[cnt++] = CMD_READ_STATUS; + if (personality) + buf[cnt++] = 0xE0; + else if (allpages) + buf[cnt++] = 0; + else + { + // Convert to page number for DS28E22 and DS28E25 + buf[cnt] = blockNum; + if ((romId.familyCode() == DS28E25_FAMILY) || (romId.familyCode() == DS28E22_FAMILY)) + buf[cnt] *= ds28e22_25_pagesPerBlock; + cnt++; + } - // adjust data length - if ((romId.familyCode() == DS28E25_FAMILY) || (romId.familyCode() == DS28E22_FAMILY)) - { - if (personality) - rdnum = 8; - else if (allpages) - rdnum = 20; - else - rdnum = 5; - } - else - { - if ((personality) || (allpages)) - rdnum = 8; - else - rdnum = 5; - } + // send the command + m_OW_master.OWWriteBlock(&buf[0], 2); + + offset = cnt + 2; + + // Set data length + std::size_t rdnum; + if (personality) + rdnum = 4; + else if (!allpages) + rdnum = 1; + else if ((romId.familyCode() == DS28E22_FAMILY) || (romId.familyCode() == DS28E25_FAMILY)) + rdnum = DS28E25_PAGES; // Need to read extra data on DS28E22 to get CRC16. + else // DS28E15 + rdnum = DS28E15_BLOCKS; + rdnum += crcLen; // Add in CRC length + + // Read the bytes + m_OW_master.OWReadBlock(&buf[cnt], rdnum); + cnt += rdnum; + + // check the first CRC16 + if (OneWireMaster::calculateCRC16(buf, 0, offset) != 0xB001) + return CommunicationError; - // Read the bytes - m_OW_master.OWReadBlock(&buf[cnt],rdnum); - cnt += rdnum; - - // check the first CRC16 - if (OneWireMaster::calculateCRC16(buf, 0, offset) != 0xB001) - return CommunicationError; + if (personality || allpages) + { + // check the second CRC16 + if (OneWireMaster::calculateCRC16(buf, offset, (cnt - offset)) != 0xB001) + return CommunicationError; + } - if ((((romId.familyCode() == DS28E25_FAMILY) || (romId.familyCode() == DS28E22_FAMILY)) - && (allpages || (pageNum == 15))) || - (personality || allpages || (pageNum == 1))) - { - // check the second CRC16 - if (OneWireMaster::calculateCRC16(buf, offset, (cnt - offset)) != 0xB001) - return CommunicationError; - } + // copy the data to the read buffer + rdnum -= crcLen; + if (allpages && ((romId.familyCode() == DS28E25_FAMILY) || (romId.familyCode() == DS28E22_FAMILY))) + { + if (romId.familyCode() == DS28E22_FAMILY) + rdnum -= (DS28E25_PAGES - DS28E22_PAGES); + + for (std::size_t i = 0; i < (rdnum / ds28e22_25_pagesPerBlock); i++) + { + rdbuf[i] = (buf[offset + (i * ds28e22_25_pagesPerBlock)] & 0xF0); // Upper nibble + rdbuf[i] |= ((buf[offset + (i * ds28e22_25_pagesPerBlock)] & 0x0F) / ds28e22_25_pagesPerBlock); // Lower nibble + } + } + else + { + std::memcpy(rdbuf, &buf[offset], rdnum); + } - // copy the data to the read buffer - memcpy(rdbuf, &buf[offset], rdnum - 4); - - return Success; + return Success; } ISha256MacCoprocessor::CmdResult DS28E15_22_25::computeAuthMac(const ISha256MacCoprocessor & MacCoproc, const Page & pageData, unsigned int pageNum, const Scratchpad & challenge, const RomId & romId, const ManId & manId, Mac & mac) @@ -872,19 +903,27 @@ return OperationFailure; } -OneWireSlave::CmdResult DS28E15_22_25::readSegment(unsigned int page, unsigned int segment, Segment & data) const +OneWireSlave::CmdResult DS28E15_22_25::readSegment(unsigned int page, unsigned int segment, Segment & data, bool continuing) const { OneWireMaster::CmdResult result; std::uint8_t buf[2]; - buf[0] = CMD_READ_MEMORY; - buf[1] = (segment << 5) | page; + if (!continuing) + { + buf[0] = CMD_READ_MEMORY; + buf[1] = (segment << 5) | page; + + // Transmit command + m_OW_master.OWWriteBlock(buf, 2); - // Transmit command - m_OW_master.OWWriteBlock(buf, 2); - - // Receive CRC - result = m_OW_master.OWReadBlock(buf, 2); + // Receive CRC + result = m_OW_master.OWReadBlock(buf, 2); + } + else if (segment == 0) + { + // Receive CRC from previous read + result = m_OW_master.OWReadBlock(buf, 2); + } // Receive data if (result == OneWireMaster::Success)