Fork to see if I can get working

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

Fork of xDotBridge_update_test20180823 by Matt Briggs

Committer:
Matt Briggs
Date:
Wed Mar 01 17:28:32 2017 -0700
Revision:
58:15aa7a785b9f
Parent:
57:bdac7dd17af2
Child:
60:5179449a684f
Major updates to power down during sleep code.  Other minior updates.  Used for test on 2017/03/01.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matt Briggs 34:5618603e5fc3 1 #include <math.h>
Matt Briggs 22:9453658b8d4b 2 #include "config.h"
Matt Briggs 30:2e673a672884 3 #include "xdot_flash.h"
Mike Fiore 11:d2e31743433a 4 #include "dot_util.h"
Mike Fiore 14:19fae4509473 5 #include "RadioEvent.h"
Matt Briggs 47:a68747642a7a 6 #include "WinbondSPIFlash.h"
Matt Briggs 58:15aa7a785b9f 7 #include "UserInterface.h"
Matt Briggs 41:9ef4c4d77711 8 //#include <xdot_low_power.h>
Matt Briggs 58:15aa7a785b9f 9 #include "mDot.h"
mfiore 17:d4f82e16de5f 10
Matt Briggs 47:a68747642a7a 11 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 12 #include "CommProtocolPeerBrute.h"
Matt Briggs 31:9c535a708ae9 13
Matt Briggs 47:a68747642a7a 14 #ifndef __TEST__ // Exclude code for tests
Mike Fiore 11:d2e31743433a 15 Serial pc(USBTX, USBRX);
Mike Fiore 11:d2e31743433a 16
Matt Briggs 47:a68747642a7a 17 mDot* dot = NULL; // Used by dot-utils
Matt Briggs 39:64f79fa6e3cc 18
Matt Briggs 58:15aa7a785b9f 19 DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
Matt Briggs 48:bab9f747d9ed 20
Matt Briggs 50:e89647e77fd5 21 volatile bool ccIntFlag;
Matt Briggs 50:e89647e77fd5 22 volatile bool tamperIntFlag;
Matt Briggs 50:e89647e77fd5 23 volatile bool pairBtnIntFlag;
Matt Briggs 50:e89647e77fd5 24 void ccInIntCallback () {
Matt Briggs 50:e89647e77fd5 25 ccIntFlag = true;
Matt Briggs 50:e89647e77fd5 26 }
Matt Briggs 50:e89647e77fd5 27 void tamperIntCallback () {
Matt Briggs 50:e89647e77fd5 28 tamperIntFlag = true;
Matt Briggs 50:e89647e77fd5 29 }
Matt Briggs 50:e89647e77fd5 30 void pairBtnIntCallback () {
Matt Briggs 50:e89647e77fd5 31 pairBtnIntFlag = true;
Matt Briggs 50:e89647e77fd5 32 }
Matt Briggs 50:e89647e77fd5 33
Mike Fiore 11:d2e31743433a 34 int main() {
Matt Briggs 53:a1563574a980 35 CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
Matt Briggs 53:a1563574a980 36 BaseboardIO *bbio = new BaseboardIO();
Matt Briggs 58:15aa7a785b9f 37 WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
Matt Briggs 58:15aa7a785b9f 38
Matt Briggs 52:64a2c71c7c49 39 CmdResult result;
Matt Briggs 50:e89647e77fd5 40 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 41 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 42 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 43 pc.baud(115200);
Matt Briggs 48:bab9f747d9ed 44
Matt Briggs 31:9c535a708ae9 45 RadioEvent events; // Custom event handler for automatically displaying RX data
Mike Fiore 11:d2e31743433a 46
Matt Briggs 27:6b68ff715ae1 47 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 47:a68747642a7a 48
Mike Fiore 11:d2e31743433a 49 dot = mDot::getInstance();
Mike Fiore 11:d2e31743433a 50
Mike Fiore 16:a3832552dfe1 51 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 55:79ab0bbc5008 52 logInfo(" libxDot-mbed5 library ID: %s", dot->getId().c_str());
Mike Fiore 16:a3832552dfe1 53
Mike Fiore 12:ec9768677cea 54 // start from a well-known state
Matt Briggs 55:79ab0bbc5008 55 // logInfo("defaulting Dot configuration");
Matt Briggs 55:79ab0bbc5008 56 // dot->resetConfig();
Mike Fiore 12:ec9768677cea 57
Mike Fiore 11:d2e31743433a 58 // make sure library logging is turned on
Matt Briggs 27:6b68ff715ae1 59 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
Mike Fiore 11:d2e31743433a 60
Mike Fiore 11:d2e31743433a 61 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 62 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 63
Matt Briggs 53:a1563574a980 64 // Finish radio init
Matt Briggs 53:a1563574a980 65 protocol->init();
Matt Briggs 53:a1563574a980 66
Matt Briggs 53:a1563574a980 67 // save changes to configuration
Matt Briggs 53:a1563574a980 68 // logInfo("saving configuration");
Matt Briggs 53:a1563574a980 69 // core_util_critical_section_enter();
Matt Briggs 53:a1563574a980 70 // bool dotConfigSaved = dot->saveConfig();
Matt Briggs 53:a1563574a980 71 // core_util_critical_section_exit();
Matt Briggs 53:a1563574a980 72 // if (!dotConfigSaved) {
Matt Briggs 53:a1563574a980 73 // logError("failed to save configuration");
Matt Briggs 53:a1563574a980 74 // }
Mike Fiore 11:d2e31743433a 75
Matt Briggs 31:9c535a708ae9 76 // Setup programmable voltage detector
Matt Briggs 31:9c535a708ae9 77 // PVD_LEVEL0 Falling 1.85
Matt Briggs 31:9c535a708ae9 78 // PVD_LEVEL1 Falling 2.04
Matt Briggs 31:9c535a708ae9 79 // PVD_LEVEL2 Falling 2.24
Matt Briggs 31:9c535a708ae9 80 // PVD_LEVEL3 Falling 2.44
Matt Briggs 31:9c535a708ae9 81 // PVD_LEVEL4 Falling 2.64
Matt Briggs 31:9c535a708ae9 82 // PVD_LEVEL5 Falling 2.84
Matt Briggs 31:9c535a708ae9 83 // PVD_LEVEL6 Falling 3.05
Matt Briggs 50:e89647e77fd5 84 // PWR_PVDTypeDef pvdConfig;
Matt Briggs 50:e89647e77fd5 85 // pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
Matt Briggs 50:e89647e77fd5 86 // pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
Matt Briggs 50:e89647e77fd5 87 //
Matt Briggs 50:e89647e77fd5 88 // HAL_PWR_ConfigPVD(&pvdConfig);
Matt Briggs 50:e89647e77fd5 89 // HAL_PWR_EnablePVD();
Matt Briggs 50:e89647e77fd5 90 // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
Matt Briggs 50:e89647e77fd5 91 // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
Mike Fiore 11:d2e31743433a 92
Matt Briggs 58:15aa7a785b9f 93 dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
Matt Briggs 54:c04d7b6fa075 94 dot->setWakePin(UART1_RX);
Matt Briggs 24:fdf87e4b72e5 95
Mike Fiore 11:d2e31743433a 96 // display configuration
Matt Briggs 53:a1563574a980 97 // display_config();
Matt Briggs 52:64a2c71c7c49 98
Matt Briggs 53:a1563574a980 99 pc.printf("= Baseboard Init Starting =\r\n");
Matt Briggs 53:a1563574a980 100 result = bbio->init();
Matt Briggs 52:64a2c71c7c49 101 if (result == cmdSuccess) {
Matt Briggs 52:64a2c71c7c49 102 pc.printf("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 52:64a2c71c7c49 103 }
Matt Briggs 52:64a2c71c7c49 104 else {
Matt Briggs 52:64a2c71c7c49 105 pc.printf("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 52:64a2c71c7c49 106 }
Mike Fiore 11:d2e31743433a 107
Matt Briggs 57:bdac7dd17af2 108 uint16_t txSeqNum=0;
Matt Briggs 57:bdac7dd17af2 109 uint16_t rxSeqNum=0;
Matt Briggs 50:e89647e77fd5 110 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 50:e89647e77fd5 111 Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 50:e89647e77fd5 112 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 50:e89647e77fd5 113
Matt Briggs 53:a1563574a980 114 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 115 bbio->relayNormal(); // Always force relay in known state
Matt Briggs 58:15aa7a785b9f 116 bbio->regCCInInt(ccInIntObj);
Matt Briggs 58:15aa7a785b9f 117 bbio->regTamperInt(tamperIntObj);
Matt Briggs 58:15aa7a785b9f 118 bbio->regPairBtnInt(pairBtnIntObj);
Matt Briggs 26:9411b26a5084 119
Matt Briggs 58:15aa7a785b9f 120 // Start flash powered down
Matt Briggs 58:15aa7a785b9f 121 flash->powerDown();
Matt Briggs 58:15aa7a785b9f 122
Matt Briggs 58:15aa7a785b9f 123 unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
Matt Briggs 58:15aa7a785b9f 124 bool prevCCNormallyOpen;
Matt Briggs 47:a68747642a7a 125 /**
Matt Briggs 47:a68747642a7a 126 * Main Loop
Matt Briggs 47:a68747642a7a 127 */
Mike Fiore 11:d2e31743433a 128 while (true) {
Matt Briggs 25:fea776239709 129 std::vector<uint8_t> data;
Matt Briggs 53:a1563574a980 130 bbio->ledOff();
Matt Briggs 47:a68747642a7a 131
Matt Briggs 50:e89647e77fd5 132 // Sample IO and update any configuration
Matt Briggs 58:15aa7a785b9f 133 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 53:a1563574a980 134 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 135 if (prevCCNormallyOpen == bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 53:a1563574a980 136 bbio->regCCInInt(ccInIntObj);
Matt Briggs 53:a1563574a980 137 bbio->relayNormal();
Matt Briggs 50:e89647e77fd5 138 }
Matt Briggs 53:a1563574a980 139 if (bbio->isTx()) {
Matt Briggs 53:a1563574a980 140 protocol->setTx(true);
Matt Briggs 50:e89647e77fd5 141 }
Matt Briggs 50:e89647e77fd5 142 else { // RX
Matt Briggs 53:a1563574a980 143 protocol->setTx(false);
Matt Briggs 50:e89647e77fd5 144 }
Matt Briggs 50:e89647e77fd5 145 // End sample and update
Matt Briggs 47:a68747642a7a 146
Matt Briggs 53:a1563574a980 147 if (protocol->isTx()) {
Matt Briggs 58:15aa7a785b9f 148 logInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
Matt Briggs 58:15aa7a785b9f 149 loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
Matt Briggs 53:a1563574a980 150 // TODO add tamper
Matt Briggs 53:a1563574a980 151 if (pairBtnIntFlag) { // Wait up to 1 second for short button hit
Matt Briggs 53:a1563574a980 152 for (int i=0; i<10;i++) {
Matt Briggs 53:a1563574a980 153 if (bbio->isPairBtn() == false){ // Button released
Matt Briggs 53:a1563574a980 154 break;
Matt Briggs 53:a1563574a980 155 }
Matt Briggs 53:a1563574a980 156 wait(0.1);
Matt Briggs 53:a1563574a980 157 }
Matt Briggs 53:a1563574a980 158 }
Matt Briggs 50:e89647e77fd5 159 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 160 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 53:a1563574a980 161 (pairBtnIntFlag && (bbio->isPairBtn() == false))) { // If short pair btn hit
Matt Briggs 47:a68747642a7a 162
Matt Briggs 50:e89647e77fd5 163 ccIntFlag = false;
Matt Briggs 57:bdac7dd17af2 164 pairBtnIntFlag = false;
Matt Briggs 50:e89647e77fd5 165 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 166 #if LED_FEEDBACK
Matt Briggs 53:a1563574a980 167 bbio->ledOn();
Matt Briggs 50:e89647e77fd5 168 #endif
Matt Briggs 50:e89647e77fd5 169
Matt Briggs 57:bdac7dd17af2 170 data.push_back((txSeqNum >> 8) & 0xFF);
Matt Briggs 57:bdac7dd17af2 171 data.push_back(txSeqNum & 0xFF);
Matt Briggs 57:bdac7dd17af2 172 std::string dataStr(data.begin(), data.end());
Matt Briggs 57:bdac7dd17af2 173 logInfo("Sent msg num: %d, payload: %s", txSeqNum, dataStr.c_str());
Matt Briggs 53:a1563574a980 174 protocol->send(data);
Matt Briggs 57:bdac7dd17af2 175 txSeqNum++;
Matt Briggs 50:e89647e77fd5 176 wait(0.5); // Leave the LED on so a person can see it.
Matt Briggs 50:e89647e77fd5 177 }
Matt Briggs 50:e89647e77fd5 178
Matt Briggs 53:a1563574a980 179 bbio->ledOff();
Matt Briggs 58:15aa7a785b9f 180 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 181 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 58:15aa7a785b9f 182 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 2 secs if CCInAlert is asserted
Matt Briggs 57:bdac7dd17af2 183 }
Matt Briggs 57:bdac7dd17af2 184 else {
Matt Briggs 57:bdac7dd17af2 185 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 57:bdac7dd17af2 186 }
Matt Briggs 58:15aa7a785b9f 187 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 188 }
Mike Fiore 11:d2e31743433a 189
Matt Briggs 53:a1563574a980 190 if (protocol->isRx()) {
Matt Briggs 47:a68747642a7a 191 bool msgPending;
Matt Briggs 53:a1563574a980 192 protocol->listen(msgPending);
Matt Briggs 58:15aa7a785b9f 193 logInfo("Loop Cnt %d. Listening.", loopCnt);
Matt Briggs 47:a68747642a7a 194 if (msgPending) {
Matt Briggs 53:a1563574a980 195 protocol->recv(data);
Matt Briggs 47:a68747642a7a 196 std::string dataStr(data.begin(), data.end());
Matt Briggs 57:bdac7dd17af2 197 logInfo("Got msg num: %d, payload: %s", rxSeqNum, dataStr.c_str());
Matt Briggs 53:a1563574a980 198 bbio->relayAlert();
Matt Briggs 57:bdac7dd17af2 199 rxSeqNum++;
Matt Briggs 50:e89647e77fd5 200 #if LED_FEEDBACK
Matt Briggs 53:a1563574a980 201 bbio->ledOn();
Matt Briggs 50:e89647e77fd5 202 #endif
Matt Briggs 58:15aa7a785b9f 203 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 204 // TODO maybe use sleep instead of wait
Matt Briggs 58:15aa7a785b9f 205 logInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 206 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 207 bbio->ledOff();
Matt Briggs 58:15aa7a785b9f 208 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 209 }
Matt Briggs 47:a68747642a7a 210 logInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 54:c04d7b6fa075 211 protocol->sampleDLC();
Matt Briggs 58:15aa7a785b9f 212 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 213 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 214 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 215 }
Matt Briggs 31:9c535a708ae9 216
Matt Briggs 50:e89647e77fd5 217 // TODO maybe a good place to put pairing logic
Matt Briggs 29:e05e35976cfe 218
Matt Briggs 58:15aa7a785b9f 219 logInfo("\r\n================================");
Matt Briggs 57:bdac7dd17af2 220 wait(1.0); // May want to remove
Matt Briggs 58:15aa7a785b9f 221 loopCnt++;
Mike Fiore 11:d2e31743433a 222 }
Matt Briggs 47:a68747642a7a 223
Matt Briggs 55:79ab0bbc5008 224 delete protocol;
Matt Briggs 55:79ab0bbc5008 225 delete bbio;
Mike Fiore 11:d2e31743433a 226 return 0;
Mike Fiore 11:d2e31743433a 227 }
Matt Briggs 47:a68747642a7a 228 #endif
Mike Fiore 11:d2e31743433a 229
Matt Briggs 27:6b68ff715ae1 230