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:
Tue Mar 14 11:53:53 2017 -0600
Revision:
64:46c8819c07cc
Parent:
63:e1efbe3402d9
Child:
65:d546060aa03d
Continuted debug of pairing code.  Qcuik hack to disable counters.

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