Index: firmware/App/Controllers/BloodLeak.c =================================================================== diff -u -r8b3e76567196376979b8173147b2dea2506c5f31 -r4e4ab946c0bc4b668cf5b197c7f841355814ccf5 --- firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 8b3e76567196376979b8173147b2dea2506c5f31) +++ firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 4e4ab946c0bc4b668cf5b197c7f841355814ccf5) @@ -16,6 +16,7 @@ ***************************************************************************/ #include // For sprintf and strlen +#include #include "AlarmMgmt.h" #include "BloodLeak.h" @@ -36,25 +37,16 @@ #define BLOOD_LEAK_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Interval (ms/task time) at which the blood leak data is published on the CAN bus. #define BLOOD_LEAK_PERSISTENCE ( 10 * MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Persistence for blood leak detected alarm. #define BLOOD_LEAK_RESET_TX_FIFO 2 ///< Blood leak reset transmit FIFO command. -#define BLOOD_LEAK_START_COMM_CTRL_U_ASCII 21 ///< Blood leak start communication command, ^U (Ctrl-U) in ascii. #define BLOOD_LEAK_UART_COMM_ACTIVE_LOW 0 ///< Blood leak UART communication active low command. #define BLOOD_LEAK_UART_COMM_ACTIVE_HIGH 1 ///< Blood leak UART communication active high command. -#define BLOOD_LEAK_STARTUP_SEQ_LENGTH 6 ///< Blood leak start up sequence array length. -#define BLOOD_LEAK_START_FIFO_CTRL_U_INDEX 2 ///< Blood leak start communication sequence ^U index. -#define BLOOD_LEAK_START_FIFO_STOP_INDEX 3 ///< Blood leak start communication sequence stop write to FIFO index. - #define BLOOD_LEAK_SET_POINT_MAX_CHAR_LENGTH 11 ///< Blood leak set point maximum character length. #define BLOOD_LEAK_SET_POINT_START_CHAR_ASCII 83 ///< Blood leak set point sequence start character in ASCII (letter S). #define BLOOD_LEAK_STOP_WRITE_FIFO_COMMAND 0 ///< Blood leak set point stop writing to FIFO command. #define BLOOD_LEAK_SET_POINT_START_CHAR_INDEX 0 ///< Blood leak set point sequence start character index number. #define BLOOD_LEAK_CARRIAGE_RETURN_ASCII 13 ///< Blood leak set point sequence carriage return character in ASCII. #define BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH 17 ///< Blood leak set point sequence maximum length. -#define BLOOD_LEAK_WAIT_2_READ_SET_POINT ( 1 * MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Blood leak wait to read set point in counts. -#define BLOOD_LEAK_WAIT_2_READ_CTRL_U ( 1 * MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Blood leak wait to read control U in counts. -#define BLOOD_LEAK_MAX_SET_POINT_WRITE_TRIALS 3 ///< Blood leak maximum number of trials to write the set point. -#define BLOOD_LEAK_MAX_CTRL_U_WRITE_TRIALS 3 ///< Blood leak maximum number of trials to write the control U command. #define BLOOD_LEAK_ZERO_CMD_STATUS_READY 0x80 ///< Blood leak zero command status ready value. #define BLOOD_LEAK_SELF_TEST_CMD_STATUS_READY 0x80 ///< Blood leak self test command status ready value. #define BLOOD_LEAK_STATUS_BIT_HIGH 1 ///< Blood leak status bit high. @@ -63,7 +55,6 @@ #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 @@ -75,7 +66,7 @@ #define BLOOD_LEAK_EMB_MODE_COMM_READ_REQST 12 ///< Blood leak embedded mode communication read Rx byte request. #define BLOOD_LEAK_EMB_MODE_RX_BUFFER_EMPTY 0x80 ///< Blood leak embedded mode buffer empty value. #define BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN 5 ///< Blood leak embedded mode number of commands. -#define BLOOD_LEAK_EMB_MODE_MAX_NUM_CMD_TRIES 4 ///< Blood leak embedded mode max number of command tries. +#define BLOOD_LEAK_EMB_MODE_MAX_NUM_CMD_TRIES 3 ///< Blood leak embedded mode max number of command tries. #define BLOOD_LEAK_EMB_MODE_0_NUM_ASCII 48 ///< Blood leak embedded mode character 0 in ASCII. #define BLOOD_LEAK_EMB_MODE_9_NUM_ASCII 57 ///< Blood leak embedded mode character 9 in ASCII. #define BLOOD_LEAK_EMB_MODE_SET_PNT_RESP_LEN 4 ///< Blood leak embedded mode set point response length. @@ -95,17 +86,10 @@ typedef enum BloodLeakStates { BLOOD_LEAK_WAIT_FOR_POST_STATE = 0, ///< Wait for post state. - BLOOD_LEAK_START_UP_STATE, ///< Start up state. - BLOOD_LEAK_CHECK_COMM_STATE, ///< Check communication (Control U) state. BLOOD_LEAK_CHECK_SET_POINT_STATE, ///< Check set point state. - BLOOD_LEAK_SET_SET_POINT_STATE, ///< Set set point state. BLOOD_LEAK_INIT_STATE, ///< Init state. - BLOOD_LEAK_PREP_FOR_ZERO_CMD_STATE, ///< Prepare for zero command state. - BLOOD_LEAK_ZERO_STATE, ///< Zero state. - BLOOD_LEAK_PREP_FOR_SELF_TEST_CMD_STATE, ///< Prepare for self test command state. - BLOOD_LEAK_SELF_TEST_STATE, ///< Self-test state. + BLOOD_LEAK_CHECK_ZERO_AND_SELF_TEST_STATE, ///< Blood leak check for zero and self test commands state. BLOOD_LEAK_NORMAL_STATE, ///< Normal state. - BLOOD_LEAK_CALIBRATION_STATE, ///< Calibration state. NUM_OF_BLOOD_LEAK_STATES ///< Number of blood leak detector states. } BLOOD_LEAK_STATE_T; @@ -142,24 +126,16 @@ U08 expChar2; ///< Blood leak sensor expected response in character. U32 length; ///< Blood leak sensor expected response length in bytes. U32 timeoutMS; ///< Blood leak sensor receive timeout in milliseconds. - U16 commandResp; ///< Blood leak sensor command response back. + 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. } EMB_MODE_CMD_T; -/// Zero command structure -typedef struct -{ - BOOL cmdZeroRequested; ///< Blood leak zero command requested. - U08 cmdPrevStatus; ///< Blood leak previous zero/self test status. - U32 cmdStartTimeMS; ///< Blood leak zero/self test command start time in milliseconds. - U32 cmdWaitBtwnZeroSTestMS; ///< Blood leak wait time in between zero and self test in milliseconds. -} BLOOD_LEAK_GPIO_CMD_T; - // ********** private data ********** static BLOOD_LEAK_STATE_T bloodLeakState; ///< Current state of blood leak state machine. static OVERRIDE_U32_T bloodLeakStatus; ///< Detected blood leak status for blood leak detector. static SELF_TEST_STATUS_T bloodLeakSelfTestStatus; ///< Current status of blood leak self-test. -static BOOL bloodLeakIsPOSTComplete; ///< Blood leak is POST complete flag. static U32 bloodLeakPersistenceCtr; ///< Blood leak alarm persistence timer counter. static OVERRIDE_U32_T bloodLeakDataPublishInterval = { BLOOD_LEAK_PUB_INTERVAL, @@ -168,21 +144,10 @@ static U32 bloodLeakUARTCmdIndex; ///< Blood leak UART command index. 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 U32 bloodLeakWait2ReadResponseCounter; ///< Blood leak wait to read response counter. -static U32 bloodLeakCommandWriteTryCount; ///< Blood leak current set point write try number. static HD_BLOOD_LEAK_SENSOR_CAL_RECORD_T bloodLeakCalRecord; ///< Blood leak calibration record structure. static U32 bloodLeakGetCalStartTime; ///< Blood leak get calibration start time. -static U32 bloodLeakPrevFPGARegisterCount; ///< Blood leak previous FPGA communications counter. static BOOL bloodLeakExitNormalRequested; ///< Blood leak exit normal state requested. -static BLOOD_LEAK_GPIO_CMD_T bloodLeakGPIOCmd; ///< Blood leak zero/self test GPIO command. -/// Blood leak start up sequence array. -static const U08 BLOOD_LEAK_START_UP_SEQUENCE[ BLOOD_LEAK_STARTUP_SEQ_LENGTH ] = { BLOOD_LEAK_RESET_TX_FIFO, - BLOOD_LEAK_UART_COMM_ACTIVE_LOW, - BLOOD_LEAK_START_COMM_CTRL_U_ASCII, - BLOOD_LEAK_STOP_WRITE_FIFO_COMMAND, - BLOOD_LEAK_UART_COMM_ACTIVE_HIGH, - BLOOD_LEAK_UART_COMM_ACTIVE_LOW }; // TODO remove // Embedded mode variables static BOOL bloodLeakSignalEmbModeReq; ///< Blood leak signal embedded mode has been requested. static U08 bloodLeakEmbModeRqstedCmd; ///< Blood leak signal embedded mode requested command. @@ -195,35 +160,29 @@ static U32 bloodLeakEmbModeRespIndex; ///< Blood leak embedded mode response buffer index. static U32 bloodLeakEmbModeCmdSeqLength; ///< Blood leak embedded mode command sequence length. static BOOL bloodLeakEmbModeHasRxRqstBeenSent; ///< Blood leak embedded mode Rx request has been sent signal. -static U08 bloodLeakEmbModeCmdQ[ BLOOD_LEAK_EMB_MODE_CMD_Q_MAX_SIZE ]; -static U08 bloodLeakEmbModeCmdQRearIndex; -static U08 bloodLeakEmbModeCmdQFrontIndex; -static U08 bloodLeakEmbModeCmdQCount; +static U08 bloodLeakEmbModeCmdQ[ BLOOD_LEAK_EMB_MODE_CMD_Q_MAX_SIZE ]; ///< Blood leak embedded mode command queue. +static U08 bloodLeakEmbModeCmdQRearIndex; ///< Blood leak embedded mode command queue rear index. +static U08 bloodLeakEmbModeCmdQFrontIndex; ///< Blood leak embedded mode command queue front index. +static U08 bloodLeakEmbModeCmdQCount; ///< Blood leak embedded mode command queue count. +static BOOL bloodLeakEmbModeHasZeroBeenRqustd; ///< Blood leak embedded mode flag to indicate zero has been requested. // ********** private function prototypes ********** static BLOOD_LEAK_STATE_T handleBloodLeakWaitForPostState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakStartupState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakCheckCommState( void ); static BLOOD_LEAK_STATE_T handleBloodLeakCheckSetPointState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakSetSetPointState( void ); static BLOOD_LEAK_STATE_T handleBloodLeakInitState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakPrepForZeroCmdState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakZeroState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakPrepForSelfTestCmdState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakSelfTestState( void ); +static BLOOD_LEAK_STATE_T handleBloodLeakCheckZeroAndSelfTestState( void ); static BLOOD_LEAK_STATE_T handleBloodLeakNormalState( void ); -static BLOOD_LEAK_STATE_T handleBloodLeakCalibrationState( void ); static BLOOD_LEAK_EMB_MODE_STATE_T handleBloodLeakEmbModeWaitForCommandState( void ); static BLOOD_LEAK_EMB_MODE_STATE_T handleBloodLeakEmbModeSendCommandState( void ); static BLOOD_LEAK_EMB_MODE_STATE_T handleBloodLeakEmbModeWaitForCommandResponseState( void ); static void processReceivedEmbModeChar( U08 data ); -static void prepareSetPointSeq( U16 setPoint ); // TODO remove +static void convertString2Integer( U08 cmd, U32 respLength ); +static void prepareSetPointSeq( U16 setPoint ); static void publishBloodLeakData( void ); static void initEmbModeSpecs( void ); -static BOOL hasZeroBloodLeakBeenRequested( void ); static void enqueueEmbModeCmd( U08 cmd ); static U08 dequeueEmbModeCmd( void ); static BOOL isEmbModeCmdQueueEmpty( void ); @@ -233,10 +192,10 @@ * The initBloodLeak function initializes the Blood Leak module. * @details Inputs: none * @details Outputs: bloodLeakState, bloodLeakStatus, bloodLeakSelfTestStatus, - * bloodLeakUARTCmdIndex, bloodLeakSetPointSequence, bloodLeakPrevFPGARegisterCount - * bloodLeakWait2ReadResponseCounter, bloodLeakDataPublicationTimerCounter, - * bloodLeakCommandWriteTryCount, bloodLeakGetCalStartTime, bloodLeakEmbModeSubstate - * bloodLeakIsPOSTComplete, bloodLeakPersistenceCtr, bloodLeakSignalEmbModeReq, + * bloodLeakUARTCmdIndex, bloodLeakSetPointSequence + * bloodLeakDataPublicationTimerCounter, bloodLeakEmbModeHasZeroBeenRqustd + * bloodLeakGetCalStartTime, bloodLeakEmbModeSubstate + * bloodLeakPersistenceCtr, bloodLeakSignalEmbModeReq, * bloodLeakEmbModeRqstedCmd, bloodLeakEmbModeOpsStartTime, bloodLeakEmbModeRespBuffer, * bloodLeakEmbModeRespIndex, bloodLeakExitNormalRequested, * bloodLeakEmbModeCmdSeqLength, bloodLeakEmbModeHasRxRqstBeenSent @@ -256,11 +215,7 @@ bloodLeakSelfTestStatus = SELF_TEST_STATUS_IN_PROGRESS; bloodLeakUARTCmdIndex = 0; bloodLeakSetPointSeqLength = 0; - bloodLeakWait2ReadResponseCounter = 0; - bloodLeakCommandWriteTryCount = 0; bloodLeakGetCalStartTime = getMSTimerCount(); - bloodLeakIsPOSTComplete = FALSE; - bloodLeakPrevFPGARegisterCount = 0; bloodLeakPersistenceCtr = 0; bloodLeakSignalEmbModeReq = FALSE; bloodLeakEmbModeRqstedCmd = NU_EMB_MODE_CMD; @@ -273,6 +228,7 @@ bloodLeakEmbModeCmdQFrontIndex = 0; bloodLeakEmbModeCmdQRearIndex = 0; bloodLeakEmbModeCmdQCount = 0; + bloodLeakEmbModeHasZeroBeenRqustd = FALSE; // Set the blood leak embedded mode command queue to zero memset( bloodLeakEmbModeCmdQ, 0x0, BLOOD_LEAK_EMB_MODE_CMD_Q_MAX_SIZE ); @@ -286,8 +242,9 @@ // Initialize the blood leak embedded mode command sequence memset( bloodLeakEmbModeCmdSeq, 0x0, BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH ); + // Enqueue the commands to set the embedded mode and request the set point of the blood leak sensor enqueueEmbModeCmd( CS_EMB_MODE_CMD ); - enqueueEmbModeCmd( SP_EMB_MODE_CMD ); + enqueueEmbModeCmd( D_EMB_MODE_CMD ); } /*********************************************************************//** @@ -310,72 +267,36 @@ getNVRecord2Driver( GET_CAL_BLOOD_LEAK_SENSOR, (U08*)&bloodLeakCalRecord, length, 0, ALARM_ID_HD_BLOOD_LEAK_INVALID_CAL_RECORD ); prepareSetPointSeq( bloodLeakCalRecord.setPoint ); - // Force the state machine to go back to set the set point that has been received from - // the calibration data - bloodLeakState = BLOOD_LEAK_SET_SET_POINT_STATE; - // Set the calibration start time to make sure the prepare buffer is not set multiple times in a row bloodLeakGetCalStartTime = getMSTimerCount(); } - // Check if the mode is not in init and then run the sensor - if ( getCurrentOperationMode() != MODE_INIT ) + switch( bloodLeakState ) { - switch( bloodLeakState ) - { - case BLOOD_LEAK_WAIT_FOR_POST_STATE: - bloodLeakState = handleBloodLeakWaitForPostState(); - break; + case BLOOD_LEAK_WAIT_FOR_POST_STATE: + bloodLeakState = handleBloodLeakWaitForPostState(); + break; - case BLOOD_LEAK_START_UP_STATE: - bloodLeakState = handleBloodLeakStartupState(); - break; + case BLOOD_LEAK_CHECK_SET_POINT_STATE: + bloodLeakState = handleBloodLeakCheckSetPointState(); + break; - case BLOOD_LEAK_CHECK_COMM_STATE: - bloodLeakState = handleBloodLeakCheckCommState(); - break; + case BLOOD_LEAK_INIT_STATE: + bloodLeakState = handleBloodLeakInitState(); + break; - case BLOOD_LEAK_CHECK_SET_POINT_STATE: - bloodLeakState = handleBloodLeakCheckSetPointState(); - break; + case BLOOD_LEAK_CHECK_ZERO_AND_SELF_TEST_STATE: + bloodLeakState = handleBloodLeakCheckZeroAndSelfTestState(); + break; - case BLOOD_LEAK_SET_SET_POINT_STATE: - bloodLeakState = handleBloodLeakSetSetPointState(); - break; + case BLOOD_LEAK_NORMAL_STATE: + bloodLeakState = handleBloodLeakNormalState(); + break; - case BLOOD_LEAK_INIT_STATE: - bloodLeakState = handleBloodLeakInitState(); - break; - - case BLOOD_LEAK_PREP_FOR_ZERO_CMD_STATE: - bloodLeakState = handleBloodLeakPrepForZeroCmdState(); - break; - - case BLOOD_LEAK_ZERO_STATE: - bloodLeakState = handleBloodLeakZeroState(); - break; - - case BLOOD_LEAK_PREP_FOR_SELF_TEST_CMD_STATE: - bloodLeakState = handleBloodLeakPrepForSelfTestCmdState(); - break; - - case BLOOD_LEAK_SELF_TEST_STATE: - bloodLeakState = handleBloodLeakSelfTestState(); - break; - - case BLOOD_LEAK_NORMAL_STATE: - bloodLeakState = handleBloodLeakNormalState(); - break; - - case BLOOD_LEAK_CALIBRATION_STATE: - //bloodLeakState = handleBloodLeakCalibrationState(); - break; - - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_HD_INVALID_BLOOD_LEAK_STATE, bloodLeakState ) - bloodLeakState = BLOOD_LEAK_INIT_STATE; - break; - } + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_HD_INVALID_BLOOD_LEAK_STATE, bloodLeakState ) + bloodLeakState = BLOOD_LEAK_INIT_STATE; + break; } // Publish blood leak data if due @@ -392,15 +313,6 @@ *************************************************************************/ void execBloodLeakEmbModeCommand( void ) { - // If a request to transition to embedded mode has been made, change the state to calibration state, and request a transition to service mode. - // This type of work can only be done in the service mode and then the user has to restart the device to return to the normal operations of BLD. - if ( TRUE == bloodLeakSignalEmbModeReq ) - { - //bloodLeakState = BLOOD_LEAK_CALIBRATION_STATE; - - //requestNewOperationMode( MODE_SERV ); // TODO do we need this - } - switch ( bloodLeakEmbModeSubstate ) { case BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMAND_STATE: @@ -427,8 +339,7 @@ *************************************************************************/ void zeroBloodLeak( void ) { - bloodLeakGPIOCmd.cmdZeroRequested = TRUE; - bloodLeakUARTCmdIndex = 0; + bloodLeakEmbModeHasZeroBeenRqustd = TRUE; // Enqueue the zero and self test sequence (Z->G->Z->Q->T) enqueueEmbModeCmd( Z_EMB_MODE_CMD ); @@ -471,8 +382,7 @@ if ( TRUE == calStatus ) { - result = SELF_TEST_STATUS_PASSED; - bloodLeakIsPOSTComplete = TRUE; + result = SELF_TEST_STATUS_PASSED; } else { @@ -486,110 +396,43 @@ * @brief * The handleBloodLeakWaitForPostState function handles the wait for POST * state of the of blood leak state machine. - * @details Inputs: bloodLeakIsPOSTComplete, bloodLeakPrevFPGARegisterCount + * @details Inputs: bloodLeakEmbModeCmd * @details Outputs: none * @return next state *************************************************************************/ static BLOOD_LEAK_STATE_T handleBloodLeakWaitForPostState( void ) { BLOOD_LEAK_STATE_T state = BLOOD_LEAK_WAIT_FOR_POST_STATE; + BOOL isEmbModeReady = FALSE; + U32 cmdResp = bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].commandResp; - if ( TRUE == bloodLeakIsPOSTComplete ) + if ( BLOOD_LEAK_EMB_MODE_PASS_ASCII == cmdResp ) { - // Read the value that is in the FPGA counter so it can be used to make sure the blood leak is communicating after - // sending Ctrl U - bloodLeakPrevFPGARegisterCount = getFPGABloodLeakRegisterCounter(); - // TODO this has been disabled for the systems team testing. In their test, ^U is not sent - //state = BLOOD_LEAK_START_UP_STATE; - // TODO decide whether we need to go to ^U upon startup - state = BLOOD_LEAK_INIT_STATE; + isEmbModeReady = TRUE; } - - return state; -} - -/*********************************************************************//** - * @brief - * The handleBloodLeakStartupState function handles the startup state of the - * of blood leak state machine. - * @details Inputs: bloodLeakUARTCmdIndex - * @details Outputs: bloodLeakUARTCmdIndex - * @return next state - *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakStartupState( void ) -{ - // TODO NOTE: This function is bypassed until the use of ^U is finalized by the system's team - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_START_UP_STATE; - - U32 command = BLOOD_LEAK_START_UP_SEQUENCE[ bloodLeakUARTCmdIndex ]; - - // Check if ^U index <= index <= 0 to Tx index. If the current value is either ^U or the 0 to command the FIFO to stop writing - // use the transmit function otherwise, use the UART control. - if ( ( bloodLeakUARTCmdIndex >= BLOOD_LEAK_START_FIFO_CTRL_U_INDEX ) && ( bloodLeakUARTCmdIndex <= BLOOD_LEAK_START_FIFO_STOP_INDEX ) ) + else if ( ( TRUE == bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].isCmdRespRdy ) && ( BLOOD_LEAK_EMB_MODE_FAIL_ASCII == cmdResp ) ) { - setFPGABloodLeakUARTTransmit( (U08)command ); - } - else - { - setFPGABloodLeakUARTControl( (U08)command ); - } - - // Done with writing all the commands, reset the index and transition - if ( bloodLeakUARTCmdIndex >= ( BLOOD_LEAK_STARTUP_SEQ_LENGTH - 1 ) ) - { - // Wait for the sensor to start communication - if ( ++bloodLeakWait2ReadResponseCounter > BLOOD_LEAK_WAIT_2_READ_CTRL_U ) + if ( bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].commandRetryCount < BLOOD_LEAK_EMB_MODE_MAX_NUM_CMD_TRIES ) { - bloodLeakUARTCmdIndex = 0; - state = BLOOD_LEAK_CHECK_COMM_STATE; - } - } - else - { - bloodLeakUARTCmdIndex++; - } + // Enqueue the commands to set the embedded mode and request the set point of the blood leak sensor + // Since set point was requested in the init function as well, both are requested here because we have to be + // in the embedded mode first + enqueueEmbModeCmd( CS_EMB_MODE_CMD ); + enqueueEmbModeCmd( SP_EMB_MODE_CMD ); - return state; -} - -/*********************************************************************//** - * @brief - * The handleBloodLeakCheckCommState function handles the check communication - * state to ensure the sensor is communicating. - * @details Inputs: bloodLeakCommandWriteTryCount, bloodLeakPrevFPGARegisterCount - * @details Outputs: bloodLeakCommandWriteTryCount, bloodLeakPrevFPGARegisterCount - * @return next state - *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakCheckCommState( void ) -{ - // TODO NOTE: This function is bypassed until the use of ^U is finalized by the system's team - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_CHECK_COMM_STATE; - - // Check if the blood leak sensor has started communicating - BOOL isBloodLeakCommunicating = ( bloodLeakPrevFPGARegisterCount != getFPGABloodLeakRegisterCounter() ? TRUE : FALSE ); - - // The sensor is communicating move on - if ( TRUE == isBloodLeakCommunicating ) - { - state = BLOOD_LEAK_CHECK_SET_POINT_STATE; - bloodLeakCommandWriteTryCount = 0; - } - else - { - // If the sensor is not communicating, and the number of writes has not exceeded try again, otherwise alarm - if ( bloodLeakCommandWriteTryCount < BLOOD_LEAK_MAX_CTRL_U_WRITE_TRIALS ) - { - bloodLeakCommandWriteTryCount++; - - bloodLeakPrevFPGARegisterCount = getFPGABloodLeakRegisterCounter(); - state = BLOOD_LEAK_START_UP_STATE; + bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].commandRetryCount++; } else { - // TODO alarm. Should we check the BLD communications all the time like in the monitor? + // TODO fault, we cannot get to the embedded mode } } + if ( ( getCurrentOperationMode() != MODE_INIT ) && ( TRUE == isEmbModeReady ) ) + { + state = BLOOD_LEAK_CHECK_SET_POINT_STATE; + } + return state; } @@ -604,21 +447,16 @@ static BLOOD_LEAK_STATE_T handleBloodLeakCheckSetPointState( void ) { BLOOD_LEAK_STATE_T state = BLOOD_LEAK_CHECK_SET_POINT_STATE; + U16 bloodLeakSetPoint = bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].commandResp; + BOOL isCommandRespReady = bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].isCmdRespRdy; - U16 bloodLeakSetPoint = getFPGABloodLeakDetectSetPoint(); // TODO is this working in embedded mode? - -#ifdef BOARD_WITH_NO_HARDWARE - // In case it is just a bare board, use this value - bloodLeakCalRecord.setPoint = 20; -#endif - - if ( bloodLeakSetPoint != bloodLeakCalRecord.setPoint ) + if ( ( bloodLeakSetPoint != bloodLeakCalRecord.setPoint ) && ( TRUE == isCommandRespReady ) ) { - if ( bloodLeakCommandWriteTryCount < BLOOD_LEAK_MAX_SET_POINT_WRITE_TRIALS ) + if ( bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].commandRetryCount < BLOOD_LEAK_EMB_MODE_MAX_NUM_CMD_TRIES ) { - prepareSetPointSeq( bloodLeakCalRecord.setPoint ); - bloodLeakCommandWriteTryCount++; - state = BLOOD_LEAK_SET_SET_POINT_STATE; + enqueueEmbModeCmd( D_EMB_MODE_CMD ); + + bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].commandRetryCount++; } else { @@ -630,63 +468,16 @@ } } } - else + else if ( TRUE == isCommandRespReady ) { - state = BLOOD_LEAK_INIT_STATE; - bloodLeakCommandWriteTryCount = 0; + state = BLOOD_LEAK_INIT_STATE; } return state; } /*********************************************************************//** * @brief - * The handleBloodLeakSetSetPointState function handles the set set point state. - * @details Inputs: bloodLeakUARTCmdIndex - * @details Outputs: bloodLeakUARTCmdIndex, bloodLeakSetPointSeqLength - * @return next state - *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakSetSetPointState( void ) -{ - // TODO remove this function - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_SET_SET_POINT_STATE; - - // Check if the current buffer index is less than the buffer length - if( bloodLeakUARTCmdIndex < bloodLeakSetPointSeqLength ) - { - U32 command = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ]; - - // The active high index is the length - 2 they are the last two elements - U32 activeHighIndex = bloodLeakSetPointSeqLength - 2; - - // Check if the current index towards the end of the buffer which are FIFO set and FIFO reset - if( activeHighIndex > bloodLeakUARTCmdIndex ) - { - setFPGABloodLeakUARTTransmit( (U08)command ); - } - else - { - setFPGABloodLeakUARTControl( (U08)command ); - } - - bloodLeakUARTCmdIndex++; - } - else - { - if ( ++bloodLeakWait2ReadResponseCounter > BLOOD_LEAK_WAIT_2_READ_SET_POINT ) - { - bloodLeakWait2ReadResponseCounter = 0; - bloodLeakUARTCmdIndex = 0; - // Done with writing the set point - state = BLOOD_LEAK_CHECK_SET_POINT_STATE; - } - } - - return state; -} - -/*********************************************************************//** - * @brief * The handleBloodLeakInitState function handles the Blood Leak module in init * state. * @details Inputs: none @@ -698,174 +489,75 @@ BLOOD_LEAK_STATE_T state = BLOOD_LEAK_INIT_STATE; // Check if the zero command has been requested - state = ( TRUE == hasZeroBloodLeakBeenRequested() ? BLOOD_LEAK_PREP_FOR_ZERO_CMD_STATE : state ); - - return state; -} - -/*********************************************************************//** - * @brief - * The handleBloodLeakPrepForZeroCmdState function handles the blood leak - * prepare for zero command state. - * @details Inputs: bloodLeakCmd - * @details Outputs: bloodLeakCmd - * @return next state - *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakPrepForZeroCmdState( void ) -{ - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_PREP_FOR_ZERO_CMD_STATE; - U08 zeroCmdStatus = getFPGABloodLeakZeroErrorCounter() & BLOOD_LEAK_ZERO_CMD_STATUS_READY; - - // Check if the zero command is ready to receive a command and the zero status is high - if ( ( BLOOD_LEAK_ZERO_CMD_STATUS_READY == zeroCmdStatus ) && ( BLOOD_LEAK_STATUS_BIT_HIGH == getFPGABloodLeakZeroStatus() ) ) + if ( TRUE == bloodLeakEmbModeHasZeroBeenRqustd ) { - // The conditions are met start the command - // Start with the previous status bit is high - state = BLOOD_LEAK_ZERO_STATE; - bloodLeakGPIOCmd.cmdStartTimeMS = getMSTimerCount(); - bloodLeakGPIOCmd.cmdPrevStatus = BLOOD_LEAK_STATUS_BIT_HIGH; - setFPGABloodLeakZero(); + bloodLeakEmbModeHasZeroBeenRqustd = FALSE; + state = BLOOD_LEAK_CHECK_ZERO_AND_SELF_TEST_STATE; } - else if ( TRUE == didTimeout( bloodLeakGPIOCmd.cmdStartTimeMS, BLOOD_LEAK_ZERO_CMD_TIMEOUT_MS ) ) - { -#ifndef _RELEASE_ - if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_BLOOD_LEAK_ALARM ) != SW_CONFIG_ENABLE_VALUE ) -#endif - { - activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_ZERO_CMD_SEND_TIMEOUT ); - } - state = BLOOD_LEAK_INIT_STATE; - } - return state; } /*********************************************************************//** * @brief - * The handleBloodLeakZeroState function handles the Blood Leak module in zeroing - * state. - * @details Inputs: none - * @details Outputs: Blood Leak module zeroing. + * The handleBloodLeakCheckZeroAndSelfTestState function handles the blood + * leak check zero and self test state. + * @details Inputs: bloodLeakEmbModeCmd + * @details Outputs: bloodLeakEmbModeCmd * @return next state *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakZeroState( void ) +static BLOOD_LEAK_STATE_T handleBloodLeakCheckZeroAndSelfTestState( void ) { - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_ZERO_STATE; - U08 bloodLeakZeroStatus = getFPGABloodLeakZeroStatus(); + U08 i; + BLOOD_LEAK_STATE_T state = BLOOD_LEAK_CHECK_ZERO_AND_SELF_TEST_STATE; + BOOL areCommandsReady = TRUE; - // Check if the previous zero status bit was a 0 (low) and the current zero status bit is a 1 (high) - // Clear the blood leak zero and self test commands and get ready to do the self test - if ( ( BLOOD_LEAK_STATUS_BIT_LOW == bloodLeakGPIOCmd.cmdPrevStatus ) && ( BLOOD_LEAK_STATUS_BIT_HIGH == bloodLeakZeroStatus ) ) + for ( i = 0; i < NUM_OF_EMB_CMDS; i++ ) { - // Clear the zero command since it is done and clear the self test since we are getting ready to do a self test. - clearFPGABloodLeakZero(); - clearFPGABloodLeakSelfTest(); - // Get ready for the next state. - state = BLOOD_LEAK_PREP_FOR_SELF_TEST_CMD_STATE; - bloodLeakGPIOCmd.cmdStartTimeMS = getMSTimerCount(); - bloodLeakGPIOCmd.cmdWaitBtwnZeroSTestMS = getMSTimerCount(); - } - else if ( TRUE == didTimeout( bloodLeakGPIOCmd.cmdStartTimeMS, BLOOD_LEAK_ZERO_CMD_TIMEOUT_MS ) ) - { - // Zero command timed out -#ifndef _RELEASE_ - if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_BLOOD_LEAK_ALARM ) != SW_CONFIG_ENABLE_VALUE ) -#endif + switch( i ) { - activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_ZERO_CMD_FAULT ); + case Z_EMB_MODE_CMD: + case G_EMB_MODE_CMD: + case Q_EMB_MODE_CMD: + case T_EMB_MODE_CMD: + areCommandsReady &= bloodLeakEmbModeCmd[ i ].isCmdRespRdy; + break; } - // Get back to init state - state = BLOOD_LEAK_INIT_STATE; } - else - { - // Update the previous status with the current zero status since the low to high edge has been observed yet - bloodLeakGPIOCmd.cmdPrevStatus = bloodLeakZeroStatus; - } - return state; -} - -/*********************************************************************//** - * @brief - * The handleBloodLeakPrepForSelfTestCmdState function handles the blood leak - * prepare for self test command state. - * @details Inputs: bloodLeakGPIOCmd - * @details Outputs: bloodLeakGPIOCmd - * @return next state - *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakPrepForSelfTestCmdState( void ) -{ - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_PREP_FOR_SELF_TEST_CMD_STATE; - U08 selfTestCmdStatus = getFPGABloodLeakSelfTestErrorCounter() & BLOOD_LEAK_SELF_TEST_CMD_STATUS_READY; - - // Check if the self test command status is ready and self test status bit is low (0) - if ( ( BLOOD_LEAK_SELF_TEST_CMD_STATUS_READY == selfTestCmdStatus ) && ( BLOOD_LEAK_STATUS_BIT_LOW == getFPGABloodLeakStatus() ) ) + if ( TRUE == areCommandsReady ) { - // Check if the delay in between the zero command and the self test command has been elapsed - if ( TRUE == didTimeout( bloodLeakGPIOCmd.cmdWaitBtwnZeroSTestMS, BLOOD_LEAK_BETWEEN_ZERO_ST_WAIT_MS ) ) + // Enqueue the zero and self test sequence (Z->G->Z->Q->T) + BOOL hasCmdSqncFailed = FALSE; + + for ( i = 0; i < NUM_OF_EMB_CMDS; i++ ) { - // Update the variables and set the self test - state = BLOOD_LEAK_SELF_TEST_STATE; - bloodLeakGPIOCmd.cmdStartTimeMS = getMSTimerCount(); - bloodLeakGPIOCmd.cmdPrevStatus = BLOOD_LEAK_STATUS_BIT_LOW; - setFPGABloodLeakSelfTest(); + switch( i ) + { + case Z_EMB_MODE_CMD: + case G_EMB_MODE_CMD: + case Q_EMB_MODE_CMD: + case T_EMB_MODE_CMD: + hasCmdSqncFailed |= ( BLOOD_LEAK_EMB_MODE_FAIL_ASCII == bloodLeakEmbModeCmd[ i ].commandResp ? TRUE : FALSE ); + break; + } } - } - else if ( TRUE == didTimeout( bloodLeakGPIOCmd.cmdStartTimeMS, BLOOD_LEAK_SELF_TEST_CMD_TIMEOUT_MS ) ) - { -#ifndef _RELEASE_ - if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_BLOOD_LEAK_ALARM ) != SW_CONFIG_ENABLE_VALUE ) -#endif + + if ( FALSE == hasCmdSqncFailed ) { - activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_SELF_TEST_SEND_TIMEOUT ); + state = BLOOD_LEAK_NORMAL_STATE; } - // Self test command failed - state = BLOOD_LEAK_INIT_STATE; - } - - return state; -} - -/*********************************************************************//** - * @brief - * The handleBloodLeakSelfTestState function handles the Blood Leak module - * in self-test state. - * @details Inputs: none - * @details Outputs: Blood Leak module self test. - * @return next state - *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakSelfTestState( void ) -{ - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_SELF_TEST_STATE; - U08 bloodLeakSelfTestStatus = getFPGABloodLeakStatus(); - - // Check if the previous status bit was a high (1) and the current status bit is a low (0). This means that the self test was - // successfully finished - if ( ( BLOOD_LEAK_STATUS_BIT_HIGH == bloodLeakGPIOCmd.cmdPrevStatus ) && ( BLOOD_LEAK_STATUS_BIT_LOW == bloodLeakSelfTestStatus ) ) - { - // Clear the self test command since it is done - clearFPGABloodLeakSelfTest(); - state = BLOOD_LEAK_NORMAL_STATE; - bloodLeakSelfTestStatus = SELF_TEST_STATUS_PASSED; - } - else if ( TRUE == didTimeout( bloodLeakGPIOCmd.cmdStartTimeMS, BLOOD_LEAK_SELF_TEST_CMD_TIMEOUT_MS ) ) - { -#ifndef _RELEASE_ - if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_BLOOD_LEAK_ALARM ) != SW_CONFIG_ENABLE_VALUE ) -#endif + else if ( bloodLeakEmbModeCmd[ Z_EMB_MODE_CMD ].commandRetryCount < BLOOD_LEAK_EMB_MODE_MAX_NUM_CMD_TRIES ) { - activateAlarmNoData( ALARM_ID_HD_BLOOD_LEAK_SELF_TEST_FAULT ); + // NOTE: assume one of the commands' retries is less than the allowed and try it again + // Enqueue the zero and self test sequence (Z->G->Z->Q->T) + enqueueEmbModeCmd( Z_EMB_MODE_CMD ); + enqueueEmbModeCmd( G_EMB_MODE_CMD ); + enqueueEmbModeCmd( Z_EMB_MODE_CMD ); + enqueueEmbModeCmd( Q_EMB_MODE_CMD ); + enqueueEmbModeCmd( T_EMB_MODE_CMD ); } - // Self test failed due to timeout - state = BLOOD_LEAK_INIT_STATE; - bloodLeakSelfTestStatus = SELF_TEST_STATUS_FAILED; } - else - { - bloodLeakGPIOCmd.cmdPrevStatus = bloodLeakSelfTestStatus; - } return state; } @@ -928,50 +620,20 @@ if ( TRUE == bloodLeakExitNormalRequested ) { - state = BLOOD_LEAK_INIT_STATE; bloodLeakExitNormalRequested = FALSE; + state = BLOOD_LEAK_INIT_STATE; } - else if ( TRUE == hasZeroBloodLeakBeenRequested() ) + else if ( TRUE == bloodLeakEmbModeHasZeroBeenRqustd ) { // Check whether zeroing the sensor has been requested or not if yes, transition to zero command state otherwise, stay in this state - state = BLOOD_LEAK_PREP_FOR_ZERO_CMD_STATE; + state = BLOOD_LEAK_CHECK_ZERO_AND_SELF_TEST_STATE; } return state; } /*********************************************************************//** * @brief - * The handleBloodLeakCalibrationState function handles the Blood leak - * calibration state. - * @details Inputs: bloodLeakCalSubstate - * @details Outputs: bloodLeakCalSubstate - * @return next state - *************************************************************************/ -static BLOOD_LEAK_STATE_T handleBloodLeakCalibrationState( void ) -{ - BLOOD_LEAK_STATE_T state = BLOOD_LEAK_CALIBRATION_STATE; - - switch ( bloodLeakEmbModeSubstate ) - { - case BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMAND_STATE: - bloodLeakEmbModeSubstate = handleBloodLeakEmbModeWaitForCommandState(); - break; - - case BLOOD_LEAK_EMB_MODE_SEND_COMMAND_STATE: - bloodLeakEmbModeSubstate = handleBloodLeakEmbModeSendCommandState(); - break; - - case BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMMAND_RESPONSE_STATE: - bloodLeakEmbModeSubstate = handleBloodLeakEmbModeWaitForCommandResponseState(); - break; - } - - return state; -} - -/*********************************************************************//** - * @brief * The getBloodLeakStatus function gets the current reading for the blood * leak detector. * @details Inputs: bloodLeakStatus @@ -1211,64 +873,68 @@ } // If wait for the receive FIFO has timed out or all there is no buffer left in the Rx FIFO transition back to wait for command state for the next command - if ( TRUE == hasCharBeenReceived ) + if ( ( TRUE == hasCharBeenReceived ) && ( bloodLeakEmbModeRespIndex >= length ) ) { - if ( bloodLeakEmbModeRespIndex >= length ) + U08 i; + BOOL isNull = FALSE; + + for ( i = 0; i < length; i++ ) { - U08 i; - BOOL isNull = FALSE; + // Loop through the elements of the buffer and make sure none of the elements are not NULL. + // The elements are checked until the specified length that is expected for a command + isNull |= ( bloodLeakEmbModeRespBuffer[ i ] != NU_EMB_MODE_CMD ? FALSE : TRUE ); + } - for ( i = 0; i < length; i++ ) - { - // Loop through the elements of the buffer and make sure none of the elements are not NULL. - // The elements are checked until the specified length that is expected for a command - isNull |= ( bloodLeakEmbModeRespBuffer[ i ] != NU_EMB_MODE_CMD ? FALSE : TRUE ); - } + // Check if the failed command is Control S which is switch to embedded mode and if + // it failed set the embedded mode request to false so the other commands cannot be sent again + bloodLeakSignalEmbModeReq = ( ( CS_EMB_MODE_CMD == bloodLeakEmbModeRqstedCmd ) && ( NU_EMB_MODE_CMD == bloodLeakEmbModeRespBuffer[ 0 ] ) ? FALSE : TRUE ); - // Check if the failed command is Control S which is switch to embedded mode and if - // it failed set the embedded mode request to false so the other commands cannot be sent again - bloodLeakSignalEmbModeReq = ( ( CS_EMB_MODE_CMD == bloodLeakEmbModeRqstedCmd ) && ( NU_EMB_MODE_CMD == bloodLeakEmbModeRespBuffer[ 0 ] ) ? FALSE : TRUE ); + if ( TRUE == isNull ) + { + // There is null in the characters that were supposed to be received and not be null but null was found + // Clear the response buffer and write the ASCII fail in the first element of the response buffer + // Set the length to be 1 because there is only 1 character (fail) is in the buffer + memset( bloodLeakEmbModeRespBuffer, 0x0, BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ); - if ( TRUE == isNull ) - { - // There is null in the characters that were supposed to be received and not be null but null was found - // Clear the response buffer and write the ASCII fail in the first element of the response buffer - // Set the length to be 1 because there is only 1 character (fail) is in the buffer - memset( bloodLeakEmbModeRespBuffer, 0x0, BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ); + length = 1; + bloodLeakEmbModeRespBuffer[ 0 ] = BLOOD_LEAK_EMB_MODE_FAIL_ASCII; + } + else if ( CS_EMB_MODE_CMD == bloodLeakEmbModeRqstedCmd ) + { + memset( bloodLeakEmbModeRespBuffer, 0x0, BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ); - length = 1; - bloodLeakEmbModeRespBuffer[ 0 ] = BLOOD_LEAK_EMB_MODE_FAIL_ASCII; - } - else if ( CS_EMB_MODE_CMD == bloodLeakEmbModeRqstedCmd ) - { - memset( bloodLeakEmbModeRespBuffer, 0x0, BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ); + length = 1; + bloodLeakEmbModeRespBuffer[ 0 ] = BLOOD_LEAK_EMB_MODE_PASS_ASCII; + } + else if ( 1 == length ) + { + U08 passChar = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].expChar1; + U08 data = bloodLeakEmbModeRespBuffer[ 0 ]; + bloodLeakEmbModeRespBuffer[ 0 ] = ( passChar == data ? BLOOD_LEAK_EMB_MODE_PASS_ASCII : BLOOD_LEAK_EMB_MODE_FAIL_ASCII ); + } - length = 1; - bloodLeakEmbModeRespBuffer[ 0 ] = BLOOD_LEAK_EMB_MODE_PASS_ASCII; - } - else if ( 1 == length ) - { - U08 passChar = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].expChar1; - U08 data = bloodLeakEmbModeRespBuffer[ 0 ]; - bloodLeakEmbModeRespBuffer[ 0 ] = ( passChar == data ? BLOOD_LEAK_EMB_MODE_PASS_ASCII : BLOOD_LEAK_EMB_MODE_FAIL_ASCII ); - } + convertString2Integer( bloodLeakEmbModeRqstedCmd, length ); - // Done with receiving the data - bloodLeakEmbModeRqstedCmd = NU_EMB_MODE_CMD; - state = BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMAND_STATE; - sendBloodLeakEmbeddedModeCommandResponse( length, bloodLeakEmbModeRespBuffer ); - } + // Done with receiving the data + bloodLeakEmbModeRqstedCmd = NU_EMB_MODE_CMD; + state = BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMAND_STATE; + sendBloodLeakEmbeddedModeCommandResponse( length, bloodLeakEmbModeRespBuffer ); } if ( TRUE == didTimeout( bloodLeakEmbModeOpsStartTime, commandTimeoutMS ) ) { // Command timed out. Clear the receive buffer from any data and put a Fail in there to be sent up memset( bloodLeakEmbModeRespBuffer, 0x0, BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ); + // Only F (fail) will be sent so the length of the buffer is 1 length = 1; bloodLeakEmbModeRespBuffer[ 0 ] = BLOOD_LEAK_EMB_MODE_FAIL_ASCII; - bloodLeakEmbModeRqstedCmd = NU_EMB_MODE_CMD; - state = BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMAND_STATE; + + convertString2Integer( bloodLeakEmbModeRqstedCmd, length ); + + bloodLeakEmbModeRqstedCmd = NU_EMB_MODE_CMD; + state = BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMAND_STATE; + sendBloodLeakEmbeddedModeCommandResponse( length, bloodLeakEmbModeRespBuffer ); } @@ -1348,6 +1014,46 @@ /*********************************************************************//** * @brief + * The convertString2Integer function converts the buffer of the answers in + * string (ASCII) to integer. + * @details Inputs: bloodLeakEmbModeCmd + * @details Outputs: bloodLeakEmbModeRespBuffer, bloodLeakEmbModeCmd + * @param cmd which is the embedded mode command + * @param respLength the length of the received response from the sensor + * @return none + *************************************************************************/ +static void convertString2Integer( U08 cmd, U32 respLength ) +{ + if ( 1 == respLength ) + { + bloodLeakEmbModeCmd[ cmd ].commandResp = bloodLeakEmbModeRespBuffer[ 0 ]; + } + else + { + U08 i; + U32 bufferValue; + U32 respExpectedLength = bloodLeakEmbModeCmd[ cmd ].length; + + bloodLeakEmbModeCmd[ cmd ].commandResp = 0; + + for ( i = 0; i < respExpectedLength; i++ ) + { + bufferValue = bloodLeakEmbModeRespBuffer[ respExpectedLength - 1 - i ]; + + if ( ( bufferValue >= BLOOD_LEAK_EMB_MODE_0_NUM_ASCII ) && ( bufferValue <= BLOOD_LEAK_EMB_MODE_9_NUM_ASCII ) ) + { + bufferValue -= BLOOD_LEAK_EMB_MODE_0_NUM_ASCII; + bloodLeakEmbModeCmd[ cmd ].commandResp += ( bufferValue * pow( 10, i ) ); + } + } + } + + // Set the command response ready + bloodLeakEmbModeCmd[ cmd ].isCmdRespRdy = TRUE; +} + +/*********************************************************************//** + * @brief * The prepareSetPointSeq function prepares the set point sequence to be * written to the blood leak sensor. * @details Inputs: none @@ -1435,6 +1141,7 @@ if ( ++bloodLeakDataPublicationTimerCounter >= getU32OverrideValue( &bloodLeakDataPublishInterval ) ) { BLOOD_LEAK_DATA_T data; + data.bloodLeakStatus = (U32)getBloodLeakStatus(); data.bloodLeakState = (U32)bloodLeakState; data.bloodLeakZeroStatusCounter = (U32)getFPGABloodLeakZeroStatusCounter(); @@ -1468,6 +1175,7 @@ bloodLeakEmbModeCmd[ NU_EMB_MODE_CMD ].length = 0; bloodLeakEmbModeCmd[ NU_EMB_MODE_CMD ].timeoutMS = 0; bloodLeakEmbModeCmd[ NU_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ NU_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Control S command bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].commandASCII = 19; // ASCII for Control S @@ -1476,6 +1184,7 @@ bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].length = 5; bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].timeoutMS = 5 * MS_PER_SECOND; bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Set point command bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].commandASCII = 83; // ASCII for S @@ -1484,6 +1193,7 @@ bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].length = 5; bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].timeoutMS = 10 * MS_PER_SECOND; bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Self test command bloodLeakEmbModeCmd[ T_EMB_MODE_CMD ].commandASCII = 84; // ASCII for T @@ -1492,6 +1202,7 @@ bloodLeakEmbModeCmd[ T_EMB_MODE_CMD ].length = 1; bloodLeakEmbModeCmd[ T_EMB_MODE_CMD ].timeoutMS = 5 * MS_PER_SECOND; bloodLeakEmbModeCmd[ T_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ T_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Get self test command bloodLeakEmbModeCmd[ G_EMB_MODE_CMD ].commandASCII = 71; // ASCII for G @@ -1500,6 +1211,7 @@ bloodLeakEmbModeCmd[ G_EMB_MODE_CMD ].length = 5; bloodLeakEmbModeCmd[ G_EMB_MODE_CMD ].timeoutMS = 10 * MS_PER_SECOND; bloodLeakEmbModeCmd[ G_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ G_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Intensity command bloodLeakEmbModeCmd[ I_EMB_MODE_CMD ].commandASCII = 73; // ASCII for I @@ -1508,6 +1220,7 @@ bloodLeakEmbModeCmd[ I_EMB_MODE_CMD ].length = 5; bloodLeakEmbModeCmd[ I_EMB_MODE_CMD ].timeoutMS = 5 * MS_PER_SECOND; bloodLeakEmbModeCmd[ I_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ I_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Blood detection command bloodLeakEmbModeCmd[ V_EMB_MODE_CMD ].commandASCII = 86; // ASCII for V @@ -1516,6 +1229,7 @@ bloodLeakEmbModeCmd[ V_EMB_MODE_CMD ].length = 5; bloodLeakEmbModeCmd[ V_EMB_MODE_CMD ].timeoutMS = 5 * MS_PER_SECOND; bloodLeakEmbModeCmd[ V_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ V_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Zero sensor command bloodLeakEmbModeCmd[ Z_EMB_MODE_CMD ].commandASCII = 90; // ASCII for Z @@ -1524,6 +1238,7 @@ bloodLeakEmbModeCmd[ Z_EMB_MODE_CMD ].length = 1; bloodLeakEmbModeCmd[ Z_EMB_MODE_CMD ].timeoutMS = 10 * MS_PER_SECOND; bloodLeakEmbModeCmd[ Z_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ Z_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Zero confirm command bloodLeakEmbModeCmd[ Q_EMB_MODE_CMD ].commandASCII = 81; // ASCII for Q @@ -1532,6 +1247,7 @@ bloodLeakEmbModeCmd[ Q_EMB_MODE_CMD ].length = 1; bloodLeakEmbModeCmd[ Q_EMB_MODE_CMD ].timeoutMS = 5 * MS_PER_SECOND; bloodLeakEmbModeCmd[ Q_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ Q_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Display blood detection command bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].commandASCII = 68; @@ -1540,6 +1256,7 @@ bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].length = 5; bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].timeoutMS = 5 * MS_PER_SECOND; bloodLeakEmbModeCmd[ D_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].isCmdRespRdy = FALSE; // Calibrate command bloodLeakEmbModeCmd[ C_EMB_MODE_CMD ].commandASCII = 67; @@ -1548,42 +1265,23 @@ bloodLeakEmbModeCmd[ C_EMB_MODE_CMD ].length = 4; bloodLeakEmbModeCmd[ C_EMB_MODE_CMD ].timeoutMS = 5 * MS_PER_SECOND; bloodLeakEmbModeCmd[ C_EMB_MODE_CMD ].commandResp = 0; + bloodLeakEmbModeCmd[ CS_EMB_MODE_CMD ].isCmdRespRdy = FALSE; } /*********************************************************************//** * @brief - * The hasZeroBloodLeakBeenRequested function checks whether zeroing blood - * leak has been requested or not. - * @details Inputs: bloodLeakCmd - * @details Outputs: bloodLeakCmd - * @return TURE if request otherwise, FLASE - *************************************************************************/ -static BOOL hasZeroBloodLeakBeenRequested( void ) -{ - BOOL status = FALSE; - - if ( TRUE == bloodLeakGPIOCmd.cmdZeroRequested ) - { - status = TRUE; - bloodLeakGPIOCmd.cmdZeroRequested = FALSE; - bloodLeakGPIOCmd.cmdStartTimeMS = getMSTimerCount(); - clearFPGABloodLeakZero(); - } - - return status; -} - -/*********************************************************************//** - * @brief * The enqueueEmbModeCmd function enqueues the embedded mode command. * @details Inputs: bloodLeakEmbModeCmdQRearIndex * @details Outputs: bloodLeakEmbModeCmdQRearIndex, bloodLeakEmbModeCmdQCount * @return none *************************************************************************/ static void enqueueEmbModeCmd( U08 cmd ) { + // Enqueue the command and increment the rear embedded mode index + // Set the command response to be false so the command was just queued to be read bloodLeakEmbModeCmdQ[ bloodLeakEmbModeCmdQRearIndex ] = cmd; bloodLeakEmbModeCmdQRearIndex = INC_WRAP( bloodLeakEmbModeCmdQRearIndex, 0, BLOOD_LEAK_EMB_MODE_CMD_Q_MAX_SIZE - 1 ); + bloodLeakEmbModeCmd[ cmd ].isCmdRespRdy = FALSE; bloodLeakEmbModeCmdQCount++; } @@ -1768,8 +1466,8 @@ * @brief * The testSetBloodLeakEmbeddedModeCommand function sets the blood leak * calibration command. - * @details Inputs: bloodLeakSignalEmbeddedModeReq, bloodLeakCalSubstate - * @details Outputs: bloodLeakCalASCIIChar + * @details Inputs: none + * @details Outputs: bloodLeakEmbModeRqstedCmd, 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 @@ -1781,14 +1479,10 @@ if ( ( TRUE == isTestingActivated() ) && ( command < NUM_OF_EMB_CMDS ) ) { - // If the HD is already in service mode and in the blood leak is in the embedded mode and the sensor is waiting for another command - if ( ( MODE_SERV == getCurrentOperationMode() ) && ( TRUE == bloodLeakSignalEmbModeReq ) && - ( BLOOD_LEAK_EMB_MODE_WAIT_FOR_COMAND_STATE == bloodLeakEmbModeSubstate ) ) - { - bloodLeakEmbModeRqstedCmd = command; - bloodLeakEmbModeSetPoint = setPointPayload; - result = TRUE; - } + enqueueEmbModeCmd( command ); + + bloodLeakEmbModeSetPoint = setPointPayload; + result = TRUE; } return result;