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