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:
mbriggs_vortex
Date:
Thu Nov 16 20:59:24 2017 +0000
Revision:
81:1eb0f16b2ab9
Parent:
78:43f074baac34
Child:
89:edfe5d376e55
Added json and bootloader dir

Who changed what in which revision?

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