Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Files at this revision

API Documentation at this revision

Comitter:
Matt Briggs
Date:
Fri Feb 17 11:58:40 2017 -0700
Parent:
49:18f1354f9e51
Child:
51:58d2a1b8f9d2
Commit message:
Added retries in ds2408 lib and started updating main to make static wireless bridge

Changed in this revision

xDotBridge/TESTS/autoLibs/testDs2408/testAPI.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/inc/BaseboardIO.h Show annotated file Show diff for this revision Revisions of this file
xDotBridge/inc/DS2408.h Show annotated file Show diff for this revision Revisions of this file
xDotBridge/manualTest/testBaseboardIO/testBaseboardIO.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/BaseboardIO.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/DS2408.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/xDotBridge/TESTS/autoLibs/testDs2408/testAPI.cpp	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/TESTS/autoLibs/testDs2408/testAPI.cpp	Fri Feb 17 11:58:40 2017 -0700
@@ -15,6 +15,8 @@
 
 #define __TEST__ 1
 
+#define USE_RELIABLE 1
+
 using namespace utest::v1;
 
 DigitalOut switchedIO(I2C_SCL, 0); // Always enable Switched IO for test
@@ -26,17 +28,16 @@
  */
 void test_search() {
     uint8_t result;
-    for (int i=0; i<1; i++) {
+    for (int i=0; i<5; i++) {
         OWMaster.reset();
+        OWMaster.reset_search(); // TODO Check if this is helpful
         wait(1.0);
         result = OWMaster.search(ds2408Addr);
-        TEST_ASSERT(result == 1);
-        result = OWMaster.search(ds2408Addr);
         if (result == 1) {
             break;
         }
-        TEST_ASSERT(result == 1);
     }
+    TEST_ASSERT(result == 1);
 }
 
 /**
@@ -57,11 +58,19 @@
     result = ds2408.init();
     TEST_ASSERT(result == cmdSuccess);
 
+    #ifdef USE_RELIABLE
+    result = ds2408.pioLogicReliableWrite(0xFF); // Leave all IOs high Z e.g. should float high
+    #else
     result = ds2408.pioLogicWrite(0xFF); // Leave all IOs high Z e.g. should float high
+    #endif
     TEST_ASSERT_MESSAGE(result == cmdSuccess, "Error returned during write operation");
 
     uint8_t val = 0;
+    #ifdef USE_RELIABLE
+    result = ds2408.pioLogicReliableRead(val);
+    #else
     result = ds2408.pioLogicRead(val);
+    #endif
     TEST_ASSERT_MESSAGE(result == cmdSuccess, "Error returned during read operation");
     TEST_ASSERT_MESSAGE(val == 0xFF, "Bits not correct");
 }
@@ -75,21 +84,37 @@
     CmdResult result = ds2408.init();
     TEST_ASSERT(result == cmdSuccess);
 
+    #ifdef USE_RELIABLE
+    result = ds2408.pioLogicReliableWrite(0xFF); // Leave all IOs high Z e.g. should float high
+    #else
     result = ds2408.pioLogicWrite(0xFF); // Leave all IOs high Z e.g. should float high
+    #endif
     TEST_ASSERT_MESSAGE(result == cmdSuccess, "Error returned during write operation");
 
     uint8_t readVal = 0;
+    #ifdef USE_RELIABLE
+    result = ds2408.pioLogicReliableRead(readVal);
+    #else
     result = ds2408.pioLogicRead(readVal);
+    #endif
     TEST_ASSERT_MESSAGE(result == cmdSuccess, "Error returned during read operation");
     TEST_ASSERT_MESSAGE(readVal == 0xFF, "Bits not correct");
 
     uint8_t writeVal;
     for (int i=0; i<8; i++) {
         writeVal = ~(1 << i);
+        #ifdef USE_RELIABLE
+        result = ds2408.pioLogicReliableWrite(writeVal); // Leave all IOs high Z e.g. should float high
+        #else
         result = ds2408.pioLogicWrite(writeVal); // Leave all IOs high Z e.g. should float high
+        #endif
         TEST_ASSERT_MESSAGE(result == cmdSuccess, "Error returned during write operation");
 
+        #ifdef USE_RELIABLE
+        result = ds2408.pioLogicReliableRead(readVal);
+        #else
         result = ds2408.pioLogicRead(readVal);
+        #endif
         TEST_ASSERT_MESSAGE(result == cmdSuccess, "Error returned during read operation");
         TEST_ASSERT_MESSAGE(readVal == writeVal, "Bits not correct");
     }
--- a/xDotBridge/inc/BaseboardIO.h	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/inc/BaseboardIO.h	Fri Feb 17 11:58:40 2017 -0700
@@ -220,6 +220,22 @@
     bool isLoRaWANMode();
 
     /**
+    * @brief Returns the current state of the serial switch
+    *
+    * @details This just simply uses the last sample of the IO to return a bool.
+    * If enabled then the serial chip will be listening for traffic at least in
+    * the case of a TX.
+    *
+    * On Entry:
+    * IO should be sampled recently
+    *
+    * On Exit:
+    *
+    * @return bool
+    */
+    bool isSerialEnabled();
+
+    /**
     * @brief Returns value of a rotary switch (TODO give board location)
     *
     * On Entry:
--- a/xDotBridge/inc/DS2408.h	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/inc/DS2408.h	Fri Feb 17 11:58:40 2017 -0700
@@ -8,6 +8,8 @@
 #include "../config.h"
 #include "OneWire.h"
 
+const uint8_t DS2408_NRETRIES = 5;
+
 /**
  *  @class DS2408
  *  @brief This class abstracts communicating with the DS2408 port expander.
@@ -82,9 +84,36 @@
     * @return Result of operation zero for success
     */
     CmdResult pioLogicRead(uint8_t &val);
+
+    /**
+    * @brief pioLogicReliableRead()
+    *
+    * @details Uses regisrterRead to get logic values.  Then reads again to check for bit errors.
+    *
+    *
+    * On Exit:
+    *     @param[out] val - lsb represents the state of the pio
+    *
+    * @return Result of operation zero for success
+    */
+    CmdResult pioLogicReliableRead(uint8_t &val);
     // TODO implement other register based functions
 
     /**
+    * @brief pioLogicReliableWrite()
+    *
+    * @details writes to pio.  Note 0 means active pull down and 1 high-Z to
+    * allow PIO to float high.  This will automatically retry for DS2408_NRETRIES
+    * times.
+    *
+    * On Entry:
+    *    @param[in] val - Value for IO.
+    *
+    * @return CmdResult - result of operation
+    */
+    CmdResult pioLogicReliableWrite(uint8_t val);
+
+    /**
     * @brief pioLogicWrite()
     *
     * @details writes to pio.  Note 0 means active pull down and 1 high-Z to
@@ -101,6 +130,16 @@
     OneWire *mMaster;
     uint8_t mRomAddr[8];
 
+    /**
+    * @brief disableTestMode()
+    *
+    * @details Per datasheet if slew power on has a specific slew rate the
+    * chip may enter a test mode.
+    *
+    * @return CmdResult - result of operation
+    */
+    CmdResult disableTestMode();
+
 };
 
 #endif
