/************************************************************************** * * 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) Vinayakam Mani * @date (last) 05-Aug-2024 * * @author (original) Vinayakam Mani * @date (original) 05-Aug-2024 * ***************************************************************************/ #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 "ConcentratePumps.h" //#include "ConductivitySensors.h" #include "FpgaDD.h" //#include "Integrity.h" #include "Messaging.h" #include "ModeInitPOST.h" //#include "NVDataMgmt.h" #include "OperationModes.h" //#include "Pressures.h" #include "SafetyShutdown.h" #include "TaskGeneral.h" //#include "TemperatureSensors.h" //#include "Thermistors.h" #include "WatchdogMgmt.h" /** * @addtogroup DDInitAndPOSTMode * @{ */ // ********** private definitions ********** #define START_POST_DELAY_COUNT ( ( 1 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) ///< Start POST delay in count. // ********** private data ********** static DD_POST_STATE_T postState; ///< Currently active initialize & POST state. static BOOL postCompleted; ///< Flag indicating POST completed. static BOOL postPassed; ///< Flag indicating all POST tests passed. static BOOL tempPOSTPassed; ///< Temporary flag indicating all POST tests completed so far have passed. static U32 startPOSTDelayCounter; ///< Start POST delay counter. // ********** private function prototypes ********** static DD_POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ); static DD_POST_STATE_T handlePOSTStateStart( void ); static SELF_TEST_STATUS_T execFWCompatibilityTest( void ); /*********************************************************************//** * @brief * The initInitAndPOSTMode function initializes the Initialization and POST * mode unit. * @details \b Inputs: none * @details \b Outputs: unit variables are initialized. * @return none *************************************************************************/ void initInitAndPOSTMode( void ) { postState = DD_POST_STATE_START; postCompleted = FALSE; postPassed = FALSE; tempPOSTPassed = TRUE; startPOSTDelayCounter = 0; } /*********************************************************************//** * @brief * The transitionToInitAndPOSTMode function prepares for transition to * initialization and POST mode. * @details \b Inputs: none * @details \b Outputs: none * @return initial state *************************************************************************/ U32 transitionToInitAndPOSTMode( void ) { initInitAndPOSTMode(); // setCurrentSubState( NO_SUB_STATE ); // resetNVDataMgmtPOSTState(); // resetPressuresPOSTState(); // resetWatchdogPOSTState(); // resetSafetyShutdownPOSTState(); return postState; } /*********************************************************************//** * @brief * The execInitAndPOSTMode function executes the initialization and POST * mode state machine. * @details \b Inputs: postState * @details \b Outputs: Initialization and POST mode state machine executed * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when any of the post step fails. * @Warning 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 * @return current state. *************************************************************************/ U32 execInitAndPOSTMode( void ) { SELF_TEST_STATUS_T testStatus = SELF_TEST_STATUS_IN_PROGRESS; // execute current POST state switch ( postState ) { case DD_POST_STATE_START: postState = handlePOSTStateStart(); break; case DD_POST_STATE_FW_COMPATIBILITY: //testStatus = execFWCompatibilityTest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_FW_INTEGRITY: //testStatus = execIntegrityTest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_FPGA: //testStatus = execFPGATest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_NVDATAMGMT: //testStatus = execNVDataMgmtSelfTest(); 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 DD_POST_STATE_TEMPERATURE_SENSORS: //testStatus = execTemperatureSensorsSelfTest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_PRESSURES: //testStatus = execPressureSelfTest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_CONCENTRATE_PUMPS: //testStatus = execConcenratePumpsSelfTest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_CONDUCTIVITY_SENSORS: //testStatus = execConductivitySensorsSelfTest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_THERMISTORS: //testStatus = execThermistorsSelfTest(); postState = handlePOSTStatus( testStatus ); break; case DD_POST_STATE_WATCHDOG: //testStatus = execWatchdogTest(); postState = handlePOSTStatus( testStatus ); break; // Should be last POST (and last POST test must be a test that completes in a single call) case DD_POST_STATE_SAFETY_SHUTDOWN: //testStatus = execSafetyShutdownTest(); handlePOSTStatus( testStatus ); // Ignoring return value because last test if ( TRUE == tempPOSTPassed ) { postState = DD_POST_STATE_COMPLETED; } else { postState = DD_POST_STATE_FAILED; } break; case DD_POST_STATE_COMPLETED: // Set overall HD POST status to "passed" postPassed = TRUE; // Set overall HD POST completed status to TRUE postCompleted = TRUE; // Broadcast final POST passed //sendPOSTFinalResult( TRUE ); // Request whether we are in the RO only mode from UI //requestROOnlyModeStatusFromUI(); // Go to standby mode requestNewOperationMode( DD_MODE_STAN ); break; case DD_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_DD_SOFTWARE_FAULT, SW_FAULT_ID_MODE_INIT_POST_INVALID_POST_STATE, postState ) postState = DD_POST_STATE_FAILED; break; } return postState; } /*********************************************************************//** * @brief * The isPOSTCompleted function determines whether all DD 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 DD POST tests have completed, false if not *************************************************************************/ BOOL isPOSTCompleted( void ) { return postCompleted; } /*********************************************************************//** * @brief * The isPOSTPassed function determines whether all DD 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 DD POST tests have passed, false if not *************************************************************************/ BOOL isPOSTPassed( void ) { return postPassed; } /*********************************************************************//** * @brief * The handlePOSTStatus function handles a status result returned by a POST function. * @details \b Inputs: postPassed * @details \b Outputs: none * @param testStatus status reported by last test * @return recommended next POST state *************************************************************************/ static DD_POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ) { DD_POST_STATE_T result = postState; if ( ( SELF_TEST_STATUS_PASSED == testStatus ) || ( SELF_TEST_STATUS_FAILED == testStatus ) ) { BOOL passed = ( SELF_TEST_STATUS_PASSED == testStatus ? TRUE : FALSE ); // Broadcast POST result //sendPOSTTestResult( (DD_POST_STATE_T)((int)postState), passed ); // Move on to next POST test result = (DD_POST_STATE_T)((int)postState + 1); if ( SELF_TEST_STATUS_FAILED == testStatus ) { tempPOSTPassed = FALSE; } } return result; } /*********************************************************************//** * @brief * The handlePOSTStateStart function handles the POST start state. * @details \b Inputs: startPOSTDelayCounter, systemREG1 * @details \b Outputs: startPOSTDelayCounter, systemREG1 * @details \b Message \b Sent: DD_EVENT_STARTUP with system register details and * DD_EVENT_OP_MODE_CHANGE listing previous to new mode change details. * @return next POST state *************************************************************************/ static DD_POST_STATE_T handlePOSTStateStart( void ) { DD_POST_STATE_T state = DD_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. // Send the startup event SEND_EVENT_WITH_2_U32_DATA( DD_EVENT_STARTUP, systemREG1->DEV, 0x12345678 ) // log startup event w/ device ID bits SEND_EVENT_WITH_2_U32_DATA( DD_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( DD_EVENT_OP_MODE_CHANGE, DD_MODE_INIT, DD_MODE_INIT ) state = DD_POST_STATE_FW_COMPATIBILITY; startPOSTDelayCounter = 0; } return state; } /*********************************************************************//** * @brief * The getCurrentInitAndPOSTState function returns the current state of the * initialization and POST mode. * @details \b Inputs: postState * @details \b Outputs: none * @return the current state of initialization and POST mode *************************************************************************/ DD_POST_STATE_T getCurrentInitAndPOSTState( void ) { return postState; } /*********************************************************************//** * @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; // TODO - implement return result; } /**@}*/