/************************************************************************** * * Copyright (c) 2019-2023 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 ModeInitPOST.c * * @author (last) Sean Nash * @date (last) 30-Jun-2023 * * @author (original) Dara Navaei * @date (original) 05-Nov-2019 * ***************************************************************************/ #include "reg_crc.h" // Used to hold reset reason code at startup before bits get cleared #include "reg_system.h" // Used to access exception status registers for reset reason code at startup #include "system.h" #include "Accel.h" #include "AlarmLamp.h" #include "BloodFlow.h" #include "BloodLeak.h" #include "Buttons.h" #include "Compatible.h" #include "CPLD.h" #include "DialInFlow.h" #include "DialOutFlow.h" #include "Fans.h" #include "FPGA.h" #include "Integrity.h" #include "ModeInitPOST.h" #include "NVDataMgmt.h" #include "OperationModes.h" #include "PresOccl.h" #include "RTC.h" #include "SafetyShutdown.h" #include "SyringePump.h" #include "SystemComm.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "Temperatures.h" #include "Valves.h" #include "WatchdogMgmt.h" /** * @addtogroup HDInitAndPOSTMode * @{ */ // ********** private definitions ********** #define START_POST_DELAY_COUNT ( ( 1 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) ///< Start POST delay in count. /// Delay (in task intervals) after POST completes. #define POST_COMPLETED_DELAY ( 2 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) /// Maximum wait time for UI to send its final POST result. #define POST_UI_MAX_WAIT_TIME ( 2 * MS_PER_SECOND / TASK_GENERAL_INTERVAL ) // ********** private data ********** static HD_POST_STATE_T postState; ///< Current state of initialize and POST mode. static BOOL postCompleted; ///< Flag indicates whether POST is completed. static BOOL postPassed; ///< Flag indicates all POST tests passed. static BOOL tempPOSTPassed; ///< Flag indicates all POST tests have passed so far. static BOOL uiPOSTPassed; ///< Final result for UI POST tests (TRUE = passed, FALSE = failed). static BOOL dgPOSTPassed; ///< Final result for DG POST tests (TRUE = passed, FALSE = failed). static BOOL uiPOSTResultReceived; ///< Have we received a final POST result from the UI? static BOOL dgPOSTResultReceived; ///< Have we received a final POST result from the DG? static U32 waitForUIPostTimerCtr; ///< Timer counter to limit wait for UI final POST result. static U32 postCompleteDelayTimerCtr; ///< Timer counter for 2 second delay after POST completes and before transitioning to Standbymode. static UI_VERSIONS_T uiVersion = { 0, 0, 0, 0, 0 }; ///< Version and compatibility information reported by UI. static DG_VERSIONS_T dgVersion = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; ///< Version and compatibility information reported by DG. static U32 startPOSTDelayCounter = 0; ///< Start POST delay counter. extern U32 savedResetReasonCode; ///< Saved reset reason code from sys_startup.c. // ********** private function prototypes ********** static HD_POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ); static HD_POST_STATE_T handlePOSTStateStart( void ); static SELF_TEST_STATUS_T execFWCompatibilityTest( void ); static SELF_TEST_STATUS_T execUITest( void ); /*********************************************************************//** * @brief * The initInitAndPOSTMode function initializes the Initialize & POST Mode module. * @details Inputs: none * @details Outputs: Initialize & POST Mode module initialized. * @return none *************************************************************************/ void initInitAndPOSTMode( void ) { postState = POST_STATE_START; postCompleted = FALSE; postPassed = FALSE; tempPOSTPassed = TRUE; uiPOSTPassed = FALSE; dgPOSTPassed = FALSE; uiPOSTResultReceived = FALSE; dgPOSTResultReceived = FALSE; waitForUIPostTimerCtr = 0; postCompleteDelayTimerCtr = 0; startPOSTDelayCounter = 0; } /*********************************************************************//** * @brief * The transitionToInitAndPOSTMode function prepares for transition to * initialize & POST mode. * @details Inputs: none * @details Outputs: none * @return initial state *************************************************************************/ U32 transitionToInitAndPOSTMode( void ) { initInitAndPOSTMode(); // Set user alarm recovery actions allowed in this mode setAlarmUserActionEnabled( ALARM_USER_ACTION_RESUME, FALSE ); setAlarmUserActionEnabled( ALARM_USER_ACTION_RINSEBACK, FALSE ); setAlarmUserActionEnabled( ALARM_USER_ACTION_END_TREATMENT, FALSE ); resetRTCPOSTState(); resetNVDataMgmtPOSTState(); resetAccelPOSTState(); resetWatchdogPOSTState(); resetSafetyShutdownPOSTState(); resetValvesPOSTState(); resetAlarmAudioPOSTState(); resetAlarmLampPOSTState(); resetStuckButtonPOSTState(); return postState; } /*********************************************************************//** * @brief * The execInitAndPOSTMode function executes the Initialize & POST Mode * state machine. * @details Inputs: postState * @details Outputs: postState, postPassed, postCompleted * @return current state (sub-mode) *************************************************************************/ U32 execInitAndPOSTMode( void ) { SELF_TEST_STATUS_T testStatus = SELF_TEST_STATUS_IN_PROGRESS; BOOL stop = isStopButtonPressed(); if ( TRUE == stop ) { // Ignore stop button in this mode. } // Execute current POST state *Note - these switch cases must be in same order as enum HD_POST_States switch ( postState ) { case POST_STATE_START: postState = handlePOSTStateStart(); break; case POST_STATE_FW_INTEGRITY: testStatus = execIntegrityTest(); postState = handlePOSTStatus( testStatus ); break; // NOTE: RTC's POST must go before NVDataMgmt case POST_STATE_RTC: testStatus = execRTCSelfTest(); postState = handlePOSTStatus( testStatus ); break; // NOTE: all the actuators and sensors must execute their POST after NVDataMgmt // NVDataMgmt must load all the calibration data into RAM so the actuators // can query their corresponding calibration values successfully case POST_STATE_NVDATAMGMT: testStatus = execNVDataMgmtSelfTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_WATCHDOG: testStatus = execWatchdogTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_SAFETY_SHUTDOWN: testStatus = execSafetyShutdownTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_BLOOD_FLOW: testStatus = execBloodFlowTest(); postState = handlePOSTStatus( testStatus ); sendUIRequestPOSTFinalResult(); // request UI POST final result here so we have it before UI test below break; case POST_STATE_DIALYSATE_INLET_FLOW: testStatus = execDialInFlowTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_DIALYSATE_OUTLET_FLOW: testStatus = execDialOutFlowTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_BLOOD_LEAK: testStatus = execBloodLeakSelfTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_VALVES: testStatus = execValvesSelfTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_SYRINGE_PUMP: testStatus = execSyringePumpSelfTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_PRES_OCCL: testStatus = execPresOcclSelfTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_ALARM_AUDIO: #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_ALARM_AUDIO ) != SW_CONFIG_ENABLE_VALUE ) #endif { testStatus = execAlarmAudioSelfTest(); } #ifndef _RELEASE_ else { testStatus = SELF_TEST_STATUS_PASSED; } #endif postState = handlePOSTStatus( testStatus ); break; case POST_STATE_ALARM_LAMP: testStatus = execAlarmLampTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_ACCELEROMETER: #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_ACCELEROMETERS ) != SW_CONFIG_ENABLE_VALUE ) #endif { testStatus = execAccelTest(); } #ifndef _RELEASE_ else { testStatus = SELF_TEST_STATUS_PASSED; } #endif postState = handlePOSTStatus( testStatus ); break; case POST_STATE_TEMPERATURES: testStatus = execTemperaturesSelfTest(); postState = handlePOSTStatus( testStatus ); break; // NOTE: fans self test must be called after temperatures since the // temperatures must get their calibration first before the fans start monitoring // for RPM out of range case POST_STATE_FANS: testStatus = execFansSelfTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_STUCK_BUTTON: testStatus = execStuckButtonTest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_UI_POST: testStatus = execUITest(); postState = handlePOSTStatus( testStatus ); break; case POST_STATE_FW_COMPATIBILITY: testStatus = execFWCompatibilityTest(); postState = handlePOSTStatus( testStatus ); break; // Should be last POST (and last POST test must be a test that completes in a single call) case POST_STATE_FPGA: testStatus = execFPGATest(); handlePOSTStatus( testStatus ); // Ignoring return value because last test if ( TRUE == tempPOSTPassed ) { postState = POST_STATE_COMPLETED; } else { postState = POST_STATE_FAILED; } break; case POST_STATE_COMPLETED: // Set overall HD POST completed status to TRUE postCompleted = TRUE; // Broadcast final POST passed once. We stay in this state for POST_COMPLETED_DELAY. if ( FALSE == postPassed ) { sendPOSTFinalResult( TRUE ); } // Set overall HD POST status to "passed" postPassed = TRUE; // Delay before going to standby mode if ( ++postCompleteDelayTimerCtr > POST_COMPLETED_DELAY ) { requestNewOperationMode( MODE_STAN ); } break; case POST_STATE_FAILED: // Should not get here - any failed post test should have already triggered a fault and taken us to fault mode default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_MODE_INIT_POST_INVALID_POST_STATE, postState ) break; } return postState; } /*********************************************************************//** * @brief * The signalAlarmActionToInitAndPOSTMode function executes the given alarm action * as appropriate while in InitAndPOST Mode. * @details Inputs: none * @details Outputs: given alarm action executed * @param action ID of alarm action to execute * @return none *************************************************************************/ void signalAlarmActionToInitAndPOSTMode( ALARM_ACTION_T action ) { // TODO - anything required here? } /*********************************************************************//** * @brief * The signalUIPOSTFinalResult function records the final POST result for * the UI. * @details Inputs: none * @details Outputs: uiPOSTPassed * @param passed TRUE if UI POST tests all passed, FALSE if any UI POST test failed * @return none *************************************************************************/ void signalUIPOSTFinalResult( BOOL passed ) { uiPOSTPassed = passed; uiPOSTResultReceived = TRUE; } /*********************************************************************//** * @brief * The signalDGPOSTFinalResult function records the final POST result for * the DG. * @details Inputs: none * @details Outputs: dgPOSTPassed, dgPOSTResultReceived * @param passed TRUE if DG POST tests all passed, FALSE if any DG POST test failed * @return none *************************************************************************/ void signalDGPOSTFinalResult( BOOL passed ) { dgPOSTPassed = passed; dgPOSTResultReceived = TRUE; } /*********************************************************************//** * @brief * The getDGPOSTPassed function gets the POST passed status of the DG sub-system. * @details Inputs: dgPOSTPassed, dgPOSTResultReceived * @details Outputs: none * @return TRUE if DG POST result received and passed, FALSE if not *************************************************************************/ BOOL getDGPOSTPassed( void ) { BOOL result = ( TRUE == dgPOSTResultReceived && TRUE == dgPOSTPassed ? TRUE : FALSE ); return result; } /*********************************************************************//** * @brief * The signalUIVersion function records the reported UI version information. * @details Inputs: none * @details Outputs: uiVersion * @param version Version information reported by UI * @return none *************************************************************************/ void signalUIVersion( UI_VERSIONS_T version ) { uiVersion = version; } /*********************************************************************//** * @brief * The signalDGVersion function records the reported DG version information. * @details Inputs: none * @details Outputs: dgVersion * @param version Version information reported by DG * @return none *************************************************************************/ void signalDGVersion( DG_VERSIONS_T version ) { dgVersion = version; } /*********************************************************************//** * @brief * The getDGVersion function returns the DG version information. * @details Inputs: dgVersion * @details Outputs: none * @return dgVersion *************************************************************************/ DG_VERSIONS_T getDGVersion( void ) { return dgVersion; } /*********************************************************************//** * @brief * The isPOSTCompleted function determines whether all HD POST have * been run and completed. If true, call the isPOSTPassed() to see final * result (pass/fail). * @details Inputs: postCompleted * @details Outputs: none * @return true if all HD POST tests have completed, false if not *************************************************************************/ BOOL isPOSTCompleted( void ) { return postCompleted; } /*********************************************************************//** * @brief * The isPOSTPassed function determines whether all HD POST have passed. * Call this function after POST is complete (call isPOSTCompleted function). * @details Inputs: postPassed * @details Outputs: none * @return true if all HD POST tests have passed, false if not *************************************************************************/ BOOL isPOSTPassed( void ) { return postPassed; } /*********************************************************************//** * @brief * The isDGPOSTPassed function determines whether all DG POST have passed. * @details Inputs: dgPOSTPassed * @details Outputs: none * @return true if all DG POST tests have passed, false if not *************************************************************************/ BOOL isDGPOSTPassed( void ) { return dgPOSTPassed; } /*********************************************************************//** * @brief * The handlePOSTStatus function handles a status result returned by a * POST function. * @details Inputs: testStatus * @details Outputs: tempPOSTPassed * @param testStatus status reported by last test * @return recommended next POST state *************************************************************************/ static HD_POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ) { HD_POST_STATE_T result = postState; #ifdef BOARD_WITH_NO_HARDWARE testStatus = SELF_TEST_STATUS_PASSED; #endif if ( testStatus == SELF_TEST_STATUS_PASSED ) { // Broadcast passed POST result sendPOSTTestResult( (HD_POST_STATE_T)((int)postState), TRUE ); // Move on to next POST test result = (HD_POST_STATE_T)((int)postState + 1); } else if ( testStatus == SELF_TEST_STATUS_FAILED ) { // At least one POST has failed tempPOSTPassed = FALSE; // Broadcast failed POST results sendPOSTTestResult( (HD_POST_STATE_T)((int)postState), FALSE ); sendPOSTFinalResult( FALSE ); // Test that failed should have triggered a fault which will request fault mode, so should POST state machine should never see FAILED state and will fault if it does result = POST_STATE_FAILED; } else { // Test still in progress - do nothing } return result; } /*********************************************************************//** * @brief * The handlePOSTStateStart function handles the POST start state. * @details Inputs: startPOSTDelayCounter * @details Outputs: startPOSTDelayCounter * @return next POST state *************************************************************************/ static HD_POST_STATE_T handlePOSTStateStart( void ) { HD_POST_STATE_T state = POST_STATE_START; // There is a delay before starting POST to make sure the CAN bus is up and listening so // when the event data can be sent if ( ++startPOSTDelayCounter > START_POST_DELAY_COUNT ) { U32 resetReason = systemREG1->SYSESR | crcREG->PSA_SIGREGL1; // power-on reset bit preserved in an unused CRC register systemREG1->SYSESR = systemREG1->SYSESR; // clear reset bit(s) after logging event so subsequent resets can be properly identified as not being power-on resets. crcREG->PSA_SIGREGL1 = 0; // clear preserved power-on reset bit. sendUIVersionRequest(); // request UI to send us its version - needed for compatibility check if ( TRUE == isDGCommunicating() ) { sendRequestForDGResendAlarms(); // request DG to re-send any active alarms that may have been triggered before we came up } // Send the startup event SEND_EVENT_WITH_2_U32_DATA( HD_EVENT_STARTUP, systemREG1->DEV, 0x12345678 ) // log startup event w/ device ID bits SEND_EVENT_WITH_2_U32_DATA( HD_EVENT_STARTUP, resetReason, systemREG1->GBLSTAT ) // log startup event w/ reset reason(s) bits // Send the first submode change event. It is the mode Init and it does not start from a previous // mode previous and current are both published as Init SEND_EVENT_WITH_2_U32_DATA( HD_EVENT_OP_MODE_CHANGE, MODE_INIT, MODE_INIT ) state = POST_STATE_FW_INTEGRITY; startPOSTDelayCounter = 0; } return state; } /*********************************************************************//** * @brief * The execFWCompatibilityTest function executes the firmware compatibility test. * @details Inputs: none * @details Outputs: none * @return in progress, passed, or failed *************************************************************************/ static SELF_TEST_STATUS_T execFWCompatibilityTest( void ) { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_PASSED; if ( uiVersion.compatibility != SW_COMPATIBILITY_REV ) { result = SELF_TEST_STATUS_FAILED; SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_UI_COMPATIBILITY_ERROR, uiVersion.compatibility, SW_COMPATIBILITY_REV ) } return result; } /*********************************************************************//** * @brief * The execUITest function executes the UI POST passed test. * @details Inputs: uiPOSTResultReceived, uiPOSTPassed, waitForUIPostTimerCtr * @details Outputs: waitForUIPostTimerCtr * @return in progress, passed, or failed *************************************************************************/ static SELF_TEST_STATUS_T execUITest( void ) { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_IN_PROGRESS; // UI should have sent POST results before we start this test if ( TRUE == uiPOSTResultReceived ) { if ( TRUE == uiPOSTPassed ) { result = SELF_TEST_STATUS_PASSED; } else { SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_UI_POST_TIMEOUT, 0 ) result = SELF_TEST_STATUS_FAILED; } } // If UI had not already sent POST results before we started, allow finite period for UI to send. else if ( ++waitForUIPostTimerCtr > POST_UI_MAX_WAIT_TIME ) { SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_UI_POST_TIMEOUT, 1 ) result = SELF_TEST_STATUS_FAILED; } return result; } /**@}*/