Index: firmware/App/Controllers/BloodLeak.c =================================================================== diff -u -r62e9c09c41d40bae4326dce1c19a62020168d057 -r46b163d19c65e8c21db7b0247bbb1af0dba1ece5 --- firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 62e9c09c41d40bae4326dce1c19a62020168d057) +++ firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 46b163d19c65e8c21db7b0247bbb1af0dba1ece5) @@ -8,7 +8,7 @@ * @file BloodLeak.c * * @author (last) Dara Navaei -* @date (last) 04-Aug-2022 +* @date (last) 22-Sep-2022 * * @author (original) Peman Montazemi * @date (original) 18-Mar-2021 @@ -54,7 +54,6 @@ #define BLOOD_LEAK_ZERO_CMD_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Blood leak zero command timeout in milliseconds. #define BLOOD_LEAK_SELF_TEST_CMD_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Blood leak self test command timeout in milliseconds. #define BLOOD_LEAK_BETWEEN_ZERO_ST_WAIT_MS 250 ///< Blood leak wait time in between zero and self test commands in milliseconds. -#define BLOOD_LEAK_MIN_WAIT_TIME_2_GET_CAL_MS ( 2 * MS_PER_SECOND ) ///< Blood leak minimum wait time to get calibration in milliseconds. #define DATA_PUBLISH_COUNTER_START_COUNT 60 ///< Data publish counter start count. // Embedded mode defines @@ -128,7 +127,7 @@ U32 timeoutMS; ///< Blood leak sensor receive timeout in milliseconds. U32 commandResp; ///< Blood leak sensor command response back. U08 commandRetryCount; ///< Blood leak sensor command retry count. - BOOL isCmdRespRdy; ///< Blood leak sensor is command response ready flag. + BOOL isCmdRespRdy; ///< Blood leak sensor is command response ready flag. } EMB_MODE_CMD_T; // ********** private data ********** @@ -145,7 +144,6 @@ static U32 bloodLeakSetPointSeqLength; ///< Blood leak set point sequence actual length. static U08 bloodLeakSetPointSequence[ BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH ][ 2 ]; ///< Blood leak set point sequence array. static HD_BLOOD_LEAK_SENSOR_CAL_RECORD_T bloodLeakCalRecord; ///< Blood leak calibration record structure. -static U32 bloodLeakGetCalStartTime; ///< Blood leak get calibration start time. static BOOL bloodLeakExitNormalRequested; ///< Blood leak exit normal state requested. // Embedded mode variables @@ -194,7 +192,7 @@ * @details Outputs: bloodLeakState, bloodLeakStatus, bloodLeakSelfTestStatus, * bloodLeakUARTCmdIndex, bloodLeakSetPointSequence * bloodLeakDataPublicationTimerCounter, bloodLeakEmbModeHasZeroBeenRqustd - * bloodLeakGetCalStartTime, bloodLeakEmbModeSubstate + * bloodLeakEmbModeSubstate * bloodLeakPersistenceCtr, bloodLeakSignalEmbModeReq, * bloodLeakEmbModeRqstedCmd, bloodLeakEmbModeOpsStartTime, bloodLeakEmbModeRespBuffer, * bloodLeakEmbModeRespIndex, bloodLeakExitNormalRequested, @@ -215,7 +213,6 @@ bloodLeakSelfTestStatus = SELF_TEST_STATUS_IN_PROGRESS; bloodLeakUARTCmdIndex = 0; bloodLeakSetPointSeqLength = 0; - bloodLeakGetCalStartTime = getMSTimerCount(); bloodLeakPersistenceCtr = 0; bloodLeakSignalEmbModeReq = FALSE; bloodLeakEmbModeRqstedCmd = NU_EMB_MODE_CMD; @@ -250,25 +247,17 @@ /*********************************************************************//** * @brief * The execBloodLeak function executes the blood leak detector driver. - * @details Inputs: bloodLeakGetCalStartTime, bloodLeakSignalEmbeddedModeReq - * @details Outputs: bloodLeakStatus, bloodLeakGetCalStartTime + * @details Inputs: bloodLeakState + * @details Outputs: bloodLeakCalRecord, bloodLeakState * @return none *************************************************************************/ void execBloodLeak( void ) { - // Check if there is a new calibration data available and whether there has been sufficient time elapsed from the last data calibration get - // The elapsed time is used to make sure the prepare buffer is not called several time when the new calibration signal is TRUE for one second - // otherwise, the prepareSetPointSeq is called multiple time and zeros the start index. In the mean time, the state might have changed to set - // the set point and with the start index being reset to 0, it keeps sending the first character of the set point ('S'). - if ( ( TRUE == isNewCalibrationRecordAvailable() ) && ( calcTimeSince( bloodLeakGetCalStartTime ) > BLOOD_LEAK_MIN_WAIT_TIME_2_GET_CAL_MS ) ) + if ( TRUE == isNewCalibrationRecordAvailable() ) { U32 length = sizeof( HD_BLOOD_LEAK_SENSOR_CAL_RECORD_T ); getNVRecord2Driver( GET_CAL_BLOOD_LEAK_SENSOR, (U08*)&bloodLeakCalRecord, length, 0, ALARM_ID_HD_BLOOD_LEAK_INVALID_CAL_RECORD ); - prepareSetPointSeq( bloodLeakCalRecord.setPoint ); - - // Set the calibration start time to make sure the prepare buffer is not set multiple times in a row - bloodLeakGetCalStartTime = getMSTimerCount(); } switch( bloodLeakState ) @@ -307,8 +296,8 @@ * @brief * The execBloodLeakEmbModeCommand function executes the blood leak embedded * mode command. - * @details Inputs: bloodLeakGetCalStartTime, bloodLeakSignalEmbeddedModeReq - * @details Outputs: bloodLeakStatus, bloodLeakGetCalStartTime + * @details Inputs: bloodLeakEmbModeSubstate + * @details Outputs: bloodLeakEmbModeSubstate * @return none *************************************************************************/ void execBloodLeakEmbModeCommand( void ) @@ -397,7 +386,7 @@ * The handleBloodLeakWaitForPostState function handles the wait for POST * state of the of blood leak state machine. * @details Inputs: bloodLeakEmbModeCmd - * @details Outputs: none + * @details Outputs: bloodLeakEmbModeCmd * @return next state *************************************************************************/ static BLOOD_LEAK_STATE_T handleBloodLeakWaitForPostState( void ) @@ -445,8 +434,8 @@ * @brief * The handleBloodLeakCheckSetPointState function handles the check set point * state to ensure the set point is set correctly. - * @details Inputs: none - * @details Outputs: bloodLeakUARTCmdIndex + * @details Inputs: bloodLeakCalRecord + * @details Outputs: bloodLeakCalRecord * @return next state *************************************************************************/ static BLOOD_LEAK_STATE_T handleBloodLeakCheckSetPointState( void ) @@ -485,7 +474,7 @@ * @brief * The handleBloodLeakInitState function handles the Blood Leak module in init * state. - * @details Inputs: none + * @details Inputs: bloodLeakEmbModeHasZeroBeenRqustd * @details Outputs: none * @return next state *************************************************************************/ @@ -535,6 +524,7 @@ { // Enqueue the zero and self test sequence (Z->G->Z->Q->T) BOOL hasCmdSqncFailed = FALSE; + BOOL hasCurrCmdFailed = FALSE; U08 failedCmd; for ( i = 0; i < NUM_OF_EMB_CMDS; i++ ) @@ -545,14 +535,16 @@ case Q_EMB_MODE_CMD: case T_EMB_MODE_CMD: // Check to see if any of the command responses failed - hasCmdSqncFailed |= ( BLOOD_LEAK_EMB_MODE_FAIL_ASCII == bloodLeakEmbModeCmd[ i ].commandResp ? TRUE : FALSE ); - failedCmd = ( TRUE == hasCmdSqncFailed ? i : NU_EMB_MODE_CMD ); + hasCurrCmdFailed = ( BLOOD_LEAK_EMB_MODE_FAIL_ASCII == bloodLeakEmbModeCmd[ i ].commandResp ? TRUE : FALSE ); + hasCmdSqncFailed |= hasCurrCmdFailed; + failedCmd = ( TRUE == hasCurrCmdFailed ? i : NU_EMB_MODE_CMD ); break; case G_EMB_MODE_CMD: // G command will return a value that has to be greater than 0 - hasCmdSqncFailed |= ( bloodLeakEmbModeCmd[ i ].commandResp > 0 ? FALSE : TRUE ); - failedCmd = ( TRUE == hasCmdSqncFailed ? i : NU_EMB_MODE_CMD ); + hasCurrCmdFailed = ( bloodLeakEmbModeCmd[ i ].commandResp > 0 ? FALSE : TRUE ); + hasCmdSqncFailed |= hasCurrCmdFailed; + failedCmd = ( TRUE == hasCurrCmdFailed ? i : NU_EMB_MODE_CMD ); break; } } @@ -584,24 +576,16 @@ * @brief * The handleBloodLeakNormalState function handles the Blood Leak module * in normal state. - * @details Inputs: none - * @details Outputs: Blood Leak module normal. + * @details Inputs: bloodLeakStatus, bloodLeakPersistenceCtr, + * bloodLeakExitNormalRequested + * @details Outputs: bloodLeakStatus, bloodLeakPersistenceCtr, + * bloodLeakExitNormalRequested, bloodLeakEmbModeHasZeroBeenRqustd * @return next state *************************************************************************/ static BLOOD_LEAK_STATE_T handleBloodLeakNormalState( void ) { BLOOD_LEAK_STATE_T state = BLOOD_LEAK_NORMAL_STATE; - // TODO remove - /*if ( TRUE == noFPGABloodLeakDetected() ) // TODO remove noFPGABloodLeakDetected() - { - bloodLeakStatus.data = BLOOD_LEAK_NOT_DETECTED; - } - else - { - bloodLeakStatus.data = BLOOD_LEAK_DETECTED; - }*/ - // If the blood leak status bit is low (0) it means blood has not been detected, otherwise, blood has been detected bloodLeakStatus.data = ( BLOOD_LEAK_STATUS_BIT_LOW == getFPGABloodLeakStatus() ? BLOOD_LEAK_NOT_DETECTED : BLOOD_LEAK_DETECTED ); @@ -695,8 +679,10 @@ * The handleBloodLeakEmbModeWaitForCommandState function handles the wait for * command state. The state prepares the message to be sent to the blood leak * sensor. - * @details Inputs: bloodLeakCalCharacter - * @details Outputs: bloodLeakCalCommandSquence, bloodLeakUARTCmdIndex + * @details Inputs: bloodLeakEmbModeRqstedCmd, bloodLeakEmbModeCmd + * @details Outputs: bloodLeakEmbModeRqstedCmd, bloodLeakEmbModeCmdSeq, + * bloodLeakUARTCmdIndex, bloodLeakEmbModeRespIndex, bloodLeakEmbModeCmdSeqLength, + * bloodLeakEmbModeOpsStartTime, bloodLeakEmbModeSetPoint, bloodLeakEmbModeRespBuffer * @return next state *************************************************************************/ static BLOOD_LEAK_EMB_MODE_STATE_T handleBloodLeakEmbModeWaitForCommandState( void ) @@ -771,10 +757,12 @@ /*********************************************************************//** * @brief * The handleBloodLeakEmbModeSendCommandState function handles the send command - * state. The state sends the command sequence to the blood leak sensor one by - * one. - * @details Inputs: bloodLeakCalCharacter, bloodLeakUARTCmdIndex - * @details Outputs: bloodLeakCalCommandSquence, bloodLeakUARTCmdIndex + * state. The state sends the command sequence to the blood leak sensor one by one. + * @details Inputs: bloodLeakEmbModeRqstedCmd, bloodLeakCalCharacter, bloodLeakUARTCmdIndex + * bloodLeakEmbModeCmdSeqLength + * @details Outputs: bloodLeakEmbModeRqstedCmd, bloodLeakCalCommandSquence, + * bloodLeakUARTCmdIndex, bloodLeakEmbModeCmdSeqLength, bloodLeakEmbModeCmdSeqLength, + * bloodLeakSetPointSequence * @return next state *************************************************************************/ static BLOOD_LEAK_EMB_MODE_STATE_T handleBloodLeakEmbModeSendCommandState( void ) @@ -834,7 +822,14 @@ // Check if the current index towards the end of the buffer which are FIFO set and FIFO reset if ( FALSE == isUARTCtrlDone ) { - FALSE == isUARTCtrlCmd ? setFPGABloodLeakUARTTransmit( command ) : setFPGABloodLeakUARTControl( command ); + if ( FALSE == isUARTCtrlCmd ) + { + setFPGABloodLeakUARTTransmit( command ); + } + else + { + setFPGABloodLeakUARTControl( command ); + } } else { @@ -854,8 +849,10 @@ * @brief * The handleBloodLeakEmbModeWaitForCommandResponseState function handles the * wait for command responses state. The state receives data in the buffer. - * @details Inputs: bloodLeakEmbModeCmd, bloodLeakEmbModeRespIndex - * @details Outputs: bloodLeakEmbModeRespBuffer, bloodLeakEmbModeRespIndex + * @details Inputs: bloodLeakEmbModeHasRxRqstBeenSent, bloodLeakEmbModeRespBuffer, + * bloodLeakEmbModeRespIndex, bloodLeakEmbModeRqstedCmd, bloodLeakEmbModeOpsStartTime + * @details Outputs: bloodLeakEmbModeCmd, bloodLeakEmbModeRqstedCmd, + * bloodLeakEmbModeCmdSeq, bloodLeakEmbModeRespBuffer * @return next state *************************************************************************/ static BLOOD_LEAK_EMB_MODE_STATE_T handleBloodLeakEmbModeWaitForCommandResponseState( void ) @@ -1294,9 +1291,10 @@ /*********************************************************************//** * @brief - * The enqueueEmbModeCmd function enqueues the embedded mode command. - * @details Inputs: bloodLeakEmbModeCmdQRearIndex + * The enqueueEmbModeCmd function enqueues an embedded mode command. + * @details Inputs: none * @details Outputs: bloodLeakEmbModeCmdQRearIndex, bloodLeakEmbModeCmdQCount + * bloodLeakEmbModeCmdQ, bloodLeakEmbModeCmd * @return none *************************************************************************/ static void enqueueEmbModeCmd( U08 cmd ) @@ -1491,7 +1489,7 @@ * The testSetBloodLeakEmbeddedModeCommand function sets the blood leak * calibration command. * @details Inputs: none - * @details Outputs: bloodLeakEmbModeRqstedCmd, bloodLeakEmbModeSetPoint + * @details Outputs: bloodLeakEmbModeSetPoint * @param command the command ID to be sent to blood leak in embedded mode * @param setpointPayload the set point value that is sent with the set point * command. This value is 0 with other commands since they do not have a payload @@ -1505,8 +1503,8 @@ { enqueueEmbModeCmd( command ); - bloodLeakEmbModeSetPoint = setPointPayload; - result = TRUE; + bloodLeakEmbModeSetPoint = setPointPayload; + result = TRUE; } return result;