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 Mar 01 17:28:32 2017 -0700
Parent:
57:bdac7dd17af2
Child:
59:485545afc4dc
Commit message:
Major updates to power down during sleep code. Other minior updates. Used for test on 2017/03/01.

Changed in this revision

xDotBridge/config.h 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/src/BaseboardIO.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/config.h	Tue Feb 28 09:26:27 2017 -0700
+++ b/xDotBridge/config.h	Wed Mar 01 17:28:32 2017 -0700
@@ -5,10 +5,10 @@
 #ifndef CONFIG_H_
 #define CONFIG_H_
 
-#define __TEST__ 1 // Comment out for main application
+//#define __TEST__ 1 // Comment out for main application
 
 // Manual test.  Comment out for main application.  Or uncomment for individual test.
-#define __TEST_BBIO__
+//#define __TEST_BBIO__
 //#define __TEST_LRR__
 //#define __TEST_PVD__
 
--- a/xDotBridge/inc/BaseboardIO.h	Tue Feb 28 09:26:27 2017 -0700
+++ b/xDotBridge/inc/BaseboardIO.h	Wed Mar 01 17:28:32 2017 -0700
@@ -55,6 +55,13 @@
 class BaseboardIO
 {
 public:
+    static const PinName OneWireMasterPinName  = I2C_SDA;
+    static const PinName CCInPinName           = WAKE; // Interrupt pin PA_0
+    static const PinName TamperPinName         = GPIO1; // Interrupt pin PA_5
+    static const PinName PairBtnPinName        = UART_CTS; // Interrupt pin PA_11
+    static const PinName LedPinName            = GPIO0;
+    static const PinName SwitchedIOCtrlPinName = I2C_SCL;
+
     /**
     * @brief BaseboardIO constructor
     *
@@ -357,6 +364,9 @@
     */
     CmdResult serialTx(bool enable);
 
+    CmdResult prepareSleep();
+    CmdResult exitSleep();
+
 private:
     // Initialized during constructor
     NvmBBIOObj mNvmObj;
--- a/xDotBridge/src/BaseboardIO.cpp	Tue Feb 28 09:26:27 2017 -0700
+++ b/xDotBridge/src/BaseboardIO.cpp	Wed Mar 01 17:28:32 2017 -0700
@@ -8,6 +8,7 @@
 #include "BaseboardIO.h"
 #include "MTSLog.h"
 #include "dot_util.h" // FIXME just need the reference to dot somehow
+#include "xdot_low_power.h"
 
 const float COIL_ON_TIME = 0.030; // 30 ms
 
@@ -39,13 +40,12 @@
  */
 
 BaseboardIO::BaseboardIO()
-    : mOWMaster(I2C_SDA),
-      mCCIn(WAKE), // Interrupt pin PA_0
-      mTamper(GPIO1), // Interrupt pin PA_5
-      mPairBtn(UART_CTS), // Interrupt pin PA_11
-//      mLed(SWDIO),
-      mLed(GPIO0),
-      mSwitchedIOCtrl(I2C_SCL, 0)
+    : mOWMaster(OneWireMasterPinName),
+      mCCIn(CCInPinName),
+      mTamper(TamperPinName),
+      mPairBtn(PairBtnPinName),
+      mLed(LedPinName),
+      mSwitchedIOCtrl(SwitchedIOCtrlPinName, 0)
 {
     mPortExpanderVal0 = 0x00;
     mPortExpanderVal1 = 0x00;
@@ -159,7 +159,7 @@
 void BaseboardIO::regPairBtnInt(Callback<void()> func)
 {
     // Pulled low, switched high
-    mPairBtn.mode(PullDown);
+    mPairBtn.mode(PullNone);
     mPairBtn.rise(func);
     mPairBtn.enable_irq();
 }
@@ -509,6 +509,100 @@
     return cmdSuccess;
 }
 
+CmdResult BaseboardIO::prepareSleep()
+{
+	// Save current GPUIO state
+    xdot_save_gpio_state();
+
+    // Configure all IO expect for pins for interrupt in lowest mode possible
+    // GPIO Ports Clock Enable
+    __GPIOA_CLK_ENABLE();
+    __GPIOB_CLK_ENABLE();
+    __GPIOC_CLK_ENABLE();
+    __GPIOH_CLK_ENABLE();
+
+    GPIO_InitTypeDef GPIO_InitStruct;
+
+    // UART1_TX, UART1_RTS & UART1_CTS to analog nopull - RX could be a wakeup source
+    // UART1_TX, UART1_RTS & UART1_CTS to analog nopull - RX could be a wakeup source
+//    GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_11 | GPIO_PIN_12;
+    GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_12;
+    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+    GPIO_InitStruct.Pull = GPIO_NOPULL;
+    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+
+    // I2C_SDA & I2C_SCL to analog nopull
+    GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
+    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+    GPIO_InitStruct.Pull = GPIO_NOPULL;
+    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+
+    // SPI_MOSI, SPI_MISO, SPI_SCK, & SPI_NSS to analog nopull
+    GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
+    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+    GPIO_InitStruct.Pull = GPIO_NOPULL;
+    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+
+    // iterate through potential wake pins - leave the configured wake pin alone if one is needed
+    if ((CCInPinName != WAKE && TamperPinName != WAKE && PairBtnPinName != WAKE)
+            || dot->getWakeMode() == mDot::RTC_ALARM) {
+        GPIO_InitStruct.Pin = GPIO_PIN_0;
+        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+        GPIO_InitStruct.Pull = GPIO_NOPULL;
+        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+    }
+    if ((CCInPinName != GPIO0 && TamperPinName != GPIO0 && PairBtnPinName != GPIO0)
+            || dot->getWakeMode() == mDot::RTC_ALARM) {
+        GPIO_InitStruct.Pin = GPIO_PIN_4;
+        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+        GPIO_InitStruct.Pull = GPIO_NOPULL;
+        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+    }
+    if ((CCInPinName != GPIO1 && TamperPinName != GPIO1 && PairBtnPinName != GPIO1)
+            || dot->getWakeMode() == mDot::RTC_ALARM) {
+        GPIO_InitStruct.Pin = GPIO_PIN_5;
+        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+        GPIO_InitStruct.Pull = GPIO_NOPULL;
+        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+    }
+    if ((CCInPinName != GPIO2 && TamperPinName != GPIO2 && PairBtnPinName != GPIO2)
+            || dot->getWakeMode() == mDot::RTC_ALARM) {
+        GPIO_InitStruct.Pin = GPIO_PIN_0;
+        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+        GPIO_InitStruct.Pull = GPIO_NOPULL;
+        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+    }
+    if ((CCInPinName != GPIO3 && TamperPinName != GPIO3 && PairBtnPinName != GPIO3)
+            || dot->getWakeMode() == mDot::RTC_ALARM) {
+        GPIO_InitStruct.Pin = GPIO_PIN_2;
+        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+        GPIO_InitStruct.Pull = GPIO_NOPULL;
+        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+    }
+    if ((CCInPinName != UART1_RX && TamperPinName != UART1_RX && PairBtnPinName != UART1_RX)
+            || dot->getWakeMode() == mDot::RTC_ALARM) {
+        GPIO_InitStruct.Pin = GPIO_PIN_10;
+        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+        GPIO_InitStruct.Pull = GPIO_NOPULL;
+        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+    }
+    if ((CCInPinName != UART_CTS && TamperPinName != UART_CTS && PairBtnPinName != UART_CTS)
+            || dot->getWakeMode() == mDot::RTC_ALARM) {
+        GPIO_InitStruct.Pin = GPIO_PIN_11;
+        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+        GPIO_InitStruct.Pull = GPIO_NOPULL;
+        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+    }
+
+    return cmdSuccess;
+}
+
+CmdResult BaseboardIO::exitSleep()
+{
+    xdot_restore_gpio_state();
+    return cmdSuccess;
+}
+
 // NvmBBIOObj
 NvmBBIOObj::NvmBBIOObj()
 {
--- a/xDotBridge/src/main.cpp	Tue Feb 28 09:26:27 2017 -0700
+++ b/xDotBridge/src/main.cpp	Wed Mar 01 17:28:32 2017 -0700
@@ -4,7 +4,9 @@
 #include "dot_util.h"
 #include "RadioEvent.h"
 #include "WinbondSPIFlash.h"
+#include "UserInterface.h"
 //#include <xdot_low_power.h>
+#include "mDot.h"
 
 #include "BaseboardIO.h"
 #include "CommProtocolPeerBrute.h"
@@ -14,7 +16,7 @@
 
 mDot* dot = NULL; // Used by dot-utils
 
-//DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
+DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
 
 volatile bool ccIntFlag;
 volatile bool tamperIntFlag;
@@ -29,10 +31,11 @@
     pairBtnIntFlag = true;
 }
 
-
 int main() {
     CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
     BaseboardIO *bbio = new BaseboardIO();
+    WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
+
     CmdResult result;
     ccIntFlag = false;
     tamperIntFlag = false;
@@ -87,9 +90,9 @@
 //    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->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
     dot->setWakePin(UART1_RX);
 
-
     // display configuration
 //    display_config();
 
@@ -110,8 +113,15 @@
 
     bbio->sampleUserSwitches();
     bbio->relayNormal(); // Always force relay in known state
+    bbio->regCCInInt(ccInIntObj);
+    bbio->regTamperInt(tamperIntObj);
+    bbio->regPairBtnInt(pairBtnIntObj);
 
-    unsigned int intCnt = 0; // Just a quick temp varaible to keep track of ints
+    // Start flash powered down
+    flash->powerDown();
+
+    unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
+    bool prevCCNormallyOpen;
     /**
      * Main Loop
      */
@@ -120,12 +130,10 @@
         bbio->ledOff();
 
         // Sample IO and update any configuration
-        bool prevCCNormallyOpen = bbio->isCCNO();
+        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()) {
@@ -137,8 +145,8 @@
         // End sample and update
 
 		if (protocol->isTx()) {
-            logInfo("Status #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
-                    intCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
+            logInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
+                    loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
             // TODO add tamper
             if (pairBtnIntFlag) { // Wait up to 1 second for short button hit
                 for (int i=0; i<10;i++) {
@@ -169,21 +177,20 @@
 		    }
 
             bbio->ledOff();
-//            sleep_save_io();
-//            sleep_configure_io();
+            bbio->prepareSleep();
 		    if (bbio->isCCInAlert()) { // Still in alert mode
-                dot->sleep(10, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep and check in 2 secs if CCInAlert is asserted
+                dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep and check in 2 secs if CCInAlert is asserted
 		    }
 		    else {
                 dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until wake button
 		    }
-//            sleep_restore_io();
+		    bbio->exitSleep();
 		}
 
 		if (protocol->isRx()) {
 		    bool msgPending;
 		    protocol->listen(msgPending);
-		    logInfo("Listening.");
+		    logInfo("Loop Cnt %d.  Listening.", loopCnt);
 		    if (msgPending) {
 		        protocol->recv(data);
                 std::string dataStr(data.begin(), data.end());
@@ -193,27 +200,25 @@
                 #if LED_FEEDBACK
                 bbio->ledOn();
                 #endif
-                wait(5.0); // TODO this should be configurable
+                // Hold time for alert
+                // TODO maybe use sleep instead of wait
+                logInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
+                wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
+                bbio->ledOff();
+                bbio->relayNormal();
 		    }
-            bbio->ledOff();
-            bbio->relayNormal();
             logInfo("Sleeping.  Time %d", us_ticker_read());
-//            sleep_save_io();
-//            sleep_configure_io();
-            // TODO maybe add if statement here to prevent double hits by sleeping for a longer time
             protocol->sampleDLC();
+            bbio->prepareSleep();
             dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep until wake button
-//            sleep_restore_io();
+            bbio->exitSleep();
 		}
 
 		// TODO maybe a good place to put pairing logic
 
-        logInfo("================================");
+        logInfo("\r\n================================");
         wait(1.0); // May want to remove
-        // Need to re-implement some of these sleep functions
-//		sleep_save_io();
-//		sleep_configure_io();
-        intCnt++;
+        loopCnt++;
     }
 
     delete protocol;