/**********************************************************************//** * * Copyright (c) 2020-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 Valves.c * * @date 21-Apr-2020 * @author P. Montazemi * * @brief Valves source file. * **************************************************************************/ #include "AlarmMgmt.h" #include "FPGA.h" #include "OperationModes.h" #include "SystemCommMessages.h" #include "TaskPriority.h" #include "Timers.h" #include "Valves.h" /** * @addtogroup DGValves * @{ */ // ********** private definitions ********** #define DEENERGIZED 0 #define ENERGIZED 1 #define INIT_VALVES_STATES 0b0000000000000000 /// Default publication interval for valve states #define VALVES_STATE_PUB_INTERVAL ( 500 / TASK_PRIORITY_INTERVAL ) ///< interval (ms/task time) at which the valves state is published on the CAN bus /// Defined states for the valves self test state machine typedef enum Valves_Self_Test_States { VALVE_SELF_TEST_STATE_START = 0, ///< Self test start state VALVE_TEST_STATE_IN_PROGRESS, ///< Self test in progress state VALVE_TEST_STATE_COMPLETE, ///< Self test completed state NUM_OF_VALVE_SELF_TEST_STATES ///< Number of valve self test states } VALVE_SELF_TEST_STATE_T; // ********** private data ********** static VALVE_STATES_T valvesStates = INIT_VALVES_STATES; ///< current state of valve driver state machine static U32 valvesStatesPublicationTimerCounter = 0; ///< used to schedule valve state publication to CAN bus static OVERRIDE_U32_T valvesStatesPublishInterval = { VALVES_STATE_PUB_INTERVAL, VALVES_STATE_PUB_INTERVAL, 0, 0 }; ///< interval (in ms/task interval) at which to publish valves state to CAN bus static OVERRIDE_F32_T valves[ NUM_OF_VALVES ]; ///< Obtained state from valves static VALVE_SELF_TEST_STATE_T valvesSelfTestState = VALVE_SELF_TEST_STATE_START; ///< current valve self test state static U32 valvesSelfTestTimerCount = 0; ///< timer counter for valve self test // ********** private function prototypes ********** static void checkValves( void ); static void publishValvesStates( void ); static DATA_GET_PROTOTYPE( U32, getPublishValvesStatesInterval ); /**@}*/ /*********************************************************************//** * @brief * The initValves function initializes the Valves module. * @details * Inputs : none * Outputs : Valves module initialized. * @return none *************************************************************************/ void initValves( void ) { setFPGAValveStates( INIT_VALVES_STATES ); valvesStatesPublicationTimerCounter = 0; valvesSelfTestTimerCount = 0; } /*********************************************************************//** * @brief * The execValves function executes the valve driver. * @details * Inputs : valvesStates * Outputs : valvesStates * @return none *************************************************************************/ void execValves( void ) { setFPGAValveStates( valvesStates ); // publish valves states on interval publishValvesStates(); } /*********************************************************************//** * @brief * The checkValveState function checks the validity of the requested valve state. * @details * Inputs : valveID, valveState * Outputs : none * @return none *************************************************************************/ static void checkValves( void ) { /* TODO: compare the requested valve state to the allowable valves \n * states for a given valve; * - if checks, return True * - if not, return false */ return; } /*********************************************************************//** * @brief * The getPublishValveStateInterval function gets the valves states \n * publication interval. * @details * Inputs : valvesStatesPublishInterval * Outputs : none * @return the current valves states publication interval (in task intervals). *************************************************************************/ U32 getPublishValvesStatesInterval( void ) { U32 result = valvesStatesPublishInterval.data; if ( OVERRIDE_KEY == valvesStatesPublishInterval.override ) { result = valvesStatesPublishInterval.ovData; } return result; } /*********************************************************************//** * @brief * The getValveState function gets the current valve state for given valve. * @details * Inputs : valveState * Outputs : none * @return the current valve state for given valve. *************************************************************************/ F32 getValveState( U32 valve ) { BOOL valveState = DEENERGIZED; if ( valve < NUM_OF_VALVES ) { if ( OVERRIDE_KEY == valves[ valve ].override ) { valveState = valves[ valve ].ovData; } else { valveState = valves[ valve ].data; } } else { activateAlarmNoData( ALARM_ID_VALVE_STATE_ERROR ); } return valveState; } /*********************************************************************//** * * @brief * The publishValvesStates function publishes DG valves states at the \n * set interval. * @details * Inputs : valvesStatesPublicationTimerCounter * Outputs : DG valves states are published to CAN bus * @return none *************************************************************************/ static void publishValvesStates( void ) { // publish valve state on interval if ( ++valvesStatesPublicationTimerCounter >= getPublishValvesStatesInterval() ) { broadcastValvesStates( getFPGAValveStates() ); valvesStatesPublicationTimerCounter = 0; } } /*********************************************************************//** * @brief * The execValvesTest function executes the state machine for the \n * valves self test. * @details * Inputs : none * Outputs : none * @return the current state of the valves self test. *************************************************************************/ SELF_TEST_STATUS_T execValvesTest( void ) { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_FAILED; // TODO - implement self test(s) return result; } /**@}*/ /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testSetValvesStatesPublishIntervalOverride function overrides the \n * valves states publish interval. * @details * Inputs : none * Outputs : valvesStatesPublishInterval * @param value : override valves states publish interval with (in ms) * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetValvesStatesPublishIntervalOverride( U32 value ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { U32 intvl = value / TASK_PRIORITY_INTERVAL; result = TRUE; valvesStatesPublishInterval.ovData = intvl; valvesStatesPublishInterval.override = OVERRIDE_KEY; } return result; } /*********************************************************************//** * @brief * The testResetValvesStatesPublishIntervalOverride function resets the override \n * of the valves states publish interval. * @details * Inputs : none * Outputs : valvesStatesPublishInterval * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetValvesStatesPublishIntervalOverride( void ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { result = TRUE; valvesStatesPublishInterval.override = OVERRIDE_RESET; valvesStatesPublishInterval.ovData = valvesStatesPublishInterval.ovInitData; } return result; } /*********************************************************************//** * @brief * The testSetValveStateOverride function overrides the value of the \n * specified valve with a given value. * Inputs : none * Outputs : valves[] * @param valve : ID of valve to override for * @param value : override value for the given valve ID * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetValveStateOverride( U32 valve, F32 value ) { BOOL result = FALSE; if ( valve < NUM_OF_VALVES ) { if ( TRUE == isTestingActivated() ) { result = TRUE; valves[ valve ].ovData = value; valves[ valve ].override = OVERRIDE_KEY; } } return result; } /*********************************************************************//** * @brief * The testResetValveStateOverride function resets the override of the \n * specified valve. * @details * Inputs : none * Outputs : valves[] * @param value : ID of valve to reset override state for * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testResetValveStateOverride( U32 valve ) { BOOL result = FALSE; if ( valve < NUM_OF_VALVES ) { if ( TRUE == isTestingActivated() ) { result = TRUE; valves[ valve ].override = OVERRIDE_RESET; valves[ valve ].ovData = valves[ valve ].ovInitData; } } return result; }