--- a/xDotBridge/manualTest/testBaseboardIO/testBaseboardIO.cpp	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/manualTest/testBaseboardIO/testBaseboardIO.cpp	Fri Feb 17 11:58:40 2017 -0700
@@ -122,6 +122,7 @@
         pc.printf("= Is TX. State: %s                  =\r\n", bool2Str(mBbio->isTx()));
         pc.printf("= CC Normally Closed. State: %s     =\r\n", bool2Str(mBbio->isCCNC()));
         pc.printf("= Is LoraWAN. State: %s             =\r\n", bool2Str(mBbio->isLoRaWANMode()));
+        pc.printf("= Is Serial En. State: %s           =\r\n", bool2Str(mBbio->isSerialEnabled()));
         pc.printf("= Rotary Switch 1.  Value: %02d                 =\r\n", mBbio->rotarySwitch1());
         pc.printf("= Rotary Switch 2.  Value: %02d                 =\r\n", mBbio->rotarySwitch2());
         pc.printf("===============================================\r\n");
--- a/xDotBridge/src/BaseboardIO.cpp	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/src/BaseboardIO.cpp	Fri Feb 17 11:58:40 2017 -0700
@@ -162,6 +162,11 @@
     // When DIP switch is not closed (i.e. value reads high) assume P2P not WAN
     return (mPortExpanderVal1 & pEx1WanSel) == 0;
 }
