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 May 16 15:15:58 2017 -0600
Revision:
76:d1b20a259d8f
Parent:
75:600cb3a9f126
Child:
77:176e3eb8f712
Quick commit before lib add.

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"
Matt Briggs 69:eb391644b346 11 #include "SimpleRxSeqLog.h"
mfiore 17:d4f82e16de5f 12
Matt Briggs 47:a68747642a7a 13 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 14 #include "CommProtocolPeerBrute.h"
Matt Briggs 75:600cb3a9f126 15 #include "SerialTermMgr.h"
Matt Briggs 31:9c535a708ae9 16
Matt Briggs 69:eb391644b346 17 #define RX_SEQ_LOG 1
Matt Briggs 69:eb391644b346 18
Matt Briggs 75:600cb3a9f126 19 const float BridgeVersion = 1.01;
Matt Briggs 69:eb391644b346 20
Matt Briggs 47:a68747642a7a 21 #ifndef __TEST__ // Exclude code for tests
Mike Fiore 11:d2e31743433a 22 Serial pc(USBTX, USBRX);
Matt Briggs 75:600cb3a9f126 23 //RawSerial outPc(UART_TX, UART_RX);
Matt Briggs 75:600cb3a9f126 24 Serial *outPc;
Matt Briggs 75:600cb3a9f126 25 InterruptIn uart1RxIntIn (UART_RX);
Matt Briggs 75:600cb3a9f126 26
Matt Briggs 75:600cb3a9f126 27 //// Serial interrupts / buffer code
Matt Briggs 75:600cb3a9f126 28 //const int buffer_size = 255;
Matt Briggs 75:600cb3a9f126 29 //// might need to increase buffer size for high baud rates
Matt Briggs 75:600cb3a9f126 30 //char tx_buffer[buffer_size+1];
Matt Briggs 75:600cb3a9f126 31 //char rx_buffer[buffer_size+1];
Matt Briggs 75:600cb3a9f126 32 //// Circular buffer pointers
Matt Briggs 75:600cb3a9f126 33 //// volatile makes read-modify-write atomic
Matt Briggs 75:600cb3a9f126 34 //volatile int tx_in=0;
Matt Briggs 75:600cb3a9f126 35 //volatile int tx_out=0;
Matt Briggs 75:600cb3a9f126 36 //volatile int rx_in=0;
Matt Briggs 75:600cb3a9f126 37 //volatile int rx_out=0;
Matt Briggs 75:600cb3a9f126 38 //
Matt Briggs 75:600cb3a9f126 39 //// Interupt Routine to read in data from serial port
Matt Briggs 75:600cb3a9f126 40 //void Rx_interrupt() {
Matt Briggs 75:600cb3a9f126 41 // led1=1;
Matt Briggs 75:600cb3a9f126 42 //// Loop just in case more than one character is in UART's receive FIFO buffer
Matt Briggs 75:600cb3a9f126 43 //// Stop if buffer full
Matt Briggs 75:600cb3a9f126 44 // while ((outPc.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
Matt Briggs 75:600cb3a9f126 45 // rx_buffer[rx_in] = outPc.getc();
Matt Briggs 75:600cb3a9f126 46 //// Uncomment to Echo to USB serial to watch data flow
Matt Briggs 75:600cb3a9f126 47 //// monitor_device.putc(rx_buffer[rx_in]);
Matt Briggs 75:600cb3a9f126 48 // rx_in = (rx_in + 1) % buffer_size;
Matt Briggs 75:600cb3a9f126 49 // }
Matt Briggs 75:600cb3a9f126 50 // led1=0;
Matt Briggs 75:600cb3a9f126 51 // return;
Matt Briggs 75:600cb3a9f126 52 //}
Matt Briggs 75:600cb3a9f126 53 //
Matt Briggs 75:600cb3a9f126 54 //// Interupt Routine to write out data to serial port
Matt Briggs 75:600cb3a9f126 55 //void Tx_interrupt() {
Matt Briggs 75:600cb3a9f126 56 // led2=1;
Matt Briggs 75:600cb3a9f126 57 //// Loop to fill more than one character in UART's transmit FIFO buffer
Matt Briggs 75:600cb3a9f126 58 //// Stop if buffer empty
Matt Briggs 75:600cb3a9f126 59 // while ((outPc.writeable()) && (tx_in != tx_out)) {
Matt Briggs 75:600cb3a9f126 60 // outPc.putc(tx_buffer[tx_out]);
Matt Briggs 75:600cb3a9f126 61 // tx_out = (tx_out + 1) % buffer_size;
Matt Briggs 75:600cb3a9f126 62 // }
Matt Briggs 75:600cb3a9f126 63 // led2=0;
Matt Briggs 75:600cb3a9f126 64 // return;
Matt Briggs 75:600cb3a9f126 65 //}
Mike Fiore 11:d2e31743433a 66
Matt Briggs 47:a68747642a7a 67 mDot* dot = NULL; // Used by dot-utils
Matt Briggs 39:64f79fa6e3cc 68
Matt Briggs 75:600cb3a9f126 69 volatile bool uartRxFlag;
Matt Briggs 50:e89647e77fd5 70 volatile bool ccIntFlag;
Matt Briggs 50:e89647e77fd5 71 volatile bool tamperIntFlag;
Matt Briggs 50:e89647e77fd5 72 volatile bool pairBtnIntFlag;
Matt Briggs 75:600cb3a9f126 73 void uart1RxCallback () {
Matt Briggs 75:600cb3a9f126 74 uartRxFlag = true;
Matt Briggs 75:600cb3a9f126 75 }
Matt Briggs 50:e89647e77fd5 76 void ccInIntCallback () {
Matt Briggs 50:e89647e77fd5 77 ccIntFlag = true;
Matt Briggs 50:e89647e77fd5 78 }
Matt Briggs 50:e89647e77fd5 79 void tamperIntCallback () {
Matt Briggs 50:e89647e77fd5 80 tamperIntFlag = true;
Matt Briggs 50:e89647e77fd5 81 }
Matt Briggs 50:e89647e77fd5 82 void pairBtnIntCallback () {
Matt Briggs 50:e89647e77fd5 83 pairBtnIntFlag = true;
Matt Briggs 50:e89647e77fd5 84 }
Matt Briggs 50:e89647e77fd5 85
Mike Fiore 11:d2e31743433a 86 int main() {
Matt Briggs 53:a1563574a980 87 CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
Matt Briggs 53:a1563574a980 88 BaseboardIO *bbio = new BaseboardIO();
Matt Briggs 58:15aa7a785b9f 89 WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
Matt Briggs 75:600cb3a9f126 90 SerialTermMgr serialTermMgr(bbio, BridgeVersion);
Matt Briggs 58:15aa7a785b9f 91
Matt Briggs 75:600cb3a9f126 92 pc.baud(115200);
Matt Briggs 52:64a2c71c7c49 93 CmdResult result;
Matt Briggs 75:600cb3a9f126 94 uartRxFlag = false;
Matt Briggs 50:e89647e77fd5 95 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 96 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 97 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 98
Matt Briggs 31:9c535a708ae9 99 RadioEvent events; // Custom event handler for automatically displaying RX data
Matt Briggs 69:eb391644b346 100 #if RX_SEQ_LOG
Matt Briggs 69:eb391644b346 101 SimpleRxSeqLog rxSeqLog(0x0000);
Matt Briggs 69:eb391644b346 102 rxSeqLog.read();
Matt Briggs 69:eb391644b346 103 #endif
Mike Fiore 11:d2e31743433a 104
Matt Briggs 27:6b68ff715ae1 105 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 66:bcaa6dbf538a 106 MyLog::setLogLevel(MyLog::DEBUG_LEVEL);
Matt Briggs 47:a68747642a7a 107
Mike Fiore 11:d2e31743433a 108 dot = mDot::getInstance();
Mike Fiore 11:d2e31743433a 109
Mike Fiore 11:d2e31743433a 110 // make sure library logging is turned on
Matt Briggs 66:bcaa6dbf538a 111 dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
Mike Fiore 11:d2e31743433a 112
Matt Briggs 63:e1efbe3402d9 113 myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 63:e1efbe3402d9 114 myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
Matt Briggs 69:eb391644b346 115 myLogInfo("Vortex Wireless Bridge SW Version %0.2f", BridgeVersion);
Matt Briggs 62:9751a8504c82 116
Mike Fiore 11:d2e31743433a 117 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 118 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 119
Matt Briggs 53:a1563574a980 120 // Finish radio init
Matt Briggs 63:e1efbe3402d9 121 myLogInfo("= Protocol Init Starting =\r\n");
Matt Briggs 53:a1563574a980 122 protocol->init();
Matt Briggs 62:9751a8504c82 123 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 124 myLogInfo("= Protocol Init Finished Successfully =\r\n");
Matt Briggs 62:9751a8504c82 125 }
Matt Briggs 62:9751a8504c82 126 else {
Matt Briggs 63:e1efbe3402d9 127 myLogInfo("= Protocol Init Finished with Error =\r\n");
Matt Briggs 62:9751a8504c82 128 }
Matt Briggs 63:e1efbe3402d9 129 protocol->printDotConfig();
Matt Briggs 53:a1563574a980 130
Matt Briggs 53:a1563574a980 131 // save changes to configuration
Matt Briggs 53:a1563574a980 132 // logInfo("saving configuration");
Matt Briggs 53:a1563574a980 133 // core_util_critical_section_enter();
Matt Briggs 53:a1563574a980 134 // bool dotConfigSaved = dot->saveConfig();
Matt Briggs 53:a1563574a980 135 // core_util_critical_section_exit();
Matt Briggs 53:a1563574a980 136 // if (!dotConfigSaved) {
Matt Briggs 53:a1563574a980 137 // logError("failed to save configuration");
Matt Briggs 53:a1563574a980 138 // }
Mike Fiore 11:d2e31743433a 139
Matt Briggs 31:9c535a708ae9 140 // Setup programmable voltage detector
Matt Briggs 31:9c535a708ae9 141 // PVD_LEVEL0 Falling 1.85
Matt Briggs 31:9c535a708ae9 142 // PVD_LEVEL1 Falling 2.04
Matt Briggs 31:9c535a708ae9 143 // PVD_LEVEL2 Falling 2.24
Matt Briggs 31:9c535a708ae9 144 // PVD_LEVEL3 Falling 2.44
Matt Briggs 31:9c535a708ae9 145 // PVD_LEVEL4 Falling 2.64
Matt Briggs 31:9c535a708ae9 146 // PVD_LEVEL5 Falling 2.84
Matt Briggs 31:9c535a708ae9 147 // PVD_LEVEL6 Falling 3.05
Matt Briggs 50:e89647e77fd5 148 // PWR_PVDTypeDef pvdConfig;
Matt Briggs 50:e89647e77fd5 149 // pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
Matt Briggs 50:e89647e77fd5 150 // pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
Matt Briggs 50:e89647e77fd5 151 //
Matt Briggs 50:e89647e77fd5 152 // HAL_PWR_ConfigPVD(&pvdConfig);
Matt Briggs 50:e89647e77fd5 153 // HAL_PWR_EnablePVD();
Matt Briggs 50:e89647e77fd5 154 // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
Matt Briggs 50:e89647e77fd5 155 // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
Mike Fiore 11:d2e31743433a 156
Matt Briggs 58:15aa7a785b9f 157 dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
Matt Briggs 75:600cb3a9f126 158 dot->setWakePin(GPIO0);
Matt Briggs 75:600cb3a9f126 159 // dot->setWakePin(UART_RX);
Matt Briggs 24:fdf87e4b72e5 160
Matt Briggs 62:9751a8504c82 161 display_config(); // Print configuration for now
Matt Briggs 62:9751a8504c82 162
Mike Fiore 11:d2e31743433a 163 // display configuration
Matt Briggs 53:a1563574a980 164 // display_config();
Matt Briggs 52:64a2c71c7c49 165
Matt Briggs 63:e1efbe3402d9 166 myLogInfo("= Baseboard Init Starting =\r\n");
Matt Briggs 53:a1563574a980 167 result = bbio->init();
Matt Briggs 52:64a2c71c7c49 168 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 169 myLogInfo("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 52:64a2c71c7c49 170 }
Matt Briggs 52:64a2c71c7c49 171 else {
Matt Briggs 63:e1efbe3402d9 172 myLogInfo("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 52:64a2c71c7c49 173 }
Matt Briggs 64:46c8819c07cc 174 LedPatterns ledPatterns(bbio);
Mike Fiore 11:d2e31743433a 175
Matt Briggs 76:d1b20a259d8f 176 uart1RxIntIn.rise(&uart1RxCallback); // Rising does appear to work better than falling edge
Matt Briggs 75:600cb3a9f126 177 uart1RxIntIn.enable_irq();
Matt Briggs 50:e89647e77fd5 178 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 69:eb391644b346 179 // Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 50:e89647e77fd5 180 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 50:e89647e77fd5 181
Matt Briggs 53:a1563574a980 182 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 183 bbio->relayNormal(); // Always force relay in known state
Matt Briggs 58:15aa7a785b9f 184 bbio->regCCInInt(ccInIntObj);
Matt Briggs 69:eb391644b346 185 // bbio->regTamperInt(tamperIntObj);
Matt Briggs 58:15aa7a785b9f 186 bbio->regPairBtnInt(pairBtnIntObj);
Matt Briggs 26:9411b26a5084 187
Matt Briggs 66:bcaa6dbf538a 188 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 189 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 190 }
Matt Briggs 66:bcaa6dbf538a 191 else { // RX
Matt Briggs 66:bcaa6dbf538a 192 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 193 }
Matt Briggs 66:bcaa6dbf538a 194
Matt Briggs 58:15aa7a785b9f 195 // Start flash powered down
Matt Briggs 58:15aa7a785b9f 196 flash->powerDown();
Matt Briggs 58:15aa7a785b9f 197
Matt Briggs 69:eb391644b346 198 unsigned int loopCnt = 0; // Just a quick temp variable to keep track of loopNums
Matt Briggs 58:15aa7a785b9f 199 bool prevCCNormallyOpen;
Matt Briggs 61:8d9efd33cac9 200 PairBtnState pairBtnState;
Matt Briggs 67:2115a2f1b945 201 uint32_t rxMsgCnt = 0;
Matt Briggs 69:eb391644b346 202 uint32_t maxSeenMsgSeqNum = 0;
Matt Briggs 47:a68747642a7a 203 /**
Matt Briggs 47:a68747642a7a 204 * Main Loop
Matt Briggs 47:a68747642a7a 205 */
Mike Fiore 11:d2e31743433a 206 while (true) {
Matt Briggs 69:eb391644b346 207 // myLogInfo("Start of loop time %d", us_ticker_read());
Matt Briggs 64:46c8819c07cc 208 ledPatterns.turnOff();
Matt Briggs 47:a68747642a7a 209
Matt Briggs 66:bcaa6dbf538a 210 // Pair logic and switch sampling
Matt Briggs 61:8d9efd33cac9 211 if (pairBtnIntFlag) {
Matt Briggs 61:8d9efd33cac9 212 pairBtnState = PairBtnInterp::read(bbio);
Matt Briggs 66:bcaa6dbf538a 213
Matt Briggs 66:bcaa6dbf538a 214 // Sample IO and update any configuration
Matt Briggs 66:bcaa6dbf538a 215 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 66:bcaa6dbf538a 216 bbio->sampleUserSwitches();
Matt Briggs 66:bcaa6dbf538a 217 if (prevCCNormallyOpen != bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 66:bcaa6dbf538a 218 bbio->regCCInInt(ccInIntObj);
Matt Briggs 66:bcaa6dbf538a 219 bbio->relayNormal();
Matt Briggs 66:bcaa6dbf538a 220 }
Matt Briggs 66:bcaa6dbf538a 221 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 222 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 223 }
Matt Briggs 66:bcaa6dbf538a 224 else { // RX
Matt Briggs 66:bcaa6dbf538a 225 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 226 }
Matt Briggs 61:8d9efd33cac9 227 if (protocol->isTx()) {
Matt Briggs 61:8d9efd33cac9 228 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 229 protocol->configForPairingNetwork();
Matt Briggs 61:8d9efd33cac9 230 protocol->sendPairReq();
Matt Briggs 63:e1efbe3402d9 231 myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 232 result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 233 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 234 myLogInfo("Got accept");
Matt Briggs 64:46c8819c07cc 235 ledPatterns.turnOff();
Matt Briggs 64:46c8819c07cc 236 wait(0.5);
Matt Briggs 64:46c8819c07cc 237 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 238 }
Matt Briggs 61:8d9efd33cac9 239 else {
Matt Briggs 63:e1efbe3402d9 240 myLogInfo("Did not receive accept");
Matt Briggs 61:8d9efd33cac9 241 }
Matt Briggs 62:9751a8504c82 242 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 243 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 244 protocol->resetCounters();
Matt Briggs 71:2b57529df137 245
Matt Briggs 71:2b57529df137 246 // Clear test log
Matt Briggs 71:2b57529df137 247 rxSeqLog.clear();
Matt Briggs 71:2b57529df137 248 rxSeqLog.save();
Matt Briggs 71:2b57529df137 249 myLogInfo("NVM Cleared Seq Log");
Matt Briggs 61:8d9efd33cac9 250 }
Matt Briggs 61:8d9efd33cac9 251 }
Matt Briggs 61:8d9efd33cac9 252 if (protocol->isRx()) {
Matt Briggs 61:8d9efd33cac9 253 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 254 protocol->configForPairingNetwork();
Matt Briggs 64:46c8819c07cc 255 ledPatterns.turnOn();
Matt Briggs 63:e1efbe3402d9 256 myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 257 result = protocol->waitForPairing(RX_PAIR_WAIT_TIME);
Matt Briggs 64:46c8819c07cc 258 ledPatterns.turnOff();
Matt Briggs 61:8d9efd33cac9 259 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 260 myLogInfo("Got pair request and responded");
Matt Briggs 64:46c8819c07cc 261 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 262 }
Matt Briggs 61:8d9efd33cac9 263 else if (result == cmdTimeout) {
Matt Briggs 63:e1efbe3402d9 264 myLogInfo("Did not receive request");
Matt Briggs 61:8d9efd33cac9 265 }
Matt Briggs 61:8d9efd33cac9 266 else {
Matt Briggs 63:e1efbe3402d9 267 myLogInfo("Unknown pair error");
Matt Briggs 61:8d9efd33cac9 268 }
Matt Briggs 62:9751a8504c82 269 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 270 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 271 protocol->resetCounters();
Matt Briggs 71:2b57529df137 272
Matt Briggs 71:2b57529df137 273 // Clear test log
Matt Briggs 71:2b57529df137 274 rxSeqLog.clear();
Matt Briggs 71:2b57529df137 275 rxSeqLog.save();
Matt Briggs 71:2b57529df137 276 myLogInfo("NVM Cleared Seq Log");
Matt Briggs 61:8d9efd33cac9 277 }
Matt Briggs 61:8d9efd33cac9 278 else if (pairBtnState == pairBtnLongPress) {
Matt Briggs 63:e1efbe3402d9 279 myLogInfo("Clearing pair values and generating new ones.");
Matt Briggs 61:8d9efd33cac9 280 protocol->clearPair();
Matt Briggs 63:e1efbe3402d9 281 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 282 ledPatterns.tenBlinks();
Matt Briggs 64:46c8819c07cc 283 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 284 }
Matt Briggs 61:8d9efd33cac9 285 }
Matt Briggs 61:8d9efd33cac9 286 }
Matt Briggs 69:eb391644b346 287 else {
Matt Briggs 69:eb391644b346 288 pairBtnState = pairBtnNoPress;
Matt Briggs 69:eb391644b346 289 }
Matt Briggs 69:eb391644b346 290 pairBtnIntFlag = false;
Matt Briggs 47:a68747642a7a 291
Matt Briggs 75:600cb3a9f126 292 // Serial Terminal
Matt Briggs 75:600cb3a9f126 293 if (uartRxFlag) {
Matt Briggs 75:600cb3a9f126 294 uart1RxIntIn.disable_irq();
Matt Briggs 75:600cb3a9f126 295 // pc.printf("Got uart flag!!!\r\n");
Matt Briggs 75:600cb3a9f126 296 uartRxFlag = false;
Matt Briggs 75:600cb3a9f126 297 outPc = new Serial(UART1_TX, UART1_RX);
Matt Briggs 76:d1b20a259d8f 298 outPc->baud(TERM_BAUD);
Matt Briggs 75:600cb3a9f126 299 serialTermMgr.regSerial(outPc);
Matt Briggs 75:600cb3a9f126 300 char c;
Matt Briggs 75:600cb3a9f126 301 if (outPc->readable()) {
Matt Briggs 75:600cb3a9f126 302 c = outPc->getc(); // Throw away the first char
Matt Briggs 75:600cb3a9f126 303 pc.printf("Got %d, %c\r\n", c, c);
Matt Briggs 75:600cb3a9f126 304 }
Matt Briggs 75:600cb3a9f126 305
Matt Briggs 75:600cb3a9f126 306 outPc->printf("Starting Terminal...\r\n");
Matt Briggs 75:600cb3a9f126 307 serialTermMgr.printScreen();
Matt Briggs 75:600cb3a9f126 308 time_t termLastAction = time(NULL);
Matt Briggs 75:600cb3a9f126 309 bool quit = false;
Matt Briggs 75:600cb3a9f126 310 while(!quit && (time(NULL) < (termLastAction + TERM_TIMEOUT))) {
Matt Briggs 75:600cb3a9f126 311 if (outPc->readable()) {
Matt Briggs 75:600cb3a9f126 312 // pc.printf("Got %d\r\n", outPc->getc());
Matt Briggs 75:600cb3a9f126 313 termLastAction = time(NULL);
Matt Briggs 75:600cb3a9f126 314 quit = serialTermMgr.input();
Matt Briggs 75:600cb3a9f126 315 }
Matt Briggs 75:600cb3a9f126 316 }
Matt Briggs 75:600cb3a9f126 317 if (quit) {
Matt Briggs 75:600cb3a9f126 318 outPc->printf("Terminal quit resuming operation <press any key to reactivate>...\r\n");
Matt Briggs 75:600cb3a9f126 319 }
Matt Briggs 75:600cb3a9f126 320 else {
Matt Briggs 75:600cb3a9f126 321 outPc->printf("Terminal timeout resuming operation <press any key to reactivate>...\r\n");
Matt Briggs 75:600cb3a9f126 322 }
Matt Briggs 75:600cb3a9f126 323 serialTermMgr.regSerial(NULL);
Matt Briggs 75:600cb3a9f126 324 delete outPc;
Matt Briggs 75:600cb3a9f126 325 uart1RxIntIn.enable_irq();
Matt Briggs 75:600cb3a9f126 326 }
Matt Briggs 75:600cb3a9f126 327 wait(0.1);
Matt Briggs 75:600cb3a9f126 328
Matt Briggs 69:eb391644b346 329 myLogInfo("Loop #%d. isTX %d, CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnState %d",
Matt Briggs 69:eb391644b346 330 loopCnt, protocol->isTx(), ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnState);
Matt Briggs 61:8d9efd33cac9 331 // Alert code
Matt Briggs 53:a1563574a980 332 if (protocol->isTx()) {
Matt Briggs 53:a1563574a980 333 // TODO add tamper
Matt Briggs 50:e89647e77fd5 334 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 335 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 69:eb391644b346 336 pairBtnState == pairBtnShortPress) {
Matt Briggs 50:e89647e77fd5 337 ccIntFlag = false;
Matt Briggs 64:46c8819c07cc 338 ledPatterns.turnOn();
Matt Briggs 50:e89647e77fd5 339
Matt Briggs 66:bcaa6dbf538a 340 myLogInfo("Sending msg num: %d.", protocol->getSeqNum());
Matt Briggs 65:d546060aa03d 341 protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper
Matt Briggs 64:46c8819c07cc 342 ledPatterns.turnOff();
Matt Briggs 50:e89647e77fd5 343 }
Matt Briggs 50:e89647e77fd5 344
Matt Briggs 64:46c8819c07cc 345 ledPatterns.turnOff();
Matt Briggs 69:eb391644b346 346 // tamperIntFlag = false;
Matt Briggs 69:eb391644b346 347 if (pairBtnIntFlag) {// An Interrupt happened during transmission
Matt Briggs 69:eb391644b346 348 continue; // Go straight to pair handling
Matt Briggs 69:eb391644b346 349 }
Matt Briggs 58:15aa7a785b9f 350 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 351 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 68:51c25f4f6d9a 352 // Sleep for 5 seconds to ensure that receiver does not miss a message sequence
Matt Briggs 75:600cb3a9f126 353 dot->sleep(5, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 5 secs if CCInAlert is asserted
Matt Briggs 57:bdac7dd17af2 354 }
Matt Briggs 57:bdac7dd17af2 355 else {
Matt Briggs 75:600cb3a9f126 356 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until interrupt event
Matt Briggs 75:600cb3a9f126 357 // wait(0.5);
Matt Briggs 57:bdac7dd17af2 358 }
Matt Briggs 58:15aa7a785b9f 359 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 360 }
Mike Fiore 11:d2e31743433a 361
Matt Briggs 53:a1563574a980 362 if (protocol->isRx()) {
Matt Briggs 66:bcaa6dbf538a 363 ccIntFlag = false;
Matt Briggs 69:eb391644b346 364 // tamperIntFlag = false;
Matt Briggs 69:eb391644b346 365 // myLogInfo("Before listen time %d", us_ticker_read());
Matt Briggs 47:a68747642a7a 366 bool msgPending;
Matt Briggs 53:a1563574a980 367 protocol->listen(msgPending);
Matt Briggs 47:a68747642a7a 368 if (msgPending) {
Matt Briggs 65:d546060aa03d 369 std::vector<uint8_t> txEui;
Matt Briggs 65:d546060aa03d 370 txEui.reserve(8);
Matt Briggs 65:d546060aa03d 371 uint16_t data;
Matt Briggs 65:d546060aa03d 372 uint32_t msgSeqNum;
Matt Briggs 65:d546060aa03d 373 protocol->recvAlert(txEui, data, msgSeqNum);
Matt Briggs 69:eb391644b346 374 if (msgSeqNum > maxSeenMsgSeqNum)
Matt Briggs 69:eb391644b346 375 maxSeenMsgSeqNum = msgSeqNum;
Matt Briggs 69:eb391644b346 376 rxMsgCnt++; // for message
Matt Briggs 69:eb391644b346 377 myLogInfo("Got rxMsgCnt #%d, with Seqnum: %d", rxMsgCnt, msgSeqNum);
Matt Briggs 53:a1563574a980 378 bbio->relayAlert();
Matt Briggs 64:46c8819c07cc 379 ledPatterns.turnOn();
Matt Briggs 66:bcaa6dbf538a 380 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 381 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 382 // TODO maybe use sleep instead of wait
Matt Briggs 58:15aa7a785b9f 383 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 64:46c8819c07cc 384 ledPatterns.turnOff();
Matt Briggs 58:15aa7a785b9f 385 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 386 }
Matt Briggs 69:eb391644b346 387 if (pairBtnIntFlag) {// An Interrupt happened during reception
Matt Briggs 69:eb391644b346 388 continue; // Go straight to pair handling
Matt Briggs 69:eb391644b346 389 }
Matt Briggs 69:eb391644b346 390 //// TEMP LOGGING
Matt Briggs 69:eb391644b346 391 #if RX_SEQ_LOG
Matt Briggs 69:eb391644b346 392 if ((loopCnt % 1000 == 0) &&
Matt Briggs 69:eb391644b346 393 rxMsgCnt > rxSeqLog.rxMsgCount()) {
Matt Briggs 69:eb391644b346 394 rxSeqLog.setLoopCount(loopCnt);
Matt Briggs 69:eb391644b346 395 rxSeqLog.setRxMsgCount(rxMsgCnt);
Matt Briggs 69:eb391644b346 396 rxSeqLog.setMaxSeenMsgSeqNum(maxSeenMsgSeqNum);
Matt Briggs 69:eb391644b346 397 rxSeqLog.save();
Matt Briggs 69:eb391644b346 398 myLogInfo("EEPROM Saved.");
Matt Briggs 69:eb391644b346 399 }
Matt Briggs 69:eb391644b346 400 myLogInfo("NVM Log: Loop Cnt: %d, RxMsgCnt %d, MaxSeenSeqNum %d",
Matt Briggs 69:eb391644b346 401 rxSeqLog.loopCount(), rxSeqLog.rxMsgCount(), rxSeqLog.maxSeenMsgSeqNum());
Matt Briggs 69:eb391644b346 402 #endif
Matt Briggs 69:eb391644b346 403
Matt Briggs 63:e1efbe3402d9 404 myLogInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 58:15aa7a785b9f 405 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 406 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 407 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 408 }
Matt Briggs 31:9c535a708ae9 409
Matt Briggs 64:46c8819c07cc 410 protocol->resetCounters();
Matt Briggs 63:e1efbe3402d9 411 myLogInfo("\r\n================================");
Matt Briggs 58:15aa7a785b9f 412 loopCnt++;
Mike Fiore 11:d2e31743433a 413 }
Matt Briggs 47:a68747642a7a 414
Matt Briggs 55:79ab0bbc5008 415 delete protocol;
Matt Briggs 55:79ab0bbc5008 416 delete bbio;
Mike Fiore 11:d2e31743433a 417 return 0;
Mike Fiore 11:d2e31743433a 418 }
Matt Briggs 47:a68747642a7a 419 #endif
Mike Fiore 11:d2e31743433a 420
Matt Briggs 27:6b68ff715ae1 421