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:
Wed Feb 22 10:45:56 2017 -0700
Parent:
52:64a2c71c7c49
Child:
54:c04d7b6fa075
Commit message:
Mostly working TX in main app

Changed in this revision

xDotBridge/inc/CommProtocolPeerBrute.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/CommProtocolPeerBrute.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/inc/CommProtocolPeerBrute.h	Tue Feb 21 15:33:57 2017 -0700
+++ b/xDotBridge/inc/CommProtocolPeerBrute.h	Wed Feb 22 10:45:56 2017 -0700
@@ -161,7 +161,6 @@
     uint32_t getULC();
 
 private:
-    mDot *dot;
     uint8_t mNetworkAddr[4];
     uint8_t mNetwork_session_key[16];
     uint8_t mData_session_key[16];
--- a/xDotBridge/manualTest/testBaseboardIO/testBaseboardIO.cpp	Tue Feb 21 15:33:57 2017 -0700
+++ b/xDotBridge/manualTest/testBaseboardIO/testBaseboardIO.cpp	Wed Feb 22 10:45:56 2017 -0700
@@ -8,7 +8,6 @@
 Serial pc(USBTX, USBRX); // Externally defined
 
 const int VERSION = 2;
-extern void Error_Handler(void);
 
 char* bool2Str(bool in) {
     if (in) {
--- a/xDotBridge/src/BaseboardIO.cpp	Tue Feb 21 15:33:57 2017 -0700
+++ b/xDotBridge/src/BaseboardIO.cpp	Wed Feb 22 10:45:56 2017 -0700
@@ -102,13 +102,13 @@
     else {
         mCCIn.rise(func);
     }
-    mPairBtn.mode(PullNone);
+    mCCIn.mode(PullNone);
     mCCIn.enable_irq();
 }
 void BaseboardIO::regTamperInt(Callback<void()> func)
 {
     // Pulled high, switched low
-    mPairBtn.mode(PullNone);
+    mTamper.mode(PullNone);
     mTamper.rise(func);
     mTamper.fall(func);
     mTamper.enable_irq();
@@ -129,12 +129,12 @@
     // Sample port expanders
     enableSwitchedIO();
     wait(0.001);  // Wait 1 ms
-    if (mPortEx0->pioLogicRead(mPortExpanderVal0) != cmdSuccess) {
+    if (mPortEx0->pioLogicReliableRead(mPortExpanderVal0) != cmdSuccess) {
         disableSwitchedIO();
         logError("Error reading port expander 0.");
         return cmdError;
     }
-    if (mPortEx1->pioLogicRead(mPortExpanderVal1) != cmdSuccess) {
+    if (mPortEx1->pioLogicReliableRead(mPortExpanderVal1) != cmdSuccess) {
         disableSwitchedIO();
         logError("Error reading port expander 1.");
         return cmdError;
@@ -234,7 +234,7 @@
         logError("Error enabling 232.  Port expanders not initialized.");
         return cmdError;
     }
-    mPortEx0->pioLogicRead(val);
+    mPortEx0->pioLogicReliableRead(val);
 
     // Active low from port expander -> pmos -> 232 (active chip EN)
     if (enable) {
@@ -244,7 +244,7 @@
         val |= pEx0232En;
     }
 
-    if (mPortEx0->pioLogicWrite(val | ~pEx0OutMask) != cmdSuccess) {
+    if (mPortEx0->pioLogicReliableWrite(val | ~pEx0OutMask) != cmdSuccess) {
         logError("Error enabling 232");
         return cmdError;
     }
@@ -257,7 +257,7 @@
         logError("Error enabling 232 TX.  Port expanders not initialized.");
         return cmdError;
     }
-    mPortEx0->pioLogicRead(val);
+    mPortEx0->pioLogicReliableRead(val);
 
     // Active high tx disable therefore active low tx enable (note chip must also be enabled for TX)
     if (enable) {
@@ -267,7 +267,7 @@
         val |= pEx0232TxDis;
     }
 
-    if (mPortEx0->pioLogicWrite(val | ~pEx0OutMask) != cmdSuccess) {
+    if (mPortEx0->pioLogicReliableWrite(val | ~pEx0OutMask) != cmdSuccess) {
         logError("Error enabling 232 TX");
         return cmdError;
     }
@@ -336,14 +336,14 @@
 
 
     enableSwitchedIO();
-    if (mPortEx0->pioLogicRead(mPortExpanderVal0) != cmdSuccess) {
+    if (mPortEx0->pioLogicReliableRead(mPortExpanderVal0) != cmdSuccess) {
         logError("Error during port expander ID.  Read failed.");
         disableSwitchedIO();
         delete mPortEx0;
         delete mPortEx1;
         return cmdError;
     }
-    if (mPortEx1->pioLogicRead(mPortExpanderVal1) != cmdSuccess) {
+    if (mPortEx1->pioLogicReliableRead(mPortExpanderVal1) != cmdSuccess) {
         logError("Error during port expander ID.  Read failed.");
         disableSwitchedIO();
         delete mPortEx0;
@@ -378,15 +378,15 @@
 }
 CmdResult BaseboardIO::openRelay() {
     uint8_t val;
-    mPortEx1->pioLogicRead(val);
+    mPortEx1->pioLogicReliableRead(val);
 
     val |= pEx1RlyA; // Make sure Relay A is off
     val &= ~pEx1RlyB; // Turn on Relay B
 
-    if (mPortEx1->pioLogicWrite(val | ~pEx1OutMask) != cmdSuccess) {
+    if (mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask) != cmdSuccess) {
         val |= pEx1RlyA; // Turn Relay A off
         val |= pEx1RlyB; // Turn Relay B off
-        mPortEx1->pioLogicWrite(val | ~pEx1OutMask); // Write a non assert value just to try to overcome an error
+        mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask); // Write a non assert value just to try to overcome an error
         logError ("Error turning on coil.  Turning both coils off.");
         return cmdError;
     }
@@ -396,8 +396,8 @@
     val |= pEx1RlyA; // Turn Relay A off
     val |= pEx1RlyB; // Turn Relay B off
 
-    if (mPortEx1->pioLogicWrite(val | ~pEx1OutMask) != cmdSuccess) {
-        mPortEx1->pioLogicWrite(val | ~pEx1OutMask);
+    if (mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask) != cmdSuccess) {
+        mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask);
         logError ("Error turning off coils.  Trying again.");
         return cmdError;
     }
@@ -406,15 +406,15 @@
 }
 CmdResult BaseboardIO::closeRelay() {
     uint8_t val;
-    mPortEx1->pioLogicRead(val);
+    mPortEx1->pioLogicReliableRead(val);
 
     val &= ~pEx1RlyA; // Turn on Relay A
     val |= pEx1RlyB; // Make sure Relay B is off
 
-    if (mPortEx1->pioLogicWrite(val | ~pEx1OutMask) != cmdSuccess) {
+    if (mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask) != cmdSuccess) {
         val |= pEx1RlyA; // Turn Relay A off
         val |= pEx1RlyB; // Turn Relay B off
-        mPortEx1->pioLogicWrite(val | ~pEx1OutMask); // Write a non assert value just to try to overcome an error
+        mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask); // Write a non assert value just to try to overcome an error
         logError ("Error turning on coil.  Turning both coils off.");
         return cmdError;
     }
@@ -424,8 +424,8 @@
     val |= pEx1RlyA; // Turn Relay A off
     val |= pEx1RlyB; // Turn Relay B off
 
-    if (mPortEx1->pioLogicWrite(val | ~pEx1OutMask) != cmdSuccess) {
-        mPortEx1->pioLogicWrite(val | ~pEx1OutMask);
+    if (mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask) != cmdSuccess) {
+        mPortEx1->pioLogicReliableWrite(val | ~pEx1OutMask);
         logError ("Error turning off coils.  Trying again.");
         return cmdError;
     }
--- a/xDotBridge/src/CommProtocolPeerBrute.cpp	Tue Feb 21 15:33:57 2017 -0700
+++ b/xDotBridge/src/CommProtocolPeerBrute.cpp	Wed Feb 22 10:45:56 2017 -0700
@@ -21,7 +21,7 @@
     logInfo("RX_SLEEP_TIME %f, timeOnAir %lu, nTimesToTx %lu", RX_SLEEP_TIME, TX_TIME, nTimesToTx);
 
     mIsTx = true; // default to TX
-    dot = mDot::getInstance();
+//    dot = mDot::getInstance();
 }
 
 CmdResult CommProtocolPeerBrute::init()
@@ -77,7 +77,10 @@
     // as long as both Dots are configured correctly, they should be able to communicate
 
     // FIXME just using pairing keys for now
+    wait(0.1);
     update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power);
+    wait(0.1);
+    dot->saveConfig();
     return cmdSuccess;
 }
 void CommProtocolPeerBrute::setTx(bool isTx)
@@ -101,7 +104,7 @@
         join_network();
     }
     logInfo("Starting TX.  Time: %lu", us_ticker_read());
-    for(uint i=0;i<nTimesToTx;++i) {
+    for(uint8_t i=0;i<nTimesToTx;++i) {
         dot->send(msg);
     }
     logInfo("Finished TX.  Time: %lu", us_ticker_read());
--- a/xDotBridge/src/main.cpp	Tue Feb 21 15:33:57 2017 -0700
+++ b/xDotBridge/src/main.cpp	Wed Feb 22 10:45:56 2017 -0700
@@ -31,6 +31,8 @@
 
 
 int main() {
+    CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
+    BaseboardIO *bbio = new BaseboardIO();
     CmdResult result;
     ccIntFlag = false;
     tamperIntFlag = false;
@@ -53,7 +55,19 @@
     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
 
     // attach the custom events handler
-    dot->setEvents(&events);  // Little bonus event debug information
+//    dot->setEvents(&events);  // Little bonus event debug information
+
+    // Finish radio init
+    protocol->init();
+
+    // save changes to configuration
+//    logInfo("saving configuration");
+//    core_util_critical_section_enter();
+//    bool dotConfigSaved = dot->saveConfig();
+//    core_util_critical_section_exit();
+//    if (!dotConfigSaved) {
+//        logError("failed to save configuration");
+//    }
 
     // Setup programmable voltage detector
     // PVD_LEVEL0 Falling 1.85
@@ -72,20 +86,14 @@
 //    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);
+    dot->setWakePin(GPIO3);
 
-    // save changes to configuration
-    logInfo("saving configuration");
-    if (!dot->saveConfig()) {
-        logError("failed to save configuration");
-    }
 
     // display configuration
-    display_config();
-    CommProtocolPeerBrute protocol;
-    BaseboardIO bbio;
+//    display_config();
 
-    result = bbio.init();
+    pc.printf("= Baseboard Init Starting                     =\r\n");
+    result = bbio->init();
     if (result == cmdSuccess) {
         pc.printf("= Baseboard Init Finished Successfully    =\r\n");
     }
@@ -97,74 +105,88 @@
     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
+    bbio->sampleUserSwitches();
+    bbio->relayNormal(); // Always force relay in known state
 
+    unsigned int intCnt = 0; // Just a quick temp varaible to keep track of ints
     /**
      * Main Loop
      */
     while (true) {
         std::vector<uint8_t> data;
-        bbio.ledOff();
+        bbio->ledOff();
 
         // 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();
+        bool prevCCNormallyOpen = bbio->isCCNO();
+        bbio->sampleUserSwitches();
+        if (prevCCNormallyOpen == bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
+            bbio->regCCInInt(ccInIntObj);
+            bbio->regTamperInt(tamperIntObj);
+            bbio->regPairBtnInt(pairBtnIntObj);
+            bbio->relayNormal();
         }
-        if (bbio.isTx()) {
-            protocol.setTx(true);
+        if (bbio->isTx()) {
+            protocol->setTx(true);
         }
         else { // RX
-            protocol.setTx(false);
+            protocol->setTx(false);
         }
         // End sample and update
 
-		if (protocol.isTx()) {
+		if (protocol->isTx()) {
+            logInfo("Got int #%d. CCFlag %d, TamperFlag %d, PairBtnFlag %d",
+                    intCnt, ccIntFlag, tamperIntFlag, pairBtnIntFlag);
+            // TODO add tamper
+            if (pairBtnIntFlag) { // Wait up to 1 second for short button hit
+                for (int i=0; i<10;i++) {
+                    if (bbio->isPairBtn() == false){ // Button released
+                        break;
+                    }
+                    wait(0.1);
+                }
+            }
 		    if (ccIntFlag || // If contact closure in
-		       (tamperIntFlag && (bbio.isPairBtn() == false))) { // If short pair btn hit
+		       (pairBtnIntFlag && (bbio->isPairBtn() == false))) { // If short pair btn hit
 
                 ccIntFlag = false;
                 tamperIntFlag = false;
+                pairBtnIntFlag = false;
                 #if LED_FEEDBACK
-                bbio.ledOn();
+                bbio->ledOn();
                 #endif
 
                 data.push_back((seqNum >> 8) & 0xFF);
                 data.push_back(seqNum & 0xFF);
-                protocol.send(data);
+                protocol->send(data);
                 seqNum++;
                 wait(0.5); // Leave the LED on so a person can see it.
 		    }
 
-            bbio.ledOff();
+            bbio->ledOff();
 //            sleep_save_io();
 //            sleep_configure_io();
             dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until wake button
-            sleep_restore_io();
+//            sleep_restore_io();
 		}
 
-		if (protocol.isRx()) {
+		if (protocol->isRx()) {
 		    bool msgPending;
-		    protocol.listen(msgPending);
+		    protocol->listen(msgPending);
+		    logInfo("Listening.");
 		    if (msgPending) {
-		        protocol.recv(data);
+		        protocol->recv(data);
                 std::string dataStr(data.begin(), data.end());
                 logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str());
-                bbio.relayAlert();
+                bbio->relayAlert();
                 seqNum++;
                 #if LED_FEEDBACK
-                bbio.ledOn();
+                bbio->ledOn();
                 #endif
                 wait(0.5); // TODO this should be configurable
 		    }
-            bbio.ledOff();
-            bbio.relayNormal();
+            bbio->ledOff();
+            bbio->relayNormal();
             logInfo("Sleeping.  Time %d", us_ticker_read());
 //            sleep_save_io();
 //            sleep_configure_io();
@@ -180,6 +202,7 @@
         // Need to re-implement some of these sleep functions
 //		sleep_save_io();
 //		sleep_configure_io();
+        intCnt++;
     }
 
     return 0;