/************************************************************************** * * Copyright (c) 2019-2025 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 ModeFault.c * * @author (last) Dara Navaei * @date (last) 29-Aug-2023 * * @author (original) Dara Navaei * @date (original) 05-Nov-2019 * ***************************************************************************/ #include "ConcentratePumps.h" #include "DrainPump.h" #include "Heaters.h" #include "ModeChemicalDisinfect.h" #include "ModeFault.h" #include "ModeHeatDisinfect.h" #include "NVDataMgmt.h" #include "OperationModes.h" #include "ROPump.h" #include "RTC.h" #include "SystemCommMessages.h" #include "Utilities.h" #include "UVReactors.h" #include "Valves.h" #include "CPLD.h" /** * @addtogroup DGFaultMode * @{ */ // ********** private definitions ********** #define DELAY_VALVE_MS ( 1 * MS_PER_SECOND ) ///< Valve state change delay in ms. // ********** private data ********** static DG_FAULT_STATE_T faultState; ///< Currently active fault state. static SELF_TEST_STATUS_T faultPOSTSelfTestResult; ///< Fault POST self test result. // ********** private function prototypes ********** static DG_FAULT_STATE_T handleFaultStartState( void ); static DG_FAULT_STATE_T handleFaultRunNVPOSTsState( void ); /*********************************************************************//** * @brief * The initFaultMode function initializes the Fault Mode module. * @details Inputs: none * @details Outputs: Fault mode module initialized * @return none *************************************************************************/ void initFaultMode( void ) { faultState = DG_FAULT_STATE_START; faultPOSTSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; } /*********************************************************************//** * @brief * The transitionToFaultMode function prepares for transition to fault mode. * @details Inputs: none * @details Outputs: none * @return initial state *************************************************************************/ U32 transitionToFaultMode( void ) { deenergizeActuators( PARK_CONC_PUMPS ); initFaultMode(); setCurrentSubState( NO_SUB_STATE ); // Publish POST failure status to UI if fault triggered in Init/POST mode if ( DG_MODE_INIT == getPreviousOperationMode() ) { // Broadcast final POST failed sendPOSTFinalResult( FALSE ); } setCPLDCleanLEDColor( CPLD_CLEAN_LED_OFF ); // Release RTC in case the RTC semaphore was not released prior to transitioning to fault mode. // In fault mode, the non-volatile data mgmt POST might be run again so the RTC has to be available. Also, // the RTC time is read every second which requires the semaphore. releaseSemaphore( SEMAPHORE_RTC ); #ifndef _RELEASE_ setHeatNelsonSupportMode( NELSON_NONE ); setChemNelsonSupportMode( NELSON_NONE ); #endif return faultState; } /*********************************************************************//** * @brief * The execFaultMode function executes the fault mode state machine. * @details Inputs: none * @details Outputs: Fault mode state machine executed * @return current state of fault mode *************************************************************************/ U32 execFaultMode( void ) { deenergizeActuators( NO_PARK_CONC_PUMPS ); // execute current fault state switch ( faultState ) { case DG_FAULT_STATE_START: faultState = handleFaultStartState(); break; case DG_FAULT_STATE_RUN_NV_POSTS: faultState = handleFaultRunNVPOSTsState(); break; case DG_FAULT_STATE_COMPLETE: // Do nothing unless the test configuration to recover treatment is enabled if ( ( TRUE == getTestConfigStatus( TEST_CONFIG_RECOVER_TREATMENT ) ) && ( TRUE == hasRecoverFromFaultModeBeenSet() ) ) { DG_OP_MODE_T prevMode = getPreviousOperationMode(); requestNewOperationMode( prevMode ); } break; default: faultState = DG_FAULT_STATE_COMPLETE; break; } return faultState; } /*********************************************************************//** * @brief * The getCurrentFaultState function returns the current state of the fault mode. * @details Inputs: faultState * @details Outputs: none * @return current state of fault mode *************************************************************************/ DG_FAULT_STATE_T getCurrentFaultState( void ) { return faultState; } /*********************************************************************//** * @brief * The deenergizeActuators function sets all the actuators to reset and * de-energized state. * @details Inputs: none * @details Outputs: none * @param parkPumps TRUE if concentrate pumps should be parked, FALSE if not * @return none *************************************************************************/ void deenergizeActuators( BOOL parkPumps ) { // Turn off the UV reactors turnOffUVReactor( INLET_UV_REACTOR ); turnOffUVReactor( OUTLET_UV_REACTOR ); // De-energize all the valves setValveStateDelayed( VPI, VALVE_STATE_CLOSED, DELAY_VALVE_MS ); setValveStateDelayed( VBF, VALVE_STATE_CLOSED, DELAY_VALVE_MS ); setValveStateDelayed( VSP, VALVE_STATE_CLOSED, DELAY_VALVE_MS ); setValveStateDelayed( VPD, VALVE_STATE_DRAIN_C_TO_NO, DELAY_VALVE_MS ); setValveStateDelayed( VPO, VALVE_STATE_NOFILL_C_TO_NO, DELAY_VALVE_MS ); setValveStateDelayed( VDR, VALVE_STATE_DRAIN_C_TO_NO, DELAY_VALVE_MS ); setValveStateDelayed( VRC, VALVE_STATE_DRAIN_C_TO_NO, DELAY_VALVE_MS ); setValveStateDelayed( VRO, VALVE_STATE_R1_C_TO_NO, DELAY_VALVE_MS ); setValveStateDelayed( VRD1, VALVE_STATE_CLOSED, DELAY_VALVE_MS ); setValveStateDelayed( VRD2, VALVE_STATE_CLOSED, DELAY_VALVE_MS ); setValveStateDelayed( VRI, VALVE_STATE_R1_C_TO_NO, DELAY_VALVE_MS ); setValveStateDelayed( VRF, VALVE_STATE_R2_C_TO_NO, DELAY_VALVE_MS ); requestConcentratePumpOff( CONCENTRATEPUMPS_CP1_ACID, parkPumps ); requestConcentratePumpOff( CONCENTRATEPUMPS_CP2_BICARB, parkPumps ); signalROPumpHardStop(); signalDrainPumpHardStop(); stopHeater( DG_PRIMARY_HEATER ); stopHeater( DG_TRIMMER_HEATER ); } /*********************************************************************//** * @brief * The handleFaultStartState function handles the start state of the fault mode. * @details Inputs: none * @details Outputs: none * @return next state *************************************************************************/ static DG_FAULT_STATE_T handleFaultStartState( void ) { DG_FAULT_STATE_T state = DG_FAULT_STATE_START; NVDATAMGMT_RECORDS_READ_STATUS_T status = getNVRecordsReadStatus(); switch ( status ) { // If the records are queued or already read, go directly to NV POST to process // their CRCs. case NVDATAMGMT_RECORDS_QUEUED: case NVDATAMGMT_RECORDS_READ: state = DG_FAULT_STATE_RUN_NV_POSTS; break; // If the NV post was completed prior to transitioning to fault mode, do nothing case NVDATAMGMT_RECORDS_CRC_CHECKED: state = DG_FAULT_STATE_COMPLETE; break; } return state; } /*********************************************************************//** * @brief * The handleFaultRunNVPOSTsState function handles running non-volatile POSTs. * @details Inputs: faultPOSTSelfTestResult * @details Outputs: faultPOSTSelfTestResult * @return next state *************************************************************************/ static DG_FAULT_STATE_T handleFaultRunNVPOSTsState( void ) { DG_FAULT_STATE_T state = DG_FAULT_STATE_RUN_NV_POSTS; faultPOSTSelfTestResult = execNVDataMgmtSelfTest(); // Regardless of the status of the NV POST transition to the complete state. if ( ( SELF_TEST_STATUS_PASSED == faultPOSTSelfTestResult ) || ( SELF_TEST_STATUS_FAILED == faultPOSTSelfTestResult ) ) { state = DG_FAULT_STATE_COMPLETE; } return state; } /**@}*/