Index: firmware/App/Controllers/Valves.c =================================================================== diff -u -rcee5876d8eb2b2d358a178208c5d8b0362e81a20 -r512ff1bb2b4493965ca5d208349c85c500d3f4d0 --- firmware/App/Controllers/Valves.c (.../Valves.c) (revision cee5876d8eb2b2d358a178208c5d8b0362e81a20) +++ firmware/App/Controllers/Valves.c (.../Valves.c) (revision 512ff1bb2b4493965ca5d208349c85c500d3f4d0) @@ -42,16 +42,16 @@ static U16 valvesStates = INIT_VALVES_STATES; ///< initialize valves states for FPGA to set static U32 valvesStatesPublicationTimerCounter = 0; ///< used to schedule valve state publication to CAN bus +static U16 currentValvesStates = INIT_VALVES_STATES; ///< initialize current valves states static int execValvesCounter = 0; ///< initialize execValves counter static OVERRIDE_U32_T valves[ NUM_OF_VALVES ]; ///< Current valves states 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 // ********** private function prototypes ********** -static BOOL checkValves( VALVES_T valve, VALVE_STATES_T valveState ); ///< check validity of requested valve state -U16 fromIntArrayToU16( int array[ NUM_OF_VALVES ] ); ///< convert array of Int to U16 for statesValves preparation -int *fromU16ToIntArray( U16 vStates ); ///< convert U16 valvesStates to array of Int valve states +static BOOL checkValves( VALVES_T valve, VALVE_STATES_NAMES_T valveState ); ///< check validity of requested valve state +U16 fromU32ArrayToU16( OVERRIDE_U32_T array[ NUM_OF_VALVES ] ); ///< convert array of U32 to U16 for statesValves preparation static void publishValvesStates( void ); static DATA_GET_PROTOTYPE( U32, getPublishValvesStatesInterval ); @@ -85,37 +85,23 @@ if ( execValvesCounter <= 2 ) { - /*int i; + valvesStates = getFPGAValveStates(); // get valves states from FPGA + currentValvesStates = fromU32ArrayToU16( valves ); - for ( i = 0; i < NUM_OF_VALVES; i++ ) + // Check if valves states from FPGA (valvesStates) equals current valves states (valves) + if ( valvesStates != currentValvesStates ) { - valvesStates = getFPGAValveStates(); + valvesStates = currentValvesStates; - if ( OVERRIDE_KEY == valves[ i ].override ) - { - if ( getValveState( valves[ i ].ovData ) != fromU16ToIntArray( valvesStates )[ i ] ) - { - valvesStates = fromIntArrayToU16( valves.ovData ); - } - } - else - { - if ( getValveState( valves[ i ].data ) != fromU16ToIntArray( valvesStates )[ i ] ) - { - valvesStates = fromIntArrayToU16( valves.data ); - } - } - }*/ + // set valves states via FPGA to current valves states + setFPGAValveStates( valvesStates ); + } + else // valvesStates from FPGA match valves' current states + { + execValvesCounter = 0; // reset execValves counter + } - //setFPGAValveStates( fromIntArrayToU16( valves.data ) ); // set valves states via FPGA to current valves states - valvesStates = getFPGAValveStates(); // get valves states from FPGA - - //if ( valvesStates == fromIntArrayToU16( valves.data ) - //{ - // execValvesCounter = 0; // reset execValves counter - //} - - publishValvesStates(); // publish valves states on interval + publishValvesStates(); // publish valves states on interval } else // if requested valve state does not match FPGA-provided valve state three (3) times in row { @@ -132,7 +118,7 @@ * Outputs : none * @return none *************************************************************************/ -static BOOL checkValves( VALVES_T valve, VALVE_STATES_T valveState ) +static BOOL checkValves( VALVES_T valve, VALVE_STATES_NAMES_T valveState ) { BOOL result = FALSE; // initialize checkValves result flag to FALSE @@ -253,44 +239,28 @@ /*********************************************************************//** * @brief - * The fromU16ToIntArray function converts U16 valvesStates to array \n - * of Int valve states. - * @details - * Inputs : none - * Outputs : none - * @return converted Int valve states array - *************************************************************************/ -int *fromU16ToIntArray( U16 vStates ) -{ - int *result;//[ NUM_OF_VALVES ] = INIT_VALVES_STATES; // initialize result array to default (de-energized) valves states - int i; - - for ( i = 0; i < NUM_OF_VALVES; i++) - { - result[ i ] = vStates % 2; - vStates /= 2; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The fromIntArrayToU16 function converts an array of Int valve states \n + * The fromU32ArrayToU16 function converts an array of U32 valve states \n * to U16 valvesStates that can be passed to FPGA for setting valves. * @details * Inputs : none * Outputs : none * @return converted U16 valvesStates *************************************************************************/ -U16 fromIntArrayToU16( int array[ NUM_OF_VALVES ] ) +U16 fromU32ArrayToU16( OVERRIDE_U32_T array[ NUM_OF_VALVES ] ) { U16 result = 0; // initialize result flag to 0 int i; for ( i = 0; i < NUM_OF_VALVES; i++) { - result += array[ i ] * pow( 2, i ); + if ( OVERRIDE_KEY == array[ i ].override ) + { + result += array[ i ].ovData * pow( 2, i ); + } + else + { + result += array[ i ].data * pow( 2, i ); + } } return result; @@ -305,7 +275,7 @@ * Outputs : none * @return converted U32 vState *************************************************************************/ -U32 convertToValveState(VALVE_STATES_T valveState) +U32 convertToValveState(VALVE_STATES_NAMES_T valveState) { U32 vState = 0; // initialize state to 0 @@ -374,21 +344,13 @@ * Outputs : none * @return TRUE if new valve state is set for given valve *************************************************************************/ -BOOL setValveState( VALVES_T valve, VALVE_STATES_T valveState ) +BOOL setValveState( VALVES_T valve, VALVE_STATES_NAMES_T valveState ) { BOOL result = FALSE; // initialize result flag to FALSE if ( checkValves ( valve, valveState ) ) { - if ( OVERRIDE_KEY == valves[ valve ].override ) - { - valves[ valve ].ovData = convertToValveState( valveState ); - } - else - { - valves[ valve ].data = convertToValveState( valveState ); - } - + valves[ valve ].data = convertToValveState( valveState ); result = TRUE; } else