Index: firmware/App/Controllers/Switches.c =================================================================== diff -u -r7da76d7131d824ab15f58857eeeedcf128e57391 -r2e56d2d983becc4b15bd296d583e046e061a5719 --- firmware/App/Controllers/Switches.c (.../Switches.c) (revision 7da76d7131d824ab15f58857eeeedcf128e57391) +++ firmware/App/Controllers/Switches.c (.../Switches.c) (revision 2e56d2d983becc4b15bd296d583e046e061a5719) @@ -3,6 +3,7 @@ #include "Switches.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" +#include "Timers.h" /** * @addtogroup Switches @@ -11,17 +12,22 @@ // ********** private definitions ********** -#define SWITCHES_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the switches data is published on the CAN bus. -#define SWITCHES_FPGA_STATUS_CHECK_INTERVAL ( MS_PER_SECOND / ( 5 * TASK_GENERAL_INTERVAL ) ) ///< Switches FPGA status check interval. +#define SWITCHES_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the switches data is published on the CAN bus. +#define SWITCHES_DEBOUNCE_TIME_MS ( MS_PER_SECOND / 4 ) ///< Switches FPGA status check interval. +/// Switch status structure +typedef struct +{ + OVERRIDE_U32_T status; ///< Switch status. + U32 debounceStartTime; ///< Debounce start time. +} SWITCH_STATUS_T; + // ********** private data ********** -static U32 switchesDataPublicationCounter; ///< Switches data publication counter. +static U32 switchesDataPublicationCounter; ///< Switches data publication counter. static OVERRIDE_U32_T switchesDataPublishInterval = { SWITCHES_DATA_PUB_INTERVAL, - SWITCHES_DATA_PUB_INTERVAL, - 0, 0 }; ///< Interval (in ms) at which to publish switches data to CAN bus. -static OVERRIDE_U32_T switchStatus[ NUM_OF_DOORS_AND_CAPS ]; ///< Switches status array. -static U32 switchesFPGAStatusCheckCounter; ///< Switches FPGA status check counter. + SWITCHES_DATA_PUB_INTERVAL, 0, 0 }; ///< Interval (in ms) at which to publish switches data to CAN bus. +static SWITCH_STATUS_T switchesStatus[ NUM_OF_DOORS_AND_CAPS ]; ///< Switches status array. // ********** private function prototypes ********** @@ -40,32 +46,68 @@ U08 i; switchesDataPublicationCounter = 0; - switchesFPGAStatusCheckCounter = 0; // Initialize all the switches for ( i = 0; i < NUM_OF_DOORS_AND_CAPS; i++ ) { - switchStatus[ i ].data = (U32)STATE_CLOSED; - switchStatus[ i ].ovData = (U32)STATE_CLOSED; - switchStatus[ i ].ovInitData = (U32)STATE_CLOSED; - switchStatus[ i ].override = OVERRIDE_RESET; + switchesStatus[ i ].status.data = (U32)STATE_CLOSED; + switchesStatus[ i ].status.ovData = (U32)STATE_CLOSED; + switchesStatus[ i ].status.ovInitData = (U32)STATE_CLOSED; + switchesStatus[ i ].status.override = OVERRIDE_RESET; + switchesStatus[ i ].debounceStartTime = 0; } } /*********************************************************************//** * @brief * The execSwitches function executes the switches executive. - * @details Inputs: switchesFPGAStatusCheckCounter - * @details Outputs: switchStatus, switchesFPGAStatusCheckCounter + * @details Inputs: switchesStatus + * @details Outputs: switchStatus * @return none *************************************************************************/ void execSwitches( void ) { - if ( ++switchesFPGAStatusCheckCounter > SWITCHES_FPGA_STATUS_CHECK_INTERVAL ) + U08 i; + + U16 currentSwitchStatus = 0; + + for ( i = 0; i < NUM_OF_DOORS_AND_CAPS; i++ ) { - switchStatus[ FRONT_DOOR ].data = (U32)( getFPGAFrontDoorStatus() != 0 ? STATE_CLOSED : STATE_OPEN ); + // Get the current switch status + switch ( i ) + { + case FRONT_DOOR: + currentSwitchStatus = getFPGAFrontDoorStatus(); + break; - switchesFPGAStatusCheckCounter = 0; + case CARTRDIGE_LATCH: + currentSwitchStatus = getFPGACartridgeLatchStatus(); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_HD_INVALID_SWITCH_ID, i ) + break; + } + + // Check if the current switch status is not the same as the recorded data + if ( currentSwitchStatus != switchesStatus[ i ].status.data ) + { + // If the debounce time is 0, start the timer + if ( 0 == switchesStatus[ i ].debounceStartTime ) + { + switchesStatus[ i ].debounceStartTime = getMSTimerCount(); + } + // If the debounce time has been elapsed, update the switch status to the new status + else if ( TRUE == didTimeout( switchesStatus[ i ].debounceStartTime, SWITCHES_DEBOUNCE_TIME_MS ) ) + { + switchesStatus[ i ].debounceStartTime = 0; + switchesStatus[ i ].status.data = currentSwitchStatus; + } + } + else + { + switchesStatus[ i ].debounceStartTime = 0; + } } publishSwitchesData(); @@ -85,13 +127,13 @@ if ( switchId < NUM_OF_DOORS_AND_CAPS ) { - if ( OVERRIDE_KEY == switchStatus[ switchId ].override ) + if ( OVERRIDE_KEY == switchesStatus[ switchId ].status.override ) { - status = switchStatus[ switchId ].ovData; + status = switchesStatus[ switchId ].status.ovData; } else { - status = switchStatus[ switchId ].data; + status = switchesStatus[ switchId ].status.data; } } else @@ -118,7 +160,8 @@ { SWITCHES_DATA_T data; - data.frontDoor = (U32)getSwitchStatus( FRONT_DOOR ); + data.frontDoor = (U32)getSwitchStatus( FRONT_DOOR ); + data.cartrdigeLatch = (U32)getSwitchStatus( CARTRDIGE_LATCH ); switchesDataPublicationCounter = 0; @@ -218,8 +261,8 @@ if ( isTestingActivated() ) { result = TRUE; - switchStatus[ switchId ].ovData = status; - switchStatus[ switchId ].override = OVERRIDE_KEY; + switchesStatus[ switchId ].status.ovData = status; + switchesStatus[ switchId ].status.override = OVERRIDE_KEY; } } @@ -244,8 +287,8 @@ if ( isTestingActivated() ) { result = TRUE; - switchStatus[ switchId ].override = OVERRIDE_RESET; - switchStatus[ switchId ].ovData = switchStatus[ switchId ].ovInitData; + switchesStatus[ switchId ].status.override = OVERRIDE_RESET; + switchesStatus[ switchId ].status.ovData = switchesStatus[ switchId ].status.ovInitData; } }