/************************************************************************** * * Copyright (c) 2019-2024 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) 03-Apr-2023 * * @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. #define AC_PRESENT_SPI5_PORT_MASK 0x00000001 ///< (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 GET_AC_PRESENT() (PIN_SIGNAL_STATE_T)(mibspiREG5->PC2 & AC_PRESENT_SPI5_PORT_MASK) ///< Macro to get A/C power present 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. /*********************************************************************//** * @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 * @details 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 * @details 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. * @details 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 getCPLDACPowerLossDetected function determines whether the CPLD is * reporting A/C power has been lost. * @details Inputs: Signal from CPLD on A/C presence pin. * @details Outputs: none * @return TRUE if A/C power loss is detected, FALSE if not *************************************************************************/ BOOL getCPLDACPowerLossDetected( void ) { PIN_SIGNAL_STATE_T acLevel = GET_AC_PRESENT(); BOOL acLost = ( PIN_SIGNAL_HIGH == acLevel ? FALSE : TRUE ); return acLost; } /*********************************************************************//** * @brief * The setCPLDLampGreen function sets the alarm lamp green signal to CPLD * to given level. * @details Inputs: none * @details 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 * @details 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 * @details 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 * @details 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. * @details Outputs: none * @return level (LOW or HIGH) *************************************************************************/ PIN_SIGNAL_STATE_T getCPLDOffButton( void ) { PIN_SIGNAL_STATE_T level = GET_OFF(); 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. * @details Outputs: none * @return level (LOW or HIGH) *************************************************************************/ PIN_SIGNAL_STATE_T getCPLDStopButton( void ) { PIN_SIGNAL_STATE_T level = GET_STOP(); return level; } /**@}*/