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 15 11:46:40 2017 -0600
Revision:
66:bcaa6dbf538a
Parent:
65:d546060aa03d
Child:
67:2115a2f1b945
Changed to only sample switches with user btn press.  Also hand tuned times based off measurement.

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"
Matt Briggs 63:e1efbe3402d9 10 #include "MyLog.h"
mfiore 17:d4f82e16de5f 11
Matt Briggs 47:a68747642a7a 12 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 13 #include "CommProtocolPeerBrute.h"
Matt Briggs 31:9c535a708ae9 14
Matt Briggs 47:a68747642a7a 15 #ifndef __TEST__ // Exclude code for tests
Mike Fiore 11:d2e31743433a 16 Serial pc(USBTX, USBRX);
Mike Fiore 11:d2e31743433a 17
Matt Briggs 47:a68747642a7a 18 mDot* dot = NULL; // Used by dot-utils
Matt Briggs 39:64f79fa6e3cc 19
Matt Briggs 50:e89647e77fd5 20 volatile bool ccIntFlag;
Matt Briggs 50:e89647e77fd5 21 volatile bool tamperIntFlag;
Matt Briggs 50:e89647e77fd5 22 volatile bool pairBtnIntFlag;
Matt Briggs 50:e89647e77fd5 23 void ccInIntCallback () {
Matt Briggs 50:e89647e77fd5 24 ccIntFlag = true;
Matt Briggs 50:e89647e77fd5 25 }
Matt Briggs 50:e89647e77fd5 26 void tamperIntCallback () {
Matt Briggs 50:e89647e77fd5 27 tamperIntFlag = true;
Matt Briggs 50:e89647e77fd5 28 }
Matt Briggs 50:e89647e77fd5 29 void pairBtnIntCallback () {
Matt Briggs 50:e89647e77fd5 30 pairBtnIntFlag = true;
Matt Briggs 50:e89647e77fd5 31 }
Matt Briggs 50:e89647e77fd5 32
Mike Fiore 11:d2e31743433a 33 int main() {
Matt Briggs 53:a1563574a980 34 CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
Matt Briggs 53:a1563574a980 35 BaseboardIO *bbio = new BaseboardIO();
Matt Briggs 58:15aa7a785b9f 36 WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
Matt Briggs 58:15aa7a785b9f 37
Matt Briggs 52:64a2c71c7c49 38 CmdResult result;
Matt Briggs 50:e89647e77fd5 39 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 40 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 41 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 42 pc.baud(115200);
Matt Briggs 48:bab9f747d9ed 43
Matt Briggs 31:9c535a708ae9 44 RadioEvent events; // Custom event handler for automatically displaying RX data
Mike Fiore 11:d2e31743433a 45
Matt Briggs 27:6b68ff715ae1 46 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 66:bcaa6dbf538a 47 MyLog::setLogLevel(MyLog::DEBUG_LEVEL);
Matt Briggs 47:a68747642a7a 48
Mike Fiore 11:d2e31743433a 49 dot = mDot::getInstance();
Mike Fiore 11:d2e31743433a 50
Mike Fiore 11:d2e31743433a 51 // make sure library logging is turned on
Matt Briggs 66:bcaa6dbf538a 52 dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
Mike Fiore 11:d2e31743433a 53
Matt Briggs 63:e1efbe3402d9 54 myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 63:e1efbe3402d9 55 myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
Matt Briggs 62:9751a8504c82 56
Mike Fiore 11:d2e31743433a 57 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 58 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 59
Matt Briggs 53:a1563574a980 60 // Finish radio init
Matt Briggs 63:e1efbe3402d9 61 myLogInfo("= Protocol Init Starting =\r\n");
Matt Briggs 53:a1563574a980 62 protocol->init();
Matt Briggs 62:9751a8504c82 63 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 64 myLogInfo("= Protocol Init Finished Successfully =\r\n");
Matt Briggs 62:9751a8504c82 65 }
Matt Briggs 62:9751a8504c82 66 else {
Matt Briggs 63:e1efbe3402d9 67 myLogInfo("= Protocol Init Finished with Error =\r\n");
Matt Briggs 62:9751a8504c82 68 }
Matt Briggs 63:e1efbe3402d9 69 protocol->printDotConfig();
Matt Briggs 53:a1563574a980 70
Matt Briggs 53:a1563574a980 71 // save changes to configuration
Matt Briggs 53:a1563574a980 72 // logInfo("saving configuration");
Matt Briggs 53:a1563574a980 73 // core_util_critical_section_enter();
Matt Briggs 53:a1563574a980 74 // bool dotConfigSaved = dot->saveConfig();
Matt Briggs 53:a1563574a980 75 // core_util_critical_section_exit();
Matt Briggs 53:a1563574a980 76 // if (!dotConfigSaved) {
Matt Briggs 53:a1563574a980 77 // logError("failed to save configuration");
Matt Briggs 53:a1563574a980 78 // }
Mike Fiore 11:d2e31743433a 79
Matt Briggs 31:9c535a708ae9 80 // Setup programmable voltage detector
Matt Briggs 31:9c535a708ae9 81 // PVD_LEVEL0 Falling 1.85
Matt Briggs 31:9c535a708ae9 82 // PVD_LEVEL1 Falling 2.04
Matt Briggs 31:9c535a708ae9 83 // PVD_LEVEL2 Falling 2.24
Matt Briggs 31:9c535a708ae9 84 // PVD_LEVEL3 Falling 2.44
Matt Briggs 31:9c535a708ae9 85 // PVD_LEVEL4 Falling 2.64
Matt Briggs 31:9c535a708ae9 86 // PVD_LEVEL5 Falling 2.84
Matt Briggs 31:9c535a708ae9 87 // PVD_LEVEL6 Falling 3.05
Matt Briggs 50:e89647e77fd5 88 // PWR_PVDTypeDef pvdConfig;
Matt Briggs 50:e89647e77fd5 89 // pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
Matt Briggs 50:e89647e77fd5 90 // pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
Matt Briggs 50:e89647e77fd5 91 //
Matt Briggs 50:e89647e77fd5 92 // HAL_PWR_ConfigPVD(&pvdConfig);
Matt Briggs 50:e89647e77fd5 93 // HAL_PWR_EnablePVD();
Matt Briggs 50:e89647e77fd5 94 // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
Matt Briggs 50:e89647e77fd5 95 // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
Mike Fiore 11:d2e31743433a 96
Matt Briggs 58:15aa7a785b9f 97 dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
Matt Briggs 54:c04d7b6fa075 98 dot->setWakePin(UART1_RX);
Matt Briggs 24:fdf87e4b72e5 99
Matt Briggs 62:9751a8504c82 100 display_config(); // Print configuration for now
Matt Briggs 62:9751a8504c82 101
Mike Fiore 11:d2e31743433a 102 // display configuration
Matt Briggs 53:a1563574a980 103 // display_config();
Matt Briggs 52:64a2c71c7c49 104
Matt Briggs 63:e1efbe3402d9 105 myLogInfo("= Baseboard Init Starting =\r\n");
Matt Briggs 53:a1563574a980 106 result = bbio->init();
Matt Briggs 52:64a2c71c7c49 107 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 108 myLogInfo("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 52:64a2c71c7c49 109 }
Matt Briggs 52:64a2c71c7c49 110 else {
Matt Briggs 63:e1efbe3402d9 111 myLogInfo("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 52:64a2c71c7c49 112 }
Matt Briggs 64:46c8819c07cc 113 LedPatterns ledPatterns(bbio);
Mike Fiore 11:d2e31743433a 114
Matt Briggs 50:e89647e77fd5 115 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 50:e89647e77fd5 116 Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 50:e89647e77fd5 117 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 50:e89647e77fd5 118
Matt Briggs 53:a1563574a980 119 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 120 bbio->relayNormal(); // Always force relay in known state
Matt Briggs 58:15aa7a785b9f 121 bbio->regCCInInt(ccInIntObj);
Matt Briggs 58:15aa7a785b9f 122 bbio->regTamperInt(tamperIntObj);
Matt Briggs 58:15aa7a785b9f 123 bbio->regPairBtnInt(pairBtnIntObj);
Matt Briggs 26:9411b26a5084 124
Matt Briggs 66:bcaa6dbf538a 125 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 126 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 127 }
Matt Briggs 66:bcaa6dbf538a 128 else { // RX
Matt Briggs 66:bcaa6dbf538a 129 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 130 }
Matt Briggs 66:bcaa6dbf538a 131
Matt Briggs 58:15aa7a785b9f 132 // Start flash powered down
Matt Briggs 58:15aa7a785b9f 133 flash->powerDown();
Matt Briggs 58:15aa7a785b9f 134
Matt Briggs 58:15aa7a785b9f 135 unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
Matt Briggs 58:15aa7a785b9f 136 bool prevCCNormallyOpen;
Matt Briggs 61:8d9efd33cac9 137 PairBtnState pairBtnState;
Matt Briggs 47:a68747642a7a 138 /**
Matt Briggs 47:a68747642a7a 139 * Main Loop
Matt Briggs 47:a68747642a7a 140 */
Mike Fiore 11:d2e31743433a 141 while (true) {
Matt Briggs 66:bcaa6dbf538a 142 myLogInfo("Start of loop time %d", us_ticker_read());
Matt Briggs 64:46c8819c07cc 143 ledPatterns.turnOff();
Matt Briggs 47:a68747642a7a 144
Matt Briggs 66:bcaa6dbf538a 145 // Pair logic and switch sampling
Matt Briggs 61:8d9efd33cac9 146 if (pairBtnIntFlag) {
Matt Briggs 61:8d9efd33cac9 147 pairBtnState = PairBtnInterp::read(bbio);
Matt Briggs 66:bcaa6dbf538a 148
Matt Briggs 66:bcaa6dbf538a 149 // Sample IO and update any configuration
Matt Briggs 66:bcaa6dbf538a 150 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 66:bcaa6dbf538a 151 bbio->sampleUserSwitches();
Matt Briggs 66:bcaa6dbf538a 152 if (prevCCNormallyOpen != bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 66:bcaa6dbf538a 153 bbio->regCCInInt(ccInIntObj);
Matt Briggs 66:bcaa6dbf538a 154 bbio->relayNormal();
Matt Briggs 66:bcaa6dbf538a 155 }
Matt Briggs 66:bcaa6dbf538a 156 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 157 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 158 }
Matt Briggs 66:bcaa6dbf538a 159 else { // RX
Matt Briggs 66:bcaa6dbf538a 160 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 161 }
Matt Briggs 61:8d9efd33cac9 162 if (protocol->isTx()) {
Matt Briggs 61:8d9efd33cac9 163 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 164 protocol->configForPairingNetwork();
Matt Briggs 61:8d9efd33cac9 165 protocol->sendPairReq();
Matt Briggs 63:e1efbe3402d9 166 myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 167 result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 168 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 169 myLogInfo("Got accept");
Matt Briggs 64:46c8819c07cc 170 ledPatterns.turnOff();
Matt Briggs 64:46c8819c07cc 171 wait(0.5);
Matt Briggs 64:46c8819c07cc 172 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 173 }
Matt Briggs 61:8d9efd33cac9 174 else {
Matt Briggs 63:e1efbe3402d9 175 myLogInfo("Did not receive accept");
Matt Briggs 61:8d9efd33cac9 176 }
Matt Briggs 62:9751a8504c82 177 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 178 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 179 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 180 }
Matt Briggs 61:8d9efd33cac9 181 }
Matt Briggs 61:8d9efd33cac9 182 if (protocol->isRx()) {
Matt Briggs 61:8d9efd33cac9 183 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 184 protocol->configForPairingNetwork();
Matt Briggs 64:46c8819c07cc 185 ledPatterns.turnOn();
Matt Briggs 63:e1efbe3402d9 186 myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 187 result = protocol->waitForPairing(RX_PAIR_WAIT_TIME);
Matt Briggs 64:46c8819c07cc 188 ledPatterns.turnOff();
Matt Briggs 61:8d9efd33cac9 189 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 190 myLogInfo("Got pair request and responded");
Matt Briggs 64:46c8819c07cc 191 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 192 }
Matt Briggs 61:8d9efd33cac9 193 else if (result == cmdTimeout) {
Matt Briggs 63:e1efbe3402d9 194 myLogInfo("Did not receive request");
Matt Briggs 61:8d9efd33cac9 195 }
Matt Briggs 61:8d9efd33cac9 196 else {
Matt Briggs 63:e1efbe3402d9 197 myLogInfo("Unknown pair error");
Matt Briggs 61:8d9efd33cac9 198 }
Matt Briggs 62:9751a8504c82 199 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 200 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 201 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 202 }
Matt Briggs 61:8d9efd33cac9 203 else if (pairBtnState == pairBtnLongPress) {
Matt Briggs 63:e1efbe3402d9 204 myLogInfo("Clearing pair values and generating new ones.");
Matt Briggs 61:8d9efd33cac9 205 protocol->clearPair();
Matt Briggs 63:e1efbe3402d9 206 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 207 ledPatterns.tenBlinks();
Matt Briggs 64:46c8819c07cc 208 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 209 }
Matt Briggs 61:8d9efd33cac9 210 }
Matt Briggs 61:8d9efd33cac9 211 }
Matt Briggs 47:a68747642a7a 212
Matt Briggs 61:8d9efd33cac9 213 // Alert code
Matt Briggs 53:a1563574a980 214 if (protocol->isTx()) {
Matt Briggs 63:e1efbe3402d9 215 myLogInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
Matt Briggs 58:15aa7a785b9f 216 loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
Matt Briggs 53:a1563574a980 217 // TODO add tamper
Matt Briggs 50:e89647e77fd5 218 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 219 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 61:8d9efd33cac9 220 (pairBtnIntFlag && (pairBtnState == pairBtnShortPress))) {
Matt Briggs 50:e89647e77fd5 221 ccIntFlag = false;
Matt Briggs 64:46c8819c07cc 222 ledPatterns.turnOn();
Matt Briggs 50:e89647e77fd5 223
Matt Briggs 66:bcaa6dbf538a 224 myLogInfo("Sending msg num: %d.", protocol->getSeqNum());
Matt Briggs 65:d546060aa03d 225 protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper
Matt Briggs 64:46c8819c07cc 226 ledPatterns.turnOff();
Matt Briggs 50:e89647e77fd5 227 }
Matt Briggs 50:e89647e77fd5 228
Matt Briggs 64:46c8819c07cc 229 ledPatterns.turnOff();
Matt Briggs 62:9751a8504c82 230 pairBtnIntFlag = false;
Matt Briggs 62:9751a8504c82 231 tamperIntFlag = false;
Matt Briggs 58:15aa7a785b9f 232 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 233 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 58:15aa7a785b9f 234 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 235 }
Matt Briggs 57:bdac7dd17af2 236 else {
Matt Briggs 57:bdac7dd17af2 237 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 57:bdac7dd17af2 238 }
Matt Briggs 58:15aa7a785b9f 239 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 240 }
Mike Fiore 11:d2e31743433a 241
Matt Briggs 53:a1563574a980 242 if (protocol->isRx()) {
Matt Briggs 66:bcaa6dbf538a 243 ccIntFlag = false;
Matt Briggs 66:bcaa6dbf538a 244 pairBtnIntFlag = false;
Matt Briggs 66:bcaa6dbf538a 245 tamperIntFlag = false;
Matt Briggs 66:bcaa6dbf538a 246 myLogInfo("Before listen time %d", us_ticker_read());
Matt Briggs 47:a68747642a7a 247 bool msgPending;
Matt Briggs 53:a1563574a980 248 protocol->listen(msgPending);
Matt Briggs 63:e1efbe3402d9 249 myLogInfo("Loop Cnt %d. Listening.", loopCnt);
Matt Briggs 47:a68747642a7a 250 if (msgPending) {
Matt Briggs 65:d546060aa03d 251 std::vector<uint8_t> txEui;
Matt Briggs 65:d546060aa03d 252 txEui.reserve(8);
Matt Briggs 65:d546060aa03d 253 uint16_t data;
Matt Briggs 65:d546060aa03d 254 uint32_t msgSeqNum;
Matt Briggs 65:d546060aa03d 255 protocol->recvAlert(txEui, data, msgSeqNum);
Matt Briggs 66:bcaa6dbf538a 256 myLogInfo("Got msg num: %d", msgSeqNum);
Matt Briggs 53:a1563574a980 257 bbio->relayAlert();
Matt Briggs 64:46c8819c07cc 258 ledPatterns.turnOn();
Matt Briggs 66:bcaa6dbf538a 259 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 260 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 261 // TODO maybe use sleep instead of wait
Matt Briggs 58:15aa7a785b9f 262 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 64:46c8819c07cc 263 ledPatterns.turnOff();
Matt Briggs 58:15aa7a785b9f 264 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 265 }
Matt Briggs 63:e1efbe3402d9 266 myLogInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 58:15aa7a785b9f 267 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 268 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 269 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 270 }
Matt Briggs 31:9c535a708ae9 271
Matt Briggs 64:46c8819c07cc 272 protocol->resetCounters();
Matt Briggs 63:e1efbe3402d9 273 myLogInfo("\r\n================================");
Matt Briggs 58:15aa7a785b9f 274 loopCnt++;
Mike Fiore 11:d2e31743433a 275 }
Matt Briggs 47:a68747642a7a 276
Matt Briggs 55:79ab0bbc5008 277 delete protocol;
Matt Briggs 55:79ab0bbc5008 278 delete bbio;
Mike Fiore 11:d2e31743433a 279 return 0;
Mike Fiore 11:d2e31743433a 280 }
Matt Briggs 47:a68747642a7a 281 #endif
Mike Fiore 11:d2e31743433a 282
Matt Briggs 27:6b68ff715ae1 283