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