Index: firmware/App/Modes/ModeInitPOST.c =================================================================== diff -u -reb877ae36c28eb83553ee11ccccf42e2c4a5b4d2 -red4265fbaf50eefd9c7243fb1fc8ee7db0296746 --- firmware/App/Modes/ModeInitPOST.c (.../ModeInitPOST.c) (revision eb877ae36c28eb83553ee11ccccf42e2c4a5b4d2) +++ firmware/App/Modes/ModeInitPOST.c (.../ModeInitPOST.c) (revision ed4265fbaf50eefd9c7243fb1fc8ee7db0296746) @@ -1,123 +1,298 @@ /************************************************************************** - * - * Copyright (c) 2019-2019 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 - * - * @date 19-Sep-2019 - * @author S. Nash - * - * @brief Top-level state machine for the initialize & POST mode. - * - **************************************************************************/ +* +* Copyright (c) 2019-2022 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) Dara Navaei +* @date (last) 03-Apr-2022 +* +* @author (original) Dara Navaei +* @date (original) 05-Nov-2019 +* +***************************************************************************/ -#include -#include -#include "Common.h" +#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 "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 "system.h" +#include "SystemCommMessages.h" +#include "TaskGeneral.h" +#include "Temperatures.h" +#include "Valves.h" #include "WatchdogMgmt.h" -#include "ModeInitPOST.h" + /** + * @addtogroup HDInitAndPOSTMode + * @{ + */ + // ********** private definitions ********** -typedef enum POST_States -{ - POST_STATE_START = 0, - POST_STATE_ALARM_LAMP, - POST_STATE_STUCK_BUTTON, - POST_STATE_FPGA, - POST_STATE_WATCHDOG, - POST_STATE_COMPLETED, - POST_STATE_FAILED, - NUM_OF_POST_STATES -} POST_STATE_T; +#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 POST_STATE_T postState = POST_STATE_START; -static BOOL postCompleted = FALSE; -static BOOL postPassed = FALSE; -static BOOL tempPOSTPassed = TRUE; +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 POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ); +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 initInitAndPOSTMode +/*********************************************************************//** + * @brief * The initInitAndPOSTMode function initializes the Initialize & POST Mode module. - * @details - * Inputs : none - * Outputs : Initialize & POST Mode module initialized. - * @param none + * @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; + 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 transitionToInitAndPOSTMode - * The transitionToInitAndPOSTMode function prepares for transition to\n +/*********************************************************************//** + * @brief + * The transitionToInitAndPOSTMode function prepares for transition to * initialize & POST mode. - * @details - * Inputs : none - * Outputs : - * @param none - * @return none + * @details Inputs: none + * @details Outputs: none + * @return initial state *************************************************************************/ -void transitionToInitAndPOSTMode( void ) +U32 transitionToInitAndPOSTMode( void ) { - requestAlarmLampPattern( LAMP_PATTERN_MANUAL ); + // 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 ); + + return postState; } -/************************************************************************* - * @brief execInitAndPOSTMode - * The execInitAndPOSTMode function executes the Initialize & POST Mode state machine. - * @details - * Inputs : none - * Outputs : - * @param none - * @return none +/*********************************************************************//** + * @brief + * The execInitAndPOSTMode function executes the Initialize & POST Mode + * state machine. + * @details Inputs: postState + * @details Outputs: postState, postPassed, postCompleted + * @return current state (sub-mode) *************************************************************************/ -void execInitAndPOSTMode( void ) +U32 execInitAndPOSTMode( void ) { SELF_TEST_STATUS_T testStatus = SELF_TEST_STATUS_IN_PROGRESS; + BOOL stop = isStopButtonPressed(); - // execute current POST state + 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 = POST_STATE_ALARM_LAMP; + postState = handlePOSTStateStart(); break; + case POST_STATE_FW_INTEGRITY: + testStatus = execIntegrityTest(); + postState = handlePOSTStatus( testStatus ); + break; + + case POST_STATE_AC: + if ( getMonitoredLineLevel( MONITORED_LINE_24V ) < MIN_24V_MEASURED_FOR_AC ) + { + initiatePowerOff(); + } + postState = POST_STATE_WATCHDOG; + break; + + case POST_STATE_WATCHDOG: + testStatus = execWatchdogTest(); + postState = handlePOSTStatus( testStatus ); + break; + + case POST_STATE_SAFETY_SHUTDOWN: + testStatus = execSafetyShutdownTest(); + postState = handlePOSTStatus( testStatus ); + break; + + // NOTE: RTC's POST must go before NVDataMgmt + case POST_STATE_RTC: + testStatus = execRTCSelfTest(); + postState = handlePOSTStatus( testStatus ); + break; + + case 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 POST_STATE_BLOOD_FLOW: + testStatus = execBloodFlowTest(); + postState = handlePOSTStatus( testStatus ); + 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_FPGA: - testStatus = execFPGATest(); + case POST_STATE_UI_POST: + testStatus = execUITest(); postState = handlePOSTStatus( testStatus ); break; - case POST_STATE_WATCHDOG: - testStatus = execWatchdogTest(); - handlePOSTStatus( testStatus ); // ignoring return value because last test + 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; @@ -129,81 +304,263 @@ break; case POST_STATE_COMPLETED: - // set overall HD POST status to "passed" + // Set overall HD POST status to "passed" postPassed = TRUE; - // set overall HD POST completed status to TRUE + // Set overall HD POST completed status to TRUE postCompleted = TRUE; - // TODO - send POST status on CAN - // go to standby mode - requestNewOperationMode( MODE_STAN ); + // Broadcast final POST passed + sendPOSTFinalResult( TRUE ); + // Delay before going to standby mode + if ( ++postCompleteDelayTimerCtr > POST_COMPLETED_DELAY ) + { + requestNewOperationMode( MODE_STAN ); + } break; case POST_STATE_FAILED: - // TODO - send POST status on CAN - // will want POST faults to wait for us to get here before sending us to fault mode - requestNewOperationMode( MODE_FAUL ); - break; - + // Should not get here - any failed post test should have already triggered a fault and taken us to fault mode default: - postState = POST_STATE_FAILED; - // TODO - s/w fault + 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 isPOSTCompleted - * The isPOSTCompleted function determines whether all HD POST have \n - * been run and completed. If true, call the isPOSTPassed() to see final \n +/*********************************************************************//** + * @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 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 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 - * Outputs : none - * @param none + * @details Inputs: postCompleted + * @details Outputs: none * @return true if all HD POST tests have completed, false if not *************************************************************************/ BOOL isPOSTCompleted( void ) { return postCompleted; } -/************************************************************************* - * @brief isPOSTPassed - * The isPOSTPassed function determines whether all HD POST have passed. \n +/*********************************************************************//** + * @brief + * The isPOSTPassed function determines whether all HD POST have passed. * Call this function after POST is complete (call isPOSTCompleted function). - * @details - * Inputs : postPassed - * Outputs : none - * @param none + * @details Inputs: postPassed + * @details Outputs: none * @return true if all HD POST tests have passed, false if not *************************************************************************/ BOOL isPOSTPassed( void ) { return postPassed; } -/************************************************************************* - * @brief handlePOSTStatus +/*********************************************************************//** + * @brief * The handlePOSTStatus function handles a status result returned by a * POST function. - * @details - * Inputs : postPassed - * Outputs : none - * @param testStatus + * @details Inputs: testStatus + * @details Outputs: tempPOSTPassed + * @param testStatus status reported by last test * @return recommended next POST state *************************************************************************/ -static POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ) +static HD_POST_STATE_T handlePOSTStatus( SELF_TEST_STATUS_T testStatus ) { - POST_STATE_T result = postState; + HD_POST_STATE_T result = postState; - if ( ( testStatus == SELF_TEST_STATUS_PASSED ) || ( testStatus == SELF_TEST_STATUS_FAILED ) ) +#ifdef BOARD_WITH_NO_HARDWARE + testStatus = SELF_TEST_STATUS_PASSED; +#endif + + if ( testStatus == SELF_TEST_STATUS_PASSED ) { - result = (POST_STATE_T)((int)postState + 1); // move on to next POST test - if ( testStatus == SELF_TEST_STATUS_FAILED ) + // 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. + + sendUIVersionRequest(); + + // 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; + +#ifndef DISABLE_UI_POST_TEST + 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 ) + } +#endif + + 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 ) +{ + +#ifndef DISABLE_UI_POST_TEST + 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 ) { - tempPOSTPassed = FALSE; + result = SELF_TEST_STATUS_PASSED; } + else + { + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_UI_POST_FAILED, 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_FAILED, 1 ) + result = SELF_TEST_STATUS_FAILED; + } +#else + SELF_TEST_STATUS_T result = SELF_TEST_STATUS_PASSED; +#endif return result; + } + +/**@}*/