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:
Thu Mar 09 16:47:42 2017 -0700
Revision:
61:8d9efd33cac9
Parent:
60:5179449a684f
Child:
62:9751a8504c82
Slight modification of pair code and documentation.  Also some test code to all xDot dev boards to act like bridges.

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