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 Feb 27 08:06:36 2017 -0700
Revision:
55:79ab0bbc5008
Parent:
54:c04d7b6fa075
Child:
57:bdac7dd17af2
Adding lots of NVM code to store parameters in eeprom.  Also some work on documentation.

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 41:9ef4c4d77711 7 //#include <xdot_low_power.h>
mfiore 17:d4f82e16de5f 8
Matt Briggs 47:a68747642a7a 9 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 10 #include "CommProtocolPeerBrute.h"
Matt Briggs 31:9c535a708ae9 11
Matt Briggs 47:a68747642a7a 12 #ifndef __TEST__ // Exclude code for tests
Mike Fiore 11:d2e31743433a 13 Serial pc(USBTX, USBRX);
Mike Fiore 11:d2e31743433a 14
Matt Briggs 47:a68747642a7a 15 mDot* dot = NULL; // Used by dot-utils
Matt Briggs 39:64f79fa6e3cc 16
Matt Briggs 52:64a2c71c7c49 17 //DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
Matt Briggs 48:bab9f747d9ed 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
Matt Briggs 50:e89647e77fd5 32
Mike Fiore 11:d2e31743433a 33 int main() {
Matt Briggs 53:a1563574a980 34 CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
Matt Briggs 53:a1563574a980 35 BaseboardIO *bbio = new BaseboardIO();
Matt Briggs 52:64a2c71c7c49 36 CmdResult result;
Matt Briggs 50:e89647e77fd5 37 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 38 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 39 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 40 pc.baud(115200);
Matt Briggs 48:bab9f747d9ed 41
Matt Briggs 31:9c535a708ae9 42 RadioEvent events; // Custom event handler for automatically displaying RX data
Mike Fiore 11:d2e31743433a 43
Matt Briggs 27:6b68ff715ae1 44 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 47:a68747642a7a 45
Mike Fiore 11:d2e31743433a 46 dot = mDot::getInstance();
Mike Fiore 11:d2e31743433a 47
Mike Fiore 16:a3832552dfe1 48 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 55:79ab0bbc5008 49 logInfo(" libxDot-mbed5 library ID: %s", dot->getId().c_str());
Mike Fiore 16:a3832552dfe1 50
Mike Fiore 12:ec9768677cea 51 // start from a well-known state
Matt Briggs 55:79ab0bbc5008 52 // logInfo("defaulting Dot configuration");
Matt Briggs 55:79ab0bbc5008 53 // dot->resetConfig();
Mike Fiore 12:ec9768677cea 54
Mike Fiore 11:d2e31743433a 55 // make sure library logging is turned on
Matt Briggs 27:6b68ff715ae1 56 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
Mike Fiore 11:d2e31743433a 57
Mike Fiore 11:d2e31743433a 58 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 59 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 60
Matt Briggs 53:a1563574a980 61 // Finish radio init
Matt Briggs 53:a1563574a980 62 protocol->init();
Matt Briggs 53:a1563574a980 63
Matt Briggs 53:a1563574a980 64 // save changes to configuration
Matt Briggs 53:a1563574a980 65 // logInfo("saving configuration");
Matt Briggs 53:a1563574a980 66 // core_util_critical_section_enter();
Matt Briggs 53:a1563574a980 67 // bool dotConfigSaved = dot->saveConfig();
Matt Briggs 53:a1563574a980 68 // core_util_critical_section_exit();
Matt Briggs 53:a1563574a980 69 // if (!dotConfigSaved) {
Matt Briggs 53:a1563574a980 70 // logError("failed to save configuration");
Matt Briggs 53:a1563574a980 71 // }
Mike Fiore 11:d2e31743433a 72
Matt Briggs 31:9c535a708ae9 73 // Setup programmable voltage detector
Matt Briggs 31:9c535a708ae9 74 // PVD_LEVEL0 Falling 1.85
Matt Briggs 31:9c535a708ae9 75 // PVD_LEVEL1 Falling 2.04
Matt Briggs 31:9c535a708ae9 76 // PVD_LEVEL2 Falling 2.24
Matt Briggs 31:9c535a708ae9 77 // PVD_LEVEL3 Falling 2.44
Matt Briggs 31:9c535a708ae9 78 // PVD_LEVEL4 Falling 2.64
Matt Briggs 31:9c535a708ae9 79 // PVD_LEVEL5 Falling 2.84
Matt Briggs 31:9c535a708ae9 80 // PVD_LEVEL6 Falling 3.05
Matt Briggs 50:e89647e77fd5 81 // PWR_PVDTypeDef pvdConfig;
Matt Briggs 50:e89647e77fd5 82 // pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
Matt Briggs 50:e89647e77fd5 83 // pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
Matt Briggs 50:e89647e77fd5 84 //
Matt Briggs 50:e89647e77fd5 85 // HAL_PWR_ConfigPVD(&pvdConfig);
Matt Briggs 50:e89647e77fd5 86 // HAL_PWR_EnablePVD();
Matt Briggs 50:e89647e77fd5 87 // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
Matt Briggs 50:e89647e77fd5 88 // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
Mike Fiore 11:d2e31743433a 89
Matt Briggs 54:c04d7b6fa075 90 dot->setWakePin(UART1_RX);
Matt Briggs 24:fdf87e4b72e5 91
Mike Fiore 11:d2e31743433a 92
Mike Fiore 11:d2e31743433a 93 // display configuration
Matt Briggs 53:a1563574a980 94 // display_config();
Matt Briggs 52:64a2c71c7c49 95
Matt Briggs 53:a1563574a980 96 pc.printf("= Baseboard Init Starting =\r\n");
Matt Briggs 53:a1563574a980 97 result = bbio->init();
Matt Briggs 52:64a2c71c7c49 98 if (result == cmdSuccess) {
Matt Briggs 52:64a2c71c7c49 99 pc.printf("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 52:64a2c71c7c49 100 }
Matt Briggs 52:64a2c71c7c49 101 else {
Matt Briggs 52:64a2c71c7c49 102 pc.printf("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 52:64a2c71c7c49 103 }
Mike Fiore 11:d2e31743433a 104
Matt Briggs 41:9ef4c4d77711 105 uint16_t seqNum=0;
Matt Briggs 50:e89647e77fd5 106 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 50:e89647e77fd5 107 Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 50:e89647e77fd5 108 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 50:e89647e77fd5 109
Matt Briggs 53:a1563574a980 110 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 111 bbio->relayNormal(); // Always force relay in known state
Matt Briggs 26:9411b26a5084 112
Matt Briggs 53:a1563574a980 113 unsigned int intCnt = 0; // Just a quick temp varaible to keep track of ints
Matt Briggs 47:a68747642a7a 114 /**
Matt Briggs 47:a68747642a7a 115 * Main Loop
Matt Briggs 47:a68747642a7a 116 */
Mike Fiore 11:d2e31743433a 117 while (true) {
Matt Briggs 25:fea776239709 118 std::vector<uint8_t> data;
Matt Briggs 53:a1563574a980 119 bbio->ledOff();
Matt Briggs 47:a68747642a7a 120
Matt Briggs 50:e89647e77fd5 121 // Sample IO and update any configuration
Matt Briggs 53:a1563574a980 122 bool prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 53:a1563574a980 123 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 124 if (prevCCNormallyOpen == bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 53:a1563574a980 125 bbio->regCCInInt(ccInIntObj);
Matt Briggs 53:a1563574a980 126 bbio->regTamperInt(tamperIntObj);
Matt Briggs 53:a1563574a980 127 bbio->regPairBtnInt(pairBtnIntObj);
Matt Briggs 53:a1563574a980 128 bbio->relayNormal();
Matt Briggs 50:e89647e77fd5 129 }
Matt Briggs 53:a1563574a980 130 if (bbio->isTx()) {
Matt Briggs 53:a1563574a980 131 protocol->setTx(true);
Matt Briggs 50:e89647e77fd5 132 }
Matt Briggs 50:e89647e77fd5 133 else { // RX
Matt Briggs 53:a1563574a980 134 protocol->setTx(false);
Matt Briggs 50:e89647e77fd5 135 }
Matt Briggs 50:e89647e77fd5 136 // End sample and update
Matt Briggs 47:a68747642a7a 137
Matt Briggs 53:a1563574a980 138 if (protocol->isTx()) {
Matt Briggs 53:a1563574a980 139 logInfo("Got int #%d. CCFlag %d, TamperFlag %d, PairBtnFlag %d",
Matt Briggs 53:a1563574a980 140 intCnt, ccIntFlag, tamperIntFlag, pairBtnIntFlag);
Matt Briggs 53:a1563574a980 141 // TODO add tamper
Matt Briggs 53:a1563574a980 142 if (pairBtnIntFlag) { // Wait up to 1 second for short button hit
Matt Briggs 53:a1563574a980 143 for (int i=0; i<10;i++) {
Matt Briggs 53:a1563574a980 144 if (bbio->isPairBtn() == false){ // Button released
Matt Briggs 53:a1563574a980 145 break;
Matt Briggs 53:a1563574a980 146 }
Matt Briggs 53:a1563574a980 147 wait(0.1);
Matt Briggs 53:a1563574a980 148 }
Matt Briggs 53:a1563574a980 149 }
Matt Briggs 50:e89647e77fd5 150 if (ccIntFlag || // If contact closure in
Matt Briggs 53:a1563574a980 151 (pairBtnIntFlag && (bbio->isPairBtn() == false))) { // If short pair btn hit
Matt Briggs 47:a68747642a7a 152
Matt Briggs 50:e89647e77fd5 153 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 154 tamperIntFlag = false;
Matt Briggs 53:a1563574a980 155 pairBtnIntFlag = false;
Matt Briggs 50:e89647e77fd5 156 #if LED_FEEDBACK
Matt Briggs 53:a1563574a980 157 bbio->ledOn();
Matt Briggs 50:e89647e77fd5 158 #endif
Matt Briggs 50:e89647e77fd5 159
Matt Briggs 50:e89647e77fd5 160 data.push_back((seqNum >> 8) & 0xFF);
Matt Briggs 50:e89647e77fd5 161 data.push_back(seqNum & 0xFF);
Matt Briggs 53:a1563574a980 162 protocol->send(data);
Matt Briggs 50:e89647e77fd5 163 seqNum++;
Matt Briggs 50:e89647e77fd5 164 wait(0.5); // Leave the LED on so a person can see it.
Matt Briggs 50:e89647e77fd5 165 }
Matt Briggs 50:e89647e77fd5 166
Matt Briggs 53:a1563574a980 167 bbio->ledOff();
Matt Briggs 52:64a2c71c7c49 168 // sleep_save_io();
Matt Briggs 52:64a2c71c7c49 169 // sleep_configure_io();
Matt Briggs 47:a68747642a7a 170 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 53:a1563574a980 171 // sleep_restore_io();
Matt Briggs 47:a68747642a7a 172 }
Mike Fiore 11:d2e31743433a 173
Matt Briggs 53:a1563574a980 174 if (protocol->isRx()) {
Matt Briggs 47:a68747642a7a 175 bool msgPending;
Matt Briggs 53:a1563574a980 176 protocol->listen(msgPending);
Matt Briggs 53:a1563574a980 177 logInfo("Listening.");
Matt Briggs 47:a68747642a7a 178 if (msgPending) {
Matt Briggs 53:a1563574a980 179 protocol->recv(data);
Matt Briggs 47:a68747642a7a 180 std::string dataStr(data.begin(), data.end());
Matt Briggs 47:a68747642a7a 181 logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str());
Matt Briggs 53:a1563574a980 182 bbio->relayAlert();
Matt Briggs 47:a68747642a7a 183 seqNum++;
Matt Briggs 50:e89647e77fd5 184 #if LED_FEEDBACK
Matt Briggs 53:a1563574a980 185 bbio->ledOn();
Matt Briggs 50:e89647e77fd5 186 #endif
Matt Briggs 54:c04d7b6fa075 187 wait(5.0); // TODO this should be configurable
Matt Briggs 47:a68747642a7a 188 }
Matt Briggs 53:a1563574a980 189 bbio->ledOff();
Matt Briggs 53:a1563574a980 190 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 191 logInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 52:64a2c71c7c49 192 // sleep_save_io();
Matt Briggs 52:64a2c71c7c49 193 // sleep_configure_io();
Matt Briggs 47:a68747642a7a 194 // TODO maybe add if statement here to prevent double hits by sleeping for a longer time
Matt Briggs 54:c04d7b6fa075 195 protocol->sampleDLC();
Matt Briggs 47:a68747642a7a 196 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 52:64a2c71c7c49 197 // sleep_restore_io();
Matt Briggs 47:a68747642a7a 198 }
Matt Briggs 31:9c535a708ae9 199
Matt Briggs 50:e89647e77fd5 200 // TODO maybe a good place to put pairing logic
Matt Briggs 29:e05e35976cfe 201
Matt Briggs 44:ece6330e9b57 202 logInfo("================================");
Matt Briggs 44:ece6330e9b57 203 wait(1.0);
Matt Briggs 47:a68747642a7a 204 // Need to re-implement some of these sleep functions
Matt Briggs 41:9ef4c4d77711 205 // sleep_save_io();
Matt Briggs 41:9ef4c4d77711 206 // sleep_configure_io();
Matt Briggs 53:a1563574a980 207 intCnt++;
Mike Fiore 11:d2e31743433a 208 }
Matt Briggs 47:a68747642a7a 209
Matt Briggs 55:79ab0bbc5008 210 delete protocol;
Matt Briggs 55:79ab0bbc5008 211 delete bbio;
Mike Fiore 11:d2e31743433a 212 return 0;
Mike Fiore 11:d2e31743433a 213 }
Matt Briggs 47:a68747642a7a 214 #endif
Mike Fiore 11:d2e31743433a 215
Matt Briggs 27:6b68ff715ae1 216