Index: firmware/App/Controllers/Valves.c =================================================================== diff -u -rd4d21b42ee8bd292a6d9f7966fe37abe0271dfda -rce66580e076bffa157868ff7e422556f78a95cac --- firmware/App/Controllers/Valves.c (.../Valves.c) (revision d4d21b42ee8bd292a6d9f7966fe37abe0271dfda) +++ firmware/App/Controllers/Valves.c (.../Valves.c) (revision ce66580e076bffa157868ff7e422556f78a95cac) @@ -18,72 +18,73 @@ #include // For memcpy #include "FpgaRO.h" -#include "MessageSupport.h" -#include "Messaging.h" +#include "MessageSupport.h" +#include "Messaging.h" #include "TaskPriority.h" #include "Timers.h" #include "Valves.h" - -/** - * @addtogroup Valves - * @{ - */ - + +/** + * @addtogroup Valves + * @{ + */ + // ********** private definitions ********** #define DEENERGIZED 0 ///< 0 for de-energized valve. #define ENERGIZED 1 ///< 1 for energized valve. -#define ALL_VALVES_DEENERGIZED 0x0000 ///< 0 in U16 bit field for all valves. - -#define MAX_VALVE_STATE_MISMATCH_TIMER_COUNT (100 / TASK_PRIORITY_INTERVAL ) ///< Maximum time commanded valves state can fail to match read back valve states in a row. - -#define VALVES_STATE_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Interval ( ms / task time ) at which valves states are published on CAN bus. +#define ALL_VALVES_DEENERGIZED 0x0000 ///< 0 in U16 bit field for all valves. + +#define MAX_VALVE_STATE_MISMATCH_TIMER_COUNT (100 / TASK_PRIORITY_INTERVAL ) ///< Maximum time commanded valves state can fail to match read back valve states in a row. + +#define VALVES_STATE_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Interval ( ms / task time ) at which valves states are published on CAN bus. #define DATA_PUBLISH_COUNTER_START_COUNT 50 ///< Data publish counter start count. /// Bits associated with RO valves when interfacing with FPGA to command a valve position. 1=energized, 0=de-energized. -static const U08 ValveBits[ NUM_OF_VALVES ] = { - 0x01, - 0x02, - 0x04, - 0x08, - 0x10, - 0x20, - 0x40, - 0x80 +static const U16 ValveBits[ NUM_OF_VALVES ] = { + 0x0001, + 0x0002, + 0x0004, + 0x0008, + 0x0010, + 0x0020, + 0x0040, + 0x0080, + 0x0100 }; -// ********** private data ********** - -static U32 valvesStatesPublicationTimerCounter; ///< Timer counter used to schedule valve state publication to CAN bus. +// ********** private data ********** + +static U32 valvesStatesPublicationTimerCounter; ///< Timer counter used to schedule valve state publication to CAN bus. static U16 commandedValvesStates; ///< Initialize commanded valves states bit field. static U32 valveStateMismatchTimerCounter; ///< Initialize valve state mismatch timer. static U32 pendingValveStateChanges[ NUM_OF_VALVES ]; ///< Delayed (pending) valve state changes. -static U32 pendingValveStateChangeCountDowns[ NUM_OF_VALVES ]; ///< Delayed (pending) valve state change count down timers (in task intervals). +static U32 pendingValveStateChangeCountDowns[ NUM_OF_VALVES ]; ///< Delayed (pending) valve state change count down timers (in task intervals). static OVERRIDE_U32_T valveStates[ NUM_OF_VALVES ]; ///< Currently commanded valves states. -static OVERRIDE_U32_T valveSensedStates[ NUM_OF_VALVES ]; ///< Valve sensed states override. -static OVERRIDE_U32_T valvesStatesPublishInterval; ///< Interval (in ms/task interval) at which to publish valves state to CAN bus. +static OVERRIDE_U32_T valveSensedStates[ NUM_OF_VALVES ]; ///< Valve sensed states override. +static OVERRIDE_U32_T valvesStatesPublishInterval; ///< Interval (in ms/task interval) at which to publish valves state to CAN bus. -// ********** private function prototypes ********** +// ********** private function prototypes ********** static void publishValvesStates( void ); -static void readCommandedValveStates( void ); +static void updateValveStates( U16 readValveStates ); static U32 convertValveStateNameToValveState( VALVE_STATE_NAMES_T valveStateName ); static U32 getValveState( U32 valveID ); - -/*********************************************************************//** - * @brief - * The initValves function initializes the Valves unit. - * @details \b Inputs: none - * @details \b Outputs: Valves unit initialized - * @return none - *************************************************************************/ -void initValves( void ) -{ - U32 valve; +/*********************************************************************//** + * @brief + * The initValves function initializes the Valves unit. + * @details \b Inputs: none + * @details \b Outputs: Valves unit initialized + * @return none + *************************************************************************/ +void initValves( void ) +{ + VALVES_T valve; + // initialize valve states - for ( valve = 0; valve < NUM_OF_VALVES; valve++ ) + for ( valve = FIRST_VALVE; valve < NUM_OF_VALVES; valve++ ) { valveStates[ valve ].data = DEENERGIZED; valveStates[ valve ].ovInitData = DEENERGIZED; @@ -100,45 +101,45 @@ // initialize time intervals and timer counters valveStateMismatchTimerCounter = 0; valvesStatesPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; // reset valves states publication timer - valvesStatesPublishInterval.data = VALVES_STATE_PUB_INTERVAL; + valvesStatesPublishInterval.data = VALVES_STATE_PUB_INTERVAL; valvesStatesPublishInterval.ovData = VALVES_STATE_PUB_INTERVAL; valvesStatesPublishInterval.ovInitData = 0; valvesStatesPublishInterval.override = OVERRIDE_RESET; - // initially set all valves to de-energized state via FPGA - readCommandedValveStates(); + // initially set all valves to de-energized state via FPGA + updateValveStates( ALL_VALVES_DEENERGIZED ); setFPGAValveStates( commandedValvesStates ); -} - -/*********************************************************************//** - * @brief - * The execValves function executes the valves controller. +} + +/*********************************************************************//** + * @brief + * The execValves function executes the valves controller. * @details \b Inputs: valvesStates, valveStateMismatchCounter * pendingValveStateChangeCountDowns, commandedValvesStates * @details \b Outputs: valvesStates, valveStateMismatchCounter * pendingValveStateChanges, valve controls * @details \b Alarm: ALARM_ID_RO_VALVE_CONTROL_FAILURE when FPGA read back - * valve state mismatches with the commanded valve state after defined time (100ms). - * @return none - *************************************************************************/ -void execValves( void ) + * valve state mismatches with the commanded valve state after defined time (100ms). + * @return none + *************************************************************************/ +void execValves( void ) { U32 valve; U16 readValvesStates = getFPGAValveStates(); // get valves states from FPGA - // Verify read back FPGA valve states match last commanded valve states - if ( readValvesStates != commandedValvesStates ) - { - valveStateMismatchTimerCounter++; // increment valve state mismatch counter by 1 - if ( valveStateMismatchTimerCounter > MAX_VALVE_STATE_MISMATCH_TIMER_COUNT ) + // Verify read back FPGA valve states match last commanded valve states + if ( readValvesStates != commandedValvesStates ) + { + valveStateMismatchTimerCounter++; // increment valve state mismatch counter by 1 + if ( valveStateMismatchTimerCounter > MAX_VALVE_STATE_MISMATCH_TIMER_COUNT ) { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_VALVE_CONTROL_FAILURE, (U32)readValvesStates, (U32)commandedValvesStates ) - } - } - else - { - valveStateMismatchTimerCounter = 0; + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_VALVE_CONTROL_FAILURE, (U32)readValvesStates, (U32)commandedValvesStates ) + } } + else + { + valveStateMismatchTimerCounter = 0; + } // Handle pending delayed valve state changes for ( valve = 0; valve < NUM_OF_VALVES; valve++ ) @@ -152,38 +153,40 @@ pendingValveStateChanges[ valve ] = DEENERGIZED; } } - } - - // Set valves states (via FPGA) to currently commanded states - readCommandedValveStates(); + } + + // Set valves states (via FPGA) to currently commanded states + updateValveStates( readValvesStates ); setFPGAValveStates( commandedValvesStates ); - // Publish valve states on interval - publishValvesStates(); -} + // Publish valve states on interval + publishValvesStates(); +} /*********************************************************************//** * @brief - * The readCommandedValveStates function gets the hydraulics, Balancing chamber - * and Ultrafiltration valvesStates that can be passed to FPGA for setting valves. + * The updateValveStates function updates the sensed valve states from FPGA + * and sends latest commanded valve states to FPGA. * @details \b Inputs: valveStates[], ValveBits[] - * @details \b Outputs: commandedValvesStates + * @details \b Outputs: commandedValvesStates, valveSensedStates[] + * @param readValveStates bits indicating read valve states * @return none *************************************************************************/ -static void readCommandedValveStates( void ) +static void updateValveStates( U16 readValveStates ) { U32 valve; - // Initiliaze before updating commanded states + // Initialize before updating commanded states commandedValvesStates = ALL_VALVES_DEENERGIZED; - // flag RO valves that are currently commanded to be energized + // Update sensed valve states from FPGA and commanded valve state bits to FPGA for ( valve = FIRST_VALVE; valve < NUM_OF_VALVES; valve++ ) { + valveSensedStates[ valve ].data = ( ( readValveStates & ValveBits[ valve ] ) != 0 ? VALVE_STATE_OPEN : VALVE_STATE_CLOSED ); commandedValvesStates |= ( getValveState( valve ) == ENERGIZED ? ValveBits[ valve ] : 0 ); } } - + /*********************************************************************//** * @brief * The convertValveStateNameToValveState function converts valve state \n @@ -211,7 +214,7 @@ } return vState; -} +} /*********************************************************************//** * @brief @@ -294,39 +297,34 @@ return result; } - -/*********************************************************************//** - * @brief + +/*********************************************************************//** + * @brief * The getValveStateName function gets the current valve state enum for a - * given valve. - * @details \b Inputs: valveStates[] + * given valve. + * @details \b Inputs: valveStates[] * @details \b Outputs: none - * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT when invalid valve ID given. - * @param valveID ID of valve to get state for - * @return the current valve state for given valve in enum - *************************************************************************/ -VALVE_STATE_NAMES_T getValveStateName( VALVES_T valveID ) + * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT when invalid valve ID given. + * @param valveID ID of valve to get state for + * @return the current valve state for given valve in enum + *************************************************************************/ +VALVE_STATE_NAMES_T getValveStateName( VALVES_T valveID ) { // Initialized per CppCheck. VALVE_STATE_NAMES_T name = NUM_OF_VALVE_STATES; - - if ( valveID < NUM_OF_VALVES ) + + if ( valveID < NUM_OF_VALVES ) { - U32 valveState = getU32OverrideValue( &valveStates[ valveID ] ); + U32 valveState = getU32OverrideValue( &valveSensedStates[ valveID ] ); - if ( OVERRIDE_KEY == valveSensedStates[ valveID ].override ) - { - valveState = valveSensedStates[ valveID ].ovData; - } - name = ( ENERGIZED == valveState ? VALVE_STATE_OPEN : VALVE_STATE_CLOSED ); - } - else - { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_VALVES_INVALID_VALVE_ID3, valveID ) - } - - return name; + } + else + { + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_VALVES_INVALID_VALVE_ID3, valveID ) + } + + return name; } /*********************************************************************//** @@ -353,57 +351,57 @@ return valveState; } - -/*********************************************************************//** - * @brief - * The publishValvesStates function publishes RO valves states at the set interval. - * @details \b Inputs: valvesStatesPublicationTimerCounter + +/*********************************************************************//** + * @brief + * The publishValvesStates function publishes RO valves states at the set interval. + * @details \b Inputs: valvesStatesPublicationTimerCounter * @details \b Outputs: valvesStatesPublicationTimerCounter - * @details \b Message \b Sent: MSG_ID_RO_VALVES_STATES_DATA to publish FPGA read valve states. - * @return none - *************************************************************************/ -static void publishValvesStates( void ) -{ - // publish valve state on interval - if ( ++valvesStatesPublicationTimerCounter >= getU32OverrideValue( &valvesStatesPublishInterval ) ) + * @details \b Message \b Sent: MSG_ID_RO_VALVES_STATES_DATA to publish FPGA read valve states. + * @return none + *************************************************************************/ +static void publishValvesStates( void ) +{ + // publish valve state on interval + if ( ++valvesStatesPublicationTimerCounter >= getU32OverrideValue( &valvesStatesPublishInterval ) ) { RO_VALVES_DATA_T data; U32 valve; - data.valvesStatus = getFPGAValveStates(); + data.valvesCmdState = commandedValvesStates; for ( valve = 0; valve < NUM_OF_VALVES; valve++ ) { data.valvesSensedState[ valve ] = (U08)getValveStateName( (VALVES_T)valve ); - } + } broadcastData( MSG_ID_RO_VALVES_STATES_DATA, COMM_BUFFER_OUT_CAN_RO_BROADCAST, (U08*)&data, sizeof( RO_VALVES_DATA_T ) ); - - valvesStatesPublicationTimerCounter = 0; - } + + valvesStatesPublicationTimerCounter = 0; + } } - -/************************************************************************* - * TEST SUPPORT FUNCTIONS - *************************************************************************/ +/************************************************************************* + * TEST SUPPORT FUNCTIONS + *************************************************************************/ -/*********************************************************************//** - * @brief - * The testValvesStatesPublishIntervalOverride function overrides the - * valves states publish interval. - * @details \b Inputs: none - * @details \b Outputs: valvesStatesPublishInterval + +/*********************************************************************//** + * @brief + * The testValvesStatesPublishIntervalOverride function overrides the + * valves states publish interval. + * @details \b Inputs: none + * @details \b Outputs: valvesStatesPublishInterval * @param message Override message from Dialin which includes the value - * that override valves states publish interval with (in ms) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testValvesStatesPublishIntervalOverride( MESSAGE_T *message ) -{ - BOOL result = u32BroadcastIntervalOverride( message, &valvesStatesPublishInterval, TASK_PRIORITY_INTERVAL ); - - return result; + * that override valves states publish interval with (in ms) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testValvesStatesPublishIntervalOverride( MESSAGE_T *message ) +{ + BOOL result = u32BroadcastIntervalOverride( message, &valvesStatesPublishInterval, TASK_PRIORITY_INTERVAL ); + + return result; } /*********************************************************************//** @@ -421,7 +419,7 @@ BOOL result = u32ArrayOverride( message, &valveStates[0], NUM_OF_VALVES - 1, 0, NUM_OF_VALVE_STATES - 1 ); return result; -} +} /*********************************************************************//** * @brief @@ -440,4 +438,4 @@ return result; } -/**@}*/ +/**@}*/