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 Feb 01 15:20:45 2017 -0700
Revision:
44:ece6330e9b57
Parent:
41:9ef4c4d77711
Child:
47:a68747642a7a
First cut at BaseboardIO class implementation.
Minior cleanup in main and CommProtocolPeerBrute

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matt Briggs 40:2ec4be320961 1 /*
Matt Briggs 40:2ec4be320961 2 * baseboardIO.cpp
Matt Briggs 40:2ec4be320961 3 *
Matt Briggs 40:2ec4be320961 4 * Created on: Jan 25, 2017
Matt Briggs 40:2ec4be320961 5 * Author: mbriggs
Matt Briggs 40:2ec4be320961 6 */
Matt Briggs 40:2ec4be320961 7
Matt Briggs 40:2ec4be320961 8 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 9 #include "MTSLog.h"
Matt Briggs 40:2ec4be320961 10
Matt Briggs 44:ece6330e9b57 11 const float coilOnTime = 0.030; // 30 ms
Matt Briggs 44:ece6330e9b57 12
Matt Briggs 44:ece6330e9b57 13 // Port expander 0
Matt Briggs 44:ece6330e9b57 14 const uint8_t pEx0232En = 0x01;
Matt Briggs 44:ece6330e9b57 15 const uint8_t pEx0232TxDis = 0x02;
Matt Briggs 44:ece6330e9b57 16 const uint8_t pEx0Rot1B1 = 0x04;
Matt Briggs 44:ece6330e9b57 17 const uint8_t pEx0Rot1B2 = 0x08;
Matt Briggs 44:ece6330e9b57 18 const uint8_t pEx0Rot1B4 = 0x10;
Matt Briggs 44:ece6330e9b57 19 const uint8_t pEx0Rot1B8 = 0x20;
Matt Briggs 44:ece6330e9b57 20 const uint8_t pEx0Rot2B1 = 0x40;
Matt Briggs 44:ece6330e9b57 21 const uint8_t pEx0Rot2B2 = 0x80;
Matt Briggs 44:ece6330e9b57 22
Matt Briggs 44:ece6330e9b57 23 // Port expander 1
Matt Briggs 44:ece6330e9b57 24 const uint8_t pEx1NoNcSel = 0x01;
Matt Briggs 44:ece6330e9b57 25 const uint8_t pEx1RxTxSel = 0x02;
Matt Briggs 44:ece6330e9b57 26 const uint8_t pEx1WanSel = 0x04;
Matt Briggs 44:ece6330e9b57 27 const uint8_t pEx1SerialEn = 0x08; // Labeled as reserved
Matt Briggs 44:ece6330e9b57 28 const uint8_t pEx1Rot2B8 = 0x10;
Matt Briggs 44:ece6330e9b57 29 const uint8_t pEx1Rot2B4 = 0x20;
Matt Briggs 44:ece6330e9b57 30 const uint8_t pEx1RlyB = 0x40; // This is actually a coil
Matt Briggs 44:ece6330e9b57 31 const uint8_t pEx1RlyA = 0x80; // This is actually a coil
Matt Briggs 44:ece6330e9b57 32
Matt Briggs 44:ece6330e9b57 33 /**
Matt Briggs 44:ece6330e9b57 34 * Note for interrupt within uC cannot use two pins with the same numeric suffix (e.g. cannot
Matt Briggs 44:ece6330e9b57 35 * use both PA_0 and PB_0). Note 1, 6, 7, 8, and 13 are used by LoRa radio.
Matt Briggs 44:ece6330e9b57 36 */
Matt Briggs 44:ece6330e9b57 37
Matt Briggs 40:2ec4be320961 38 BaseboardIO::BaseboardIO()
Matt Briggs 44:ece6330e9b57 39 : mOWMaster(I2C_SDA),
Matt Briggs 44:ece6330e9b57 40 mCCIn(WAKE), // Interrupt pin PA_0
Matt Briggs 44:ece6330e9b57 41 mTamper(GPIO1), // Interrupt pin PA_5
Matt Briggs 44:ece6330e9b57 42 mPairBtn(UART_CTS), // Interrupt pin PA_11
Matt Briggs 44:ece6330e9b57 43 mLed(SWDIO),
Matt Briggs 44:ece6330e9b57 44 mSwitchedIOCtrl(I2C_SCL)
Matt Briggs 40:2ec4be320961 45 {
Matt Briggs 44:ece6330e9b57 46 // mCCInIntCallback = NULL;
Matt Briggs 44:ece6330e9b57 47 // mTamperIntCallback = NULL;
Matt Briggs 44:ece6330e9b57 48 // mPairBtnIntCallback = NULL;
Matt Briggs 40:2ec4be320961 49
Matt Briggs 44:ece6330e9b57 50 std::memset(mPortExpanderROM0, 0x00, 8);
Matt Briggs 44:ece6330e9b57 51 std::memset(mPortExpanderROM1, 0x00, 8);
Matt Briggs 44:ece6330e9b57 52 mPortExpanderVal0 = 0x00;
Matt Briggs 44:ece6330e9b57 53 mPortExpanderVal1 = 0x00;
Matt Briggs 44:ece6330e9b57 54
Matt Briggs 44:ece6330e9b57 55 mPortEx0 = NULL;
Matt Briggs 44:ece6330e9b57 56 mPortEx1 = NULL;
Matt Briggs 40:2ec4be320961 57 }
Matt Briggs 40:2ec4be320961 58 CmdResult BaseboardIO::init()
Matt Briggs 40:2ec4be320961 59 {
Matt Briggs 40:2ec4be320961 60 logError("Not implemented yet!!!");
Matt Briggs 44:ece6330e9b57 61 // Setup port expanders
Matt Briggs 44:ece6330e9b57 62 CmdResult result = readInfoFromNVM();
Matt Briggs 44:ece6330e9b57 63 if (result == cmdSuccess) {
Matt Briggs 44:ece6330e9b57 64 // Values stored just read them foo
Matt Briggs 44:ece6330e9b57 65 logError("Not implemented yet!!!");
Matt Briggs 44:ece6330e9b57 66 }
Matt Briggs 44:ece6330e9b57 67 else { // EEPROM values not there or corrupt
Matt Briggs 44:ece6330e9b57 68 result = identifyPortExpanders(); // Get ROM addresses
Matt Briggs 44:ece6330e9b57 69 if (result != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 70 logError("Error identifying port expanders");
Matt Briggs 44:ece6330e9b57 71 return cmdError;
Matt Briggs 44:ece6330e9b57 72 }
Matt Briggs 44:ece6330e9b57 73 }
Matt Briggs 44:ece6330e9b57 74 mPortEx0 = new DS2408(&mOWMaster, mPortExpanderROM0);
Matt Briggs 44:ece6330e9b57 75 mPortEx1 = new DS2408(&mOWMaster, mPortExpanderROM1);
Matt Briggs 44:ece6330e9b57 76
Matt Briggs 44:ece6330e9b57 77 // Put relay in known state
Matt Briggs 44:ece6330e9b57 78
Matt Briggs 44:ece6330e9b57 79 logInfo("Baseboard IO initialization successful");
Matt Briggs 44:ece6330e9b57 80 return cmdSuccess;
Matt Briggs 40:2ec4be320961 81 }
Matt Briggs 40:2ec4be320961 82
Matt Briggs 40:2ec4be320961 83 // Registering for interrupts
Matt Briggs 44:ece6330e9b57 84 void BaseboardIO::regCCInInt(Callback<void()> func)
Matt Briggs 40:2ec4be320961 85 {
Matt Briggs 44:ece6330e9b57 86 // Pulled high, switched low
Matt Briggs 44:ece6330e9b57 87 mCCIn.fall(func);
Matt Briggs 40:2ec4be320961 88 }
Matt Briggs 44:ece6330e9b57 89 void BaseboardIO::regTamperInt(Callback<void()> func)
Matt Briggs 40:2ec4be320961 90 {
Matt Briggs 44:ece6330e9b57 91 // Pulled high, switched low
Matt Briggs 44:ece6330e9b57 92 mTamper.fall(func);
Matt Briggs 40:2ec4be320961 93 }
Matt Briggs 44:ece6330e9b57 94 void BaseboardIO::regPairBtnInt(Callback<void()> func)
Matt Briggs 40:2ec4be320961 95 {
Matt Briggs 44:ece6330e9b57 96 // Pulled low, switched high
Matt Briggs 44:ece6330e9b57 97 mPairBtn.rise(func);
Matt Briggs 40:2ec4be320961 98 }
Matt Briggs 40:2ec4be320961 99
Matt Briggs 40:2ec4be320961 100 // Input
Matt Briggs 40:2ec4be320961 101 CmdResult BaseboardIO::sampleUserSwitches()
Matt Briggs 40:2ec4be320961 102 {
Matt Briggs 44:ece6330e9b57 103 // Sample port expanders
Matt Briggs 44:ece6330e9b57 104 if (mPortEx0->pioLogicRead(mPortExpanderVal0) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 105 logError("Error reading port expander 0.");
Matt Briggs 44:ece6330e9b57 106 return cmdError;
Matt Briggs 44:ece6330e9b57 107 }
Matt Briggs 44:ece6330e9b57 108 if (mPortEx1->pioLogicRead(mPortExpanderVal1) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 109 logError("Error reading port expander 1.");
Matt Briggs 44:ece6330e9b57 110 return cmdError;
Matt Briggs 44:ece6330e9b57 111 }
Matt Briggs 44:ece6330e9b57 112 return cmdSuccess;
Matt Briggs 40:2ec4be320961 113 }
Matt Briggs 40:2ec4be320961 114 bool BaseboardIO::isPairBtn()
Matt Briggs 40:2ec4be320961 115 {
Matt Briggs 44:ece6330e9b57 116 // Depressed button is high
Matt Briggs 44:ece6330e9b57 117 return mPairBtn.read() == 1;
Matt Briggs 40:2ec4be320961 118 }
Matt Briggs 40:2ec4be320961 119 bool BaseboardIO::isCCOutNO()
Matt Briggs 40:2ec4be320961 120 {
Matt Briggs 44:ece6330e9b57 121 // When DIP switch is not closed (i.e. value reads high) assume NO
Matt Briggs 44:ece6330e9b57 122 return (mPortExpanderVal1 & pEx1NoNcSel) != 0;
Matt Briggs 40:2ec4be320961 123 }
Matt Briggs 40:2ec4be320961 124 bool BaseboardIO::isRx()
Matt Briggs 40:2ec4be320961 125 {
Matt Briggs 44:ece6330e9b57 126 // When DIP switch is not closed (i.e. value reads high) assume RX
Matt Briggs 44:ece6330e9b57 127 return (mPortExpanderVal1 & pEx1RxTxSel) != 0;
Matt Briggs 40:2ec4be320961 128 }
Matt Briggs 40:2ec4be320961 129 bool BaseboardIO::isLoRaWANMode()
Matt Briggs 40:2ec4be320961 130 {
Matt Briggs 44:ece6330e9b57 131 // When DIP switch is not closed (i.e. value reads high) assume P2P not WAN
Matt Briggs 44:ece6330e9b57 132 return (mPortExpanderVal1 & pEx1WanSel) == 0;
Matt Briggs 40:2ec4be320961 133 }
Matt Briggs 40:2ec4be320961 134 uint8_t BaseboardIO::rotarySwitch1()
Matt Briggs 40:2ec4be320961 135 {
Matt Briggs 44:ece6330e9b57 136 // If a bit of a nibble is asserted then the port expander line is switched low.
Matt Briggs 44:ece6330e9b57 137 uint8_t val = 0;
Matt Briggs 44:ece6330e9b57 138 if ((mPortExpanderVal0 & pEx0Rot1B8) == 0)
Matt Briggs 44:ece6330e9b57 139 val |= 0x08;
Matt Briggs 44:ece6330e9b57 140 if ((mPortExpanderVal0 & pEx0Rot1B4) == 0)
Matt Briggs 44:ece6330e9b57 141 val |= 0x04;
Matt Briggs 44:ece6330e9b57 142 if ((mPortExpanderVal0 & pEx0Rot1B2) == 0)
Matt Briggs 44:ece6330e9b57 143 val |= 0x02;
Matt Briggs 44:ece6330e9b57 144 if ((mPortExpanderVal0 & pEx0Rot1B1) == 0)
Matt Briggs 44:ece6330e9b57 145 val |= 0x01;
Matt Briggs 44:ece6330e9b57 146 return val;
Matt Briggs 40:2ec4be320961 147 }
Matt Briggs 40:2ec4be320961 148 uint8_t BaseboardIO::rotarySwitch2()
Matt Briggs 40:2ec4be320961 149 {
Matt Briggs 44:ece6330e9b57 150 // If a bit of a nibble is asserted then the port expander line is switched low.
Matt Briggs 44:ece6330e9b57 151 uint8_t val = 0;
Matt Briggs 44:ece6330e9b57 152 if ((mPortExpanderVal1 & pEx1Rot2B8) == 0)
Matt Briggs 44:ece6330e9b57 153 val |= 0x08;
Matt Briggs 44:ece6330e9b57 154 if ((mPortExpanderVal1 & pEx1Rot2B4) == 0)
Matt Briggs 44:ece6330e9b57 155 val |= 0x04;
Matt Briggs 44:ece6330e9b57 156 if ((mPortExpanderVal0 & pEx0Rot2B2) == 0)
Matt Briggs 44:ece6330e9b57 157 val |= 0x02;
Matt Briggs 44:ece6330e9b57 158 if ((mPortExpanderVal0 & pEx0Rot2B1) == 0)
Matt Briggs 44:ece6330e9b57 159 val |= 0x01;
Matt Briggs 44:ece6330e9b57 160 return val;
Matt Briggs 40:2ec4be320961 161 }
Matt Briggs 40:2ec4be320961 162
Matt Briggs 40:2ec4be320961 163 // Output
Matt Briggs 40:2ec4be320961 164 CmdResult BaseboardIO::ledOn()
Matt Briggs 40:2ec4be320961 165 {
Matt Briggs 44:ece6330e9b57 166 mLed = 1;
Matt Briggs 44:ece6330e9b57 167 return cmdSuccess;
Matt Briggs 40:2ec4be320961 168 }
Matt Briggs 40:2ec4be320961 169 CmdResult BaseboardIO::ledOff()
Matt Briggs 40:2ec4be320961 170 {
Matt Briggs 44:ece6330e9b57 171 mLed = 0;
Matt Briggs 44:ece6330e9b57 172 return cmdSuccess;
Matt Briggs 40:2ec4be320961 173 }
Matt Briggs 40:2ec4be320961 174 CmdResult BaseboardIO::relayAlert()
Matt Briggs 40:2ec4be320961 175 {
Matt Briggs 44:ece6330e9b57 176 if (isCCOutNO()) { // Normally Open
Matt Briggs 44:ece6330e9b57 177 return closeRelay();
Matt Briggs 44:ece6330e9b57 178 }
Matt Briggs 44:ece6330e9b57 179 else { // Normally Close
Matt Briggs 44:ece6330e9b57 180 return openRelay();
Matt Briggs 44:ece6330e9b57 181 }
Matt Briggs 40:2ec4be320961 182 }
Matt Briggs 40:2ec4be320961 183 CmdResult BaseboardIO::relayNormal()
Matt Briggs 40:2ec4be320961 184 {
Matt Briggs 44:ece6330e9b57 185 if (isCCOutNO()) { // Normally Open
Matt Briggs 44:ece6330e9b57 186 return openRelay();
Matt Briggs 44:ece6330e9b57 187 }
Matt Briggs 44:ece6330e9b57 188 else { // Normally Close
Matt Briggs 44:ece6330e9b57 189 return closeRelay();
Matt Briggs 44:ece6330e9b57 190 }
Matt Briggs 40:2ec4be320961 191 }
Matt Briggs 40:2ec4be320961 192
Matt Briggs 40:2ec4be320961 193 // Future
Matt Briggs 40:2ec4be320961 194 CmdResult BaseboardIO::serialRx(bool enable)
Matt Briggs 40:2ec4be320961 195 {
Matt Briggs 44:ece6330e9b57 196 uint8_t val;
Matt Briggs 44:ece6330e9b57 197 mPortEx0->pioLogicRead(val);
Matt Briggs 44:ece6330e9b57 198
Matt Briggs 44:ece6330e9b57 199 // Active low from port expander -> pmos -> 232 (active chip EN)
Matt Briggs 44:ece6330e9b57 200 if (enable) {
Matt Briggs 44:ece6330e9b57 201 val &= ~pEx0232En;
Matt Briggs 44:ece6330e9b57 202 }
Matt Briggs 44:ece6330e9b57 203 else {
Matt Briggs 44:ece6330e9b57 204 val |= pEx0232En;
Matt Briggs 44:ece6330e9b57 205 }
Matt Briggs 44:ece6330e9b57 206
Matt Briggs 44:ece6330e9b57 207 if (mPortEx1->pioLogicWrite(val) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 208 logError("Error enabling 232");
Matt Briggs 44:ece6330e9b57 209 return cmdError;
Matt Briggs 44:ece6330e9b57 210 }
Matt Briggs 44:ece6330e9b57 211 return cmdSuccess;
Matt Briggs 44:ece6330e9b57 212 }
Matt Briggs 44:ece6330e9b57 213 CmdResult BaseboardIO::serialTx(bool enable)
Matt Briggs 44:ece6330e9b57 214 {
Matt Briggs 44:ece6330e9b57 215 uint8_t val;
Matt Briggs 44:ece6330e9b57 216 mPortEx0->pioLogicRead(val);
Matt Briggs 44:ece6330e9b57 217
Matt Briggs 44:ece6330e9b57 218 // Active high tx disable therefore active low tx enable (note chip must also be enabled for TX)
Matt Briggs 44:ece6330e9b57 219 if (enable) {
Matt Briggs 44:ece6330e9b57 220 val &= ~pEx0232TxDis;
Matt Briggs 44:ece6330e9b57 221 }
Matt Briggs 44:ece6330e9b57 222 else {
Matt Briggs 44:ece6330e9b57 223 val |= pEx0232TxDis;
Matt Briggs 44:ece6330e9b57 224 }
Matt Briggs 44:ece6330e9b57 225
Matt Briggs 44:ece6330e9b57 226 if (mPortEx1->pioLogicWrite(val) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 227 logError("Error enabling 232 TX");
Matt Briggs 44:ece6330e9b57 228 return cmdError;
Matt Briggs 44:ece6330e9b57 229 }
Matt Briggs 44:ece6330e9b57 230 return cmdSuccess;
Matt Briggs 44:ece6330e9b57 231 }
Matt Briggs 44:ece6330e9b57 232
Matt Briggs 44:ece6330e9b57 233 // private
Matt Briggs 44:ece6330e9b57 234 CmdResult BaseboardIO::readInfoFromNVM()
Matt Briggs 44:ece6330e9b57 235 {
Matt Briggs 40:2ec4be320961 236 logError("Not implemented yet!!!");
Matt Briggs 40:2ec4be320961 237 return cmdError;
Matt Briggs 40:2ec4be320961 238 }
Matt Briggs 44:ece6330e9b57 239 CmdResult BaseboardIO::writeInfoToNVM()
Matt Briggs 40:2ec4be320961 240 {
Matt Briggs 40:2ec4be320961 241 logError("Not implemented yet!!!");
Matt Briggs 40:2ec4be320961 242 return cmdError;
Matt Briggs 40:2ec4be320961 243 }
Matt Briggs 44:ece6330e9b57 244 CmdResult BaseboardIO::identifyPortExpanders()
Matt Briggs 44:ece6330e9b57 245 {
Matt Briggs 44:ece6330e9b57 246 uint8_t addr[8];
Matt Briggs 44:ece6330e9b57 247 uint8_t result;
Matt Briggs 40:2ec4be320961 248
Matt Briggs 44:ece6330e9b57 249 // Search Bus
Matt Briggs 44:ece6330e9b57 250 logInfo("Starting OneWire Search");
Matt Briggs 44:ece6330e9b57 251 int i=0;
Matt Briggs 44:ece6330e9b57 252 while (true) {
Matt Briggs 44:ece6330e9b57 253 // TODO maybe change to family based search
Matt Briggs 44:ece6330e9b57 254 result = mOWMaster.search(addr);
Matt Briggs 44:ece6330e9b57 255 logInfo("ROM Addr: %02x:%02x:%02x:%02x:%02x:%02x:%02x%02x found.",
Matt Briggs 44:ece6330e9b57 256 addr[7],addr[6],addr[5],addr[4],addr[3],addr[2],addr[1],addr[0]);
Matt Briggs 44:ece6330e9b57 257 if (result != 1)
Matt Briggs 44:ece6330e9b57 258 break;
Matt Briggs 44:ece6330e9b57 259 if (i == 0) {
Matt Briggs 44:ece6330e9b57 260 std::memcpy(mPortExpanderROM0, addr, sizeof(mPortExpanderROM0));
Matt Briggs 44:ece6330e9b57 261 }
Matt Briggs 44:ece6330e9b57 262 else if (i == 1) {
Matt Briggs 44:ece6330e9b57 263 std::memcpy(mPortExpanderROM1, addr, sizeof(mPortExpanderROM1));
Matt Briggs 44:ece6330e9b57 264 }
Matt Briggs 44:ece6330e9b57 265 i++;
Matt Briggs 44:ece6330e9b57 266 }
Matt Briggs 44:ece6330e9b57 267
Matt Briggs 44:ece6330e9b57 268 logInfo("Finished OneWire Search");
Matt Briggs 44:ece6330e9b57 269 if (i != 2) {
Matt Briggs 44:ece6330e9b57 270 logError("Incorrect Number (Got %d. Expected 2) OneWire port expanders found.", i);
Matt Briggs 44:ece6330e9b57 271 return cmdError;
Matt Briggs 44:ece6330e9b57 272 }
Matt Briggs 44:ece6330e9b57 273
Matt Briggs 44:ece6330e9b57 274 // If switches are set in factory default mode then port expander 1 should read 0xFF and
Matt Briggs 44:ece6330e9b57 275 // port expander 2 should read 0xF0.
Matt Briggs 40:2ec4be320961 276
Matt Briggs 44:ece6330e9b57 277 mPortEx0 = new DS2408(&mOWMaster, mPortExpanderROM0);
Matt Briggs 44:ece6330e9b57 278 mPortEx1 = new DS2408(&mOWMaster, mPortExpanderROM1);
Matt Briggs 44:ece6330e9b57 279
Matt Briggs 44:ece6330e9b57 280 if (mPortEx0->pioLogicRead(mPortExpanderVal0) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 281 logError("Error during port expander ID. Read failed.");
Matt Briggs 44:ece6330e9b57 282 delete mPortEx0;
Matt Briggs 44:ece6330e9b57 283 delete mPortEx1;
Matt Briggs 44:ece6330e9b57 284 return cmdError;
Matt Briggs 44:ece6330e9b57 285 }
Matt Briggs 44:ece6330e9b57 286 if (mPortEx1->pioLogicRead(mPortExpanderVal1) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 287 logError("Error during port expander ID. Read failed.");
Matt Briggs 44:ece6330e9b57 288 delete mPortEx0;
Matt Briggs 44:ece6330e9b57 289 delete mPortEx1;
Matt Briggs 44:ece6330e9b57 290 return cmdError;
Matt Briggs 44:ece6330e9b57 291 }
Matt Briggs 44:ece6330e9b57 292
Matt Briggs 44:ece6330e9b57 293 if ((mPortExpanderVal0 == 0xFF) and (mPortExpanderVal1 == 0xF0)) { // Luckily got it right
Matt Briggs 44:ece6330e9b57 294 logInfo("ROMS Swap Not Needed.");
Matt Briggs 44:ece6330e9b57 295 }
Matt Briggs 44:ece6330e9b57 296 else if ((mPortExpanderVal0 == 0xF0) and (mPortExpanderVal1 == 0xFF)) { // Just need to swap
Matt Briggs 44:ece6330e9b57 297 std::memcpy(addr, mPortExpanderROM0, sizeof(addr)); // Store Orig ROM0 -> addr
Matt Briggs 44:ece6330e9b57 298 std::memcpy(mPortExpanderROM0, mPortExpanderROM1, sizeof(mPortExpanderROM0)); // Store Orig ROM1 -> ROM0
Matt Briggs 44:ece6330e9b57 299 std::memcpy(mPortExpanderROM1, addr, sizeof(mPortExpanderROM1)); // Store Orig ROM0 (addr) -> ROM1
Matt Briggs 44:ece6330e9b57 300 logInfo("Swapped ROMS.");
Matt Briggs 44:ece6330e9b57 301 }
Matt Briggs 44:ece6330e9b57 302 else {
Matt Briggs 44:ece6330e9b57 303 logError("Error during port expander ID. Port expanders not in expected states. Check user switches.");
Matt Briggs 44:ece6330e9b57 304 delete mPortEx0;
Matt Briggs 44:ece6330e9b57 305 delete mPortEx1;
Matt Briggs 44:ece6330e9b57 306 return cmdError;
Matt Briggs 44:ece6330e9b57 307 }
Matt Briggs 44:ece6330e9b57 308
Matt Briggs 44:ece6330e9b57 309 // Cleanup
Matt Briggs 44:ece6330e9b57 310 delete mPortEx0;
Matt Briggs 44:ece6330e9b57 311 delete mPortEx1;
Matt Briggs 44:ece6330e9b57 312
Matt Briggs 44:ece6330e9b57 313 return cmdSuccess;
Matt Briggs 40:2ec4be320961 314 }
Matt Briggs 44:ece6330e9b57 315 CmdResult BaseboardIO::closeRelay() {
Matt Briggs 44:ece6330e9b57 316 uint8_t val;
Matt Briggs 44:ece6330e9b57 317 mPortEx1->pioLogicRead(val);
Matt Briggs 44:ece6330e9b57 318
Matt Briggs 44:ece6330e9b57 319 val |= pEx1RlyA; // Turn on Relay A
Matt Briggs 44:ece6330e9b57 320 val &= ~pEx1RlyB; // Make sure Relay B is off
Matt Briggs 44:ece6330e9b57 321
Matt Briggs 44:ece6330e9b57 322 if (mPortEx1->pioLogicWrite(val) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 323 val &= ~pEx1RlyA; // Turn Relay A off
Matt Briggs 44:ece6330e9b57 324 val &= ~pEx1RlyB; // Turn Relay B off
Matt Briggs 44:ece6330e9b57 325 mPortEx1->pioLogicWrite(val); // Write a non assert value just to try to overcome an error
Matt Briggs 44:ece6330e9b57 326 logError ("Error turning on coil. Turning both coils off.");
Matt Briggs 44:ece6330e9b57 327 return cmdError;
Matt Briggs 44:ece6330e9b57 328 }
Matt Briggs 44:ece6330e9b57 329
Matt Briggs 44:ece6330e9b57 330 wait(coilOnTime);
Matt Briggs 44:ece6330e9b57 331
Matt Briggs 44:ece6330e9b57 332 val &= ~pEx1RlyA; // Turn Relay A off
Matt Briggs 44:ece6330e9b57 333 val &= ~pEx1RlyB; // Turn Relay B off
Matt Briggs 44:ece6330e9b57 334
Matt Briggs 44:ece6330e9b57 335 if (mPortEx1->pioLogicWrite(val) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 336 mPortEx1->pioLogicWrite(val);
Matt Briggs 44:ece6330e9b57 337 logError ("Error turning off coils. Trying again.");
Matt Briggs 44:ece6330e9b57 338 return cmdError;
Matt Briggs 44:ece6330e9b57 339 }
Matt Briggs 44:ece6330e9b57 340
Matt Briggs 44:ece6330e9b57 341 return cmdSuccess;
Matt Briggs 40:2ec4be320961 342 }
Matt Briggs 44:ece6330e9b57 343 CmdResult BaseboardIO::openRelay() {
Matt Briggs 44:ece6330e9b57 344 uint8_t val;
Matt Briggs 44:ece6330e9b57 345 mPortEx1->pioLogicRead(val);
Matt Briggs 44:ece6330e9b57 346
Matt Briggs 44:ece6330e9b57 347 val &= ~pEx1RlyA; // Make sure Relay A is off
Matt Briggs 44:ece6330e9b57 348 val |= pEx1RlyB; // Turn on Relay B
Matt Briggs 44:ece6330e9b57 349
Matt Briggs 44:ece6330e9b57 350 if (mPortEx1->pioLogicWrite(val) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 351 val &= ~pEx1RlyA; // Turn Relay A off
Matt Briggs 44:ece6330e9b57 352 val &= ~pEx1RlyB; // Turn Relay B off
Matt Briggs 44:ece6330e9b57 353 mPortEx1->pioLogicWrite(val); // Write a non assert value just to try to overcome an error
Matt Briggs 44:ece6330e9b57 354 logError ("Error turning on coil. Turning both coils off.");
Matt Briggs 44:ece6330e9b57 355 return cmdError;
Matt Briggs 44:ece6330e9b57 356 }
Matt Briggs 44:ece6330e9b57 357
Matt Briggs 44:ece6330e9b57 358 wait(coilOnTime);
Matt Briggs 44:ece6330e9b57 359
Matt Briggs 44:ece6330e9b57 360 val &= ~pEx1RlyA; // Turn Relay A off
Matt Briggs 44:ece6330e9b57 361 val &= ~pEx1RlyB; // Turn Relay B off
Matt Briggs 44:ece6330e9b57 362
Matt Briggs 44:ece6330e9b57 363 if (mPortEx1->pioLogicWrite(val) != cmdSuccess) {
Matt Briggs 44:ece6330e9b57 364 mPortEx1->pioLogicWrite(val);
Matt Briggs 44:ece6330e9b57 365 logError ("Error turning off coils. Trying again.");
Matt Briggs 44:ece6330e9b57 366 return cmdError;
Matt Briggs 44:ece6330e9b57 367 }
Matt Briggs 44:ece6330e9b57 368
Matt Briggs 44:ece6330e9b57 369 return cmdSuccess;
Matt Briggs 40:2ec4be320961 370 }