Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: Authenticators/DS28E15_22_25/DS28E15_22_25.cpp
- Revision:
- 77:529edb329ee0
- Parent:
- 76:84e6c4994e29
- Child:
- 78:0cbbac7f2016
diff -r 84e6c4994e29 -r 529edb329ee0 Authenticators/DS28E15_22_25/DS28E15_22_25.cpp --- a/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Sat May 14 14:27:56 2016 -0500 +++ b/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Mon May 16 10:36:30 2016 -0500 @@ -137,8 +137,8 @@ } } -DS28E15_22_25::DS28E15_22_25(OneWireMaster & owMaster, bool lowVoltage) - : OneWireSlave(&owMaster), lowVoltage(lowVoltage) +DS28E15_22_25::DS28E15_22_25(RandomAccessRomIterator & selector, bool lowVoltage) + : OneWireSlave(selector), lowVoltage(lowVoltage) { std::memset(manId, 0x00, manId.length); } @@ -200,29 +200,34 @@ uint8_t buf[256], cs; int cnt = 0; Mac mac; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = AuthWriteBlockProtection; buf[cnt++] = newProtection.statusByte(); // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // read first CRC byte - p_owMaster->OWReadByte(buf[cnt++]); + master().OWReadByte(buf[cnt++]); // read the last CRC and enable - p_owMaster->OWReadBytePower(buf[cnt++]); + master().OWReadBytePower(buf[cnt++]); // now wait for the MAC computation. wait_ms(shaComputationDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // check CRC16 if (OneWireMaster::calculateCrc16(buf, 0, cnt) != 0xB001) { - return CommunicationError; + return CrcError; } ISha256MacCoproc::CmdResult result; @@ -234,16 +239,16 @@ cnt = 0; // send the MAC - p_owMaster->OWWriteBlock(mac, mac.length); + master().OWWriteBlock(mac, mac.length); // Read CRC and CS byte - p_owMaster->OWReadBlock(&buf[cnt], 3); + master().OWReadBlock(&buf[cnt], 3); cnt += 3; // check CRC16 if (OneWireMaster::calculateCrc16(buf, 0, (cnt - 1), OneWireMaster::calculateCrc16(mac, 0, mac.length)) != 0xB001) { - return CommunicationError; + return CrcError; } // check CS @@ -254,16 +259,16 @@ // send release and strong pull-up // DATASHEET_CORRECTION - last bit in release is a read-zero so don't check echo of write byte - p_owMaster->OWWriteBytePower(0xAA); + master().OWWriteBytePower(0xAA); // now wait for the programming. wait_ms(eepromWriteDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs == 0xAA) { @@ -277,35 +282,40 @@ { uint8_t buf[256], cs; int cnt = 0; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = WriteBlockProtection; // compute parameter byte buf[cnt++] = protection.statusByte(); - p_owMaster->OWWriteBlock(&buf[0], cnt); + master().OWWriteBlock(&buf[0], cnt); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; // check CRC16 if (OneWireMaster::calculateCrc16(buf, 0, cnt) != 0xB001) { - return CommunicationError; + return CrcError; } // sent release - p_owMaster->OWWriteBytePower(0xAA); + master().OWWriteBytePower(0xAA); // now wait for the programming. wait_ms(eepromWriteDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs == 0xAA) { @@ -318,7 +328,9 @@ OneWireSlave::CmdResult DS28E15_22_25::readBlockProtection(unsigned int blockNum, BlockProtection & protection) { uint8_t buf; - CmdResult result = readStatus(false, false, blockNum, &buf); + CmdResult result; + + result = readStatus(false, false, blockNum, &buf); if (result == Success) { protection.setStatusByte(buf); @@ -361,6 +373,11 @@ uint8_t buf[256]; size_t cnt = 0, offset = 0; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = ReadStatus; if (personality) @@ -383,7 +400,7 @@ } // send the command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); offset = cnt + 2; @@ -408,13 +425,13 @@ rdnum += crcLen; // Add in CRC length // Read the bytes - p_owMaster->OWReadBlock(&buf[cnt], rdnum); + master().OWReadBlock(&buf[cnt], rdnum); cnt += rdnum; // check the first CRC16 if (OneWireMaster::calculateCrc16(buf, 0, offset) != 0xB001) { - return CommunicationError; + return CrcError; } if (personality || allpages) @@ -422,7 +439,7 @@ // check the second CRC16 if (OneWireMaster::calculateCrc16(buf, offset, (cnt - offset)) != 0xB001) { - return CommunicationError; + return CrcError; } } @@ -477,45 +494,50 @@ { uint8_t buf[256], cs; int cnt = 0; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = ComputePageMac; buf[cnt++] = ((anon) ? 0xE0 : 0x00) | page_num; // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // read first CRC byte - p_owMaster->OWReadByte(buf[cnt++]); + master().OWReadByte(buf[cnt++]); // read the last CRC and enable - p_owMaster->OWReadBytePower(buf[cnt++]); + master().OWReadBytePower(buf[cnt++]); // now wait for the MAC computation. wait_ms(shaComputationDelayMs * 2); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // check CRC16 if (OneWireMaster::calculateCrc16(buf, 0, cnt) != 0xB001) { - return CommunicationError; + return CrcError; } // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs != 0xAA) { return OperationFailure; } // read the MAC and CRC - p_owMaster->OWReadBlock(&buf[0], (Mac::length + 2)); + master().OWReadBlock(&buf[0], (Mac::length + 2)); // check CRC16 if (OneWireMaster::calculateCrc16(buf, 0, (Mac::length + 2)) != 0xB001) { - return CommunicationError; + return CrcError; } // copy MAC to return buffer @@ -528,34 +550,39 @@ { uint8_t buf[256], cs; int cnt = 0; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = ComputeAndLockSecret; buf[cnt++] = (lock) ? (0xE0 | page_num) : page_num; // lock flag // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; // check CRC16 if (OneWireMaster::calculateCrc16(buf, 0, cnt) != 0xB001) { - return CommunicationError; + return CrcError; } // send release and strong pull-up - p_owMaster->OWWriteBytePower(0xAA); + master().OWWriteBytePower(0xAA); // now wait for the MAC computations and secret programming. wait_ms(shaComputationDelayMs * 2 + secretEepromWriteDelayMs()); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs == 0xAA) { @@ -569,6 +596,11 @@ { uint8_t buf[256]; int cnt = 0, offset; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = ReadWriteScratchpad; if ((romId.familyCode() == DS28E25_Family) || (romId.familyCode() == DS28E22_Family)) @@ -581,10 +613,10 @@ } // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; offset = cnt; @@ -594,22 +626,22 @@ cnt += data.length; // Send the data - p_owMaster->OWWriteBlock(data, data.length); + master().OWWriteBlock(data, data.length); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; // check first CRC16 if (OneWireMaster::calculateCrc16(buf, 0, offset) != 0xB001) { - return CommunicationError; + return CrcError; } // check the second CRC16 if (OneWireMaster::calculateCrc16(buf, offset, (cnt - offset)) != 0xB001) { - return CommunicationError; + return CrcError; } return Success; @@ -619,6 +651,11 @@ { uint8_t buf[256]; int cnt = 0, offset; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = ReadWriteScratchpad; if ((romId.familyCode() == DS28E25_Family) || (romId.familyCode() == DS28E22_Family)) @@ -631,32 +668,32 @@ } // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; offset = cnt; // Receive the data - p_owMaster->OWReadBlock(&buf[cnt], data.length); + master().OWReadBlock(&buf[cnt], data.length); cnt += data.length; // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; // check first CRC16 if (OneWireMaster::calculateCrc16(buf, 0, offset) != 0xB001) { - return CommunicationError; + return CrcError; } // check the second CRC16 if (OneWireMaster::calculateCrc16(buf, offset, (cnt - offset)) != 0xB001) { - return CommunicationError; + return CrcError; } // Copy to output @@ -669,34 +706,39 @@ { uint8_t buf[256], cs; int cnt = 0; + + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } buf[cnt++] = LoadAndLockSecret; buf[cnt++] = (lock) ? 0xE0 : 0x00; // lock flag // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; // check CRC16 if (OneWireMaster::calculateCrc16(buf, 0, cnt) != 0xB001) { - return CommunicationError; + return CrcError; } // send release and strong pull-up - p_owMaster->OWWriteBytePower(0xAA); + master().OWWriteBytePower(0xAA); // now wait for the secret programming. wait_ms(secretEepromWriteDelayMs()); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs == 0xAA) { @@ -717,21 +759,26 @@ // check if not continuing a previous block write if (!continuing) { + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } + buf[cnt++] = ReadMemory; buf[cnt++] = page; // address // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; offset = cnt; } // read data and CRC16 - p_owMaster->OWReadBlock(&buf[cnt], (rdbuf.length + 2)); + master().OWReadBlock(&buf[cnt], (rdbuf.length + 2)); cnt += 34; // check the first CRC16 @@ -739,14 +786,14 @@ { if (OneWireMaster::calculateCrc16(buf, 0, offset) != 0xB001) { - return CommunicationError; + return CrcError; } } // check the second CRC16 if (OneWireMaster::calculateCrc16(buf, offset, (cnt - offset)) != 0xB001) { - return CommunicationError; + return CrcError; } // copy the data to the read buffer @@ -766,14 +813,19 @@ // check if not continuing a previous block write if (!continuing) { + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } + buf[cnt++] = AuthWriteMemory; buf[cnt++] = (segmentNum << 5) | pageNum; // address // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; offset = cnt; @@ -786,26 +838,26 @@ } // Send data - p_owMaster->OWWriteBlock(newData, newData.length); + master().OWWriteBlock(newData, newData.length); // read first CRC byte - p_owMaster->OWReadByte(buf[cnt++]); + master().OWReadByte(buf[cnt++]); // read the last CRC and enable power - p_owMaster->OWReadBytePower(buf[cnt++]); + master().OWReadBytePower(buf[cnt++]); // now wait for the MAC computation. wait_ms(shaComputationDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // check the first CRC16 if (!continuing) { if (OneWireMaster::calculateCrc16(buf, 0, offset) != 0xB001) { - return CommunicationError; + return CrcError; } } @@ -825,17 +877,17 @@ if (CRC16 != 0xB001) { - return CommunicationError; + return CrcError; } // transmit MAC as a block - p_owMaster->OWWriteBlock(mac, mac.length); + master().OWWriteBlock(mac, mac.length); // calculate CRC on MAC CRC16 = OneWireMaster::calculateCrc16(mac, 0, mac.length); // append read of CRC16 and CS byte - p_owMaster->OWReadBlock(&buf[0], 3); + master().OWReadBlock(&buf[0], 3); cnt = 3; // ckeck CRC16 @@ -843,7 +895,7 @@ if (CRC16 != 0xB001) { - return CommunicationError; + return CrcError; } // check CS @@ -853,16 +905,16 @@ } // send release and strong pull-up - p_owMaster->OWWriteBytePower(0xAA); + master().OWWriteBytePower(0xAA); // now wait for the programming. wait_ms(eepromWriteDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs == 0xAA) { @@ -933,14 +985,19 @@ // check if not continuing a previous block write if (!continuing) { + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } + buf[cnt++] = AuthWriteMemory; buf[cnt++] = (segmentNum << 5) | pageNum; // address // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; offset = cnt; @@ -953,26 +1010,26 @@ } // Send data - p_owMaster->OWWriteBlock(newData, newData.length); + master().OWWriteBlock(newData, newData.length); // read first CRC byte - p_owMaster->OWReadByte(buf[cnt++]); + master().OWReadByte(buf[cnt++]); // read the last CRC and enable power - p_owMaster->OWReadBytePower(buf[cnt++]); + master().OWReadBytePower(buf[cnt++]); // now wait for the MAC computation. wait_ms(shaComputationDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // check the first CRC16 if (!continuing) { if (OneWireMaster::calculateCrc16(buf, 0, offset) != 0xB001) { - return CommunicationError; + return CrcError; } } @@ -990,7 +1047,7 @@ if (CRC16 != 0xB001) { - return CommunicationError; + return CrcError; } // compute the mac @@ -1003,13 +1060,13 @@ } // transmit MAC as a block - p_owMaster->OWWriteBlock(mac, mac.length); + master().OWWriteBlock(mac, mac.length); // calculate CRC on MAC CRC16 = OneWireMaster::calculateCrc16(mac, 0, mac.length); // append read of CRC16 and CS byte - p_owMaster->OWReadBlock(&buf[0], 3); + master().OWReadBlock(&buf[0], 3); cnt = 3; // ckeck CRC16 @@ -1017,7 +1074,7 @@ if (CRC16 != 0xB001) { - return CommunicationError; + return CrcError; } // check CS @@ -1027,16 +1084,16 @@ } // send release and strong pull-up - p_owMaster->OWWriteBytePower(0xAA); + master().OWWriteBytePower(0xAA); // now wait for the programming. wait_ms(eepromWriteDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs == 0xAA) { @@ -1053,25 +1110,30 @@ if (!continuing) { + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } + buf[0] = ReadMemory; buf[1] = (segment << 5) | page; // Transmit command - p_owMaster->OWWriteBlock(buf, 2); + master().OWWriteBlock(buf, 2); // Receive CRC - result = p_owMaster->OWReadBlock(buf, 2); + result = master().OWReadBlock(buf, 2); } else if (segment == 0) { // Receive CRC from previous read - result = p_owMaster->OWReadBlock(buf, 2); + result = master().OWReadBlock(buf, 2); } // Receive data if (result == OneWireMaster::Success) { - result = p_owMaster->OWReadBlock(data, data.length); + result = master().OWReadBlock(data, data.length); } return (result == OneWireMaster::Success ? OneWireSlave::Success : OneWireSlave::CommunicationError); @@ -1088,14 +1150,19 @@ // check if not continuing a previous block write if (!continuing) { + if (selectDevice() != OneWireMaster::Success) + { + return CommunicationError; + } + buf[cnt++] = WriteMemory; buf[cnt++] = (block << 5) | page; // address // Send command - p_owMaster->OWWriteBlock(&buf[0], 2); + master().OWWriteBlock(&buf[0], 2); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; offset = cnt; @@ -1108,10 +1175,10 @@ } // Send data - p_owMaster->OWWriteBlock(data, data.length); + master().OWWriteBlock(data, data.length); // Read CRC - p_owMaster->OWReadBlock(&buf[cnt], 2); + master().OWReadBlock(&buf[cnt], 2); cnt += 2; // check the first CRC16 @@ -1119,27 +1186,27 @@ { if (OneWireMaster::calculateCrc16(buf, 0, offset) != 0xB001) { - return CommunicationError; + return CrcError; } } // check the second CRC16 if (OneWireMaster::calculateCrc16(buf, offset, (cnt - offset)) != 0xB001) { - return CommunicationError; + return CrcError; } // send release and strong pull-up - p_owMaster->OWWriteBytePower(0xAA); + master().OWWriteBytePower(0xAA); // now wait for the programming. wait_ms(eepromWriteDelayMs); // disable strong pullup - p_owMaster->OWSetLevel(OneWireMaster::NormalLevel); + master().OWSetLevel(OneWireMaster::NormalLevel); // read the CS byte - p_owMaster->OWReadByte(cs); + master().OWReadByte(cs); if (cs == 0xAA) {