Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BaseboardIO.h Source File

BaseboardIO.h

00001 /*
00002  * baseboardIO.h
00003  *
00004  *  Created on: Jan 25, 2017
00005  *      Author: mbriggs
00006  */
00007 #include "DS2408.h"
00008 
00009 #ifndef BASEBOARDIO_H_
00010 #define BASEBOARDIO_H_
00011 
00012 const uint16_t BASEBOARDIO_NVM_START_ADDR = 0x1100;
00013 const uint16_t BASEBOARDIO_NVM_SIZE = 32; // Bytes
00014 const uint16_t BASEBOARDIO_FLAG = 0x5A00;
00015 const uint16_t BASEBOARDIO_REV = 0x0000;
00016 #define ALSO_USE_LRR_LED 1  // Only set to 1 for debug otherwise it is a waste of power
00017 
00018 class NvmBBIOObj {
00019 public:
00020     NvmBBIOObj();
00021     void setDefaults();
00022     CmdResult fromBytes(uint8_t *data, uint8_t size);
00023     CmdResult toBytes(uint8_t *data, uint8_t &size);
00024 
00025     uint16_t getBaseboardIOFlag();
00026     bool validBaseboardIOFlag();
00027     uint16_t getBaseboardIORev();
00028     bool validBaseboardIORev();
00029 
00030     uint16_t getSerialNum();
00031     void setSerialNum(uint16_t in);
00032     uint32_t getBaseboardIOConfig();
00033     void setBaseboardIOConfig(uint32_t in);
00034     void getPortExpanderROM0(uint8_t *addr);
00035     void setPortExpanderROM0(const uint8_t *addr);
00036     void getPortExpanderROM1(uint8_t *addr);
00037     void setPortExpanderROM1(const uint8_t *addr);
00038     // Make public to save memory
00039     uint8_t mPortExpanderROM0[8];
00040     uint8_t mPortExpanderROM1[8];
00041 
00042 private:
00043     uint16_t mBaseboardIOFlag;
00044     uint16_t mBaseboardIORev;
00045     uint16_t mSerialNum;
00046     uint16_t mBaseboardIOConfig;
00047 };
00048 
00049 /**
00050  *  @class BaseboardIO
00051  *  @brief This class abstracts utilizing the IO mostly found on the baseboard
00052  *  this includes IO which is implemented via portExpanders.  This API currently
00053  *  does not implement interfaces for communications like UART or for memory like
00054  *  flash devices.
00055  */
00056 class BaseboardIO
00057 {
00058 public:
00059     static const PinName OneWireMasterPinName  = I2C_SDA;
00060     static const PinName CCInPinName           = WAKE; // Interrupt pin PA_0
00061     static const PinName TamperPinName         = GPIO1; // Interrupt pin PA_5
00062     static const PinName PairBtnPinName        = UART_CTS; // Interrupt pin PA_11
00063     static const PinName LedPinName            = GPIO3;
00064     static const PinName LrrLedPinName         = GPIO0;
00065     static const PinName SwitchedIOCtrlPinName = I2C_SCL;
00066 
00067     /**
00068     * @brief BaseboardIO constructor
00069     *
00070     * @details Just initialized internal variables does not configure devices.
00071     * Should call init before other functions are called.
00072     *
00073     * On Entry:
00074     *
00075     * On Exit:
00076     * Internal variables are set to a known state but futher initialziation is required
00077     *
00078     * @return
00079     */
00080     BaseboardIO();
00081 
00082     /**
00083     * @brief Initialize IO to current values
00084     *
00085     * @details Initialize IO to current state and ensure that class variables
00086     * are updated to latest values.  The following are initialized.
00087     * 1.  PortExpanders are setup are read
00088     * 2.  Relay is forced to known state
00089     * 3.  Interrupts are setup
00090     *
00091     * On Entry:
00092     *
00093     * On Exit:
00094     * Either IO is configured or an error is returned
00095     *
00096     * @return CmdResult
00097     */
00098     CmdResult init()
00099     {
00100         return init(false);  // By default do not overwrite NVM
00101     }
00102     CmdResult init(bool overwriteNvm);
00103 
00104     ////////////////////////////////
00105     // Registering for interrupts //
00106     ////////////////////////////////
00107     /**
00108     * @brief Register for contact closure interrupt
00109     *
00110     * @details Pass function pointer which will be called when interrupt occurs.
00111     *
00112     * On Entry:
00113     *
00114     * On Exit:
00115     * Callback registered
00116     *
00117     * @return
00118     */
00119     void regCCInInt(Callback<void()> func);
00120 
00121     /**
00122     * @brief Register for tamper interrupt
00123     *
00124     * @details Pass function pointer which will be called when interrupt occurs.
00125     *
00126     * On Entry:
00127     *
00128     * On Exit:
00129     * Callback registered
00130     *
00131     * @return
00132     */
00133     void regTamperInt(Callback<void()> func);
00134 
00135     /**
00136     * @brief Register for pair button interrupt
00137     *
00138     * @details Pass function pointer which will be called when interrupt occurs.  Note
00139     * there is a hardware switch which can prevent the tamper sensor from pulling the line
00140     * down.  If this switch deasserted there is no way in software to reconfigure this.
00141     *
00142     * On Entry:
00143     *
00144     * On Exit:
00145     * Callback registered
00146     *
00147     * @return
00148     */
00149     void regPairBtnInt(Callback<void()> func);
00150 
00151     ///////////
00152     // Input //
00153     ///////////
00154     /**
00155     * @brief samples current IO including port expanders
00156     *
00157     * @details Samples values and stores them so that IO accessors can execute quicker.
00158     *
00159     * Future:
00160     * Add some detail about how robust the values displayed here are.
00161     * It might be a good idea to add a timestamp for this IO
00162     *
00163     * On Entry:
00164     *
00165     * On Exit:
00166     * Internal class variables are updated or error is returned.
00167     *
00168     * @return CmdResult
00169     */
00170     CmdResult sampleUserSwitches();
00171 
00172     /**
00173     * @brief Returns the current state of the pair button
00174     *
00175     * @details This just simply uses the last sample of the IO to return a bool.
00176     * If the button is depressed than true is returned other wise false
00177     *
00178     * On Entry:
00179     * IO should be sampled recently
00180     *
00181     * On Exit:
00182     *
00183     * @return bool
00184     */
00185     bool isPairBtn();
00186     bool isCCInAlert();
00187 
00188     /**
00189     * @brief Returns the current state of the NO/NC switch
00190     *
00191     * @details This just simply uses the last sample of the IO to return a bool.
00192     * If the switch is in asserted position that the devices is normally closed (NC)
00193     *  therefore false is returned.  Otherwise device is normally open (NO) and therefore
00194     *  returns true.
00195     *
00196     * On Entry:
00197     * IO should be sampled recently
00198     *
00199     * On Exit:
00200     *
00201     * @return bool
00202     */
00203     bool isCCNO();
00204 
00205     void setIsCCNO(bool val);
00206 
00207     void setIsCCNC(bool val) {setIsCCNO(!val);}
00208 
00209     /**
00210     * @brief Returns the current state of the NO/NC switch
00211     *
00212     * @details This just simply uses the last sample of the IO to return a bool.
00213     * If the switch is in asserted position that the devices is normally closed (NC)
00214     *  therefore true is returned.  Otherwise device is normally open (NO) and therefore
00215     *  returns false.
00216     *
00217     * On Entry:
00218     * IO should be sampled recently
00219     *
00220     * On Exit:
00221     *
00222     * @return bool
00223     */
00224     bool isCCNC() {return !isCCNO();}
00225 
00226     /**
00227     * @brief Returns the current state of the Rx/Tx switch
00228     *
00229     * @details This just simply uses the last sample of the IO to return a bool.
00230     * If the switch is in asserted position that the devices is transmitter (Tx)
00231     *  therefore false is returned.  Otherwise device is a receiver(Rx) and therefore
00232     *  returns true.
00233     *
00234     * On Entry:
00235     * IO should be sampled recently
00236     *
00237     * On Exit:
00238     *
00239     * @return bool
00240     */
00241     bool isRx();
00242 
00243     void setIsRx(bool val);
00244 
00245     void setIsTx(bool val) {setIsRx(!val);}
00246 
00247     /**
00248     * @brief Returns the current state of the Rx/Tx switch
00249     *
00250     * @details This just simply uses the last sample of the IO to return a bool.
00251     * If the switch is in asserted position that the devices is transmitter (Tx)
00252     *  therefore true is returned.  Otherwise device is a receiver(Rx) and therefore
00253     *  returns false.
00254     *
00255     * On Entry:
00256     * IO should be sampled recently
00257     *
00258     * On Exit:
00259     *
00260     * @return bool
00261     */
00262     bool isTx() {return !isRx();}
00263 
00264     /**
00265     * @brief Returns the current state of the LoRaWAN switch
00266     *
00267     * @details This just simply uses the last sample of the IO to return a bool.
00268     * If the switch is in asserted position that the device is will report via LoRaWAN
00269     * otherwise the device will use peer-to-peer mode.
00270     *
00271     * On Entry:
00272     * IO should be sampled recently
00273     *
00274     * On Exit:
00275     *
00276     * @return bool
00277     */
00278     bool isLoRaWANMode();
00279 
00280     /**
00281     * @brief Returns the current state of the serial switch
00282     *
00283     * @details This just simply uses the last sample of the IO to return a bool.
00284     * If enabled then the serial chip will be listening for traffic at least in
00285     * the case of a TX.
00286     *
00287     * On Entry:
00288     * IO should be sampled recently
00289     *
00290     * On Exit:
00291     *
00292     * @return bool
00293     */
00294     bool isSerialEnabled();
00295 
00296     /**
00297     * @brief Returns value of a rotary switch (TODO give board location)
00298     *
00299     * On Entry:
00300     * IO should be sampled recently
00301     *
00302     * On Exit:
00303     *
00304     * @return uint8_t
00305     */
00306     uint8_t rotarySwitch1();
00307 
00308     void setRotarySwitch1(uint8_t val);
00309 
00310     /**
00311     * @brief Returns value of a rotary switch (TODO give board location)
00312     *
00313     * On Entry:
00314     * IO should be sampled recently
00315     *
00316     * On Exit:
00317     *
00318     * @return uint8_t
00319     */
00320     uint8_t rotarySwitch2();
00321 
00322 //    void setRotarySwitch2(uint8_t val);
00323 
00324     ////////////
00325     // Output //
00326     ////////////
00327     /**
00328     * @brief Turns LED on
00329     *
00330     * @return CmdResult
00331     */
00332     CmdResult ledOn();
00333 
00334     /**
00335     * @brief Turns LED off
00336     *
00337     * @return CmdResult
00338     */
00339     CmdResult ledOff();
00340 
00341     /**
00342     * @brief Changes state of relay to alert
00343     *
00344     * @details If the configured as normally open then the relay closes and
00345     * vice versa for normally closed.
00346     *
00347     * @return CmdResult
00348     */
00349     CmdResult relayAlert();
00350 
00351     /**
00352     * @brief Changes state of relay to normal
00353     *
00354     * @details If the configured as normally open then the relay opens and
00355     * vice versa for normally closed.
00356     *
00357     * @return CmdResult
00358     */
00359     CmdResult relayNormal();
00360 
00361     /**
00362     * @brief Enables/disables serial chip
00363     *
00364     * @details This controls IO to ON/~OFF pin of RS232 chip.  If transmitting
00365     * ensure serialTx is called as well.
00366     *
00367     * @return CmdResult
00368     */
00369     CmdResult serialRx(bool enable);
00370 
00371     /**
00372     * @brief Controls serial chip transmission
00373     *
00374     * @details This controls IO to Tx_DIS pin of RS232 chip.  Note calling this function will
00375     * also enable serialRx.
00376     *
00377     * @return CmdResult
00378     */
00379     CmdResult serialTx(bool enable);
00380 
00381     CmdResult prepareSleep();
00382     CmdResult exitSleep();
00383 
00384 private:
00385     // Initialized during constructor
00386     NvmBBIOObj mNvmObj;
00387     OneWire mOWMaster;
00388     InterruptIn mCCIn;
00389     InterruptIn mTamper;
00390     InterruptIn mPairBtn;
00391     DigitalOut mLed;
00392     DigitalOut mLrrLed;
00393     DigitalOut mSwitchedIOCtrl;
00394 
00395 //    uint8_t mPortExpanderROM0[8]; // FIXME could probably get rid of
00396     uint8_t mPortExpanderVal0;
00397 //    uint8_t mPortExpanderROM1[8];
00398     uint8_t mPortExpanderVal1;
00399 
00400     DS2408 *mPortEx0;
00401     DS2408 *mPortEx1;
00402 
00403     void enableSwitchedIO () {
00404         mSwitchedIOCtrl = 0; // assert since PMOS
00405     }
00406     void disableSwitchedIO () {
00407         mSwitchedIOCtrl = 1; // deassertted since PMOS
00408     }
00409 
00410     /**
00411     * @brief Reads baseboard information from non-volatile memory (NVM)
00412     *
00413     * @details This data is read from the xDot's internal EEPROM.  This
00414     * method is called from init().  The following
00415     * is stored:
00416     * 1.  Storage code word
00417     * 2.  8-bit Baseboard configuration data
00418     * 3.  8-bit Baseboard revision data
00419     * 4.  16-bit Baseboard serial number
00420     * 5.  2 x 64-bit OneWire ROM port expander addresses
00421     *
00422     * TODO add memory map information
00423     *
00424     * @return CmdResult
00425     */
00426     CmdResult readInfoFromNVM();
00427 
00428     /**
00429     * @brief Stores baseboard information to non-volatile memory (NVM)
00430     *
00431     * @details This method is called during special configuration events like first time boot or factory reset.
00432     *
00433     * TODO add memory map information
00434     *
00435     * @return CmdResult
00436     */
00437     CmdResult writeInfoToNVM();
00438 
00439     /**
00440     * @brief Scans OneWire bus for port expanders and attempts to identify each.
00441     *
00442     * @details For this method to succeed switches must be in factory reset positions.
00443     * Rotary 1 and 2 should be at 0
00444     * DIP Switches should be fully closed
00445     *
00446     * @return CmdResult
00447     */
00448     CmdResult identifyPortExpanders();
00449 
00450     /**
00451      * @brief Forces relay to closed state
00452      *
00453      * @details Note coil assertion timing is done here
00454      *
00455      * @return CmdResult
00456      */
00457     CmdResult closeRelay();
00458 
00459     /**
00460      * @brief Forces relay to open state
00461      *
00462      * @details Note coil assertion timing is done here
00463      *
00464      * @return CmdResult
00465      */
00466     CmdResult openRelay();
00467 };
00468 
00469 
00470 #endif /* BASEBOARDIO_BASEBOARDIO_H_ */