Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Revision:
78:0cbbac7f2016
Parent:
76:84e6c4994e29
Child:
79:7f22823a5a2d
diff -r 529edb329ee0 -r 0cbbac7f2016 Masters/DS2465/DS2465.cpp
--- a/Masters/DS2465/DS2465.cpp	Mon May 16 10:36:30 2016 -0500
+++ b/Masters/DS2465/DS2465.cpp	Mon May 16 15:18:09 2016 -0500
@@ -34,40 +34,43 @@
 #include "I2C.h"
 #include "wait_api.h"
 
+using namespace OneWire;
+
 #define I2C_WRITE 0
 #define I2C_READ 1
 
-// DS2465 commands
-#define CMD_1WMR   0xF0
-#define CMD_WCFG   0xD2
-#define CMD_CHSL   0xC3
-#define CMD_SRP    0xE1
+/// DS2465 Commands
+enum Command
+{
+    DeviceResetCmd = 0xF0,
+    WriteDeviceConfigCmd = 0xD2,
+    OwResetCmd = 0xB4,
+    OwWriteByteCmd = 0xA5,
+    OwReadByteCmd = 0x96,
+    OwSingleBitCmd = 0x87,
+    OwTripletCmd = 0x78,
+    OwTransmitBlockCmd = 0x69,
+    OwReceiveBlockCmd = 0xE1,
+    CopyScratchpadCmd = 0x5A,
+    ComputeSlaveSecretCmd = 0x4B,
+    ComputeSlaveAuthMacCmd = 0x3C,
+    ComputeSlaveWriteMacCmd = 0x2D,
+    ComputeNextMasterSecretCmd = 0x1E,
+    SetProtectionCmd = 0x0F
+};
 
-#define CMD_1WRS   0xB4
-#define CMD_1WWB   0xA5
-#define CMD_1WRB   0x96
-#define CMD_1WSB   0x87
-#define CMD_1WT    0x78
-#define CMD_1WTB   0x69
-#define CMD_1WRF   0xE1
-#define CMD_CPS    0x5A
-#define CMD_CSS    0x4B
-#define CMD_CSAM   0x3C
-#define CMD_CSWM   0x2D
-#define CMD_CNMS   0x1E
-#define CMD_SPR    0x0F
-
-// DS2465 status bits 
-#define STATUS_1WB  0x01
-#define STATUS_PPD  0x02
-#define STATUS_SD   0x04
-#define STATUS_LL   0x08
-#define STATUS_RST  0x10
-#define STATUS_SBR  0x20
-#define STATUS_TSB  0x40
-#define STATUS_DIR  0x80
-
-using namespace OneWire;
+/// DS2465 Status Bits
+enum StatusBit
+{
+    Status_1WB = 0x01,
+    Status_PPD = 0x02,
+    Status_SD = 0x04,
+    Status_LL = 0x08,
+    Status_RST = 0x10,
+    Status_SBR = 0x20,
+    Status_TSB = 0x40,
+    Status_DIR = 0x80
+};
 
 static const int I2C_WRITE_OK = 0;
 
