Index: firmware/App/Controllers/BloodLeak.c =================================================================== diff -u -r8ae411fa0d1b5da4aefdf44d2a7f28c94c78ed8d -r958b77084f04adb4e6d0542c84d014c36fd1b760 --- firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 8ae411fa0d1b5da4aefdf44d2a7f28c94c78ed8d) +++ firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 958b77084f04adb4e6d0542c84d014c36fd1b760) @@ -146,7 +146,6 @@ static U32 bloodLeakZeroStartTime; ///< Blood leak zeroing start time. static U32 bloodLeakSelfTestStartTime; ///< Blood leak self-test start time. 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, @@ -180,7 +179,7 @@ static U08 bloodLeakEmbModeRespBuffer[ BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ]; ///< Blood leak embedded mode response buffer. static U32 bloodLeakEmbModeRespIndex; ///< Blood leak embedded mode response buffer index. static U32 bloodLeakEmbModeCmdSeqLength; ///< Blood leak embedded mode command sequence length. -static U32 bloodLeakEmbModeNumOfCmdTries; // TODO remove? ///< Blood leak embedded mode number of command trials. +static BOOL bloodLeakEmbModeHasRxRqstBeenSent; ///< Blood leak embedded mode Rx request has been sent signal. // ********** private function prototypes ********** @@ -215,8 +214,8 @@ * bloodLeakCommandWriteTryCount, bloodLeakGetCalStartTime, bloodLeakEmbModeSubstate * bloodLeakIsPOSTComplete, bloodLeakPersistenceCtr, bloodLeakSignalEmbModeReq, * bloodLeakEmbModeRqstedCmd, bloodLeakEmbModeOpsStartTime, bloodLeakEmbModeRespBuffer, - * bloodLeakEmbModeRespIndex, bloodLeakEmbModeNumOfCmdTries, bloodLeakExitNormalRequested, - * bloodLeakEmbModeCmdSeqLength + * bloodLeakEmbModeRespIndex, bloodLeakExitNormalRequested, + * bloodLeakEmbModeCmdSeqLength, bloodLeakEmbModeHasRxRqstBeenSent * @return none *************************************************************************/ void initBloodLeak( void ) @@ -244,9 +243,9 @@ bloodLeakEmbModeSubstate = BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE; bloodLeakEmbModeOpsStartTime = 0; bloodLeakEmbModeRespIndex = 0; - bloodLeakEmbModeNumOfCmdTries = 0; bloodLeakExitNormalRequested = FALSE; bloodLeakEmbModeCmdSeqLength = 0; + bloodLeakEmbModeHasRxRqstBeenSent = FALSE; // Set the blood leak set pint sequence to 0 to be initialized memset( bloodLeakSetPointSequence, 0x0, BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH ); @@ -899,19 +898,23 @@ bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_ACTIVE_HIGH_INDEX + 1 ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ] = TRUE; // Set the variables for the next state - bloodLeakUARTCmdIndex = 0; - bloodLeakEmbModeNumOfCmdTries = 0; - bloodLeakEmbModeRespIndex = 0; - bloodLeakEmbModeCmdSeqLength = BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH; - state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + bloodLeakUARTCmdIndex = 0; + bloodLeakEmbModeRespIndex = 0; + bloodLeakEmbModeCmdSeqLength = BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH; + bloodLeakEmbModeOpsStartTime = getMSTimerCount(); + state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; // Clear the response buffer to able to receive fresh data memset( bloodLeakEmbModeRespBuffer, 0x0, BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ); break; case SP_EMB_MODE_CMD: prepareSetPointSeq( bloodLeakEmbModeSetPoint ); - bloodLeakEmbModeNumOfCmdTries = 0; - state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + bloodLeakEmbModeRespIndex = 0; + bloodLeakEmbModeOpsStartTime = getMSTimerCount(); + state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + // Clear the command sequence buffer in case it is needed to send byte read request. The byte + // read requests are sent using this buffer while the set point is set using the set point buffer + memset( bloodLeakEmbModeCmdSquence, 0x0, 2 * BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH ); // Clear the response buffer to able to receive fresh data memset( bloodLeakEmbModeRespBuffer, 0x0, BLOOD_LEAK_EMB_MODE_RESP_BUFFER_LEN ); break; @@ -954,9 +957,21 @@ break; case SP_EMB_MODE_CMD: - command = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ]; - isUARTCtrlCmd = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ]; - isUARTCtrlDone= ( bloodLeakUARTCmdIndex > bloodLeakSetPointSeqLength ? TRUE : FALSE ); + // Check if the first element in the command sequence buffer is the read request. If it is, the set point command sequence has already been sent + // and we are requesting the buffer to read and get its response back so use this buffer to send data to the sensor + // Otherwise, send the set point command sequence to the sensor since the set point is sent prior to reading the response back from the sensor + if ( BLOOD_LEAK_EMB_MODE_COMM_READ_REQST == bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ] ) + { + command = bloodLeakEmbModeCmdSquence[ bloodLeakUARTCmdIndex ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ]; + isUARTCtrlCmd = bloodLeakEmbModeCmdSquence[ bloodLeakUARTCmdIndex ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ]; + isUARTCtrlDone = ( bloodLeakUARTCmdIndex > ( bloodLeakEmbModeCmdSeqLength - 1 ) ? TRUE : FALSE ); + } + else + { + command = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ]; + isUARTCtrlCmd = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ]; + isUARTCtrlDone = ( bloodLeakUARTCmdIndex > ( bloodLeakSetPointSeqLength - 1 ) ? TRUE : FALSE ); + } bloodLeakUARTCmdIndex++; break; } @@ -966,32 +981,26 @@ { if ( BLOOD_LEAK_EMB_MODE_RX_BUFFER_EMPTY == getFPGABloodLeakRxFIFOCount() ) { - // Continue. TODO what should I do if this is not empty? + // TODO do nothing right now. I case it was decided to use the terminal mode again, make sure the reset went through prior to sending the + // reset of the command sequence } } // Check if the current index towards the end of the buffer which are FIFO set and FIFO reset if ( FALSE == isUARTCtrlDone ) { - // TODO test this - //FALSE == isUARTCtrlCmd ? setFPGABloodLeakUARTTransmit( command ) : setFPGABloodLeakUARTControl( command ); - - if( FALSE == isUARTCtrlCmd ) - { - setFPGABloodLeakUARTTransmit( command ); - } - else - { - setFPGABloodLeakUARTControl( command ); - } + FALSE == isUARTCtrlCmd ? setFPGABloodLeakUARTTransmit( command ) : setFPGABloodLeakUARTControl( command ); } else { - // Reset the variables for the next - bloodLeakEmbModeOpsStartTime = getMSTimerCount(); - bloodLeakUARTCmdIndex = 0; - bloodLeakEmbModeCmdSeqLength = 0; - state = BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE; + // If the element in the first index of the command sequence buffer is the read a byte request, set the variable to TRUE so the + // wait for data to receive state knows that the request has been sent and it can read the byte. + U08 commandInIndex0 = bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ]; + bloodLeakEmbModeHasRxRqstBeenSent = ( BLOOD_LEAK_EMB_MODE_COMM_READ_REQST == commandInIndex0 ? TRUE : FALSE ); + + bloodLeakUARTCmdIndex = 0; + bloodLeakEmbModeCmdSeqLength = 0; + state = BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE; } return state; @@ -1013,13 +1022,30 @@ U32 commandTimeoutMS = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].timeoutMS; U32 length = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].length; - if ( rxFIFOCount > 0 ) + if ( TRUE == bloodLeakEmbModeHasRxRqstBeenSent ) { - U08 data = getFPGABloodLeakRxFIFODataOut(); - hasCharBeenReceived = TRUE; + // If the read request sequence has been sent already, read the next byte in the buffer and process it + U08 data = getFPGABloodLeakRxFIFODataOut(); + hasCharBeenReceived = TRUE; + bloodLeakEmbModeHasRxRqstBeenSent = FALSE; processReceivedEmbModeChar( data ); } + else if ( ( rxFIFOCount > 0 ) && ( FALSE == bloodLeakEmbModeHasRxRqstBeenSent ) ) + { + // If there is still more data in the buffer and a read sequence has not been sent, create the sequence and send it + // First clear the buffer from the older data. Set to 2 times the length because the array is 2D + memset( bloodLeakEmbModeCmdSquence, 0x0, 2 * BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH ); + bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ] = BLOOD_LEAK_EMB_MODE_COMM_READ_REQST; + bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ] = TRUE; + bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX + 1 ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ] = BLOOD_LEAK_EMB_MODE_COMM_ACTIVE_LOW; + bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX + 1 ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ] = TRUE; + // Set the variables for the next state + bloodLeakUARTCmdIndex = 0; + bloodLeakEmbModeCmdSeqLength = BLOOD_LEAK_EMB_MODE_RQST_RX_LENGTH; + state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + } + // 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 ) { @@ -1066,26 +1092,13 @@ state = BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE; handleSendBloodLeakEmbeddedModeCommandResponse( length, bloodLeakEmbModeRespBuffer ); } - else if ( getFPGABloodLeakRxFIFOCount() > 0 ) - { - // First clear the buffer from the older data. Set to 2 times the length because the array is 2D - memset( bloodLeakEmbModeCmdSquence, 0x0, 2 * BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH ); - - bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ] = BLOOD_LEAK_EMB_MODE_COMM_READ_REQST; - bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ] = TRUE; - bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX + 1 ][ BLOOD_LEAK_EMB_MODE_CMD_COL_INDEX ] = BLOOD_LEAK_EMB_MODE_COMM_ACTIVE_LOW; - bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_REQUEST_RX_INDEX + 1 ][ BLOOD_LEAK_EMB_MODE_IS_UART_COL_INDEX ] = TRUE; - - // Set the variables for the next state - bloodLeakUARTCmdIndex = 0; - bloodLeakEmbModeCmdSeqLength = BLOOD_LEAK_EMB_MODE_RQST_RX_LENGTH; - state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; - } } 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; @@ -1116,7 +1129,6 @@ U08 length = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].length; U08 expChar1 = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].expChar1; U08 expChar2 = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].expChar2; - U08 command = bloodLeakEmbModeCmd[ bloodLeakEmbModeRqstedCmd ].commandASCII; if ( ( expChar1 != NU_EMB_MODE_CMD ) && ( NU_EMB_MODE_CMD == expChar2 ) ) { @@ -1152,30 +1164,6 @@ bloodLeakEmbModeRespIndex++; } } - - if ( BLOOD_LEAK_SET_POINT_START_CHAR_ASCII == command ) - { - // There is an exception in this case which is the set point command. - // The set point command can either receive X for timeout or F if the command failed. - // But if the command was received properly, the response is XXXX. For example if set point is 200, the response is 0200 - // The response buffer length of a successful set point is 4 characters (XXXX) - if ( bloodLeakEmbModeRespIndex < BLOOD_LEAK_EMB_MODE_SET_PNT_RESP_LEN ) - { - U08 spResponse = bloodLeakEmbModeRespBuffer[ 0 ]; - length = BLOOD_LEAK_EMB_MODE_SET_PNT_RESP_LEN; - - if ( ( spResponse != expChar1 ) && ( spResponse != expChar2 ) ) - { - // Check if the failed and timeout characters are not inserted - // Check if the response buffer has not been filled and the current received data is a number in between 0 to 9 - if ( ( data >= BLOOD_LEAK_EMB_MODE_0_NUM_ASCII ) && ( data <= BLOOD_LEAK_EMB_MODE_9_NUM_ASCII ) ) - { - bloodLeakEmbModeRespBuffer[ bloodLeakEmbModeRespIndex ] = data; - bloodLeakEmbModeRespIndex++; - } - } - } - } } else if ( ( NU_EMB_MODE_CMD == expChar1 ) && ( NU_EMB_MODE_CMD == expChar2 ) ) { @@ -1324,9 +1312,9 @@ // Set point command bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].commandASCII = 83; // ASCII for S - bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].expChar1 = 88; // ASCII for X (Timeout) - bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].expChar2 = 70; // ASCII for F (Fail) - bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].length = 1; + bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].expChar1 = 32; // For space (Set point is returned by a space at the beginning) + bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].expChar2 = NU_EMB_MODE_CMD; + bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].length = 5; bloodLeakEmbModeCmd[ SP_EMB_MODE_CMD ].timeoutMS = 10 * MS_PER_SECOND;