Index: firmware/App/Controllers/BloodLeak.c =================================================================== diff -u -rfc5a5c7406958c9bcf3fab8405abccf3ad7322f0 -r494fa9614f7ec4cb0f677763c499044336ea8214 --- firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision fc5a5c7406958c9bcf3fab8405abccf3ad7322f0) +++ firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 494fa9614f7ec4cb0f677763c499044336ea8214) @@ -32,20 +32,32 @@ #define BLOOD_LEAK_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Interval (ms/task time) at which the blood leak data is published on the CAN bus. +/// Defined states for the blood leak detector state machine. +typedef enum BloodLeakStates +{ + BLOOD_LEAK_INIT_STATE = 0, ///< Initial state + BLOOD_LEAK_ZERO_STATE, ///< Zero state + BLOOD_LEAK_SELF_TEST_STATE, ///< Self-test state + BLOOD_LEAK_NORMAL_STATE, ///< Normal state + NUM_OF_BLOOD_LEAK_STATES ///< Number of blood leak detector states +} BLOOD_LEAK_STATES_T; + // ********** private data ********** -static OVERRIDE_U32_T bloodLeakState; ///< Current state of blood leak state machine. +static BLOOD_LEAK_STATES_T bloodLeakState; ///< Current state of blood leak state machine. static OVERRIDE_U32_T bloodLeakStatus; ///< Detected blood leak status for blood leak detector. -static U32 bloodLeakZeroTimerCount = 0; ///< Timer counter for blood leak zeroing. -static U32 bloodLeakSelfTestTimerCount = 0; ///< Timer counter for blood leak self-test. +static BOOL bloodLeakZeroRequested = FALSE; ///< Blood leak zero requested flag +static U32 bloodLeakZeroStartTime = 0; ///< Blood leak zeroing start time. +static U32 bloodLeakSelfTestStartTime = 0; ///< Blood leak self-test start time. /// Interval (in ms) at which to publish blood leak data to CAN bus. static OVERRIDE_U32_T bloodLeakDataPublishInterval = { BLOOD_LEAK_PUB_INTERVAL, BLOOD_LEAK_PUB_INTERVAL, 0, 0 }; static U32 bloodLeakDataPublicationTimerCounter = 0; ///< Timer counter used to schedule blood leak data publication to CAN bus. // ********** private function prototypes ********** +static BLOOD_LEAK_STATES_T handleBloodLeakInitState( void ); static BLOOD_LEAK_STATES_T handleBloodLeakZeroState( void ); static BLOOD_LEAK_STATES_T handleBloodLeakSelfTestState( void ); static BLOOD_LEAK_STATES_T handleBloodLeakNormalState( void ); @@ -62,8 +74,11 @@ *************************************************************************/ void initBloodLeak( void ) { - bloodLeakState.data = BLOOD_LEAK_INIT_STATE; + bloodLeakState = BLOOD_LEAK_INIT_STATE; bloodLeakStatus.data = BLOOD_LEAK_NOT_DETECTED; + bloodLeakZeroRequested = FALSE; + bloodLeakZeroStartTime = 0; + bloodLeakSelfTestStartTime = 0; } /*********************************************************************//** @@ -78,27 +93,27 @@ if ( getCurrentOperationMode() != MODE_INIT ) { // Execute blood leak state machine - switch( bloodLeakState.data ) + switch( bloodLeakState ) { case BLOOD_LEAK_INIT_STATE: - // Do nothing; + bloodLeakState = handleBloodLeakInitState(); break; case BLOOD_LEAK_ZERO_STATE: - bloodLeakState.data = handleBloodLeakZeroState(); + bloodLeakState = handleBloodLeakZeroState(); break; case BLOOD_LEAK_SELF_TEST_STATE: - bloodLeakState.data = handleBloodLeakSelfTestState(); + bloodLeakState = handleBloodLeakSelfTestState(); break; case BLOOD_LEAK_NORMAL_STATE: - bloodLeakState.data = handleBloodLeakNormalState(); + bloodLeakState = handleBloodLeakNormalState(); break; default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, (U32)SW_FAULT_ID_HD_INVALID_BLOOD_LEAK_STATE, (U32)bloodLeakState.data ) - initBloodLeak(); + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, (U32)SW_FAULT_ID_HD_INVALID_BLOOD_LEAK_STATE, (U32)bloodLeakState ) + bloodLeakState = BLOOD_LEAK_INIT_STATE; break; } } @@ -109,6 +124,42 @@ /*********************************************************************//** * @brief + * The zeroBloodLeak function zeroes the Blood Leak Detector. + * @details Inputs: none + * @details Outputs: Blood Leak module zeroing. + * @return Boolean as success or failure + *************************************************************************/ +void zeroBloodLeak( void ) +{ + bloodLeakZeroRequested = TRUE; +} + +/*********************************************************************//** + * @brief + * The handleBloodLeakInitState function handles the Blood Leak module in init + * state. + * @details Inputs: none + * @details Outputs: Blood Leak module init. + * @return next state + *************************************************************************/ +static BLOOD_LEAK_STATES_T handleBloodLeakInitState( void ) +{ + BLOOD_LEAK_STATES_T state = BLOOD_LEAK_INIT_STATE; + + if ( TRUE == bloodLeakZeroRequested ) + { + state = BLOOD_LEAK_ZERO_STATE; + bloodLeakZeroRequested = FALSE; + + setFPGABloodLeakZero(); + bloodLeakZeroStartTime = getMSTimerCount(); + } + + return state; +} + +/*********************************************************************//** + * @brief * The handleBloodLeakZeroState function handles the Blood Leak module in zeroing * state. * @details Inputs: none @@ -117,19 +168,24 @@ *************************************************************************/ static BLOOD_LEAK_STATES_T handleBloodLeakZeroState( void ) { - BLOOD_LEAK_STATES_T result = BLOOD_LEAK_ZERO_STATE; + BLOOD_LEAK_STATES_T state = BLOOD_LEAK_ZERO_STATE; - // TODO: Complete - //if ( FALSE == zeroBloodLeak() ) - //{ - //activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_FAULT ); - //} - //else - //{ - // TODO: Fill in - //} + if ( TRUE == didTimeout( bloodLeakZeroStartTime, 10 ) ) // TODO: Define 10 up in the code + { + if ( TRUE == FPGABloodLeakZeroDetected() ) + { + state = BLOOD_LEAK_SELF_TEST_STATE; + clearFPGABloodLeakZero(); + setFPGABloodLeakSelfTest(); + bloodLeakSelfTestStartTime = getMSTimerCount(); + } + else + { + activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_FAULT ); + } + } - return result; + return state; } /*********************************************************************//** @@ -138,23 +194,26 @@ * in self-test state. * @details Inputs: none * @details Outputs: Blood Leak module self test. - * @return next state + * @return none *************************************************************************/ static BLOOD_LEAK_STATES_T handleBloodLeakSelfTestState( void ) { - BLOOD_LEAK_STATES_T result = BLOOD_LEAK_SELF_TEST_STATE; + BLOOD_LEAK_STATES_T state = BLOOD_LEAK_SELF_TEST_STATE; - // TODO: Complete - //if ( FALSE == selfTestBloodLeak() ) - //{ - //activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_FAULT ); - //} - //else - //{ - // TODO: Fill in - //} + if ( TRUE == didTimeout( bloodLeakSelfTestStartTime, 10 ) ) // TODO: Define 10 up in the code + { + if ( FALSE == noFPGABloodLeakDetected() ) + { + state = BLOOD_LEAK_NORMAL_STATE; + clearFPGABloodLeakSelfTest(); + } + else + { + activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_SELF_TEST_FAILURE ); + } + } - return result; + return state; } /*********************************************************************//** @@ -167,13 +226,12 @@ *************************************************************************/ static BLOOD_LEAK_STATES_T handleBloodLeakNormalState( void ) { - BLOOD_LEAK_STATES_T result = BLOOD_LEAK_NORMAL_STATE; + BLOOD_LEAK_STATES_T state = BLOOD_LEAK_NORMAL_STATE; // Check status reading and act upon if ( BLOOD_LEAK_DETECTED == getBloodLeakStatus() ) { - if ( ( getCurrentOperationMode() >= TREATMENT_BLOOD_PRIME_STATE ) && - ( getCurrentOperationMode() <= TREATMENT_RECIRC_STATE ) ) // TODO: Not sure this is correct range of states + if ( getCurrentOperationMode() == MODE_TREA ) { activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_DETECTED ); bloodLeakStatus.data = BLOOD_LEAK_DETECTED; @@ -188,7 +246,16 @@ clearAlarmCondition( ALARM_ID_HD_BLOOD_LEAK_DETECTED ); } - return result; + if ( TRUE == bloodLeakZeroRequested ) + { + state = BLOOD_LEAK_ZERO_STATE; + bloodLeakZeroRequested = FALSE; + + setFPGABloodLeakZero(); + bloodLeakZeroStartTime = getMSTimerCount(); + } + + return state; } /*********************************************************************//** @@ -213,22 +280,15 @@ /*********************************************************************//** * @brief - * The getBloodLeakState function gets the current state of the blood - * leak detector state machine. - * @details Inputs: bloodLeakState + * The getBloodLeakSelfTestStatus function gets the status for the blood + * leak detector self-test. + * @details Inputs: bloodLeakStatus * @details Outputs: none - * @return the current blood leak state. + * @return status of blood leak detector self-test. *************************************************************************/ -BLOOD_LEAK_STATES_T getBloodLeakState( void ) +BOOL getBloodLeakSelfTestStatus( void ) { - BLOOD_LEAK_STATES_T result = (BLOOD_LEAK_STATES_T)bloodLeakState.data; - - if ( OVERRIDE_KEY == bloodLeakState.override ) - { - result = (BLOOD_LEAK_STATES_T)bloodLeakState.ovData; - } - - return result; + return ( bloodLeakState == BLOOD_LEAK_NORMAL_STATE ); } /*********************************************************************//** @@ -264,9 +324,8 @@ if ( ++bloodLeakDataPublicationTimerCounter >= getPublishBloodLeakDataInterval() ) { BLOOD_LEAK_STATUS_T status = getBloodLeakStatus(); - BLOOD_LEAK_STATES_T state = getBloodLeakState(); - broadcastBloodLeakData( status, state ); + broadcastBloodLeakData( status, bloodLeakState ); bloodLeakDataPublicationTimerCounter = 0; } } @@ -374,54 +433,4 @@ return result; } -/*********************************************************************//** - * @brief - * The testSetBloodLeakStateOverride function overrides the state - * of the blood leak detector. - * @details Inputs: none - * @details Outputs: bloodLeakState - * @param none - * @param state override blood leak detector with this - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetBloodLeakStateOverride( BLOOD_LEAK_STATES_T state ) -{ - BOOL result = FALSE; - - if ( state < NUM_OF_BLOOD_LEAK_STATES ) - { - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - bloodLeakState.ovData = (U32)state; - bloodLeakState.override = OVERRIDE_KEY; - } - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetBloodLeakStateOverride function resets the override of the - * blood leak detector state. - * @details Inputs: none - * @details Outputs: bloodLeakState - * @param none - * @return TRUE if reset successful, FALSE if not - *************************************************************************/ -BOOL testResetBloodLeakStateOverride( void ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - bloodLeakState.override = OVERRIDE_RESET; - bloodLeakState.ovData = bloodLeakState.ovInitData; - } - - return result; -} - /**@}*/