Index: firmware/App/Controllers/BloodLeak.c =================================================================== diff -u -r5b448e81db260e5b041699a83c6f2a3faa260ea9 -r208ceefa8e8b1cc30feb93be771ced6d313e6995 --- firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 5b448e81db260e5b041699a83c6f2a3faa260ea9) +++ firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 208ceefa8e8b1cc30feb93be771ced6d313e6995) @@ -62,9 +62,13 @@ #define DATA_PUBLISH_COUNTER_START_COUNT 60 ///< Data publish counter start count. // Calibration mode defines -#define BLOOD_LEAK_EMB_MODE_CTRL_S_ASCII 19 ///< Blood leak embedded mode start command, ^S in ascii. -#define BLOOD_LEAK_CAL_COMMAND_SEQ_LENGTH 4 ///< Blood leak calibration state command length. +#define BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH 4 ///< Blood leak embedded mode command sequence length. #define BLOOD_LEAK_EMB_MODE_STOP_INDEX 1 ///< Blood leak embedded mode stop writing to FIFO index. +#define BLOOD_LEAK_EMB_MODE_COMM_ACTIVE_HIGH 5 ///< Blood leak embedded mode communication active high command. +#define BLOOD_LEAK_EMB_MODE_COMM_ACTIVE_LOW 4 ///< Blood leak embedded mode communication active low command. +#define BLOOD_LEAK_EMB_MODE_RX_TIMEOUT_MS ( 5 * MS_PER_SECOND ) ///< Blood leak embedded mode calibration receive timeout in milliseconds. +#define BLOOD_LEAK_EMB_MODE_RX_BUFFER_EMPTY 0x80 ///< Blood leak embedded mode buffer empty value. +#define BLOOD_LEAK_EMB_MODE_NUM_OF_CMDS 10 // TODO remove ///< Blood leak embedded mode number of commands. /// Defined states for the blood leak detector state machine. typedef enum BloodLeakStates @@ -82,14 +86,40 @@ NUM_OF_BLOOD_LEAK_STATES ///< Number of blood leak detector states. } BLOOD_LEAK_STATE_T; -typedef enum CalibrationStates +/// Defined embedded states +typedef enum EmbStates { BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE = 0, ///< Blood leak calibration state wait for command state. BLOOD_LEAK_CAL_SEND_COMMAND_STATE, ///< Blood leak calibration state send command state. BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE, ///< Blood leak calibration state wait for command response state. NUM_CAL_STATES ///< Number of blood leak calibration states. -} BLOOD_LEAK_CAL_STATE_T; +} BLOOD_LEAK_EMB_MODE_STATE_T; +/// Defined embedded commands +typedef enum EmbCommands +{ + NU = 0, ///< Null command. + CS = 19, ///< Control S command to switch to embedded mode. + SP = 83, ///< Set point command to set the set point. + T = 84, ///< Self test command. + G = 71, ///< Get self test command. + I = 73, ///< Intensity command. + V = 86, ///< Blood detection level command. + Z = 90, ///< Zero sensor command. + Q = 81, ///< Zero sensor confirm command. + D = 68, ///< Display blood detection command. + C = 67, ///< Calibration sensor command. + NUM_OF_EMB_CMDS, ///< Number of embedded mode commands. +} EMB_MODE_CMD_T; + +/// +typedef struct +{ + U08 expChar1; + U08 expChar2; + U08 length; +}; + // ********** private data ********** static BLOOD_LEAK_STATE_T bloodLeakState; ///< Current state of blood leak state machine. @@ -123,11 +153,12 @@ BLOOD_LEAK_UART_COMM_ACTIVE_HIGH, BLOOD_LEAK_UART_COMM_ACTIVE_LOW }; // Embedded mode variables -static BOOL bloodLeakSignalEmbeddedModeReq; ///< Blood leak signal embedded mode has been requested. -static U08 bloodLeakCalASCIIChar; ///< Blood leak signal calibration command. -static U08 bloodLeakCalCommandSquence[ BLOOD_LEAK_CAL_COMMAND_SEQ_LENGTH ]; ///< Blood leak calibration command sequence. -static BLOOD_LEAK_CAL_STATE_T bloodLeakCalSubstate; ///< Blood leak calibration substate. -static U16 bloodLeakCalSetPoint; ///< Blood leak calibration set point command. +static BOOL bloodLeakSignalEmbModeReq; ///< Blood leak signal embedded mode has been requested. +static EMB_MODE_CMD_T bloodLeakEmbModeASCIICmd; ///< Blood leak signal embedded mode ASCII command. +static U08 bloodLeakEmbModeCmdSquence[ BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH ]; ///< Blood leak embedded mode command sequence. +static BLOOD_LEAK_EMB_MODE_STATE_T bloodLeakEmbModeSubstate; ///< Blood leak embedded mode state. +static U32 bloodLeakEmbModeRxTimeout; ///< Blood leak embedded mode receive timeout. +static U16 bloodLeakEmbModeSetPoint; ///< Blood leak embedded mode set point command. // ********** private function prototypes ********** @@ -142,9 +173,9 @@ static BLOOD_LEAK_STATE_T handleBloodLeakNormalState( void ); static BLOOD_LEAK_STATE_T handleBloodLeakCalibrationState( void ); -static BLOOD_LEAK_CAL_STATE_T handleCalBloodLeakWaitForCommandState( void ); -static BLOOD_LEAK_CAL_STATE_T handleCalBloodLeakSendCommandState( void ); -static BLOOD_LEAK_CAL_STATE_T handleCalBloodLeakWaitForCommandResponseState( void ); +static BLOOD_LEAK_EMB_MODE_STATE_T handleCalBloodLeakWaitForCommandState( void ); +static BLOOD_LEAK_EMB_MODE_STATE_T handleCalBloodLeakSendCommandState( void ); +static BLOOD_LEAK_EMB_MODE_STATE_T handleCalBloodLeakWaitForCommandResponseState( void ); static void prepareSetPointSeq( U16 setPoint ); static void publishBloodLeakData( void ); @@ -157,9 +188,9 @@ * bloodLeakZeroRequested, bloodLeakZeroRequested, bloodLeakSelfTestStartTime, * bloodLeakUARTCmdIndex, bloodLeakSetPointSequence, bloodLeakPrevFPGARegisterCount * bloodLeakWait2ReadResponseCounter, bloodLeakDataPublicationTimerCounter, - * bloodLeakCommandWriteTryCount, bloodLeakGetCalStartTime, bloodLeakCalSubstate - * bloodLeakIsPOSTComplete, bloodLeakPersistenceCtr, bloodLeakSignalEmbeddedModeReq, - * bloodLeakCalASCIIChar + * bloodLeakCommandWriteTryCount, bloodLeakGetCalStartTime, bloodLeakEmbModeSubstate + * bloodLeakIsPOSTComplete, bloodLeakPersistenceCtr, bloodLeakSignalEmbModeReq, + * bloodLeakEmbModeASCIICmd, bloodLeakEmbModeRxTimeout * @return none *************************************************************************/ void initBloodLeak( void ) @@ -182,20 +213,21 @@ bloodLeakIsPOSTComplete = FALSE; bloodLeakPrevFPGARegisterCount = 0; bloodLeakPersistenceCtr = 0; - bloodLeakSignalEmbeddedModeReq = FALSE; - bloodLeakCalASCIIChar = 0; - bloodLeakCalSubstate = BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE; + bloodLeakSignalEmbModeReq = FALSE; + bloodLeakEmbModeASCIICmd = NU; + bloodLeakEmbModeSubstate = BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE; + bloodLeakEmbModeRxTimeout = 0; // Set the blood leak set pint sequence to 0 to be initialized memset( bloodLeakSetPointSequence, 0x0, BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH ); // Initialize the blood leak calibration command sequence array. // The first index is the command character so it can be changed upon request. // The reset of the commands are never changing. - bloodLeakCalCommandSquence[ 0 ] = 0; - bloodLeakCalCommandSquence[ 1 ] = BLOOD_LEAK_STOP_WRITE_FIFO_COMMAND; - bloodLeakCalCommandSquence[ 2 ] = BLOOD_LEAK_UART_COMM_ACTIVE_HIGH; - bloodLeakCalCommandSquence[ 3 ] = BLOOD_LEAK_UART_COMM_ACTIVE_LOW; + bloodLeakEmbModeCmdSquence[ 0 ] = 0; + bloodLeakEmbModeCmdSquence[ 1 ] = BLOOD_LEAK_STOP_WRITE_FIFO_COMMAND; + bloodLeakEmbModeCmdSquence[ 2 ] = BLOOD_LEAK_EMB_MODE_COMM_ACTIVE_HIGH; + bloodLeakEmbModeCmdSquence[ 3 ] = BLOOD_LEAK_EMB_MODE_COMM_ACTIVE_LOW; } /*********************************************************************//** @@ -228,7 +260,7 @@ // 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 == bloodLeakSignalEmbeddedModeReq ) + if ( TRUE == bloodLeakSignalEmbModeReq ) { bloodLeakState = BLOOD_LEAK_CALIBRATION_STATE; @@ -705,18 +737,18 @@ { BLOOD_LEAK_STATE_T state = BLOOD_LEAK_CALIBRATION_STATE; - switch ( bloodLeakCalSubstate ) + switch ( bloodLeakEmbModeSubstate ) { case BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE: - bloodLeakCalSubstate = handleCalBloodLeakWaitForCommandState(); + bloodLeakEmbModeSubstate = handleCalBloodLeakWaitForCommandState(); break; case BLOOD_LEAK_CAL_SEND_COMMAND_STATE: - bloodLeakCalSubstate = handleCalBloodLeakSendCommandState(); + bloodLeakEmbModeSubstate = handleCalBloodLeakSendCommandState(); break; case BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE: - bloodLeakCalSubstate = handleCalBloodLeakWaitForCommandResponseState(); + bloodLeakEmbModeSubstate = handleCalBloodLeakWaitForCommandResponseState(); break; } @@ -772,23 +804,37 @@ * @details Outputs: bloodLeakCalCommandSquence, bloodLeakUARTCmdIndex * @return next state *************************************************************************/ -static BLOOD_LEAK_CAL_STATE_T handleCalBloodLeakWaitForCommandState( void ) +static BLOOD_LEAK_EMB_MODE_STATE_T handleCalBloodLeakWaitForCommandState( void ) { - BLOOD_LEAK_CAL_STATE_T state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + BLOOD_LEAK_EMB_MODE_STATE_T state = BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE; - // Check if the requested command is set point, so if the character is S (for set point) - // then prepare the set point buffer to be sent - if ( bloodLeakCalASCIIChar == BLOOD_LEAK_SET_POINT_START_CHAR_ASCII ) + switch( bloodLeakEmbModeASCIICmd ) { - prepareSetPointSeq( bloodLeakCalSetPoint ); + case NU: + // Null command do nothing + break; + + case CS: + case T: + case G: + case I: + case V: + case Z: + case Q: + case D: + case C: + // The requested command is not set point and so update the command sequence buffer. + // The first index of calibration command sequence is the requested command in ASCII. + bloodLeakEmbModeCmdSquence[ 0 ] = bloodLeakEmbModeASCIICmd; + bloodLeakUARTCmdIndex = 0; + state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + break; + + case SP: + prepareSetPointSeq( bloodLeakEmbModeSetPoint ); + state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + break; } - else - { - // The requested command is not set point and so update the command sequence buffer. - // The first index of calibration command sequence is the requested command in ASCII. - bloodLeakCalCommandSquence[ 0 ] = bloodLeakCalASCIIChar; - bloodLeakUARTCmdIndex = 0; - } return state; } @@ -802,75 +848,99 @@ * @details Outputs: bloodLeakCalCommandSquence, bloodLeakUARTCmdIndex * @return next state *************************************************************************/ -static BLOOD_LEAK_CAL_STATE_T handleCalBloodLeakSendCommandState( void ) +static BLOOD_LEAK_EMB_MODE_STATE_T handleCalBloodLeakSendCommandState( void ) { - BLOOD_LEAK_CAL_STATE_T state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + U32 command; - if ( bloodLeakCalASCIIChar == BLOOD_LEAK_SET_POINT_START_CHAR_ASCII ) + BLOOD_LEAK_EMB_MODE_STATE_T state = BLOOD_LEAK_CAL_SEND_COMMAND_STATE; + BOOL isUARTTxDone = FALSE; + BOOL isUARTCtrlDone = TRUE; + + switch ( bloodLeakEmbModeASCIICmd ) { - // Check if the current buffer index is less than the buffer length - if( bloodLeakUARTCmdIndex < bloodLeakSetPointSeqLength ) - { - U32 command = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ]; + case CS: + case T: + case G: + case I: + case V: + case Z: + case Q: + case D: + case C: + command = bloodLeakEmbModeCmdSquence[ bloodLeakUARTCmdIndex ]; + isUARTTxDone = ( bloodLeakUARTCmdIndex <= BLOOD_LEAK_EMB_MODE_STOP_INDEX ? FALSE : TRUE ); + isUARTCtrlDone = ( bloodLeakUARTCmdIndex > ( BLOOD_LEAK_EMB_MODE_CMD_SEQ_LENGTH - 1 ) ? TRUE : FALSE ); - // The active high index is the length - 2 they are the last two elements - U32 activeHighIndex = bloodLeakSetPointSeqLength - 2; + bloodLeakUARTCmdIndex++; + break; - // Check if the current index towards the end of the buffer which are FIFO set and FIFO reset - if( activeHighIndex > bloodLeakUARTCmdIndex ) + case SP: + // Check if the current buffer index is less than the buffer length + if( bloodLeakUARTCmdIndex < bloodLeakSetPointSeqLength ) { - setFPGABloodLeakUARTTransmit( (U08)command ); + // The active high index is the length - 2 they are the last two elements + U32 activeHighIndex = bloodLeakSetPointSeqLength - 2; + command = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ]; + isUARTTxDone = ( activeHighIndex > bloodLeakUARTCmdIndex ? TRUE : FALSE ); + isUARTCtrlDone = FALSE; + + bloodLeakUARTCmdIndex++; } - else - { - setFPGABloodLeakUARTControl( (U08)command ); - } + break; - bloodLeakUARTCmdIndex++; - } - else - { - bloodLeakCalASCIIChar = 0; - bloodLeakUARTCmdIndex = 0; - state = BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE; - } } - else - { - U32 command = bloodLeakCalCommandSquence[ bloodLeakUARTCmdIndex ]; - // Check if the index is the stop writing to the FIFO bit. Up until the stop writing - // bit they are written to the UART channel and the high, low bits are written to the UART control - if ( bloodLeakUARTCmdIndex <= BLOOD_LEAK_EMB_MODE_STOP_INDEX ) + // Check if the current index towards the end of the buffer which are FIFO set and FIFO reset + if ( FALSE == isUARTCtrlDone ) + { + if( FALSE == isUARTTxDone ) { setFPGABloodLeakUARTTransmit( (U08)command ); } else { setFPGABloodLeakUARTControl( (U08)command ); } - - // Done with writing all the commands, reset the index and transition - if ( bloodLeakUARTCmdIndex >= ( BLOOD_LEAK_CAL_COMMAND_SEQ_LENGTH - 1 ) ) - { - bloodLeakCalASCIIChar = 0; - bloodLeakUARTCmdIndex = 0; - state = BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE; - } - else - { - bloodLeakUARTCmdIndex++; - } } + else + { + // Reset the variables for the next + bloodLeakEmbModeRxTimeout = getMSTimerCount(); + bloodLeakEmbModeASCIICmd = NU; + bloodLeakUARTCmdIndex = 0; + state = BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE; + } return state; } -static BLOOD_LEAK_CAL_STATE_T handleCalBloodLeakWaitForCommandResponseState( void ) +/*********************************************************************//** + * @brief + * The handleCalBloodLeakWaitForCommandResponseState function handles the + * wait for command responses state. The state receives data in the buffer. + * @details Inputs: TODO fill up + * @details Outputs: TODo fill up + * @return next state + *************************************************************************/ +static BLOOD_LEAK_EMB_MODE_STATE_T handleCalBloodLeakWaitForCommandResponseState( void ) { - BLOOD_LEAK_CAL_STATE_T state = BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE; + BLOOD_LEAK_EMB_MODE_STATE_T state = BLOOD_LEAK_CAL_WAIT_FOR_COMMAND_RESPONSE_STATE; + U08 rxFIFOCount = getFPGABloodLeakRxFIFOCount(); + if ( rxFIFOCount > 0 ) + { + U08 data = getFPGABloodLeakRxFIFODataOut(); + BOOL test = FALSE; + } + + // 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 == didTimeout( bloodLeakEmbModeRxTimeout, BLOOD_LEAK_EMB_MODE_RX_TIMEOUT_MS ) ) || ( BLOOD_LEAK_EMB_MODE_RX_BUFFER_EMPTY == rxFIFOCount ) ) + { + state = BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE; + } + return state; } @@ -886,7 +956,6 @@ static void prepareSetPointSeq( U16 setPoint ) { U08 i; - // Set the local variables. U32 digitCount; char tempCharBuffer[ BLOOD_LEAK_SET_POINT_MAX_CHAR_LENGTH ]; @@ -960,6 +1029,7 @@ data.bloodLeakStCount = (U32)getFPGABloodLeakStCount(); data.bloodLeakLEDIntesity = (U32)getFPGABloodLeakLEDIntensity(); data.bloodLeakRegisterCounter = (U32)getFPGABloodLeakRegisterCounter(); + data.bloodLeakEmbModeRxChars = 0; broadcastData( MSG_ID_HD_BLOOD_LEAK_DATA, COMM_BUFFER_OUT_CAN_HD_BROADCAST, (U08*)&data, sizeof( BLOOD_LEAK_DATA_T ) ); bloodLeakDataPublicationTimerCounter = 0; @@ -1089,8 +1159,9 @@ // the embedded mode if ( ( MODE_FAUL == mode ) || ( MODE_SERV == mode ) || ( MODE_STAN == mode ) ) { - bloodLeakSignalEmbeddedModeReq = TRUE; - result = TRUE; + bloodLeakSignalEmbModeReq = TRUE; + bloodLeakEmbModeASCIICmd = CS; + result = TRUE; } } @@ -1099,23 +1170,23 @@ /*********************************************************************//** * @brief - * The testSetBloodLeakCalibrationCommand function sets the blood leak calibration - * command. + * The testSetBloodLeakEmbeddedModeCommand function sets the blood leak + * calibration command. * @details Inputs: bloodLeakSignalEmbeddedModeReq, bloodLeakCalSubstate * @details Outputs: bloodLeakCalASCIIChar * @return TRUE if the command is accepted otherwise, FALSE *************************************************************************/ -BOOL testSetBloodLeakCalibrationCommand( U08 command ) +BOOL testSetBloodLeakEmbeddedModeCommand( U08 command ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { // 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 == bloodLeakSignalEmbeddedModeReq ) && - ( BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE == bloodLeakCalSubstate ) ) + if ( ( MODE_SERV == getCurrentOperationMode() ) && ( TRUE == bloodLeakSignalEmbModeReq ) && + ( BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE == bloodLeakEmbModeSubstate ) ) { - bloodLeakCalASCIIChar = command; + bloodLeakEmbModeASCIICmd = (EMB_MODE_CMD_T)command; } } @@ -1137,11 +1208,11 @@ if ( TRUE == isTestingActivated() ) { // 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 == bloodLeakSignalEmbeddedModeReq ) && - ( BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE == bloodLeakCalSubstate ) ) + if ( ( MODE_SERV == getCurrentOperationMode() ) && ( TRUE == bloodLeakSignalEmbModeReq ) && + ( BLOOD_LEAK_CAL_WAIT_FOR_COMAND_STATE == bloodLeakEmbModeSubstate ) ) { - bloodLeakCalASCIIChar = BLOOD_LEAK_SET_POINT_START_CHAR_ASCII; - bloodLeakCalSetPoint = setPoint; + bloodLeakEmbModeASCIICmd = SP; + bloodLeakEmbModeSetPoint = setPoint; } }