/************************************************************************** * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file CPLD.c * * @author (last) Sean Nash * @date (last) 04-Dec-2019 * * @author (original) Dara Navaei * @date (original) 05-Nov-2019 * ***************************************************************************/ #include "gio.h" #include "mibspi.h" #include "AlarmLamp.h" #include "Buttons.h" #include "WatchdogMgmt.h" #include "CPLD.h" /** * @addtogroup CPLD * @{ */ // ********** private definitions ********** // GIO port A pin assignments for pins connected to CPLD #define OFF_BUTTON_GIO_PORT_PIN 0U ///< GPIO pin ID on port A for off button input signal. #define STOP_BUTTON_GIO_PORT_PIN 1U ///< GPIO pin ID on port A for stop button input signal. // GIO port B pin assignments for pins connected to CPLD #define OFF_REQUEST_GIO_PORT_PIN 0U ///< GPIO pin ID on port B for power off request output signal. #define WD_PET_GIO_PORT_PIN 1U ///< GPIO pin ID on port B for watchdog pet output signal. #define WD_EXP_GIO_PORT_PIN 2U ///< GPIO pin ID on port B for watchdog expired input signal. // MIBSPI5 port pin assignments for pins connected to CPLD #define GREEN_SPI5_PORT_MASK 0x00000200 ///< (CLK - re-purposed as output GPIO) for green alarm lamp signal. #define BLUE_SPI5_PORT_MASK 0x00000400 ///< (SIMO[0] - re-purposed as output GPIO) for blue alarm lamp signal. #define RED_SPI5_PORT_MASK 0x00000800 ///< (SOMI[0] - re-purposed as output GPIO) for red alarm lamp signal. // CPLD pin I/O macros #define GET_OFF() (PIN_SIGNAL_STATE_T)(gioGetBit(gioPORTA, OFF_BUTTON_GIO_PORT_PIN)) ///< Macro to get off button signal state. #define GET_STOP() (PIN_SIGNAL_STATE_T)(gioGetBit(gioPORTA, STOP_BUTTON_GIO_PORT_PIN)) ///< Macro to get stop button signal state. #define GET_WD_EXP() (PIN_SIGNAL_STATE_T)(gioGetBit(gioPORTB, WD_EXP_GIO_PORT_PIN)) ///< Macro to get watchdog expired signal state. #define TGL_WD_PET() gioToggleBit( gioPORTB, WD_PET_GIO_PORT_PIN ) ///< Macro to toggle watchdog pet signal state. #define TGL_OFF_REQ() gioToggleBit( gioPORTB, OFF_REQUEST_GIO_PORT_PIN) ///< Macro to toggle power off request signal state. #define SET_WD_PET() gioSetBit( gioPORTB, WD_PET_GIO_PORT_PIN, PIN_SIGNAL_HIGH ) ///< Macro to set watchdog pet signal state high. #define SET_OFF_REQ() gioSetBit( gioPORTB, OFF_REQUEST_GIO_PORT_PIN, PIN_SIGNAL_HIGH ) ///< Macro to set power off request signal state high. #define SET_GREEN() {mibspiREG5->PC3 |= GREEN_SPI5_PORT_MASK;} ///< Macro to set green alarm lamp signal state high. #define SET_BLUE() {mibspiREG5->PC3 |= BLUE_SPI5_PORT_MASK;} ///< Macro to set blue alarm lamp signal state high. #define SET_RED() {mibspiREG5->PC3 |= RED_SPI5_PORT_MASK;} ///< Macro to set red alarm lamp signal state high. #define CLR_WD_PET() gioSetBit( gioPORTB, WD_PET_GIO_PORT_PIN, PIN_SIGNAL_LOW ) ///< Macro to set watchdog pet signal state low. #define CLR_OFF_REQ() gioSetBit( gioPORTB, OFF_REQUEST_GIO_PORT_PIN, PIN_SIGNAL_LOW ) ///< Macro to set power off request signal state low. #define CLR_GREEN() {mibspiREG5->PC3 &= ~GREEN_SPI5_PORT_MASK;} ///< Macro to set green alarm lamp signal state low. #define CLR_BLUE() {mibspiREG5->PC3 &= ~BLUE_SPI5_PORT_MASK;} ///< Macro to set blue alarm lamp signal state low. #define CLR_RED() {mibspiREG5->PC3 &= ~RED_SPI5_PORT_MASK;} ///< Macro to set red alarm lamp signal state low. #ifdef RM46_EVAL_BOARD_TARGET // for RM46 eval board, user button B uses the MIBSPI1_nCS[4] pin, so need to re-purpose that pin as GPIO to see the button #define USER_BUTTON_MASK 0x00000010 // (nCS[4] #define GET_USER_BUTTON() (PIN_SIGNAL_STATE_T)(((mibspiREG1->PC2 & USER_BUTTON_MASK) == 0 ? PIN_SIGNAL_LOW : PIN_SIGNAL_HIGH)) PIN_SIGNAL_STATE_T getUserButtonState( void ) { PIN_SIGNAL_STATE_T result = GET_USER_BUTTON(); return result; } // for RM46 eval board, user LED A uses the same GPIO pin that CPLD uses for watchdog pet. Had to disable watchdog pet to use as LED. void setUserLED( BOOL on ) { if ( on == TRUE ) { SET_WD_PET(); } else { CLR_WD_PET(); } } #endif /*********************************************************************//** * @brief * The initCPLD function initializes the CPLD module. All outputs to CPLD * (watchdog pet, off request, and alarm lamp LEDs) are set to low. * @details * Inputs : none * Outputs : CPLD module signal outputs set to initial states. * @return none *************************************************************************/ void initCPLD( void ) { // initialize watchdog pet output low (inactive) CLR_WD_PET(); // initialize power off request output low (inactive) CLR_OFF_REQ(); // initialize alarm lamp color LED outputs low (off) CLR_GREEN(); CLR_RED(); CLR_BLUE(); } /*********************************************************************//** * @brief * The toggleCPLDWatchdog function toggles the watchdog pet signal to CPLD. * @details * Inputs : none * Outputs : watchdog pet signal toggled. * @return none *************************************************************************/ void toggleCPLDWatchdog( void ) { TGL_WD_PET(); } /*********************************************************************//** * @brief * The getCPLDWatchdogExpired function determines the current signal level * on the watchdog expired pin from the CPLD. * @details * Inputs : Signal from CPLD on watchdog expired pin. * Outputs : none * @return level (LOW or HIGH) *************************************************************************/ PIN_SIGNAL_STATE_T getCPLDWatchdogExpired( void ) { PIN_SIGNAL_STATE_T level = GET_WD_EXP(); return level; } /*********************************************************************//** * @brief * The setCPLDLampGreen function sets the alarm lamp green signal to CPLD * to given level. * @details * Inputs : none * Outputs : alarm lamp green signal set to given level. * @param level LOW or HIGH * @return none *************************************************************************/ void setCPLDLampGreen( PIN_SIGNAL_STATE_T level ) { if ( level == PIN_SIGNAL_HIGH ) { SET_GREEN(); } else { CLR_GREEN(); } } /*********************************************************************//** * @brief * The setCPLDLampBlue function sets the alarm lamp blue signal to CPLD * to given level. * @details * Inputs : none * Outputs : alarm lamp blue signal set to given level. * @param level LOW or HIGH * @return none *************************************************************************/ void setCPLDLampBlue( PIN_SIGNAL_STATE_T level ) { if ( level == PIN_SIGNAL_HIGH ) { SET_BLUE(); } else { CLR_BLUE(); } } /*********************************************************************//** * @brief * The setCPLDLampRed function sets the alarm lamp red signal to CPLD * to given level. * @details * Inputs : none * Outputs : alarm lamp red signal set to given level. * @param level LOW or HIGH * @return none *************************************************************************/ void setCPLDLampRed( PIN_SIGNAL_STATE_T level ) { if ( level == PIN_SIGNAL_HIGH ) { SET_RED(); } else { CLR_RED(); } } /*********************************************************************//** * @brief * The toggleCPLDOffRequest function toggles the off request signal to CPLD. * The off request signal must be toggled 4 times, once every 50 ms, in order * for the CPLD to accept the off request sequence and initiate system power * down. * @details * Inputs : none * Outputs : off request signal toggled. * @return none *************************************************************************/ void toggleCPLDOffRequest( void ) { TGL_OFF_REQ(); } /*********************************************************************//** * @brief * The getCPLDOffButton function determines the current signal level * on the off button pin from the CPLD. * @details * Inputs : Signal from CPLD on off button pin. * Outputs : none * @return level (LOW or HIGH) *************************************************************************/ PIN_SIGNAL_STATE_T getCPLDOffButton( void ) { PIN_SIGNAL_STATE_T level = GET_OFF(); #ifdef RM46_EVAL_BOARD_TARGET // temporary test code for eval board - buttons have opposite polarity level = GET_TOGGLE( level, PIN_SIGNAL_LOW, PIN_SIGNAL_LOW ); #endif return level; } /*********************************************************************//** * @brief * The getCPLDStopButton function determines the current signal level * on the stop button pin from the CPLD. * @details * Inputs : Signal from CPLD on off button pin. * Outputs : none * @return level (LOW or HIGH) *************************************************************************/ PIN_SIGNAL_STATE_T getCPLDStopButton( void ) { PIN_SIGNAL_STATE_T level = GET_STOP(); #ifdef RM46_EVAL_BOARD_TARGET // temporary test code for eval board - buttons have opposite polarity level = GET_TOGGLE( level, PIN_SIGNAL_LOW, PIN_SIGNAL_LOW ); #endif return level; } /**@}*/