/************************************************************************** * * Copyright (c) 2024-2024 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 * @date (last) 30-Jul-2024 * * @author (original) Sean * @date (original) 30-Jul-2024 * ***************************************************************************/ #include "reg_crc.h" // Used to hold reset reason code at startup before bits get cleared #include "system.h" #include "AlarmLamp.h" //#include "BloodFlow.h" //#include "Buttons.h" #include "Compatible.h" #include "CpldInterface.h" //#include "Fans.h" #include "FpgaTD.h" //#include "Integrity.h" #include "Messaging.h" #include "ModeInitPOST.h" //#include "NVDataMgmt.h" #include "OperationModes.h" #include "PAL.h" //#include "Pressures.h" #include "SystemCommTD.h" #include "TaskGeneral.h" //#include "Temperatures.h" //#include "Valves.h" #include "WatchdogMgmt.h" /** * @addtogroup TDInitAndPOSTMode * @{ */ // ********** 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 TD_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 DD_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 TD_POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ); static TD_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 unit. * @details \b Inputs: none * @details \b Outputs: Initialize & POST Mode unit 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 \b Inputs: none * @details \b 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 ); // resetNVDataMgmtPOSTState(); // resetWatchdogPOSTState(); // resetSafetyShutdownPOSTState(); // resetValvesPOSTState(); resetAlarmAudioPOSTState(); resetAlarmLampPOSTState(); // resetStuckButtonPOSTState(); requestAlarmLampPattern( LAMP_PATTERN_OK ); // TODO remove once POST is setup correctly return postState; } /*********************************************************************//** * @brief * The execInitAndPOSTMode function executes the Initialize & POST Mode * state machine. * @details \b Inputs: postState * @details \b 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: 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_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_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_TD_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 \b Inputs: none * @details \b 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 \b Inputs: none * @details \b 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 signalDDPOSTFinalResult function records the final POST result for * the DD. * @details \b Inputs: none * @details \b Outputs: dgPOSTPassed, dgPOSTResultReceived * @param passed TRUE if DD POST tests all passed, FALSE if any DD POST test failed * @return none *************************************************************************/ void signalDDPOSTFinalResult( BOOL passed ) { dgPOSTPassed = passed; dgPOSTResultReceived = TRUE; } /*********************************************************************//** * @brief * The getDDPOSTPassed function gets the POST passed status of the DD sub-system. * @details \b Inputs: dgPOSTPassed, dgPOSTResultReceived * @details \b Outputs: none * @return TRUE if DD POST result received and passed, FALSE if not *************************************************************************/ BOOL getDDPOSTPassed( void ) { BOOL result = ( TRUE == dgPOSTResultReceived && TRUE == dgPOSTPassed ? TRUE : FALSE ); return result; } /*********************************************************************//** * @brief * The signalUIVersion function records the reported UI version information. * @details \b Inputs: none * @details \b Outputs: uiVersion * @param version Version information reported by UI * @return none *************************************************************************/ void signalUIVersion( UI_VERSIONS_T version ) { uiVersion = version; } /*********************************************************************//** * @brief * The signalDDVersion function records the reported DD version information. * @details \b Inputs: none * @details \b Outputs: dgVersion * @param version Version information reported by DD * @return none *************************************************************************/ void signalDDVersion( DD_VERSIONS_T version ) { dgVersion = version; } /*********************************************************************//** * @brief * The getDDVersion function returns the DD version information. * @details \b Inputs: dgVersion * @details \b Outputs: none * @return dgVersion *************************************************************************/ DD_VERSIONS_T getDDVersion( 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 \b Inputs: postCompleted * @details \b 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 \b Inputs: postPassed * @details \b 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 \b Inputs: dgPOSTPassed * @details \b 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 \b Inputs: testStatus * @details \b Outputs: tempPOSTPassed * @param testStatus status reported by last test * @return recommended next POST state *************************************************************************/ static TD_POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ) { TD_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 = (TD_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 \b Inputs: startPOSTDelayCounter * @details \b Outputs: startPOSTDelayCounter * @return next POST state *************************************************************************/ static TD_POST_STATE_T handlePOSTStateStart( void ) { TD_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 = getSystemREG1_SYSECR() | getCrcREG_PSA_SIGREGL1(); // power-on reset bit preserved in an unused CRC register setSystemREG1_SYSECR( getSystemREG1_SYSECR() ); // clear reset bit(s) after logging event so subsequent resets can be properly identified as not being power-on resets. setCrcREG_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_COMPLETED; //POST_STATE_FW_INTEGRITY; startPOSTDelayCounter = 0; } return state; } /*********************************************************************//** * @brief * The execFWCompatibilityTest function executes the firmware compatibility test. * @details \b Inputs: none * @details \b 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 \b Inputs: uiPOSTResultReceived, uiPOSTPassed, waitForUIPostTimerCtr * @details \b 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; } /**@}*/