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:
Mon Mar 06 15:16:58 2017 -0700
Revision:
60:5179449a684f
Parent:
58:15aa7a785b9f
Child:
61:8d9efd33cac9
Few quick changes before adding pair code

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