+bool BaseboardIO::isSerialEnabled()
+{
+    // When DIP switch is not closed (i.e. value reads high) assume not in serial mode
+    return (mPortExpanderVal1 & pEx1SerialEn) == 0;
+}
 uint8_t BaseboardIO::rotarySwitch1()
 {
     // If a bit of a nibble is asserted then the port expander line is switched low.
--- a/xDotBridge/src/DS2408.cpp	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/src/DS2408.cpp	Fri Feb 17 11:58:40 2017 -0700
@@ -37,6 +37,29 @@
     return registerRead(pioLogicStateReg, val);
 }
 
+CmdResult DS2408::pioLogicReliableRead(uint8_t &val)
+{
+    uint8_t result = 0;
+    uint8_t result1 = 0xFF;
+    uint8_t cmdResult;
+    cmdResult = pioLogicRead(result);
+    for (int i=0; i < DS2408_NRETRIES; i++) {
+        cmdResult = pioLogicRead(result1);
+        if (cmdResult != cmdSuccess) {
+            continue;
+        }
+        // Check they match
+        if (result == result1) {
+            val = result;
+            return cmdSuccess;
+        }
+        else {
+            result = result1;
+        }
+    }
+    return cmdTimeout;
+}
+
 CmdResult DS2408::pioLogicWrite(uint8_t val)
 {
     mMaster->reset();
@@ -52,3 +75,14 @@
         return cmdError;
     }
 }
+
+CmdResult DS2408::pioLogicReliableWrite(uint8_t val) {
+    uint8_t result;
+    for (int i=0; i < DS2408_NRETRIES; i++) {
+        result = pioLogicWrite(val);
+        if (result == cmdSuccess) {
+            return cmdSuccess;
+        }
+    }
+    return cmdTimeout;
+}
--- a/xDotBridge/src/main.cpp	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/src/main.cpp	Fri Feb 17 11:58:40 2017 -0700
@@ -16,14 +16,29 @@
 
 DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
 
