/************************************************************************** * * Copyright (c) 2020-2022 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file TemperatureSensors.c * * @author (last) Dara Navaei * @date (last) 25-May-2022 * * @author (original) Dara Navaei * @date (original) 08-Apr-2020 * ***************************************************************************/ #include // For temperature calculation #include // For memset() #include "FPGA.h" #include "MessageSupport.h" #include "PersistentAlarm.h" #include "SystemCommMessages.h" #include "TemperatureSensors.h" #include "Timers.h" #include "TaskPriority.h" #include "TaskGeneral.h" #include "Utilities.h" /** * @addtogroup TemperatureSensors * @{ */ // ********** private definitions ********** #define PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN 8U ///< Primary heater external temperature sensors gain. #define PRIMARY_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE 19600U ///< Primary heater external temperature sensors reference resistance. #define PRIMARY_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE 1000U ///< Primary heater external temperature sensors zero degree resistance. #define COND_SENSORS_TEMP_SENSOR_GAIN 8U ///< Temperature sensor for conductivity gain. #define COND_SENSORS_TEMP_SENSOR_REF_RESISTANCE 19600U ///< Temperature sensor for conductivity reference resistance. #define COND_SENSORS_TEMP_SENSOR_0_DEGREE_RESISTANCE 1000U ///< Temperature sensor for conductivity zero degree resistance. #define TRIMMER_HEATER_EXT_TEMP_SENSORS_GAIN 32U ///< Trimmer heater external temperature sensors gain. #define TRIMMER_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE 5110U ///< Trimmer heater external temperature sensors reference resistance. #define TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE 100U ///< Trimmer heater external temperature sensors zero degree resistance. #define TEMP_SENSORS_ADC_BITS 24U ///< External temperature sensors ADC bits. #define ADC_FPGA_READ_DELAY 30U ///< Delay in ms before reading the ADC values from FPGA. #define MAX_NUM_OF_RAW_ADC_SAMPLES 4U ///< Number of ADC reads for moving average calculations. #define MAX_ALLOWED_TEMP_DELTA_BETWEEN_SENSORS 2U ///< Maximum allowed temperature delta between sensors. #define SHIFT_BITS_BY_2 2U ///< Shift bits by 2 to create a 4 for averaging 4 samples. #define SHIFT_BITS_BY_2_FOR_AVERAGING 2U ///< Shift the ADCs of the temperature sensors by 2 to average them. #define INLET_WATER_TEMPERATURE_PERSISTENCE_PERIOD ( 5 * MS_PER_SECOND ) ///< Persistence period for temperature sensors out of range error period. #define MIN_WATER_TEMPERATURE_WARNING_LOW_RANGE 22.0F ///< Low range minimum water input temperature. #define MAX_WATER_TEMPERATURE_WARNING_LOW_RANGE 24.0F ///< Low range maximum water input temperature. #define MIN_WATER_TEMPERATURE_WARNING_HIGH_RANGE 37.0F ///< High range minimum water input temperature. #define MAX_WATER_TEMPERATURE_WARNING_HIGH_RANGE 39.0F ///< High range maximum water input temperature. #define MAX_WATER_TEMPERATURE_ALARM 50.0F ///< High water temperature alarm. #define HEATERS_INTERNAL_TEMPERTURE_CALCULATION_INTERVAL 20U ///< Time interval that is used to calculate the heaters internal temperature. #define HEATERS_INTERNAL_TC_ADC_TO_TEMP_CONVERSION_COEFF 0.25F ///< Heaters internal temperature sensors ADC to temperature conversion coefficient. #define HEATERS_COLD_JUNCTION_ADC_TO_TEMP_CONVERSION_COEFF 0.0625F ///< Heaters cold junction temperature sensors ADC to temperature conversion coefficient. #define K_THERMOCOUPLE_TEMP_2_MILLI_VOLT_CONVERSION_COEFF 0.041276F ///< K thermocouple temperature to millivolt conversion coefficient. #define SIZE_OF_THERMOCOUPLE_COEFFICIENTS 10U ///< Size of the thermocouple coefficients. #define CELSIUS_TO_KELVIN_CONVERSION 273.15F ///< Celsius to Kelvin temperature conversion. #define ADC_BOARD_TEMP_SENSORS_CONVERSION_CONST 272.5F ///< ADC board temperature sensors conversion constant. #define TWELVE_BIT_RESOLUTION 4096U ///< 12 bit resolution conversion. #define ADC_BOARD_TEMP_SENSORS_CONST 0x800000 ///< ADC board temperature sensors constant. #define EXTERNAL_TEMP_SENSORS_ERROR_VALUE 0x80 ///< External temperature sensors error value. #define HEATERS_INTERNAL_TEMP_SENSOR_FAULT 0x01 ///< Heaters internal temperature sensor fault. #define TEMP_SENSORS_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) ///< Temperature sensors publish data time interval. #define TEMPERATURE_SENSORS_FPGA_ERROR_PERSISTENT_PERIOD ( 5 * MS_PER_SECOND ) ///< Temperature sensors FPGA error persistent period. #define TEMPERATURE_SENSORS_INTERNAL_ERROR_PERSISTENT_PERIOD ( 3 * MS_PER_SECOND ) ///< Temperature sensors internal error persistent period. #define FPGA_RAW_ADC_READ_INTERVAL_COUNT 8 ///< Time interval in counts to read the raw ADC reads from FPGA. #define TEMPERATURE_SENSORS_ERROR_FLAG_PERSISTENT_PERIOD ( 5 * MS_PER_SECOND ) ///< Temperature sensors error flag persistent period. #define TEMP_SENSORS_MIN_ALLOWED_DEGREE_C 0.0F ///< Temperature sensors minimum allowed temperature in C. #define TEMP_SENSORS_MAX_ALLOWED_DEGREE_C 120.0F ///< Temperature sensors maximum allowed temperature in C. #define NON_FLUID_PATH_TEMP_SENSORS_MAX_ALLOWED_DEGREE_C 80.0F ///< Non fluid temperature sensors path maximum allowed temperature in C. #define TEMP_SENSORS_OUT_OF_RANGE_PERSISTENT_PEROID_MS ( 5 * MS_PER_SECOND ) ///< Temperature sensor out of range persistent period in milliseconds. #define DATA_PUBLISH_COUNTER_START_COUNT 30 ///< Data publish counter start count. // The count cannot be within 0.1V of the rail on both sides therefore: // ADC count = ((2^12) - 1 / ref voltage) * voltage // Max allowed voltage = 3.0 - 0.1 = 2.0V // Min allowed voltage = 0.1 - 0.0 = 0.1V // Max count = ((2^12) - 1 / ref voltage) * voltage -> ((4096 - 1)/3.0) * (3.0 - 0.1) // Min count = ((2^12) - 1 / ref voltage) * voltage -> ((4096 - 1)/3.0) * (0.1 - 0.0) #define TEMP_SENSORS_MAX_ALLOWED_ADC_COUNT 3959U ///< Temperature sensors max allowed ADC count. #define TEMP_SESNORS_MIN_ALLOWED_ADC_COUNT 137U ///< Temperature sensors min allowed ADC count. /// Temperature sensor exec states. typedef enum tempSensors_Exec_States { TEMPSENSORS_EXEC_STATE_START = 0, ///< Temperature sensors exec start TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES, ///< Temperature sensors exec get ADC values NUM_OF_TEMPSENSORS_EXEC_STATES, ///< Total number of exec states } TEMPSENSORS_EXEC_STATES_T; /// Temperature sensor struct. typedef struct { F32 gain; ///< ADC gain F32 refResistance; ///< ADC reference resistance F32 conversionCoeff; ///< ADC conversion coefficient F32 zeroDegreeResistance; ///< ADC zero degree resistance S32 rawADCReads[ MAX_NUM_OF_RAW_ADC_SAMPLES ]; ///< Raw ADC reads array S32 adcNextIndex; ///< Next ADC read index S32 adcRunningSum; ///< ADC running sum U32 readCount; ///< Read counts from FPGA OVERRIDE_F32_T temperatureValues; ///< Temperature values with override F32 maxAllowedTemperature; ///< Maximum allowed temperature of the sensor U32 alarmStartTime; ///< Alarm start time } TEMP_SENSOR_T; // ********** private data ********** static TEMPSENSORS_EXEC_STATES_T tempSensorsExecState; ///< TemperatureSensor exec state. static TEMP_SENSOR_T tempSensors [ NUM_OF_TEMPERATURE_SENSORS ]; ///< Temperature sensors' data structure. static U32 fpgaRawADCReadInterval; ///< FPGA raw ADC read interval count. static U32 elapsedTime; ///< Elapsed time variable. static U32 internalHeatersConversionTimer; ///< Conversion timer variable to calculate the heaters internal temperature. static U32 dataPublicationTimerCounter; ///< Temperature sensors data publish timer counter. static OVERRIDE_U32_T tempSensorsPublishInterval = { TEMP_SENSORS_DATA_PUBLISH_INTERVAL, TEMP_SENSORS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Temperature sensors publish time interval override. static DG_TEMP_SENSORS_CAL_RECORD_T tempSensorCalRecord; ///< Temperature sensors calibration record. static const F32 POSITIVE_TC_EXP_A0 = 0.118597600000E0; ///< K TC positive temperature exponent coefficient A0. static const F32 POSITIVE_TC_EXP_A1 = -0.118343200000E-3; ///< K TC positive temperature exponent coefficient A1. static const F32 POSITIVE_TC_EXP_A2 = 0.126968600000E3; ///< K TC positive temperature exponent coefficient A2. static const F32 POSITIVE_TC_COEFFS [ SIZE_OF_THERMOCOUPLE_COEFFICIENTS ] = { -0.176004136860E-1, 0.389212049750E-1, 0.185587700320E-4, -0.994575928740E-7, 0.318409457190E-9, -0.560728448890E-12, 0.560750590590E-15,-0.320207200030E-18, 0.971511471520E-22,-0.121047212750E-25 }; ///< Thermocouple correction coefficients for positive cold junction temperature. static const F32 POSITIVE_TC_INVERSER_COEFFS [ SIZE_OF_THERMOCOUPLE_COEFFICIENTS ] = { 0.0, 2.508355E1, 7.860106E-2, -2.503131E-1, 8.315270E-2, -1.228034E-2, 9.804036E-4, -4.413030E-5, 1.057734E-6, -1.052755E-8 }; ///< Thermocouple inverse coefficient for positive cold junction temperature. static const U32 TEMP_EQUATION_RESISTOR_CALC = 1 << ( TEMP_SENSORS_ADC_BITS - 1 ); ///< Temperature sensors resistor calculation (2^(24 - 1)). static const F32 TEMP_EQUATION_COEFF_A = 3.9083E-3; ///< ADC to temperature conversion coefficient A. static const F32 TEMP_EQUATION_COEFF_B = -5.775E-7; ///< ADC to temperature conversion coefficient B. // ********** private function prototypes ********** static TEMPSENSORS_EXEC_STATES_T handleExecStart( void ); static TEMPSENSORS_EXEC_STATES_T handleExecGetADCValues( void ); static F32 getADC2TempConversion( F32 avgADC, U32 gain, U32 refResistance, U32 zeroDegResistance, F32 adcConversionCoeff ); static void getHeaterInternalTemp( U32 TCIndex, U32 CJIndex ); static void processTempSnsrsADCRead( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ); static void processHtrsTempSnsrsADCRead( U32 sensorIndex, U16 adc, U32 fpgaError, U32 fpgaCount ); static BOOL isADCReadValid( U32 sensorIndex, U32 fpgaError, U32 fpgaCount ); static void processADCRead( U32 sensorIndex, S32 adc ); static void publishTemperatureSensorsData( void ); static void monitorTemperatureSnsrs( U32 sensorIndex ); static void checkAlarmStatus( U32 sensorIndex, ALARM_ID_T alarm, BOOL alarmOccurred, U32 alarmTimeout ); /*********************************************************************//** * @brief * The initTemperatureSensors function initializes the module. * @details Inputs: none * @details Outputs: tempSensorsSelfTestState, tempSensorsExecState, * elapsedTime, internalHeatersConversionTimer, dataPublicationTimerCounter, * tempSensors, fpgaRawADCReadInterval * @return none *************************************************************************/ void initTemperatureSensors( void ) { U08 i; // Initialize the variables tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; elapsedTime = 0; internalHeatersConversionTimer = 0; dataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; fpgaRawADCReadInterval = 0; /* NOTE: The temperature sensors do not have conversion coefficient. * The conversion coefficients are used for the heaters internal temperature sensors and * the temperature will be calculated using a quadratic equation * The internal thermocouple has 0.25 conversion coefficient * The heaters cold junction sensor has 0.0625 conversion coefficient * The conversion coefficient will be set to 0 for the temperature sensors */ for ( i = 0; i < NUM_OF_TEMPERATURE_SENSORS; ++i ) { memset( &tempSensors[ i ], 0x0, sizeof( TEMP_SENSOR_T ) ); benignPolynomialCalRecord( &tempSensorCalRecord.tempSensors[ i ] ); } // Initialize TPi and TPo constants tempSensors[ TEMPSENSORS_INLET_PRIMARY_HEATER ].gain = PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN; tempSensors[ TEMPSENSORS_INLET_PRIMARY_HEATER ].refResistance = PRIMARY_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE; tempSensors[ TEMPSENSORS_INLET_PRIMARY_HEATER ].zeroDegreeResistance = PRIMARY_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE; tempSensors[ TEMPSENSORS_INLET_PRIMARY_HEATER ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_OUTLET_PRIMARY_HEATER ].gain = PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN; tempSensors[ TEMPSENSORS_OUTLET_PRIMARY_HEATER ].refResistance = PRIMARY_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE; tempSensors[ TEMPSENSORS_OUTLET_PRIMARY_HEATER ].zeroDegreeResistance = PRIMARY_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE; tempSensors[ TEMPSENSORS_OUTLET_PRIMARY_HEATER ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; // Initialize TD1 and TD2 constants tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_1 ].gain = COND_SENSORS_TEMP_SENSOR_GAIN; tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_1 ].refResistance = COND_SENSORS_TEMP_SENSOR_REF_RESISTANCE; tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_1 ].zeroDegreeResistance = COND_SENSORS_TEMP_SENSOR_0_DEGREE_RESISTANCE; tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_1 ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_2 ].gain = COND_SENSORS_TEMP_SENSOR_GAIN; tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_2 ].refResistance = COND_SENSORS_TEMP_SENSOR_REF_RESISTANCE; tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_2 ].zeroDegreeResistance = COND_SENSORS_TEMP_SENSOR_0_DEGREE_RESISTANCE; tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_2 ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; // Initialize TRo and TDi constants tempSensors[ TEMPSENSORS_OUTLET_REDUNDANT ].gain = TRIMMER_HEATER_EXT_TEMP_SENSORS_GAIN; tempSensors[ TEMPSENSORS_OUTLET_REDUNDANT ].refResistance = TRIMMER_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE; tempSensors[ TEMPSENSORS_OUTLET_REDUNDANT ].zeroDegreeResistance = TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE; tempSensors[ TEMPSENSORS_OUTLET_REDUNDANT ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_INLET_DIALYSATE ].gain = TRIMMER_HEATER_EXT_TEMP_SENSORS_GAIN; tempSensors[ TEMPSENSORS_INLET_DIALYSATE ].refResistance = TRIMMER_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE; tempSensors[ TEMPSENSORS_INLET_DIALYSATE ].zeroDegreeResistance = TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE; tempSensors[ TEMPSENSORS_INLET_DIALYSATE ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; // Initialize the heaters internal thermocouples constants tempSensors[ TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE ].conversionCoeff = HEATERS_INTERNAL_TC_ADC_TO_TEMP_CONVERSION_COEFF; tempSensors[ TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE ].conversionCoeff = HEATERS_INTERNAL_TC_ADC_TO_TEMP_CONVERSION_COEFF; tempSensors[ TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; // Initialize the heaters cold junction constants tempSensors[ TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION ].conversionCoeff = HEATERS_COLD_JUNCTION_ADC_TO_TEMP_CONVERSION_COEFF; tempSensors[ TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION ].conversionCoeff = HEATERS_COLD_JUNCTION_ADC_TO_TEMP_CONVERSION_COEFF; tempSensors[ TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION ].maxAllowedTemperature = TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; // FPGA board temperature conversion coefficient tempSensors[ TEMPSENSORS_FPGA_BOARD_SENSOR ].conversionCoeff = 503.975 / (F32)TWELVE_BIT_RESOLUTION; tempSensors[ TEMPSENSORS_FPGA_BOARD_SENSOR ].maxAllowedTemperature = NON_FLUID_PATH_TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; F32 const conversionCoeff = 1.0 / 13584.0; // Board temperature sensors conversion coefficient tempSensors[ TEMPSENSORS_LOAD_CELL_A1_B1 ].conversionCoeff = conversionCoeff; tempSensors[ TEMPSENSORS_LOAD_CELL_A1_B1 ].maxAllowedTemperature = NON_FLUID_PATH_TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_LOAD_CELL_A2_B2 ].conversionCoeff = conversionCoeff; tempSensors[ TEMPSENSORS_LOAD_CELL_A2_B2 ].maxAllowedTemperature = NON_FLUID_PATH_TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_INTERNAL_TRO_RTD ].conversionCoeff = conversionCoeff; tempSensors[ TEMPSENSORS_INTERNAL_TRO_RTD ].maxAllowedTemperature = NON_FLUID_PATH_TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_INTERNAL_TDI_RTD ].conversionCoeff = conversionCoeff; tempSensors[ TEMPSENSORS_INTERNAL_TDI_RTD ].maxAllowedTemperature = NON_FLUID_PATH_TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; tempSensors[ TEMPSENSORS_INTERNAL_COND_TEMP_SENSOR ].conversionCoeff = conversionCoeff; tempSensors[ TEMPSENSORS_INTERNAL_COND_TEMP_SENSOR ].maxAllowedTemperature = NON_FLUID_PATH_TEMP_SENSORS_MAX_ALLOWED_DEGREE_C; // Persistent alarms for inlet water high/low temperature initPersistentAlarm( ALARM_ID_INLET_WATER_HIGH_TEMPERATURE, INLET_WATER_TEMPERATURE_PERSISTENCE_PERIOD, INLET_WATER_TEMPERATURE_PERSISTENCE_PERIOD ); initPersistentAlarm( ALARM_ID_INLET_WATER_TEMPERATURE_IN_LOW_RANGE, INLET_WATER_TEMPERATURE_PERSISTENCE_PERIOD, INLET_WATER_TEMPERATURE_PERSISTENCE_PERIOD ); initPersistentAlarm( ALARM_ID_INLET_WATER_TEMPERATURE_IN_HIGH_RANGE, INLET_WATER_TEMPERATURE_PERSISTENCE_PERIOD, INLET_WATER_TEMPERATURE_PERSISTENCE_PERIOD ); // Persistent alarm for the temperature sensors range check initPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSOR_OUT_OF_RANGE, TEMP_SENSORS_OUT_OF_RANGE_PERSISTENT_PEROID_MS, TEMP_SENSORS_OUT_OF_RANGE_PERSISTENT_PEROID_MS ); // Persistent alarm for the temperature sensors range check initPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSOR_ADC_OUT_OF_RANGE, TEMP_SENSORS_OUT_OF_RANGE_PERSISTENT_PEROID_MS, TEMP_SENSORS_OUT_OF_RANGE_PERSISTENT_PEROID_MS ); // Persistent alarm for the temperature sensors error bit fault check initPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSOR_FAULT, TEMP_SENSORS_OUT_OF_RANGE_PERSISTENT_PEROID_MS, TEMP_SENSORS_OUT_OF_RANGE_PERSISTENT_PEROID_MS ); // TODO remove? // Persistent alarm for temperature sensors ADC error // When the FPGA read count does not increment for a period of time, it is considered as an internal error of the temperature sensors // driver. This is internal because FPGA does not error out if the FPGA read count does not increment. initPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSORS_ADC_FAULT, TEMPERATURE_SENSORS_FPGA_ERROR_PERSISTENT_PERIOD, TEMPERATURE_SENSORS_FPGA_ERROR_PERSISTENT_PERIOD ); } /*********************************************************************//** * @brief * The execTemperatureSensorsSelfTest function runs the TemperatureSensors * POST during the self-test. * @details Inputs: none * @details Outputs: none * @return tempSensorsSelfTestState which is the status of the self test *************************************************************************/ SELF_TEST_STATUS_T execTemperatureSensorsSelfTest( void ) { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_IN_PROGRESS; BOOL calStatus = getNVRecord2Driver( GET_CAL_TEMP_SENSORS, (U08*)&tempSensorCalRecord, sizeof( DG_TEMP_SENSORS_CAL_RECORD_T ), NUM_OF_CAL_DATA_TEMP_SENSORS, ALARM_ID_NO_ALARM ); if ( TRUE == calStatus ) { result = SELF_TEST_STATUS_PASSED; } else { result = SELF_TEST_STATUS_FAILED; } return result; } /*********************************************************************//** * @brief * The execTemperatureSensors function executes the temperature sensors' * state machine. * @details Inputs: tempSensorsExecState * @details Outputs: tempSensorsExecState * @return none *************************************************************************/ void execTemperatureSensors( void ) { // Read the sensors all the time switch ( tempSensorsExecState ) { case TEMPSENSORS_EXEC_STATE_START: tempSensorsExecState = handleExecStart(); break; case TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES: tempSensorsExecState = handleExecGetADCValues(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_TEMPERATURE_SENSORS_EXEC_INVALID_STATE, tempSensorsExecState ); tempSensorsExecState = TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; break; } // Publish the data publishTemperatureSensorsData(); } /*********************************************************************//** * @brief * The checkInletWaterTemperature function checks inlet water temperature value * and triggers an alarm when temperature value is out of allowed range. * @details Inputs: none * @details Outputs: none * @return none *************************************************************************/ void checkInletWaterTemperature( void ) { #ifndef DISABLE_WATER_QUALITY_CHECK #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_WATER_QUALITY_CHECK ) != SW_CONFIG_ENABLE_VALUE ) #endif { F32 temperature = getTemperatureValue( TEMPSENSORS_INLET_PRIMARY_HEATER ); BOOL isWaterTempInHighRange = ( temperature > MAX_WATER_TEMPERATURE_WARNING_HIGH_RANGE ? TRUE : FALSE ); BOOL isWaterTempInLowRange = ( temperature < MIN_WATER_TEMPERATURE_WARNING_LOW_RANGE ? TRUE : FALSE ); BOOL isWaterTempTooHigh = ( temperature > MAX_WATER_TEMPERATURE_ALARM ? TRUE : FALSE ); // Fault alarm per PRS 557 for high temperature checkPersistentAlarm( ALARM_ID_INLET_WATER_HIGH_TEMPERATURE, isWaterTempTooHigh, temperature, MAX_WATER_TEMPERATURE_ALARM ); if ( TRUE == isWaterTempInHighRange ) // warning alarm per PRS 406 { checkPersistentAlarm( ALARM_ID_INLET_WATER_TEMPERATURE_IN_HIGH_RANGE, isWaterTempInHighRange, temperature, MAX_WATER_TEMPERATURE_WARNING_HIGH_RANGE ); } else if ( temperature <= MIN_WATER_TEMPERATURE_WARNING_HIGH_RANGE ) { checkPersistentAlarm( ALARM_ID_INLET_WATER_TEMPERATURE_IN_HIGH_RANGE, FALSE, temperature, MAX_WATER_TEMPERATURE_WARNING_HIGH_RANGE ); } if ( TRUE == isWaterTempInLowRange ) // warning alarm per PRS 405 { checkPersistentAlarm( ALARM_ID_INLET_WATER_TEMPERATURE_IN_LOW_RANGE, isWaterTempInLowRange, temperature, MIN_WATER_TEMPERATURE_WARNING_LOW_RANGE ); } else if ( temperature >= MAX_WATER_TEMPERATURE_WARNING_LOW_RANGE ) { checkPersistentAlarm( ALARM_ID_INLET_WATER_TEMPERATURE_IN_LOW_RANGE, FALSE, temperature, MIN_WATER_TEMPERATURE_WARNING_LOW_RANGE ); } } #endif } /*********************************************************************//** * @brief * The getTemperatureValue function gets the temperature of the requested * sensor. * @details Inputs: tempSensors * @details Outputs: none * @param sensorIndex which is the temperature sensor index * @return temperature of the requested sensor *************************************************************************/ F32 getTemperatureValue( U32 sensorIndex ) { F32 temperature = 0.0; if ( sensorIndex < NUM_OF_TEMPERATURE_SENSORS ) { if ( OVERRIDE_KEY == tempSensors[ sensorIndex ].temperatureValues.override ) { temperature = tempSensors[ sensorIndex ].temperatureValues.ovData; } else { #ifdef THD_USING_TRO_CONNECTOR // In V3 THd is connected to TRo // In V3 TDi represents TRo since they are very close to each other if ( TEMPSENSORS_HEAT_DISINFECT == sensorIndex ) { temperature = tempSensors[ TEMPSENSORS_OUTLET_REDUNDANT ].temperatureValues.data; } else { temperature = tempSensors[ sensorIndex ].temperatureValues.data; } #else temperature = tempSensors[ sensorIndex ].temperatureValues.data; #endif } } else { // Wrong sensor was called, raise an alarm SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_TEMPERATURE_SENSOR_SELECTED, sensorIndex ); } return temperature; } /*********************************************************************//** * @brief * The getADC2TempConversion function calculates the temperature from the * moving average ADC samples. * @details Inputs: tempEquationCoeffA, tempEquationCoeffB * @details Outputs: none * @param avgADC moving average ADC * @param gain ADC gain * @param refResistance ADC reference resistance * @param zeroDegResistance ADC zero degree resistance * @param adcConversionCoeff ADC conversion coefficient * @return calculated temperature *************************************************************************/ static F32 getADC2TempConversion( F32 avgADC, U32 gain, U32 refResistance, U32 zeroDegResistance, F32 adcConversionCoeff ) { F32 temperature = 0.0; if ( fabs( adcConversionCoeff ) <= NEARLY_ZERO ) { // R(RTD) = R(ref) * ( adc – 2^(N - 1) ) / ( G * 2^(N - 1) ); F32 resistance = ( refResistance * ( avgADC - TEMP_EQUATION_RESISTOR_CALC ) ) / ( gain * TEMP_EQUATION_RESISTOR_CALC ); // T = (-A + √( A^2 - 4B * ( 1 - R_T / R_0 ) ) ) / 2B F32 secondSqrtPart = 4 * TEMP_EQUATION_COEFF_B * ( 1 - ( resistance / zeroDegResistance ) ); temperature = ( -TEMP_EQUATION_COEFF_A + sqrt( pow( TEMP_EQUATION_COEFF_A, 2 ) - secondSqrtPart ) ) / ( 2 * TEMP_EQUATION_COEFF_B ); } else { temperature = avgADC * adcConversionCoeff; } return temperature; } /*********************************************************************//** * @brief * The getHeaterInternalTemp function calculates the heaters' internal * temperature. * @details Inputs: tempSensors * @details Outputs: tempSensors * @param TCIndex thermocouple index * @param CJIndex cold junction index * @return none *************************************************************************/ static void getHeaterInternalTemp( U32 TCIndex, U32 CJIndex ) { /* voltage = 0.041276 * ( cold junction temp - thermo-couple temp ) * E = Ci * T^i + a0 * exp( a1 * ( T - a2 ) ^ 2 ) * i is the positive thermo-couple coefficients * a0 is the positive thermo-couple coefficient * a1 is the positive thermo-couple coefficient * a2 is the positive thermo-couple coefficient * E = voltage + E (E is the corrected voltage) * Temperature = di * E^i * d is positive inverse thermo-couple coefficient * E is corrected voltage */ F32 temperature = 0.0; F32 equiVoltage = 0.0; F32 correctedVoltage = 0.0; F32 TCTemp = tempSensors[ TCIndex ].temperatureValues.data; F32 CJTemp = tempSensors[ CJIndex ].temperatureValues.data; // Value in mV F32 rawVoltage = ( TCTemp - CJTemp ) * K_THERMOCOUPLE_TEMP_2_MILLI_VOLT_CONVERSION_COEFF; U08 i; // Check if cold junction is positive if ( CJTemp > 0 ) { for ( i = 0; i < SIZE_OF_THERMOCOUPLE_COEFFICIENTS; i++ ) { equiVoltage = equiVoltage + ( POSITIVE_TC_COEFFS[ i ] * pow( CJTemp, i ) ); } equiVoltage = equiVoltage + ( POSITIVE_TC_EXP_A0 * ( exp( POSITIVE_TC_EXP_A1 * pow( ( CJTemp - POSITIVE_TC_EXP_A2 ), 2 ) ) ) ); correctedVoltage = rawVoltage + equiVoltage; for ( i = 0; i < SIZE_OF_THERMOCOUPLE_COEFFICIENTS; i++ ) { temperature = temperature + ( POSITIVE_TC_INVERSER_COEFFS[ i ] * pow( correctedVoltage, i ) ); } } // Check which heater's internal temperature is being calculated if ( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE == TCIndex ) { tempSensors[ TEMPSENSORS_PRIMARY_HEATER_INTERNAL ].temperatureValues.data = temperature; } else { tempSensors[ TEMPSENSORS_TRIMMER_HEATER_INTERNAL ].temperatureValues.data = temperature; } } /*********************************************************************//** * @brief * The processTemperatureSensorsADCRead function masks the MSB of the ADC * read from FPGA and converts it to an S32. Then it calls another function * to check if the read ADC is valid or not and if it is, it calls another * function to process the ADC value and covert it to temperature. * @details Inputs: none * @details Outputs: none * @param sensorIndex ID of temperature sensor to process * @param adc ADC value for the temperature sensor * @param fpgaError reported FPGA error status * @param fpgaCount reported FPGA read count * @return none *************************************************************************/ static void processTempSnsrsADCRead( U32 sensorIndex, U32 adc, U32 fpgaError, U32 fpgaCount ) { S32 convertedADC = (S32)( adc & MASK_OFF_U32_MSB ); // All the sensors have ADC read and count values that have to be checked BOOL isADCValid = isADCReadValid( sensorIndex, fpgaError, fpgaCount ); // Some of the temperature sensors have an MSB bit that is used as an // error flag. This flag will be a TRUE by default. BOOL isTemperatureNotValid = FALSE; switch( sensorIndex ) { case TEMPSENSORS_LOAD_CELL_A1_B1: // 267 case TEMPSENSORS_LOAD_CELL_A2_B2: // 279 case TEMPSENSORS_CONDUCTIVITY_SENSOR_1: // 283 case TEMPSENSORS_CONDUCTIVITY_SENSOR_2: // 287 case TEMPSENSORS_OUTLET_PRIMARY_HEATER: // 291 case TEMPSENSORS_INLET_PRIMARY_HEATER: // 295 case TEMPSENSORS_INTERNAL_COND_TEMP_SENSOR: // 299 case TEMPSENSORS_OUTLET_REDUNDANT: // 303 case TEMPSENSORS_INTERNAL_TRO_RTD: // 307 case TEMPSENSORS_INLET_DIALYSATE: // 311 case TEMPSENSORS_INTERNAL_TDI_RTD: // 315 { // The MSB bit of the last byte is the error flag, so that MSB // is shifted 31 bits to the first bit of the U32 variable. // If that bit is a 1, there is either CRC error or Status error // that are ored on top of each other U32 errorBit = adc >> 31; isTemperatureNotValid = ( errorBit > 0 ? TRUE : FALSE ); // TODO for debugging only remove if ( TRUE == isTemperatureNotValid ) { BOOL test = FALSE; } // TODO remove //SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_TEMPERATURE_SENSOR_FAULT, sensorIndex ); //checkAlarmStatus( sensorIndex, ALARM_ID_DG_TEMPERATURE_SENSOR_FAULT, isTemperatureNotValid, TEMPERATURE_SENSORS_ERROR_FLAG_PERSISTENT_PERIOD ); // TODO remove this //checkPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSOR_FAULT, isTemperatureNotValid, sensorIndex, // TEMPERATURE_SENSORS_ERROR_FLAG_PERSISTENT_PERIOD ); } break; default: // Do nothing. FPGA board temperature sensor does not have error flag bit // If a wrong temperature sensor name is picked, the function that converts // ADC counts to temperature raises an alarm. break; } // To update the moving average of a temperature sensor, both ADC and internal // error flags must be valid if ( ( TRUE == isADCValid ) && ( FALSE == isTemperatureNotValid ) ) { processADCRead( sensorIndex, convertedADC ); } } /*********************************************************************//** * @brief * The processHeatersInternalSensorsADCRead function checks whether the provided * sensor is the heaters thermocouple or cold junction sensors and performs * different bit shifts on them accordingly. Then it call another function to * check if the read ADC is valid and if it is, the function calls another function * process the ADC and convert it to temperature. * @details Inputs: none * @details Outputs: none * @param sensorIndex ID of temperature sensor to process * @param adc reported ADC value for temperature sensor * @param fpgaError reported error status by FPGA * @param fpgaCount reported read count by FPGA * @return none *************************************************************************/ static void processHtrsTempSnsrsADCRead( U32 sensorIndex, U16 adc, U32 fpgaError, U32 fpgaCount ) { if ( TRUE == isADCReadValid( sensorIndex, fpgaError, fpgaCount ) ) { S16 convert = (S16)adc; processADCRead( sensorIndex, (S32)convert ); } } /*********************************************************************//** * @brief * The isADCReadValid function checks if there is an FPGA error and FPGA * count. If there is any FPGA, it raises an alarm. If the count has changed * and the ADC value is not the same as the previous ADC read, it returns a * TRUE, signaling that the ADC is valid to be processed. * @details Inputs: tempSensors * @details Outputs: tempSensors * @param sensorIndex Temperature sensor index * @param fpgaError FPGA error count * @param fpgaCount FPGA read count * @return returns TRUE if ADC was valid otherwise FALSE *************************************************************************/ static BOOL isADCReadValid( U32 sensorIndex, U32 fpgaError, U32 fpgaCount ) { BOOL isADCValid = FALSE; // Check the status of FPGA error and FPGA count BOOL isFPGAErrorZero = ( fpgaError == 0 ? TRUE : FALSE ); BOOL isFPGACountChanging = ( tempSensors[ sensorIndex ].readCount != fpgaCount ? TRUE : FALSE ); if ( TRUE == isFPGAErrorZero ) { if ( TRUE == isFPGACountChanging ) { tempSensors[ sensorIndex ].readCount = fpgaCount; isADCValid = TRUE; } } BOOL isThereAnError = ( ( FALSE == isFPGACountChanging ) || ( FALSE == isFPGAErrorZero ) ? TRUE : FALSE ); //checkAlarmStatus( sensorIndex, ALARM_ID_DG_TEMPERATURE_SENSORS_ADC_FAULT, isThereAnError, TEMPERATURE_SENSORS_FPGA_ERROR_PERSISTENT_PERIOD ); // TODO remove this once it is finalized with Noe. //checkPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSORS_ADC_FAULT, isThereAnError, sensorIndex, TEMPERATURE_SENSORS_FPGA_ERROR_PERSISTENT_PERIOD ); //return isADCValid; return TRUE; } /*********************************************************************//** * @brief * The processADCRead function receives the ADC value and the sensor * index and calculates the running sum and the moving average of the ADCs * The temperatureSensorsADCRead and tempSensorsAvgADCValues are updated. * @details Inputs: tempSensors * @details Outputs: tempSensors * @param sensorIndex Temperature sensor index * @param adc adc reading from FPGA * @return none *************************************************************************/ static void processADCRead( U32 sensorIndex, S32 adc ) { F32 temperature; U32 index = tempSensors[ sensorIndex ].adcNextIndex; S32 indexValue = tempSensors[ sensorIndex ].rawADCReads [ index ]; tempSensors[ sensorIndex ].rawADCReads[ index ] = adc; tempSensors[ sensorIndex ].adcNextIndex = INC_WRAP( index, 0, MAX_NUM_OF_RAW_ADC_SAMPLES - 1 ); tempSensors[ sensorIndex ].adcRunningSum = tempSensors[ sensorIndex ].adcRunningSum - indexValue + adc; // Calculate the average F32 avgADCReads = tempSensors[ sensorIndex ].adcRunningSum >> SHIFT_BITS_BY_2_FOR_AVERAGING; // Check if the ADC value of the sensor is not out of range if ( ( (U32)avgADCReads < TEMP_SESNORS_MIN_ALLOWED_ADC_COUNT ) || ( (U32)avgADCReads > TEMP_SENSORS_MAX_ALLOWED_ADC_COUNT ) ) { checkPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSOR_ADC_OUT_OF_RANGE, TRUE, sensorIndex, avgADCReads ); } else { // Clear the alarm if it there was no alarm checkPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSOR_ADC_OUT_OF_RANGE, FALSE, sensorIndex, avgADCReads ); } // Different sensors have different ADC to temperature conversion methods switch( sensorIndex ) { case TEMPSENSORS_INLET_PRIMARY_HEATER: case TEMPSENSORS_OUTLET_PRIMARY_HEATER: case TEMPSENSORS_CONDUCTIVITY_SENSOR_1: case TEMPSENSORS_CONDUCTIVITY_SENSOR_2: case TEMPSENSORS_OUTLET_REDUNDANT: case TEMPSENSORS_INLET_DIALYSATE: case TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE: case TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE: case TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION: case TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION: temperature = getADC2TempConversion( avgADCReads, (U32)tempSensors [ sensorIndex ].gain,(U32)tempSensors [ sensorIndex ].refResistance, (U32)tempSensors [ sensorIndex ].zeroDegreeResistance, tempSensors [ sensorIndex ].conversionCoeff ); break; case TEMPSENSORS_FPGA_BOARD_SENSOR: // Temperature(C) = ((ADC x 503.975) / 4096) - 273.15 // The value of 503.975/4096 has been calculated and stored in the conversion coefficient variable of the structure temperature = ( avgADCReads * tempSensors[ sensorIndex ].conversionCoeff ) - CELSIUS_TO_KELVIN_CONVERSION; break; case TEMPSENSORS_LOAD_CELL_A1_B1: case TEMPSENSORS_LOAD_CELL_A2_B2: case TEMPSENSORS_INTERNAL_TRO_RTD: case TEMPSENSORS_INTERNAL_TDI_RTD: case TEMPSENSORS_INTERNAL_COND_TEMP_SENSOR: // Temperature(C) = ((ADC - 0x800000)/13584) - 272.5 // The value 1/13584 has been calculated and stored in the conversion coefficient variable of the structure temperature = ( ( avgADCReads - ADC_BOARD_TEMP_SENSORS_CONST ) * tempSensors[ sensorIndex ].conversionCoeff ) - ADC_BOARD_TEMP_SENSORS_CONVERSION_CONST; break; default: // Wrong sensor was called, raise an alarm SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_TEMPERATURE_SENSOR_SELECTED, sensorIndex ); // Wrong sensor, return temperature to be -1 temperature = -1.0; break; } // Update the temperature tempSensors[ sensorIndex ].temperatureValues.data = temperature; // Monitor the temperature value monitorTemperatureSnsrs( sensorIndex ); } /*********************************************************************//** * @brief * The handleExecStart function waits for a period of time and switches to * the state that reads the ADC values from FPGA. * @details Inputs: elapsedTime * @details Outputs: elapsedTime * @return the next state of the state machine *************************************************************************/ static TEMPSENSORS_EXEC_STATES_T handleExecStart( void ) { TEMPSENSORS_EXEC_STATES_T state = TEMPSENSORS_EXEC_STATE_START; if ( 0 == elapsedTime ) { elapsedTime = getMSTimerCount(); } // A delay to let FPGA to boot up else if ( TRUE == didTimeout( elapsedTime, ADC_FPGA_READ_DELAY ) ) { elapsedTime = 0; state = TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; } return state; } /*********************************************************************//** * @brief * The handleExecGetADCValues function reads the ADC values from FPGA and * at the specified time intervals and calls other functions to calculate * the internal temperature of the heaters. * @details Inputs: internalHeatersConversionTimer * @details Outputs: internalHeatersConversionTimer * @return the next state of the state machine *************************************************************************/ static TEMPSENSORS_EXEC_STATES_T handleExecGetADCValues( void ) { U32 rawADC = 0; U32 errorCount = 0; U32 readCount = 0; // Look at the error counter and the specific error flag to make sure the error is a temperature sensor // Add a byte array to have bits for each sensor to find out exactly what sensor failed if ( ++fpgaRawADCReadInterval >= FPGA_RAW_ADC_READ_INTERVAL_COUNT ) { rawADC = getFPGATPiTemp(); errorCount = (U32)getFPGARTDErrorCount(); readCount = (U32)getFPGARTDReadCount(); processTempSnsrsADCRead( TEMPSENSORS_INLET_PRIMARY_HEATER, rawADC, errorCount, readCount ); rawADC = getFPGATPoTemp(); processTempSnsrsADCRead( TEMPSENSORS_OUTLET_PRIMARY_HEATER, rawADC, errorCount, readCount ); rawADC = getFPGACD1Temp(); processTempSnsrsADCRead( TEMPSENSORS_CONDUCTIVITY_SENSOR_1, rawADC, errorCount, readCount ); rawADC = getFPGACD2Temp(); processTempSnsrsADCRead( TEMPSENSORS_CONDUCTIVITY_SENSOR_2, rawADC, errorCount, readCount ); rawADC = getFPGATRoTemp(); errorCount = (U32)getFPGATRoErrorCount(); readCount = (U32)getFPGATRoReadCount(); processTempSnsrsADCRead( TEMPSENSORS_OUTLET_REDUNDANT, rawADC, errorCount, readCount ); rawADC = getFPGATDiTemp(); errorCount = (U32)getFPGATDiErrorCount(); readCount = (U32)getFPGATDiReadCount(); processTempSnsrsADCRead( TEMPSENSORS_INLET_DIALYSATE, rawADC, errorCount, readCount ); rawADC = getFPGAPrimaryHeaterTemp(); errorCount = (U32)getFPGAPrimaryHeaterFlags(); readCount = (U32)getFPGAPrimaryHeaterReadCount(); processHtrsTempSnsrsADCRead( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE, rawADC, errorCount, readCount ); rawADC = getFPGAPrimaryColdJunctionTemp(); processHtrsTempSnsrsADCRead( TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION, rawADC, errorCount, readCount ); rawADC = getFPGATrimmerHeaterTemp(); errorCount = (U32)getFPGATrimmerHeaterFlags(); readCount = (U32)getFPGATrimmerHeaterReadCount(); processHtrsTempSnsrsADCRead( TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE, rawADC, errorCount, readCount ); rawADC = getFPGATrimmerColdJunctionTemp(); processHtrsTempSnsrsADCRead( TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION, rawADC, errorCount, readCount ); // NOTE: FPGA board temperature sensor is different from the rest of the sensors. This sensor does not have FPGA count and error // coming from FPGA. It is kept here to do moving average on the values. The supporting functions need to see the FPGA read count // incrementing internally so there will not be any errors. U32 simulatedCounter = tempSensors[ TEMPSENSORS_FPGA_BOARD_SENSOR ].readCount; processTempSnsrsADCRead( TEMPSENSORS_FPGA_BOARD_SENSOR, getFPGABoardTemp(), 0, ++simulatedCounter ); processTempSnsrsADCRead( TEMPSENSORS_LOAD_CELL_A1_B1, getFPGALoadCellsA1B1Temp(), getFPGAADC1ErrorCount(), getFPGAADC1ReadCount() ); processTempSnsrsADCRead( TEMPSENSORS_LOAD_CELL_A2_B2, getFPGALoadCellsA2B2Temp(), getFPGAADC2ErrorCount(), getFPGAADC2ReadCount() ); processTempSnsrsADCRead( TEMPSENSORS_INTERNAL_TRO_RTD, getFPGATRoInternalTemp(), getFPGATRoErrorCount(), getFPGATRoReadCount() ); processTempSnsrsADCRead( TEMPSENSORS_INTERNAL_TDI_RTD, getFPGATDiInternalTemp(), getFPGATDiErrorCount(), getFPGATDiReadCount() ); processTempSnsrsADCRead( TEMPSENSORS_INTERNAL_COND_TEMP_SENSOR, getFPGACondSnsrInternalTemp(), getFPGARTDErrorCount(), getFPGARTDReadCount() ); // Check if time has elapsed to calculate the internal temperature of the heaters if ( ++internalHeatersConversionTimer >= HEATERS_INTERNAL_TEMPERTURE_CALCULATION_INTERVAL ) { getHeaterInternalTemp( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE, TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION ); getHeaterInternalTemp( TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE, TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION ); internalHeatersConversionTimer = 0; } fpgaRawADCReadInterval = 0; } return TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; } /*********************************************************************//** * @brief * The publishTemperatureSensorsData function broadcasts the temperature * sensors data at the publication interval. * @details Inputs: dataPublicationTimerCounter, tempValuesForPublication * @details Outputs: dataPublicationTimerCounter, tempValuesForPublication * @return none *************************************************************************/ static void publishTemperatureSensorsData( void ) { if ( ++dataPublicationTimerCounter >= getU32OverrideValue( &tempSensorsPublishInterval ) ) { TEMPERATURE_SENSORS_DATA_T data; data.inletPrimaryHeater = getTemperatureValue( TEMPSENSORS_INLET_PRIMARY_HEATER ); data.heatDisinfect = getTemperatureValue( TEMPSENSORS_HEAT_DISINFECT ); data.outletPrimaryHeater = getTemperatureValue( TEMPSENSORS_OUTLET_PRIMARY_HEATER ); data.conductivitySensor1 = getTemperatureValue( TEMPSENSORS_CONDUCTIVITY_SENSOR_1 ); data.conductivitySensor2 = getTemperatureValue( TEMPSENSORS_CONDUCTIVITY_SENSOR_2 ); data.outletRedundant = getTemperatureValue( TEMPSENSORS_OUTLET_REDUNDANT ); data.inletDialysate = getTemperatureValue( TEMPSENSORS_INLET_DIALYSATE ); data.primaryHeaterThermocouple = getTemperatureValue( TEMPSENSORS_PRIMARY_HEATER_THERMO_COUPLE ); data.trimmerHeaterThermocouple = getTemperatureValue( TEMPSENSORS_TRIMMER_HEATER_THERMO_COUPLE ); data.priamyHeaterColdjunction = getTemperatureValue( TEMPSENSORS_PRIMARY_HEATER_COLD_JUNCTION ); data.trimmerHeaterColdjunction = getTemperatureValue( TEMPSENSORS_TRIMMER_HEATER_COLD_JUNCTION ); data.primaryHeaterInternal = getTemperatureValue( TEMPSENSORS_PRIMARY_HEATER_INTERNAL ); data.trimmerHeaterInternal = getTemperatureValue( TEMPSENSORS_TRIMMER_HEATER_INTERNAL ); data.fpgaBoard = getTemperatureValue( TEMPSENSORS_FPGA_BOARD_SENSOR ); data.loadCellA1B1 = getTemperatureValue( TEMPSENSORS_LOAD_CELL_A1_B1 ); data.loadCellA2B2 = getTemperatureValue( TEMPSENSORS_LOAD_CELL_A2_B2 ); data.internalTHDORTD = getTemperatureValue( TEMPSENSORS_INTERNAL_TRO_RTD ); data.internalTDIRTD = getTemperatureValue( TEMPSENSORS_INTERNAL_TDI_RTD ); data.internalCondSnsrTemp = getTemperatureValue( TEMPSENSORS_INTERNAL_COND_TEMP_SENSOR ); data.primaryThermoCoupleRaw = getFPGAPrimaryHeaterTemp(); data.primaryColdjuncRaw = getFPGAPrimaryColdJunctionTemp(); data.trimmerThermoCoupleRaw = getFPGATrimmerHeaterTemp(); data.trimmerColdjuncRaw = getFPGATrimmerColdJunctionTemp(); data.cond1Raw = tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_1 ].rawADCReads[ MAX_NUM_OF_RAW_ADC_SAMPLES - 1 ]; data.cond2Raw = tempSensors[ TEMPSENSORS_CONDUCTIVITY_SENSOR_2 ].rawADCReads[ MAX_NUM_OF_RAW_ADC_SAMPLES - 1 ]; broadcastData( MSG_ID_DG_TEMPERATURE_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( TEMPERATURE_SENSORS_DATA_T ) ); dataPublicationTimerCounter = 0; } } /*********************************************************************//** * @brief * The monitorTemperatureSnsrs function monitors the temperature sensors' * temperature value and raises an alarm if any of them are out of range * for more than the specified time. * @details Inputs: tempSensors * @details Outputs: tempSensors * @param sensorIndex the index of the temperature sensor * @return none *************************************************************************/ static void monitorTemperatureSnsrs( U32 sensorIndex ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_TEMPERATURE_SENSORS_ALARM ) != SW_CONFIG_ENABLE_VALUE ) #endif { F32 temperature = getTemperatureValue( sensorIndex ); // Check both temperature and to be in range if ( ( temperature < TEMP_SENSORS_MIN_ALLOWED_DEGREE_C ) || ( temperature > tempSensors[ sensorIndex ].maxAllowedTemperature ) ) { checkPersistentAlarm( ALARM_ID_DG_TEMPERATURE_SENSOR_OUT_OF_RANGE, TRUE, sensorIndex, temperature ); } } } /*********************************************************************//** * @brief * The checkAlarmStatus function checks the status of an alarm and check whether * it has timed out. * @details Inputs: tempSensors * @details Outputs: tempSensors * @param sensorIndex the index of the temperature sensor * @param alarm the alarm ID * @param alarmOccures the boolean signal that indicates whether the error has * occurred. * @param alarmTimeout the timeout of the provided timeout * @return none *************************************************************************/ static void checkAlarmStatus( U32 sensorIndex, ALARM_ID_T alarm, BOOL alarmOccurred, U32 alarmTimeout ) { U32 startTime = tempSensors[ sensorIndex ].alarmStartTime; if ( TRUE == alarmOccurred ) { if ( 0 == startTime ) { tempSensors[ sensorIndex ].alarmStartTime = getMSTimerCount(); } else if ( TRUE == didTimeout( startTime, alarmTimeout ) ) { SET_ALARM_WITH_2_U32_DATA( alarm, sensorIndex, alarmTimeout ); } } else if ( ( FALSE == alarmOccurred ) && ( startTime != 0 ) ) { tempSensors[ sensorIndex ].alarmStartTime = 0; } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testSetMeasuredTemperatureOverride function sets the override value * for a specific temperature sensor. * @details Inputs: tempSensors * @details Outputs: tempSensors * @param sensorIndex temperature sensor index * @param temperature temperature value to override if testing is activated * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetMeasuredTemperatureOverride( U32 sensorIndex, F32 temperature ) { BOOL result = FALSE; if ( sensorIndex < NUM_OF_TEMPERATURE_SENSORS ) { if ( TRUE == isTestingActivated() ) { result = TRUE; tempSensors[ sensorIndex ].temperatureValues.ovData = temperature; tempSensors[ sensorIndex ].temperatureValues.override = OVERRIDE_KEY; } } return result; } /*********************************************************************//** * @brief * The testSetMeasuredTemperatureOverride function resets the override * value of a specified temperature sensor. * @details Inputs: tempSensors * @details Outputs: tempSensors * @param sensorIndex temperature sensor index * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testResetMeasuredTemperatureOverride( U32 sensorIndex ) { BOOL result = FALSE; if ( sensorIndex < NUM_OF_TEMPERATURE_SENSORS ) { if ( TRUE == isTestingActivated() ) { result = TRUE; tempSensors[ sensorIndex ].temperatureValues.override = OVERRIDE_RESET; tempSensors[ sensorIndex ].temperatureValues.ovData = tempSensors[ sensorIndex ].temperatureValues.ovInitData; } } return result; } /*********************************************************************//** * @brief * The testSetTemperatureSensorsPublishIntervalOverride function overrides * the temperature sensors publish data interval. * @details Inputs: tempSensorsPublishInterval * @details Outputs: tempSensorsPublishInterval * @param value sensors data broadcast interval (in ms) to override * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetTemperatureSensorsPublishIntervalOverride( U32 value ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { U32 interval = value / TASK_PRIORITY_INTERVAL; result = TRUE; tempSensorsPublishInterval.ovData = interval; tempSensorsPublishInterval.override = OVERRIDE_KEY; } return result; } /*********************************************************************//** * @brief * The testResetTemperatureSensorsPublishIntervalOverride function resets * the override value of temperature sensors publish data interval. * @details Inputs: tempSensorsPublishInterval * @details Outputs: tempSensorsPublishInterval * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testResetTemperatureSensorsPublishIntervalOverride( void ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { result = TRUE; tempSensorsPublishInterval.override = OVERRIDE_RESET; tempSensorsPublishInterval.ovData = tempSensorsPublishInterval.ovInitData; } return result; } /**@}*/