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