+volatile bool ccIntFlag;
+volatile bool tamperIntFlag;
+volatile bool pairBtnIntFlag;
+void ccInIntCallback () {
+    ccIntFlag = true;
+}
+void tamperIntCallback () {
+    tamperIntFlag = true;
+}
+void pairBtnIntCallback () {
+    pairBtnIntFlag = true;
+}
+
+
 int main() {
+    ccIntFlag = false;
+    tamperIntFlag = false;
+    pairBtnIntFlag = false;
     pc.baud(115200);
 
     CommProtocolPeerBrute protocol;
     BaseboardIO bbio;
     RadioEvent events;  // Custom event handler for automatically displaying RX data
-    WinbondSPIFlash flash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
-//    flash.frequency(48e6); // TODO try overridding for faster freq (Default 1MHz)
 
     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
 
@@ -49,42 +64,14 @@
     // PVD_LEVEL4 Falling 2.64
     // PVD_LEVEL5 Falling 2.84
     // PVD_LEVEL6 Falling 3.05
-    PWR_PVDTypeDef pvdConfig;
-    pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
-    pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
-
-    HAL_PWR_ConfigPVD(&pvdConfig);
-    HAL_PWR_EnablePVD();
-    logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
-    // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
-
-    // Appears to work but do not have a good way to measure power
-    for (int i=0; i<8; i++) {
-        int data = flash.readByte(0x000000+i);
-        logInfo("Idx: %d: %02X", i, data);
-    }
-    logInfo("Powering Down");
-    flash.powerDown();
-    wait(1.0);
-    for (int i=0; i<8; i++) {
-        int data = flash.readByte(0x000000+i);
-        logInfo("Idx: %d: %02X", i, data);
-    }
-    flash.releaseFromPowerDown();
-    logInfo("Waking up");
-    wait(1.0);
-    for (int i=0; i<8; i++) {
-        int data = flash.readByte(0x000000+i);
-        logInfo("Idx: %d: %02X", i, data);
-    }
-    return 0;
-
-#if BRIDGE_TX_BRUTE
-    protocol.setTx(true);
-#else
-    protocol.setTx(false);
-#endif
-    protocol.init();
+//    PWR_PVDTypeDef pvdConfig;
+//    pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
+//    pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
+//
+//    HAL_PWR_ConfigPVD(&pvdConfig);
+//    HAL_PWR_EnablePVD();
+//    logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
+//    // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
 
     dot->setWakePin(WAKE);
 
@@ -98,6 +85,15 @@
     display_config();
 
     uint16_t seqNum=0;
+    Callback<void()> ccInIntObj (&ccInIntCallback);
+    Callback<void()> tamperIntObj (&tamperIntCallback);
+    Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
+    bbio.regCCInInt(ccInIntObj);
+    bbio.regTamperInt(tamperIntObj);
+    bbio.regPairBtnInt(pairBtnIntObj);
+
+    bbio.sampleUserSwitches();
+    bbio.relayNormal(); // Always force relay in known state
 
     /**
      * Main Loop
@@ -106,20 +102,38 @@
         std::vector<uint8_t> data;
         bbio.ledOff();
 
-		if (protocol.isTx()) {
-#if LED_FEEDBACK
-		    bbio.ledOn();
-#endif
-            // TODO check for CC_IN
+        // Sample IO and update any configuration
+        bool prevCCNormallyOpen = bbio.isCCNO();
+        bbio.sampleUserSwitches();
+        if (prevCCNormallyOpen == bbio.isCCNO()) { // Only activate the coil if the DIP SW has changed
+            bbio.relayNormal();
+        }
+        if (bbio.isTx()) {
+            protocol.setTx(true);
+        }
+        else { // RX
+            protocol.setTx(false);
+        }
+        // End sample and update
 
-            data.push_back((seqNum >> 8) & 0xFF);
-            data.push_back(seqNum & 0xFF);
-            protocol.send(data);
-            seqNum++;
+		if (protocol.isTx()) {
+		    if (ccIntFlag || // If contact closure in
+		       (tamperIntFlag && (bbio.isPairBtn() == false))) { // If short pair btn hit
 
-#if LED_FEEDBACK
+                ccIntFlag = false;
+                tamperIntFlag = false;
+                #if LED_FEEDBACK
+                bbio.ledOn();
+                #endif
+
+                data.push_back((seqNum >> 8) & 0xFF);
+                data.push_back(seqNum & 0xFF);
+                protocol.send(data);
+                seqNum++;
+                wait(0.5); // Leave the LED on so a person can see it.
+		    }
+
             bbio.ledOff();
-#endif
             sleep_save_io();
             sleep_configure_io();
             dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until wake button
@@ -133,14 +147,15 @@
 		        protocol.recv(data);
                 std::string dataStr(data.begin(), data.end());
                 logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str());
-                // TODO add CC_OUT code here
+                bbio.relayAlert();
                 seqNum++;
-#if LED_FEEDBACK
+                #if LED_FEEDBACK
                 bbio.ledOn();
-                wait(0.5);
-#endif
+                #endif
+                wait(0.5); // TODO this should be configurable
 		    }
             bbio.ledOff();
+            bbio.relayNormal();
             logInfo("Sleeping.  Time %d", us_ticker_read());
             sleep_save_io();
             sleep_configure_io();
@@ -149,6 +164,7 @@
             sleep_restore_io();
 		}
 
+		// TODO maybe a good place to put pairing logic
 
         logInfo("================================");
         wait(1.0);