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:
Fri Feb 17 08:06:37 2017 -0700
Revision:
49:18f1354f9e51
Parent:
48:bab9f747d9ed
Child:
50:e89647e77fd5
Debugged both test and bbio lib.  Most IO working at this point.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matt Briggs 48:bab9f747d9ed 1 #include "mbed.h"
Matt Briggs 48:bab9f747d9ed 2 #include <string>
Matt Briggs 48:bab9f747d9ed 3 #include "..\..\config.h"
Matt Briggs 48:bab9f747d9ed 4 #include "BaseboardIO.h"
Matt Briggs 49:18f1354f9e51 5 #include "MTSLog.h"
Matt Briggs 49:18f1354f9e51 6 #include "critical.h"
Matt Briggs 48:bab9f747d9ed 7
Matt Briggs 48:bab9f747d9ed 8 #ifdef __TEST_BBIO__
Matt Briggs 48:bab9f747d9ed 9 Serial pc(USBTX, USBRX); // Externally defined
Matt Briggs 48:bab9f747d9ed 10
Matt Briggs 49:18f1354f9e51 11 const int VERSION = 1;
Matt Briggs 49:18f1354f9e51 12
Matt Briggs 48:bab9f747d9ed 13 char* bool2Str(bool in) {
Matt Briggs 48:bab9f747d9ed 14 if (in) {
Matt Briggs 48:bab9f747d9ed 15 return "Asserted\ "; // Extra space for alignment
Matt Briggs 48:bab9f747d9ed 16 }
Matt Briggs 48:bab9f747d9ed 17 else {
Matt Briggs 48:bab9f747d9ed 18 return "Not Asserted";
Matt Briggs 48:bab9f747d9ed 19 }
Matt Briggs 48:bab9f747d9ed 20 }
Matt Briggs 48:bab9f747d9ed 21
Matt Briggs 49:18f1354f9e51 22 volatile uint8_t ccInIntCnt;
Matt Briggs 49:18f1354f9e51 23 volatile uint8_t tamperIntCnt;
Matt Briggs 49:18f1354f9e51 24 volatile uint8_t pairBtnIntCnt;
Matt Briggs 49:18f1354f9e51 25 volatile uint8_t pvdIntCnt;
Matt Briggs 48:bab9f747d9ed 26
Matt Briggs 48:bab9f747d9ed 27 void ccInIntCallback () {
Matt Briggs 49:18f1354f9e51 28 // core_util_atomic_incr_u8(&ccInIntCnt, 1); // Not needed since function is wrapped in critical section code.
Matt Briggs 48:bab9f747d9ed 29 ccInIntCnt++;
Matt Briggs 48:bab9f747d9ed 30 }
Matt Briggs 48:bab9f747d9ed 31 void tamperIntCallback () {
Matt Briggs 48:bab9f747d9ed 32 tamperIntCnt++;
Matt Briggs 48:bab9f747d9ed 33 }
Matt Briggs 48:bab9f747d9ed 34 void pairBtnIntCallback () {
Matt Briggs 48:bab9f747d9ed 35 pairBtnIntCnt++;
Matt Briggs 48:bab9f747d9ed 36 }
Matt Briggs 49:18f1354f9e51 37 void HAL_PWR_PVDCallback () { // Override callback in lib
Matt Briggs 49:18f1354f9e51 38 pvdIntCnt++;
Matt Briggs 49:18f1354f9e51 39 }
Matt Briggs 48:bab9f747d9ed 40
Matt Briggs 48:bab9f747d9ed 41 class MenuManager
Matt Briggs 48:bab9f747d9ed 42 {
Matt Briggs 48:bab9f747d9ed 43 private:
Matt Briggs 48:bab9f747d9ed 44 uint8_t mCurrSel; // Current selection
Matt Briggs 48:bab9f747d9ed 45 BaseboardIO *mBbio;
Matt Briggs 48:bab9f747d9ed 46 bool validInput(uint8_t in) {
Matt Briggs 48:bab9f747d9ed 47 if (in > 0 && in <= 8)
Matt Briggs 48:bab9f747d9ed 48 return true;
Matt Briggs 48:bab9f747d9ed 49 else {
Matt Briggs 48:bab9f747d9ed 50 return false;
Matt Briggs 48:bab9f747d9ed 51 }
Matt Briggs 48:bab9f747d9ed 52 }
Matt Briggs 48:bab9f747d9ed 53 public:
Matt Briggs 48:bab9f747d9ed 54 MenuManager() {
Matt Briggs 48:bab9f747d9ed 55 mCurrSel = 0;
Matt Briggs 48:bab9f747d9ed 56 mBbio = NULL;
Matt Briggs 48:bab9f747d9ed 57 }
Matt Briggs 48:bab9f747d9ed 58 void regBBIO (BaseboardIO *bbio) {
Matt Briggs 48:bab9f747d9ed 59 mBbio = bbio;
Matt Briggs 48:bab9f747d9ed 60 }
Matt Briggs 48:bab9f747d9ed 61 uint8_t getCurrentSel() {
Matt Briggs 48:bab9f747d9ed 62 return mCurrSel;
Matt Briggs 48:bab9f747d9ed 63 }
Matt Briggs 48:bab9f747d9ed 64 void applyInput(uint8_t in) {
Matt Briggs 48:bab9f747d9ed 65 if (validInput(in)) {
Matt Briggs 48:bab9f747d9ed 66 // mCurrSel = in;
Matt Briggs 48:bab9f747d9ed 67 if (in == 1) {
Matt Briggs 48:bab9f747d9ed 68 mBbio->ledOn();
Matt Briggs 48:bab9f747d9ed 69 }
Matt Briggs 48:bab9f747d9ed 70 else if (in == 2) {
Matt Briggs 48:bab9f747d9ed 71 mBbio->ledOff();
Matt Briggs 48:bab9f747d9ed 72 }
Matt Briggs 48:bab9f747d9ed 73 else if (in == 3) {
Matt Briggs 48:bab9f747d9ed 74 mBbio->relayNormal();
Matt Briggs 48:bab9f747d9ed 75 }
Matt Briggs 48:bab9f747d9ed 76 else if (in == 4) {
Matt Briggs 48:bab9f747d9ed 77 mBbio->relayAlert();
Matt Briggs 48:bab9f747d9ed 78 }
Matt Briggs 48:bab9f747d9ed 79 else if (in == 5) {
Matt Briggs 48:bab9f747d9ed 80 mBbio->serialRx(true);
Matt Briggs 48:bab9f747d9ed 81 }
Matt Briggs 48:bab9f747d9ed 82 else if (in == 6) {
Matt Briggs 48:bab9f747d9ed 83 mBbio->serialRx(false);
Matt Briggs 48:bab9f747d9ed 84 }
Matt Briggs 48:bab9f747d9ed 85 else if (in == 7) {
Matt Briggs 48:bab9f747d9ed 86 mBbio->serialTx(true);
Matt Briggs 48:bab9f747d9ed 87 }
Matt Briggs 48:bab9f747d9ed 88 else if (in == 8) {
Matt Briggs 48:bab9f747d9ed 89 mBbio->serialTx(false);
Matt Briggs 48:bab9f747d9ed 90 }
Matt Briggs 48:bab9f747d9ed 91 }
Matt Briggs 48:bab9f747d9ed 92 }
Matt Briggs 48:bab9f747d9ed 93 void printMenu() {
Matt Briggs 48:bab9f747d9ed 94 mBbio->sampleUserSwitches();
Matt Briggs 48:bab9f747d9ed 95 pc.printf("\r\n\r\n");
Matt Briggs 48:bab9f747d9ed 96 pc.printf("===============================================\r\n");
Matt Briggs 49:18f1354f9e51 97 pc.printf("= Baseboard I/O Tester v%02d =\r\n", VERSION);
Matt Briggs 48:bab9f747d9ed 98 pc.printf("===============================================\r\n");
Matt Briggs 48:bab9f747d9ed 99 pc.printf("===============================================\r\n");
Matt Briggs 48:bab9f747d9ed 100 pc.printf("= Selection Options =\r\n");
Matt Briggs 48:bab9f747d9ed 101 pc.printf("===============================================\r\n");
Matt Briggs 48:bab9f747d9ed 102 pc.printf("= 0: Just refresh =\r\n");
Matt Briggs 48:bab9f747d9ed 103 pc.printf("= 1: Turn on LED =\r\n");
Matt Briggs 48:bab9f747d9ed 104 pc.printf("= 2: Turn off LED =\r\n");
Matt Briggs 48:bab9f747d9ed 105 pc.printf("= 3: Toggle Relay Normal =\r\n");
Matt Briggs 48:bab9f747d9ed 106 pc.printf("= 4: Toggle Relay Alert =\r\n");
Matt Briggs 48:bab9f747d9ed 107 pc.printf("= 5: Turn on 232 RX =\r\n");
Matt Briggs 48:bab9f747d9ed 108 pc.printf("= 6: Turn off 232 RX =\r\n");
Matt Briggs 48:bab9f747d9ed 109 pc.printf("= 7: Turn on 232 TX (Note RX on as well) =\r\n");
Matt Briggs 48:bab9f747d9ed 110 pc.printf("= 8: Turn off 232 TX =\r\n");
Matt Briggs 48:bab9f747d9ed 111 pc.printf("===============================================\r\n");
Matt Briggs 48:bab9f747d9ed 112 pc.printf("= Status and Counters =\r\n");
Matt Briggs 48:bab9f747d9ed 113 pc.printf("===============================================\r\n");
Matt Briggs 48:bab9f747d9ed 114 pc.printf("= Pair btn. State: %s IntCnt: %02d =\r\n",
Matt Briggs 49:18f1354f9e51 115 bool2Str(mBbio->isPairBtn()), pairBtnIntCnt);
Matt Briggs 49:18f1354f9e51 116 pc.printf("= Tamper. State: N/A IntCnt: %04d =\r\n",
Matt Briggs 49:18f1354f9e51 117 tamperIntCnt);
Matt Briggs 48:bab9f747d9ed 118 pc.printf("= CCIN. State: %s IntCnt: %02d =\r\n",
Matt Briggs 49:18f1354f9e51 119 bool2Str(0), ccInIntCnt);
Matt Briggs 49:18f1354f9e51 120 pc.printf("= PVD. State: N/A IntCnt: %02d =\r\n",
Matt Briggs 49:18f1354f9e51 121 pvdIntCnt);
Matt Briggs 48:bab9f747d9ed 122 pc.printf("= Is TX. State: %s =\r\n", bool2Str(mBbio->isTx()));
Matt Briggs 49:18f1354f9e51 123 pc.printf("= CC Normally Closed. State: %s =\r\n", bool2Str(mBbio->isCCNC()));
Matt Briggs 48:bab9f747d9ed 124 pc.printf("= Is LoraWAN. State: %s =\r\n", bool2Str(mBbio->isLoRaWANMode()));
Matt Briggs 48:bab9f747d9ed 125 pc.printf("= Rotary Switch 1. Value: %02d =\r\n", mBbio->rotarySwitch1());
Matt Briggs 48:bab9f747d9ed 126 pc.printf("= Rotary Switch 2. Value: %02d =\r\n", mBbio->rotarySwitch2());
Matt Briggs 48:bab9f747d9ed 127 pc.printf("===============================================\r\n");
Matt Briggs 48:bab9f747d9ed 128 }
Matt Briggs 48:bab9f747d9ed 129 };
Matt Briggs 48:bab9f747d9ed 130
Matt Briggs 49:18f1354f9e51 131 char WAIT_CHARS [] = {'-', '\\', '|', '/'};
Matt Briggs 48:bab9f747d9ed 132 /**
Matt Briggs 48:bab9f747d9ed 133 * Checks that in idle state all the IOs are pulled up.
Matt Briggs 48:bab9f747d9ed 134 */
Matt Briggs 48:bab9f747d9ed 135 int main ()
Matt Briggs 48:bab9f747d9ed 136 {
Matt Briggs 48:bab9f747d9ed 137 MenuManager menuMgr;
Matt Briggs 48:bab9f747d9ed 138 CmdResult result;
Matt Briggs 48:bab9f747d9ed 139 ccInIntCnt = 0;
Matt Briggs 48:bab9f747d9ed 140 tamperIntCnt = 0;
Matt Briggs 48:bab9f747d9ed 141 pairBtnIntCnt = 0;
Matt Briggs 49:18f1354f9e51 142 pvdIntCnt = 0;
Matt Briggs 48:bab9f747d9ed 143
Matt Briggs 48:bab9f747d9ed 144 pc.baud(115200);
Matt Briggs 49:18f1354f9e51 145 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 49:18f1354f9e51 146
Matt Briggs 49:18f1354f9e51 147 // Setup programmable voltage detector
Matt Briggs 49:18f1354f9e51 148 // PVD_LEVEL0 Falling 1.85
Matt Briggs 49:18f1354f9e51 149 // PVD_LEVEL1 Falling 2.04
Matt Briggs 49:18f1354f9e51 150 // PVD_LEVEL2 Falling 2.24
Matt Briggs 49:18f1354f9e51 151 // PVD_LEVEL3 Falling 2.44
Matt Briggs 49:18f1354f9e51 152 // PVD_LEVEL4 Falling 2.64
Matt Briggs 49:18f1354f9e51 153 // PVD_LEVEL5 Falling 2.84
Matt Briggs 49:18f1354f9e51 154 // PVD_LEVEL6 Falling 3.05
Matt Briggs 49:18f1354f9e51 155 // PWR_PVDTypeDef pvdConfig;
Matt Briggs 49:18f1354f9e51 156 //// pvdConfig.Mode = PWR_PVD_MODE_EVENT_RISING_FALLING; //PWR_PVD_MODE_NORMAL;
Matt Briggs 49:18f1354f9e51 157 // pvdConfig.Mode = PWR_PVD_MODE_IT_RISING_FALLING | PWR_PVD_MODE_EVENT_RISING_FALLING;
Matt Briggs 49:18f1354f9e51 158 // pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
Matt Briggs 49:18f1354f9e51 159 //
Matt Briggs 49:18f1354f9e51 160 // HAL_PWR_ConfigPVD(&pvdConfig);
Matt Briggs 49:18f1354f9e51 161 // HAL_PWR_EnablePVD();
Matt Briggs 49:18f1354f9e51 162 // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
Matt Briggs 49:18f1354f9e51 163 //
Matt Briggs 49:18f1354f9e51 164 //// HAL_PWR_PVDCallback(); // Does inc the value
Matt Briggs 49:18f1354f9e51 165 //
Matt Briggs 49:18f1354f9e51 166 //// __HAL_PWR_PVD_EXTI_CLEAR_FLAG();
Matt Briggs 49:18f1354f9e51 167 // HAL_PWR_PVD_IRQHandler(); // This calls the callback and clears the IRQ
Matt Briggs 49:18f1354f9e51 168 // __HAL_PWR_PVD_EXTI_GENERATE_SWIT();
Matt Briggs 49:18f1354f9e51 169
Matt Briggs 49:18f1354f9e51 170 // __HAL_PPP_EXTI_ENABLE_IT();
Matt Briggs 49:18f1354f9e51 171
Matt Briggs 49:18f1354f9e51 172 // __HAL_PWR_PVD_EXTI_ENABLE_EVENT()
Matt Briggs 49:18f1354f9e51 173
Matt Briggs 48:bab9f747d9ed 174
Matt Briggs 48:bab9f747d9ed 175 wait(1.0);
Matt Briggs 48:bab9f747d9ed 176
Matt Briggs 48:bab9f747d9ed 177 pc.printf("===============================================\r\n");
Matt Briggs 48:bab9f747d9ed 178 pc.printf("= Baseboard Constructor Starting =\r\n");
Matt Briggs 48:bab9f747d9ed 179 BaseboardIO bbio;
Matt Briggs 48:bab9f747d9ed 180 pc.printf("= Baseboard Constructor Finished =\r\n");
Matt Briggs 48:bab9f747d9ed 181
Matt Briggs 48:bab9f747d9ed 182 pc.printf("= Baseboard Init Starting =\r\n");
Matt Briggs 48:bab9f747d9ed 183 result = bbio.init();
Matt Briggs 48:bab9f747d9ed 184 if (result == cmdSuccess) {
Matt Briggs 48:bab9f747d9ed 185 pc.printf("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 48:bab9f747d9ed 186 }
Matt Briggs 48:bab9f747d9ed 187 else {
Matt Briggs 48:bab9f747d9ed 188 pc.printf("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 48:bab9f747d9ed 189 }
Matt Briggs 48:bab9f747d9ed 190
Matt Briggs 49:18f1354f9e51 191 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 49:18f1354f9e51 192 Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 49:18f1354f9e51 193 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 49:18f1354f9e51 194 bbio.regCCInInt(ccInIntObj);
Matt Briggs 49:18f1354f9e51 195 bbio.regTamperInt(tamperIntObj);
Matt Briggs 49:18f1354f9e51 196 bbio.regPairBtnInt(pairBtnIntObj);
Matt Briggs 48:bab9f747d9ed 197
Matt Briggs 48:bab9f747d9ed 198 menuMgr.regBBIO(&bbio);
Matt Briggs 48:bab9f747d9ed 199 menuMgr.printMenu();
Matt Briggs 48:bab9f747d9ed 200
Matt Briggs 49:18f1354f9e51 201 uint8_t waitCharIdx = 0;
Matt Briggs 48:bab9f747d9ed 202 while (true) {
Matt Briggs 48:bab9f747d9ed 203
Matt Briggs 48:bab9f747d9ed 204 if (pc.readable()) {
Matt Briggs 48:bab9f747d9ed 205 char menuInput = pc.getc();
Matt Briggs 48:bab9f747d9ed 206 menuInput -= '0'; // Convert to raw interger value
Matt Briggs 48:bab9f747d9ed 207 menuMgr.applyInput(menuInput);
Matt Briggs 48:bab9f747d9ed 208 menuMgr.printMenu();
Matt Briggs 48:bab9f747d9ed 209 }
Matt Briggs 48:bab9f747d9ed 210 else {
Matt Briggs 49:18f1354f9e51 211 pc.printf("\b%c", WAIT_CHARS[waitCharIdx]);
Matt Briggs 49:18f1354f9e51 212 if (waitCharIdx >= sizeof(WAIT_CHARS)) {
Matt Briggs 49:18f1354f9e51 213 waitCharIdx = 0;
Matt Briggs 49:18f1354f9e51 214 }
Matt Briggs 49:18f1354f9e51 215 else {
Matt Briggs 49:18f1354f9e51 216 waitCharIdx++;
Matt Briggs 49:18f1354f9e51 217 }
Matt Briggs 48:bab9f747d9ed 218 }
Matt Briggs 49:18f1354f9e51 219 wait(0.1);
Matt Briggs 48:bab9f747d9ed 220 }
Matt Briggs 48:bab9f747d9ed 221 return 0;
Matt Briggs 48:bab9f747d9ed 222 }
Matt Briggs 48:bab9f747d9ed 223 #endif