Index: firmware/App/Controllers/BloodLeak.c =================================================================== diff -u -r68aefeff8890cdfa956c7bfdf0d4505b4ac25cb7 -r431e1dbb713aa2ce5529e807f86b34239b4210de --- firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 68aefeff8890cdfa956c7bfdf0d4505b4ac25cb7) +++ firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 431e1dbb713aa2ce5529e807f86b34239b4210de) @@ -14,7 +14,7 @@ * @date (original) 18-Mar-2021 * ***************************************************************************/ -#include // For sprintf +#include // For sprintf and strlen #include "AlarmMgmt.h" #include "BloodLeak.h" @@ -39,17 +39,23 @@ #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 5 ///< Blood leak start up sequence array length. +#define BLOOD_LEAK_STARTUP_SEQ_LENGTH 6 ///< Blood leak start up sequence array length. -#define BLOOD_LEAK_SET_POINT_MAX_CHAR_LENGTH 4 ///< Blood leak set point maximum character 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 7 ///< Blood leak set point sequence maximum length. +#define BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH 15 ///< 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_MAX_SET_POINT_WRITE_TRIALS 3 ///< Blood leak maximum number of trials to write the set point. +#define BLOOD_LEAK_MIN_WAIT_TIME_2_GET_CAL_MS ( 2 * MS_PER_SECOND ) ///< Blood leak minimum wait time to get calibration in milliseconds. + /// Defined states for the blood leak detector state machine. typedef enum BloodLeakStates { @@ -84,16 +90,16 @@ static U32 bloodLeakWait2ReadSetPointCounter = 0; ///< Blood leak wait to read delay counter. static U32 bloodLeakCurrentSetPointWriteTry = 0; ///< 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 = 0; ///< Blood leak get calibration start time. /// 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 }; -static U32 tempRemoveThis = 0; - // ********** private function prototypes ********** static BLOOD_LEAK_STATES_T handleBloodLeakStartupState( void ); @@ -115,7 +121,7 @@ * bloodLeakZeroRequested, bloodLeakZeroRequested, bloodLeakSelfTestStartTime, * bloodLeakUARTCmdIndex, bloodLeakSetPointSequence, * bloodLeakWait2ReadSetPointCounter, bloodLeakDataPublicationTimerCounter, - * bloodLeakCurrentSetPointWriteTry + * bloodLeakCurrentSetPointWriteTry, bloodLeakGetCalStartTime * @return none *************************************************************************/ void initBloodLeak( void ) @@ -134,6 +140,7 @@ bloodLeakSetPointSeqLength = 0; bloodLeakWait2ReadSetPointCounter = 0; bloodLeakCurrentSetPointWriteTry = 0; + bloodLeakGetCalStartTime = getMSTimerCount(); // Set the blood leak set pint sequence to 0 to be initialized memset( bloodLeakSetPointSequence, 0x0, BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH ); @@ -149,15 +156,18 @@ void execBloodLeak( void ) { // Check if there is a new calibration data available - if ( TRUE == isNewCalibrationRecordAvailable() ) + if ( ( TRUE == isNewCalibrationRecordAvailable() ) && ( calcTimeSince( bloodLeakGetCalStartTime ) > BLOOD_LEAK_MIN_WAIT_TIME_2_GET_CAL_MS ) ) { 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(); // 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; + + bloodLeakGetCalStartTime = getMSTimerCount(); } if ( getCurrentOperationMode() != MODE_INIT ) @@ -258,7 +268,7 @@ U32 command = BLOOD_LEAK_START_UP_SEQUENCE[ bloodLeakUARTCmdIndex ]; - if ( BLOOD_LEAK_START_COMM_CTRL_U_ASCII == command ) + if ( ( bloodLeakUARTCmdIndex >= BLOOD_LEAK_START_FIFO_CTRL_U_INDEX ) && ( bloodLeakUARTCmdIndex <= BLOOD_LEAK_START_FIFO_STOP_INDEX ) ) { setFPGABloodLeakUARTTransmit( (U08)command ); } @@ -267,10 +277,10 @@ setFPGABloodLeakUARTControl( (U08)command ); } - if ( bloodLeakUARTCmdIndex >= BLOOD_LEAK_STARTUP_SEQ_LENGTH ) + if ( bloodLeakUARTCmdIndex >= ( BLOOD_LEAK_STARTUP_SEQ_LENGTH - 1 ) ) { bloodLeakUARTCmdIndex = 0; - state = BLOOD_LEAK_CHECK_SET_POINT_STATE; + state = BLOOD_LEAK_CHECK_SET_POINT_STATE; } else { @@ -311,6 +321,7 @@ else { state = BLOOD_LEAK_INIT_STATE; + bloodLeakCurrentSetPointWriteTry = 0; } return state; @@ -346,22 +357,9 @@ } bloodLeakUARTCmdIndex++; - - // TODO remove - tempRemoveThis = getMSTimerCount(); - // TODO remove } else { - // TODO remove this is temporary code - /*U16 bld = getFPGABloodLeakDetectSetPoint(); - if ( bld == bloodLeakCalRecord.setPoint ) - { - U32 timeout = calcTimeSince(tempRemoveThis); - state = BLOOD_LEAK_INIT_STATE; - }*/ - // TODo Remove this is temporary code - if ( ++bloodLeakWait2ReadSetPointCounter > BLOOD_LEAK_WAIT_2_READ_SET_POINT ) { bloodLeakWait2ReadSetPointCounter = 0; @@ -578,7 +576,7 @@ // Set the local variables. Create a local char buffer with the maximum possible length // The set point can be maximum 3 digits and when it is converted to string, the null character // is the extra character at the end of the buffer so it maximum length is 4 bytes. - U32 digitCount = 0; + U32 digitCount; U32 setPoint = bloodLeakCalRecord.setPoint; U32 bufferIndex = BLOOD_LEAK_SET_POINT_START_CHAR_INDEX; char tempCharBuffer[ BLOOD_LEAK_SET_POINT_MAX_CHAR_LENGTH ]; @@ -587,12 +585,13 @@ // Convert the set point number to the equivalent ASCII number with the unsigned integer data type sprintf( tempCharBuffer, "%u", setPoint ); - // Calculate the length of the character buffer. strlen does not consider the null character. + // Calculate the length of the character buffer. strlen does not count the null character. digitCount = strlen( tempCharBuffer ); // Set the first item to the ASCII character of S. The format to set the set point is // SXXXCR10. It starts with S followed by the characters up to 3 digits, carriage return and a 1 and a 0 to write to the buffer. bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_SET_POINT_START_CHAR_ASCII; + // Increment the buffer index bufferIndex++; @@ -607,6 +606,11 @@ // After the characters, insert the carriage return into the buffer bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_CARRIAGE_RETURN_ASCII; bufferIndex++; + + // After the characters, insert the stop write to FIFO character which is number 0 + bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_STOP_WRITE_FIFO_COMMAND; + bufferIndex++; + // Set active high and active low into the buffer bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_UART_COMM_ACTIVE_HIGH; bufferIndex++;