![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/src/BaseboardIO.cpp@48:bab9f747d9ed, 2017-02-13 (annotated)
- Committer:
- Matt Briggs
- Date:
- Mon Feb 13 17:10:59 2017 -0700
- Revision:
- 48:bab9f747d9ed
- Parent:
- 47:a68747642a7a
- Child:
- 49:18f1354f9e51
First cut at manual testing for baseboard IO. Still needs to be debugged and used.
Who changed what in which revision?
User | Revision | Line number | New 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 | 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 | 48:bab9f747d9ed | 43 | // mLed(SWDIO), |
Matt Briggs | 48:bab9f747d9ed | 44 | mLed(GPIO0), |
Matt Briggs | 44:ece6330e9b57 | 45 | mSwitchedIOCtrl(I2C_SCL) |
Matt Briggs | 40:2ec4be320961 | 46 | { |
Matt Briggs | 44:ece6330e9b57 | 47 | // mCCInIntCallback = NULL; |
Matt Briggs | 44:ece6330e9b57 | 48 | // mTamperIntCallback = NULL; |
Matt Briggs | 44:ece6330e9b57 | 49 | // mPairBtnIntCallback = NULL; |
Matt Briggs | 40:2ec4be320961 | 50 | |
Matt Briggs | 44:ece6330e9b57 | 51 | std::memset(mPortExpanderROM0, 0x00, 8); |
Matt Briggs | 44:ece6330e9b57 | 52 | std::memset(mPortExpanderROM1, 0x00, 8); |
Matt Briggs | 44:ece6330e9b57 | 53 | mPortExpanderVal0 = 0x00; |
Matt Briggs | 44:ece6330e9b57 | 54 | mPortExpanderVal1 = 0x00; |
Matt Briggs | 44:ece6330e9b57 | 55 | |
Matt Briggs | 44:ece6330e9b57 | 56 | mPortEx0 = NULL; |
Matt Briggs | 44:ece6330e9b57 | 57 | mPortEx1 = NULL; |
Matt Briggs | 40:2ec4be320961 | 58 | } |
Matt Briggs | 40:2ec4be320961 | 59 | CmdResult BaseboardIO::init() |
Matt Briggs | 40:2ec4be320961 | 60 | { |
Matt Briggs | 44:ece6330e9b57 | 61 | // Setup port expanders |
Matt Briggs | 47:a68747642a7a | 62 | if (readInfoFromNVM() == cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 63 | // Values stored just read them foo |
Matt Briggs | 44:ece6330e9b57 | 64 | logError("Not implemented yet!!!"); |
Matt Briggs | 44:ece6330e9b57 | 65 | } |
Matt Briggs | 47:a68747642a7a | 66 | else { // EEPROM values not there or corrupt. Should only happen in factory. |
Matt Briggs | 47:a68747642a7a | 67 | // Find ROM address and test which one is which. Requires user |
Matt Briggs | 47:a68747642a7a | 68 | // switches to be in known state. |
Matt Briggs | 47:a68747642a7a | 69 | if (identifyPortExpanders() != 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 | 47:a68747642a7a | 78 | if (relayNormal() != cmdSuccess) { |
Matt Briggs | 47:a68747642a7a | 79 | logError("Error setting relay during init"); |
Matt Briggs | 47:a68747642a7a | 80 | return cmdError; |
Matt Briggs | 47:a68747642a7a | 81 | } |
Matt Briggs | 47:a68747642a7a | 82 | |
Matt Briggs | 47:a68747642a7a | 83 | if (sampleUserSwitches() != cmdSuccess) { |
Matt Briggs | 47:a68747642a7a | 84 | logError("Error sampling user switches"); |
Matt Briggs | 47:a68747642a7a | 85 | return cmdError; |
Matt Briggs | 47:a68747642a7a | 86 | } |
Matt Briggs | 44:ece6330e9b57 | 87 | |
Matt Briggs | 44:ece6330e9b57 | 88 | logInfo("Baseboard IO initialization successful"); |
Matt Briggs | 44:ece6330e9b57 | 89 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 90 | } |
Matt Briggs | 40:2ec4be320961 | 91 | |
Matt Briggs | 40:2ec4be320961 | 92 | // Registering for interrupts |
Matt Briggs | 44:ece6330e9b57 | 93 | void BaseboardIO::regCCInInt(Callback<void()> func) |
Matt Briggs | 40:2ec4be320961 | 94 | { |
Matt Briggs | 48:bab9f747d9ed | 95 | sampleUserSwitches(); |
Matt Briggs | 48:bab9f747d9ed | 96 | if (isCCNO()) { |
Matt Briggs | 48:bab9f747d9ed | 97 | // Pulled high, switched low |
Matt Briggs | 48:bab9f747d9ed | 98 | mCCIn.fall(func); |
Matt Briggs | 48:bab9f747d9ed | 99 | } |
Matt Briggs | 48:bab9f747d9ed | 100 | else { |
Matt Briggs | 48:bab9f747d9ed | 101 | mCCIn.rise(func); |
Matt Briggs | 48:bab9f747d9ed | 102 | } |
Matt Briggs | 40:2ec4be320961 | 103 | } |
Matt Briggs | 44:ece6330e9b57 | 104 | void BaseboardIO::regTamperInt(Callback<void()> func) |
Matt Briggs | 40:2ec4be320961 | 105 | { |
Matt Briggs | 44:ece6330e9b57 | 106 | // Pulled high, switched low |
Matt Briggs | 44:ece6330e9b57 | 107 | mTamper.fall(func); |
Matt Briggs | 40:2ec4be320961 | 108 | } |
Matt Briggs | 44:ece6330e9b57 | 109 | void BaseboardIO::regPairBtnInt(Callback<void()> func) |
Matt Briggs | 40:2ec4be320961 | 110 | { |
Matt Briggs | 44:ece6330e9b57 | 111 | // Pulled low, switched high |
Matt Briggs | 44:ece6330e9b57 | 112 | mPairBtn.rise(func); |
Matt Briggs | 40:2ec4be320961 | 113 | } |
Matt Briggs | 40:2ec4be320961 | 114 | |
Matt Briggs | 40:2ec4be320961 | 115 | // Input |
Matt Briggs | 40:2ec4be320961 | 116 | CmdResult BaseboardIO::sampleUserSwitches() |
Matt Briggs | 40:2ec4be320961 | 117 | { |
Matt Briggs | 48:bab9f747d9ed | 118 | if ((mPortEx0 == NULL) || (mPortEx1 == NULL)) |
Matt Briggs | 48:bab9f747d9ed | 119 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 120 | // Sample port expanders |
Matt Briggs | 44:ece6330e9b57 | 121 | if (mPortEx0->pioLogicRead(mPortExpanderVal0) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 122 | logError("Error reading port expander 0."); |
Matt Briggs | 44:ece6330e9b57 | 123 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 124 | } |
Matt Briggs | 44:ece6330e9b57 | 125 | if (mPortEx1->pioLogicRead(mPortExpanderVal1) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 126 | logError("Error reading port expander 1."); |
Matt Briggs | 44:ece6330e9b57 | 127 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 128 | } |
Matt Briggs | 44:ece6330e9b57 | 129 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 130 | } |
Matt Briggs | 40:2ec4be320961 | 131 | bool BaseboardIO::isPairBtn() |
Matt Briggs | 40:2ec4be320961 | 132 | { |
Matt Briggs | 44:ece6330e9b57 | 133 | // Depressed button is high |
Matt Briggs | 44:ece6330e9b57 | 134 | return mPairBtn.read() == 1; |
Matt Briggs | 40:2ec4be320961 | 135 | } |
Matt Briggs | 48:bab9f747d9ed | 136 | bool BaseboardIO::isCCNO() |
Matt Briggs | 40:2ec4be320961 | 137 | { |
Matt Briggs | 44:ece6330e9b57 | 138 | // When DIP switch is not closed (i.e. value reads high) assume NO |
Matt Briggs | 44:ece6330e9b57 | 139 | return (mPortExpanderVal1 & pEx1NoNcSel) != 0; |
Matt Briggs | 40:2ec4be320961 | 140 | } |
Matt Briggs | 40:2ec4be320961 | 141 | bool BaseboardIO::isRx() |
Matt Briggs | 40:2ec4be320961 | 142 | { |
Matt Briggs | 44:ece6330e9b57 | 143 | // When DIP switch is not closed (i.e. value reads high) assume RX |
Matt Briggs | 44:ece6330e9b57 | 144 | return (mPortExpanderVal1 & pEx1RxTxSel) != 0; |
Matt Briggs | 40:2ec4be320961 | 145 | } |
Matt Briggs | 40:2ec4be320961 | 146 | bool BaseboardIO::isLoRaWANMode() |
Matt Briggs | 40:2ec4be320961 | 147 | { |
Matt Briggs | 44:ece6330e9b57 | 148 | // When DIP switch is not closed (i.e. value reads high) assume P2P not WAN |
Matt Briggs | 44:ece6330e9b57 | 149 | return (mPortExpanderVal1 & pEx1WanSel) == 0; |
Matt Briggs | 40:2ec4be320961 | 150 | } |
Matt Briggs | 40:2ec4be320961 | 151 | uint8_t BaseboardIO::rotarySwitch1() |
Matt Briggs | 40:2ec4be320961 | 152 | { |
Matt Briggs | 44:ece6330e9b57 | 153 | // If a bit of a nibble is asserted then the port expander line is switched low. |
Matt Briggs | 44:ece6330e9b57 | 154 | uint8_t val = 0; |
Matt Briggs | 44:ece6330e9b57 | 155 | if ((mPortExpanderVal0 & pEx0Rot1B8) == 0) |
Matt Briggs | 44:ece6330e9b57 | 156 | val |= 0x08; |
Matt Briggs | 44:ece6330e9b57 | 157 | if ((mPortExpanderVal0 & pEx0Rot1B4) == 0) |
Matt Briggs | 44:ece6330e9b57 | 158 | val |= 0x04; |
Matt Briggs | 44:ece6330e9b57 | 159 | if ((mPortExpanderVal0 & pEx0Rot1B2) == 0) |
Matt Briggs | 44:ece6330e9b57 | 160 | val |= 0x02; |
Matt Briggs | 44:ece6330e9b57 | 161 | if ((mPortExpanderVal0 & pEx0Rot1B1) == 0) |
Matt Briggs | 44:ece6330e9b57 | 162 | val |= 0x01; |
Matt Briggs | 44:ece6330e9b57 | 163 | return val; |
Matt Briggs | 40:2ec4be320961 | 164 | } |
Matt Briggs | 40:2ec4be320961 | 165 | uint8_t BaseboardIO::rotarySwitch2() |
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 ((mPortExpanderVal1 & pEx1Rot2B8) == 0) |
Matt Briggs | 44:ece6330e9b57 | 170 | val |= 0x08; |
Matt Briggs | 44:ece6330e9b57 | 171 | if ((mPortExpanderVal1 & pEx1Rot2B4) == 0) |
Matt Briggs | 44:ece6330e9b57 | 172 | val |= 0x04; |
Matt Briggs | 44:ece6330e9b57 | 173 | if ((mPortExpanderVal0 & pEx0Rot2B2) == 0) |
Matt Briggs | 44:ece6330e9b57 | 174 | val |= 0x02; |
Matt Briggs | 44:ece6330e9b57 | 175 | if ((mPortExpanderVal0 & pEx0Rot2B1) == 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 | |
Matt Briggs | 40:2ec4be320961 | 180 | // Output |
Matt Briggs | 40:2ec4be320961 | 181 | CmdResult BaseboardIO::ledOn() |
Matt Briggs | 40:2ec4be320961 | 182 | { |
Matt Briggs | 44:ece6330e9b57 | 183 | mLed = 1; |
Matt Briggs | 44:ece6330e9b57 | 184 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 185 | } |
Matt Briggs | 40:2ec4be320961 | 186 | CmdResult BaseboardIO::ledOff() |
Matt Briggs | 40:2ec4be320961 | 187 | { |
Matt Briggs | 44:ece6330e9b57 | 188 | mLed = 0; |
Matt Briggs | 44:ece6330e9b57 | 189 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 190 | } |
Matt Briggs | 40:2ec4be320961 | 191 | CmdResult BaseboardIO::relayAlert() |
Matt Briggs | 40:2ec4be320961 | 192 | { |
Matt Briggs | 48:bab9f747d9ed | 193 | if (isCCNO()) { // Normally Open |
Matt Briggs | 44:ece6330e9b57 | 194 | return closeRelay(); |
Matt Briggs | 44:ece6330e9b57 | 195 | } |
Matt Briggs | 44:ece6330e9b57 | 196 | else { // Normally Close |
Matt Briggs | 44:ece6330e9b57 | 197 | return openRelay(); |
Matt Briggs | 44:ece6330e9b57 | 198 | } |
Matt Briggs | 40:2ec4be320961 | 199 | } |
Matt Briggs | 40:2ec4be320961 | 200 | CmdResult BaseboardIO::relayNormal() |
Matt Briggs | 40:2ec4be320961 | 201 | { |
Matt Briggs | 48:bab9f747d9ed | 202 | if (isCCNO()) { // Normally Open |
Matt Briggs | 44:ece6330e9b57 | 203 | return openRelay(); |
Matt Briggs | 44:ece6330e9b57 | 204 | } |
Matt Briggs | 44:ece6330e9b57 | 205 | else { // Normally Close |
Matt Briggs | 44:ece6330e9b57 | 206 | return closeRelay(); |
Matt Briggs | 44:ece6330e9b57 | 207 | } |
Matt Briggs | 40:2ec4be320961 | 208 | } |
Matt Briggs | 40:2ec4be320961 | 209 | |
Matt Briggs | 40:2ec4be320961 | 210 | // Future |
Matt Briggs | 40:2ec4be320961 | 211 | CmdResult BaseboardIO::serialRx(bool enable) |
Matt Briggs | 40:2ec4be320961 | 212 | { |
Matt Briggs | 44:ece6330e9b57 | 213 | uint8_t val; |
Matt Briggs | 44:ece6330e9b57 | 214 | mPortEx0->pioLogicRead(val); |
Matt Briggs | 44:ece6330e9b57 | 215 | |
Matt Briggs | 44:ece6330e9b57 | 216 | // Active low from port expander -> pmos -> 232 (active chip EN) |
Matt Briggs | 44:ece6330e9b57 | 217 | if (enable) { |
Matt Briggs | 44:ece6330e9b57 | 218 | val &= ~pEx0232En; |
Matt Briggs | 44:ece6330e9b57 | 219 | } |
Matt Briggs | 44:ece6330e9b57 | 220 | else { |
Matt Briggs | 44:ece6330e9b57 | 221 | val |= pEx0232En; |
Matt Briggs | 44:ece6330e9b57 | 222 | } |
Matt Briggs | 44:ece6330e9b57 | 223 | |
Matt Briggs | 44:ece6330e9b57 | 224 | if (mPortEx1->pioLogicWrite(val) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 225 | logError("Error enabling 232"); |
Matt Briggs | 44:ece6330e9b57 | 226 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 227 | } |
Matt Briggs | 44:ece6330e9b57 | 228 | return cmdSuccess; |
Matt Briggs | 44:ece6330e9b57 | 229 | } |
Matt Briggs | 44:ece6330e9b57 | 230 | CmdResult BaseboardIO::serialTx(bool enable) |
Matt Briggs | 44:ece6330e9b57 | 231 | { |
Matt Briggs | 44:ece6330e9b57 | 232 | uint8_t val; |
Matt Briggs | 44:ece6330e9b57 | 233 | mPortEx0->pioLogicRead(val); |
Matt Briggs | 44:ece6330e9b57 | 234 | |
Matt Briggs | 44:ece6330e9b57 | 235 | // Active high tx disable therefore active low tx enable (note chip must also be enabled for TX) |
Matt Briggs | 44:ece6330e9b57 | 236 | if (enable) { |
Matt Briggs | 44:ece6330e9b57 | 237 | val &= ~pEx0232TxDis; |
Matt Briggs | 44:ece6330e9b57 | 238 | } |
Matt Briggs | 44:ece6330e9b57 | 239 | else { |
Matt Briggs | 44:ece6330e9b57 | 240 | val |= pEx0232TxDis; |
Matt Briggs | 44:ece6330e9b57 | 241 | } |
Matt Briggs | 44:ece6330e9b57 | 242 | |
Matt Briggs | 44:ece6330e9b57 | 243 | if (mPortEx1->pioLogicWrite(val) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 244 | logError("Error enabling 232 TX"); |
Matt Briggs | 44:ece6330e9b57 | 245 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 246 | } |
Matt Briggs | 44:ece6330e9b57 | 247 | return cmdSuccess; |
Matt Briggs | 44:ece6330e9b57 | 248 | } |
Matt Briggs | 44:ece6330e9b57 | 249 | |
Matt Briggs | 44:ece6330e9b57 | 250 | // private |
Matt Briggs | 44:ece6330e9b57 | 251 | CmdResult BaseboardIO::readInfoFromNVM() |
Matt Briggs | 44:ece6330e9b57 | 252 | { |
Matt Briggs | 40:2ec4be320961 | 253 | logError("Not implemented yet!!!"); |
Matt Briggs | 40:2ec4be320961 | 254 | return cmdError; |
Matt Briggs | 40:2ec4be320961 | 255 | } |
Matt Briggs | 44:ece6330e9b57 | 256 | CmdResult BaseboardIO::writeInfoToNVM() |
Matt Briggs | 40:2ec4be320961 | 257 | { |
Matt Briggs | 40:2ec4be320961 | 258 | logError("Not implemented yet!!!"); |
Matt Briggs | 40:2ec4be320961 | 259 | return cmdError; |
Matt Briggs | 40:2ec4be320961 | 260 | } |
Matt Briggs | 44:ece6330e9b57 | 261 | CmdResult BaseboardIO::identifyPortExpanders() |
Matt Briggs | 44:ece6330e9b57 | 262 | { |
Matt Briggs | 44:ece6330e9b57 | 263 | uint8_t addr[8]; |
Matt Briggs | 44:ece6330e9b57 | 264 | uint8_t result; |
Matt Briggs | 40:2ec4be320961 | 265 | |
Matt Briggs | 44:ece6330e9b57 | 266 | // Search Bus |
Matt Briggs | 44:ece6330e9b57 | 267 | logInfo("Starting OneWire Search"); |
Matt Briggs | 44:ece6330e9b57 | 268 | int i=0; |
Matt Briggs | 47:a68747642a7a | 269 | mOWMaster.reset(); |
Matt Briggs | 44:ece6330e9b57 | 270 | while (true) { |
Matt Briggs | 44:ece6330e9b57 | 271 | // TODO maybe change to family based search |
Matt Briggs | 44:ece6330e9b57 | 272 | result = mOWMaster.search(addr); |
Matt Briggs | 44:ece6330e9b57 | 273 | logInfo("ROM Addr: %02x:%02x:%02x:%02x:%02x:%02x:%02x%02x found.", |
Matt Briggs | 44:ece6330e9b57 | 274 | addr[7],addr[6],addr[5],addr[4],addr[3],addr[2],addr[1],addr[0]); |
Matt Briggs | 44:ece6330e9b57 | 275 | if (result != 1) |
Matt Briggs | 44:ece6330e9b57 | 276 | break; |
Matt Briggs | 44:ece6330e9b57 | 277 | if (i == 0) { |
Matt Briggs | 44:ece6330e9b57 | 278 | std::memcpy(mPortExpanderROM0, addr, sizeof(mPortExpanderROM0)); |
Matt Briggs | 44:ece6330e9b57 | 279 | } |
Matt Briggs | 44:ece6330e9b57 | 280 | else if (i == 1) { |
Matt Briggs | 44:ece6330e9b57 | 281 | std::memcpy(mPortExpanderROM1, addr, sizeof(mPortExpanderROM1)); |
Matt Briggs | 44:ece6330e9b57 | 282 | } |
Matt Briggs | 44:ece6330e9b57 | 283 | i++; |
Matt Briggs | 47:a68747642a7a | 284 | // TODO maybe only allow a reasonable number of Port Expanders |
Matt Briggs | 44:ece6330e9b57 | 285 | } |
Matt Briggs | 44:ece6330e9b57 | 286 | |
Matt Briggs | 44:ece6330e9b57 | 287 | logInfo("Finished OneWire Search"); |
Matt Briggs | 44:ece6330e9b57 | 288 | if (i != 2) { |
Matt Briggs | 48:bab9f747d9ed | 289 | logError("Incorrect Number of OneWire devices (Got %d. Expected 2) OneWire port expanders found.", i); |
Matt Briggs | 44:ece6330e9b57 | 290 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 291 | } |
Matt Briggs | 44:ece6330e9b57 | 292 | |
Matt Briggs | 44:ece6330e9b57 | 293 | // If switches are set in factory default mode then port expander 1 should read 0xFF and |
Matt Briggs | 44:ece6330e9b57 | 294 | // port expander 2 should read 0xF0. |
Matt Briggs | 40:2ec4be320961 | 295 | |
Matt Briggs | 44:ece6330e9b57 | 296 | mPortEx0 = new DS2408(&mOWMaster, mPortExpanderROM0); |
Matt Briggs | 44:ece6330e9b57 | 297 | mPortEx1 = new DS2408(&mOWMaster, mPortExpanderROM1); |
Matt Briggs | 44:ece6330e9b57 | 298 | |
Matt Briggs | 44:ece6330e9b57 | 299 | if (mPortEx0->pioLogicRead(mPortExpanderVal0) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 300 | logError("Error during port expander ID. Read failed."); |
Matt Briggs | 44:ece6330e9b57 | 301 | delete mPortEx0; |
Matt Briggs | 44:ece6330e9b57 | 302 | delete mPortEx1; |
Matt Briggs | 44:ece6330e9b57 | 303 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 304 | } |
Matt Briggs | 44:ece6330e9b57 | 305 | if (mPortEx1->pioLogicRead(mPortExpanderVal1) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 306 | logError("Error during port expander ID. Read failed."); |
Matt Briggs | 44:ece6330e9b57 | 307 | delete mPortEx0; |
Matt Briggs | 44:ece6330e9b57 | 308 | delete mPortEx1; |
Matt Briggs | 44:ece6330e9b57 | 309 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 310 | } |
Matt Briggs | 44:ece6330e9b57 | 311 | |
Matt Briggs | 44:ece6330e9b57 | 312 | if ((mPortExpanderVal0 == 0xFF) and (mPortExpanderVal1 == 0xF0)) { // Luckily got it right |
Matt Briggs | 44:ece6330e9b57 | 313 | logInfo("ROMS Swap Not Needed."); |
Matt Briggs | 44:ece6330e9b57 | 314 | } |
Matt Briggs | 44:ece6330e9b57 | 315 | else if ((mPortExpanderVal0 == 0xF0) and (mPortExpanderVal1 == 0xFF)) { // Just need to swap |
Matt Briggs | 44:ece6330e9b57 | 316 | std::memcpy(addr, mPortExpanderROM0, sizeof(addr)); // Store Orig ROM0 -> addr |
Matt Briggs | 44:ece6330e9b57 | 317 | std::memcpy(mPortExpanderROM0, mPortExpanderROM1, sizeof(mPortExpanderROM0)); // Store Orig ROM1 -> ROM0 |
Matt Briggs | 44:ece6330e9b57 | 318 | std::memcpy(mPortExpanderROM1, addr, sizeof(mPortExpanderROM1)); // Store Orig ROM0 (addr) -> ROM1 |
Matt Briggs | 44:ece6330e9b57 | 319 | logInfo("Swapped ROMS."); |
Matt Briggs | 44:ece6330e9b57 | 320 | } |
Matt Briggs | 44:ece6330e9b57 | 321 | else { |
Matt Briggs | 44:ece6330e9b57 | 322 | logError("Error during port expander ID. Port expanders not in expected states. Check user switches."); |
Matt Briggs | 44:ece6330e9b57 | 323 | delete mPortEx0; |
Matt Briggs | 44:ece6330e9b57 | 324 | delete mPortEx1; |
Matt Briggs | 44:ece6330e9b57 | 325 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 326 | } |
Matt Briggs | 44:ece6330e9b57 | 327 | |
Matt Briggs | 44:ece6330e9b57 | 328 | // Cleanup |
Matt Briggs | 44:ece6330e9b57 | 329 | delete mPortEx0; |
Matt Briggs | 44:ece6330e9b57 | 330 | delete mPortEx1; |
Matt Briggs | 44:ece6330e9b57 | 331 | |
Matt Briggs | 44:ece6330e9b57 | 332 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 333 | } |
Matt Briggs | 44:ece6330e9b57 | 334 | CmdResult BaseboardIO::closeRelay() { |
Matt Briggs | 44:ece6330e9b57 | 335 | uint8_t val; |
Matt Briggs | 44:ece6330e9b57 | 336 | mPortEx1->pioLogicRead(val); |
Matt Briggs | 44:ece6330e9b57 | 337 | |
Matt Briggs | 44:ece6330e9b57 | 338 | val |= pEx1RlyA; // Turn on Relay A |
Matt Briggs | 44:ece6330e9b57 | 339 | val &= ~pEx1RlyB; // Make sure Relay B is off |
Matt Briggs | 44:ece6330e9b57 | 340 | |
Matt Briggs | 44:ece6330e9b57 | 341 | if (mPortEx1->pioLogicWrite(val) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 342 | val &= ~pEx1RlyA; // Turn Relay A off |
Matt Briggs | 44:ece6330e9b57 | 343 | val &= ~pEx1RlyB; // Turn Relay B off |
Matt Briggs | 44:ece6330e9b57 | 344 | mPortEx1->pioLogicWrite(val); // Write a non assert value just to try to overcome an error |
Matt Briggs | 44:ece6330e9b57 | 345 | logError ("Error turning on coil. Turning both coils off."); |
Matt Briggs | 44:ece6330e9b57 | 346 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 347 | } |
Matt Briggs | 44:ece6330e9b57 | 348 | |
Matt Briggs | 47:a68747642a7a | 349 | wait(COIL_ON_TIME); |
Matt Briggs | 44:ece6330e9b57 | 350 | |
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 | |
Matt Briggs | 44:ece6330e9b57 | 354 | if (mPortEx1->pioLogicWrite(val) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 355 | mPortEx1->pioLogicWrite(val); |
Matt Briggs | 44:ece6330e9b57 | 356 | logError ("Error turning off coils. Trying again."); |
Matt Briggs | 44:ece6330e9b57 | 357 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 358 | } |
Matt Briggs | 44:ece6330e9b57 | 359 | |
Matt Briggs | 44:ece6330e9b57 | 360 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 361 | } |
Matt Briggs | 44:ece6330e9b57 | 362 | CmdResult BaseboardIO::openRelay() { |
Matt Briggs | 44:ece6330e9b57 | 363 | uint8_t val; |
Matt Briggs | 44:ece6330e9b57 | 364 | mPortEx1->pioLogicRead(val); |
Matt Briggs | 44:ece6330e9b57 | 365 | |
Matt Briggs | 44:ece6330e9b57 | 366 | val &= ~pEx1RlyA; // Make sure Relay A is off |
Matt Briggs | 44:ece6330e9b57 | 367 | val |= pEx1RlyB; // Turn on Relay B |
Matt Briggs | 44:ece6330e9b57 | 368 | |
Matt Briggs | 44:ece6330e9b57 | 369 | if (mPortEx1->pioLogicWrite(val) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 370 | val &= ~pEx1RlyA; // Turn Relay A off |
Matt Briggs | 44:ece6330e9b57 | 371 | val &= ~pEx1RlyB; // Turn Relay B off |
Matt Briggs | 44:ece6330e9b57 | 372 | mPortEx1->pioLogicWrite(val); // Write a non assert value just to try to overcome an error |
Matt Briggs | 44:ece6330e9b57 | 373 | logError ("Error turning on coil. Turning both coils off."); |
Matt Briggs | 44:ece6330e9b57 | 374 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 375 | } |
Matt Briggs | 44:ece6330e9b57 | 376 | |
Matt Briggs | 47:a68747642a7a | 377 | wait(COIL_ON_TIME); |
Matt Briggs | 44:ece6330e9b57 | 378 | |
Matt Briggs | 44:ece6330e9b57 | 379 | val &= ~pEx1RlyA; // Turn Relay A off |
Matt Briggs | 44:ece6330e9b57 | 380 | val &= ~pEx1RlyB; // Turn Relay B off |
Matt Briggs | 44:ece6330e9b57 | 381 | |
Matt Briggs | 44:ece6330e9b57 | 382 | if (mPortEx1->pioLogicWrite(val) != cmdSuccess) { |
Matt Briggs | 44:ece6330e9b57 | 383 | mPortEx1->pioLogicWrite(val); |
Matt Briggs | 44:ece6330e9b57 | 384 | logError ("Error turning off coils. Trying again."); |
Matt Briggs | 44:ece6330e9b57 | 385 | return cmdError; |
Matt Briggs | 44:ece6330e9b57 | 386 | } |
Matt Briggs | 44:ece6330e9b57 | 387 | |
Matt Briggs | 44:ece6330e9b57 | 388 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 389 | } |