@@ -132,32 +135,32 @@
 
 OneWireMaster::CmdResult DS2465::computeNextMasterSecret(bool swap, unsigned int pageNum, PageRegion region)
 {
-    uint8_t command[2] = { CMD_CNMS, (uint8_t)(swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
-    return writeMemory(ADDR_CMD_REG, command, 2);
+    uint8_t command[2] = { ComputeNextMasterSecretCmd, (uint8_t)(swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
+    return writeMemory(CommandReg, command, 2);
 }
 
 OneWireMaster::CmdResult DS2465::computeWriteMac(bool regwrite, bool swap, unsigned int pageNum, unsigned int segmentNum) const
 {
-    uint8_t command[2] = { CMD_CSWM, (uint8_t)((regwrite << 7) | (swap << 6) | (pageNum << 4) | segmentNum) };
-    return cWriteMemory(ADDR_CMD_REG, command, 2);
+    uint8_t command[2] = { ComputeSlaveWriteMacCmd, (uint8_t)((regwrite << 7) | (swap << 6) | (pageNum << 4) | segmentNum) };
+    return cWriteMemory(CommandReg, command, 2);
 }
 
 OneWireMaster::CmdResult DS2465::computeAuthMac(bool swap, unsigned int pageNum, PageRegion region) const
 {
-    uint8_t command[2] = { CMD_CSAM, (uint8_t)(swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
-    return cWriteMemory(ADDR_CMD_REG, command, 2);
+    uint8_t command[2] = { ComputeSlaveAuthMacCmd, (uint8_t)(swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
+    return cWriteMemory(CommandReg, command, 2);
 }
 
 OneWireMaster::CmdResult DS2465::computeSlaveSecret(bool swap, unsigned int pageNum, PageRegion region)
 {
-    uint8_t command[2] = { CMD_CSS, (uint8_t)(swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
-    return writeMemory(ADDR_CMD_REG, command, 2);
+    uint8_t command[2] = { ComputeSlaveSecretCmd, (uint8_t)(swap ? (0xC8 | (pageNum << 4) | region) : 0xBF) };
+    return writeMemory(CommandReg, command, 2);
 }
 
 ISha256MacCoproc::CmdResult DS2465::setMasterSecret(const Secret & masterSecret)
 {
     OneWireMaster::CmdResult result;
-    result = writeMemory(ADDR_SPAD, masterSecret, masterSecret.length);
+    result = writeMemory(Scratchpad, masterSecret, masterSecret.length);
     if (result == OneWireMaster::Success)
     {
         result = copyScratchpadToSecret();
@@ -183,7 +186,7 @@
     {
         wait_ms(shaComputationDelayMs);
         // Read MAC from register
-        result = readMemory(ADDR_MAC_READ, mac, mac.length, true);
+        result = readMemory(MacReadoutReg, mac, mac.length, true);
     }
     return (result == OneWireMaster::Success ? ISha256MacCoproc::Success : ISha256MacCoproc::OperationFailure);
 }
@@ -191,7 +194,7 @@
 ISha256MacCoproc::CmdResult DS2465::computeAuthMac(const DevicePage & devicePage, const DeviceScratchpad & challenge, const AuthMacData & authMacData, Mac & mac) const
 {
     OneWireMaster::CmdResult result;
-    int addr = ADDR_SPAD;
+    int addr = Scratchpad;
     // Write input data to scratchpad
     result = cWriteMemory(addr, devicePage, devicePage.length);
     if (result == OneWireMaster::Success)
@@ -213,7 +216,7 @@
     {
         wait_ms(shaComputationDelayMs * 2);
         // Read MAC from register
-        result = readMemory(ADDR_MAC_READ, mac, mac.length, true);
+        result = readMemory(MacReadoutReg, mac, mac.length, true);
     }
     return (result == OneWireMaster::Success ? ISha256MacCoproc::Success : ISha256MacCoproc::OperationFailure);
 }
@@ -221,7 +224,7 @@
 ISha256MacCoproc::CmdResult DS2465::computeSlaveSecret(const DevicePage & devicePage, const DeviceScratchpad & deviceScratchpad, const SlaveSecretData & slaveSecretData)
 {
     OneWireMaster::CmdResult result;
-    int addr = ADDR_SPAD;
+    int addr = Scratchpad;
     // Write input data to scratchpad
     result = writeMemory(addr, devicePage, devicePage.length);
     if (result == OneWireMaster::Success)
@@ -248,8 +251,8 @@
 
 OneWireMaster::CmdResult DS2465::copyScratchpad(bool destSecret, unsigned int pageNum, bool notFull, unsigned int segmentNum)
 {
-    uint8_t command[2] = { CMD_CPS, (uint8_t)(destSecret ? 0 : (0x80 | (pageNum << 4) | (notFull << 3) | segmentNum)) };
-    return writeMemory(ADDR_CMD_REG, command, 2);
+    uint8_t command[2] = { CopyScratchpadCmd, (uint8_t)(destSecret ? 0 : (0x80 | (pageNum << 4) | (notFull << 3) | segmentNum)) };
+    return writeMemory(CommandReg, command, 2);
 }
 
 OneWireMaster::CmdResult DS2465::configureLevel(OWLevel level)
@@ -304,8 +307,8 @@
     //  SS indicates byte containing search direction bit value in msbit
 
     OneWireMaster::CmdResult result;
-    uint8_t command[2] = { CMD_1WT, (uint8_t)((searchDirection == WriteOne) ? 0x80 : 0x00) };
-    result = writeMemory(ADDR_CMD_REG, command, 2);
+    uint8_t command[2] = { OwTripletCmd, (uint8_t)((searchDirection == WriteOne) ? 0x80 : 0x00) };
+    result = writeMemory(CommandReg, command, 2);
     if (result == OneWireMaster::Success)
     {
         uint8_t status;
@@ -313,9 +316,9 @@
         if (result == OneWireMaster::Success)
         {
             // check bit results in status byte
-            sbr = ((status & STATUS_SBR) == STATUS_SBR);
-            tsb = ((status & STATUS_TSB) == STATUS_TSB);
-            searchDirection = ((status & STATUS_DIR) == STATUS_DIR) ? WriteOne : WriteZero;
+            sbr = ((status & Status_SBR) == Status_SBR);
+            tsb = ((status & Status_TSB) == Status_TSB);
+            searchDirection = ((status & Status_DIR) == Status_DIR) ? WriteOne : WriteZero;
         }
     }
     return result;
@@ -324,21 +327,21 @@
 OneWireMaster::CmdResult DS2465::OWReadBlock(uint8_t *recvBuf, uint8_t recvLen)
 {
     // 1-Wire Receive Block (Case A)
-    //   S AD,0 [A] ADDR_CMD_REG [A] 1WRF [A] PR [A] P
+    //   S AD,0 [A] CommandReg [A] 1WRF [A] PR [A] P
     //  [] indicates from slave
     //  PR indicates byte containing parameter
 
     OneWireMaster::CmdResult result;
-    uint8_t command[2] = { CMD_1WRF, recvLen };
+    uint8_t command[2] = { OwReceiveBlockCmd, recvLen };
 
-    result = writeMemory(ADDR_CMD_REG, command, 2);
+    result = writeMemory(CommandReg, command, 2);
     if (result == OneWireMaster::Success)
     {
         result = pollBusy();
     }
     if (result == OneWireMaster::Success)
     {
-        result = readMemory(ADDR_SPAD, recvBuf, recvLen, false);
+        result = readMemory(Scratchpad, recvBuf, recvLen, false);
     }
 
     return result;
@@ -357,12 +360,12 @@
 OneWireMaster::CmdResult DS2465::OWWriteBlock(bool tx_mac, const uint8_t *tran_buf, uint8_t tran_len)
 {
     OneWireMaster::CmdResult result;
-    uint8_t command[2] = { CMD_1WTB, (uint8_t)(tx_mac ? 0xFF : tran_len) };
+    uint8_t command[2] = { OwTransmitBlockCmd, (uint8_t)(tx_mac ? 0xFF : tran_len) };
 
     if (!tx_mac)
     {
         // prefill scratchpad with required data
-        result = writeMemory(ADDR_SPAD, tran_buf, tran_len);
+        result = writeMemory(Scratchpad, tran_buf, tran_len);
         if (result != OneWireMaster::Success)
         {
             return result;
@@ -370,11 +373,11 @@
     }
 
     // 1-Wire Transmit Block (Case A)
-    //   S AD,0 [A] ADDR_CMD_REG [A] 1WTB [A] PR [A] P
+    //   S AD,0 [A] CommandReg [A] 1WTB [A] PR [A] P
     //  [] indicates from slave
     //  PR indicates byte containing parameter
 
-    result = writeMemory(ADDR_CMD_REG, command, 2);
+    result = writeMemory(CommandReg, command, 2);
 
     if (result == OneWireMaster::Success)
     {
@@ -387,7 +390,7 @@
 OneWireMaster::CmdResult DS2465::OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel)
 {
     // 1-Wire Read Bytes (Case C)
-    //   S AD,0 [A] ADDR_CMD_REG [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A 
+    //   S AD,0 [A] CommandReg [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A 
     //                                                  \--------/        
     //                     Repeat until 1WB bit has changed to 0
     //   Sr AD,0 [A] SRP [A] E1 [A] Sr AD,1 [A] DD A\ P
@@ -404,8 +407,8 @@
         return result;
     }
 
-    buf = CMD_1WRB;
-    result = writeMemory(ADDR_CMD_REG, &buf, 1);
+    buf = OwReadByteCmd;
+    result = writeMemory(CommandReg, &buf, 1);
 
     if (result == OneWireMaster::Success)
     {
@@ -414,7 +417,7 @@
 
     if (result == OneWireMaster::Success)
     {
-        result = readMemory(ADDR_DATA_REG, &buf, 1);
+        result = readMemory(ReadDataReg, &buf, 1);
     }
 
     if (result == OneWireMaster::Success)
@@ -428,7 +431,7 @@
 OneWireMaster::CmdResult DS2465::OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel)
 {
     // 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
+    //   S AD,0 [A] CommandReg [A] 1WWB [A] DD [A] Sr AD,1 [A] [Status] A [Status] A\ P
     //                                                           \--------/        
     //                             Repeat until 1WB bit has changed to 0
     //  [] indicates from slave
@@ -442,9 +445,9 @@
         return result;
     }
 
-    uint8_t command[2] = { CMD_1WWB, sendByte };
+    uint8_t command[2] = { OwWriteByteCmd, sendByte };
 
-    result = writeMemory(ADDR_CMD_REG, command, 2);
+    result = writeMemory(CommandReg, command, 2);
     if (result == OneWireMaster::Success)
     {
         result = pollBusy();
@@ -456,7 +459,7 @@
 OneWireMaster::CmdResult DS2465::OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel)
 {
     // 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
+    //   S AD,0 [A] CommandReg [A] 1WSB [A] BB [A] Sr AD,1 [A] [Status] A [Status] A\ P
     //                                                          \--------/        
     //                           Repeat until 1WB bit has changed to 0
     //  [] indicates from slave
@@ -470,10 +473,10 @@
         return result;
     }
 
-    uint8_t command[2] = { CMD_1WSB, (uint8_t)(sendRecvBit ? 0x80 : 0x00) };
+    uint8_t command[2] = { OwSingleBitCmd, (uint8_t)(sendRecvBit ? 0x80 : 0x00) };
     uint8_t status;
 
-    result = writeMemory(ADDR_CMD_REG, command, 2);
+    result = writeMemory(CommandReg, command, 2);
 
     if (result == OneWireMaster::Success)
     {
@@ -482,7 +485,7 @@
 
     if (result == OneWireMaster::Success)
     {
-        sendRecvBit = (status & STATUS_SBR);
+        sendRecvBit = (status & Status_SBR);
     }
 
     return result;
@@ -574,12 +577,12 @@
     OneWireMaster::CmdResult result;
 
     configBuf = config.writeByte();
-    result = writeMemory(ADDR_WCFG_REG, &configBuf, 1);
+    result = writeMemory(ConfigReg, &configBuf, 1);
     if (verify)
     {
         if (result == OneWireMaster::Success)
         {
-            result = readMemory(ADDR_WCFG_REG, &configBuf, 1);
+            result = readMemory(ConfigReg, &configBuf, 1);
         }
         if (result == OneWireMaster::Success)
         {
@@ -606,7 +609,7 @@
 
     do
     {
-        result = readMemory(ADDR_STATUS_REG, &status, 1, true);
+        result = readMemory(StatusReg, &status, 1, true);
         if (result != OneWireMaster::Success)
         {
             return result;
@@ -619,7 +622,7 @@
         {
             return OneWireMaster::TimeoutError;
         }
-    } while (status & STATUS_1WB);
+    } while (status & Status_1WB);
 
     return OneWireMaster::Success;
 }
@@ -627,7 +630,7 @@
 OneWireMaster::CmdResult DS2465::OWReset()
 {
     // 1-Wire reset (Case B)
-    //   S AD,0 [A] ADDR_CMD_REG  [A] 1WRS [A] Sr AD,1 [A] [Status] A [Status] A\ P
+    //   S AD,0 [A] CommandReg  [A] 1WRS [A] Sr AD,1 [A] [Status] A [Status] A\ P
     //                                                  \--------/        
     //                       Repeat until 1WB bit has changed to 0
     //  [] indicates from slave
@@ -635,8 +638,8 @@
     OneWireMaster::CmdResult result;
     uint8_t buf;
 
-    buf = CMD_1WRS;
-    result = writeMemory(ADDR_CMD_REG, &buf, 1);
+    buf = OwResetCmd;
+    result = writeMemory(CommandReg, &buf, 1);
 
     if (result == OneWireMaster::Success)
     {
@@ -646,7 +649,7 @@
     if (result == OneWireMaster::Success)
     {
         // check for presence detect
-        if ((buf & STATUS_PPD) != STATUS_PPD)
+        if ((buf & Status_PPD) != Status_PPD)
         {
             result = OneWireMaster::OperationFailure;
         }
@@ -658,19 +661,19 @@
 OneWireMaster::CmdResult DS2465::reset()
 {
     // Device Reset
-    //   S AD,0 [A] ADDR_CMD_REG [A] 1WMR [A] Sr AD,1 [A] [SS] A\ P
+    //   S AD,0 [A] CommandReg [A] 1WMR [A] Sr AD,1 [A] [SS] A\ P
     //  [] indicates from slave
     //  SS status byte to read to verify state
 
     OneWireMaster::CmdResult result;
     uint8_t buf;
 
-    buf = CMD_1WMR;
-    result = writeMemory(ADDR_CMD_REG, &buf, 1);
+    buf = DeviceResetCmd;
+    result = writeMemory(CommandReg, &buf, 1);
 
     if (result == OneWireMaster::Success)
     {
-        result = readMemory(ADDR_STATUS_REG, &buf, 1, true);
+        result = readMemory(StatusReg, &buf, 1, true);
     }
 
     if (result == OneWireMaster::Success)