#include #include "Thermistors.h" #include "FPGA.h" #include "TaskGeneral.h" #include "InternalADC.h" #include "SystemCommMessages.h" /** * @addtogroup Thermistors * @{ */ // ********** private definitions ********** #define THERMISTORS_DATA_PUBLISH_INTERVAL (MS_PER_SECOND / TASK_GENERAL_INTERVAL) ///< Thermistors publish data time interval. #define THERMISTORS_ADC_READ_INTERVAL (MS_PER_SECOND / (2 * TASK_GENERAL_INTERVAL)) ///< Thermistors ADC read time interval. #define ADC_FPGA_READ_DELAY_COUNT 1 ///< FGPA read delay upon startup. #define ONBOARD_THERMISTOR_SOURCE_VOLTAGE 3 ///< Onboard thermistor source voltage. #define ONBOARD_THERMISTOR_REFERENCE_RESISTOR 10 ///< Onboard thermistor reference resistor. #define ONBOARD_THERMISTOR_BETA_VALUE 3380 ///< Onboard thermistor beta value. #define ONBOARD_THERMISTOR_REFERENCE_TEMPERATURE 298 ///< Onboard thermistor reference temperature. #define TWELVE_BIT_RESOLUTION 4096 ///< 12 bit resolution conversion. #define CELSIUS_TO_KELVIN_CONVERSION 273.15 ///< Celsius to Kelvin temperature conversion. #define ADC_TEMP_SENSORS_CONVERSION_CONST 272.5 ///< ADC temperature sensors conversion constant. #define MIN_ALLOWED_TEMPERATURE 10 ///< Thermistors/sensors minimum allowed temperature reading. #define MAX_ALLOWED_TEMPERATURE 70 ///< Thermistors/sensors maximum allowed temperature reading. /// Thermistors self test states typedef enum thermistors_Self_Test_States { THERMISTORS_SELF_TEST_START = 0, ///< Thermistors self test start THERMISTROS_SELF_TEST_ADC_CHECK, ///< Thermistors self test ADC check THERMISTORS_SELF_TEST_COMPLETE, ///< Thermistors self test complete NUM_OF_THERMISTORS_SEsLF_TEST_STATES, ///< Number of thermistors self test states } THERMISTORS_SELF_TEST_STATES_T; /// Thermistors exec states typedef enum thermistors_Exec_States { THERMISTORS_EXEC_STATE_START = 0, ///< Thermistors exec state start THERMISTORS_EXEC_STATE_GET_ADC_VALUES, ///< Thermistors exec state get ADC values NUM_OF_THERMISTORS_EXEC_STATES, ///< Number of thermistors exec state } THERMISTORS_EXEC_STATES_T; /// Thermistor struct typedef struct { S32 rawADCRead; ///< Thermistor raw ADC read OVERRIDE_F32_T temperatureValue; ///< Thermistor temperature value } THERMISTOR_T; static SELF_TEST_STATUS_T thermistorsSelfTestReslt = SELF_TEST_STATUS_IN_PROGRESS; ///< Thermistors self test result static THERMISTORS_SELF_TEST_STATES_T thermistorsSelfTestState = THERMISTORS_SELF_TEST_START; ///< Thermistors self test state static THERMISTORS_EXEC_STATES_T thermistorsExecState = THERMISTORS_EXEC_STATE_START; ///< Thermistors exec state static THERMISTOR_T thermistorsStatus[ NUM_OF_THERMISTORS ]; ///< Thermistors array static OVERRIDE_U32_T thermistorsPublishInterval = { THERMISTORS_DATA_PUBLISH_INTERVAL, THERMISTORS_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Thermistors publish time interval override static U32 dataPublishCounter; ///< Thermistors data publish timer counter static U32 adcReadCounter; ///< Thermistors ADC read counter static const F32 fpgaBoardTempSensorConvCoeff = 503.975 / TWELVE_BIT_RESOLUTION; ///< FPGA board temperature sensor conversion coefficient static const F32 adcTempSensorsConversionCoeff1 = 1 / 13584; ///< ADC temperature sensors conversion coefficient 1 static const F32 adcTempSensorsConversionCoeff2 = 0x800000 / 13584; ///< ADC temperature sensors conversion coefficient 2 // ********** private function prototypes ********** static THERMISTORS_EXEC_STATES_T handleExecStart( void ); static THERMISTORS_EXEC_STATES_T handleExecGetADCValues( void ); static void monitorThermistors( void ); static void convertADCtoTemperature( void ); static F32 calculateOnBoardThemristorTemperature( U32 adcValue ); static void publishThermistorsData( void ); static U32 getPublishThermistorsDataInterval( void ); /*********************************************************************//** * @brief * The initThermistors function initializes the thermistors module. * @details Inputs: thermistorsSelfTestReslt, thermistorsExecState, * thermistorsExecState, dataPublishCounter * @details Outputs: thermistorsSelfTestReslt, thermistorsExecState, * thermistorsExecState, dataPublishCounter * @return none *************************************************************************/ void initThermistors( void ) { thermistorsSelfTestReslt = SELF_TEST_STATUS_IN_PROGRESS; thermistorsExecState = THERMISTORS_EXEC_STATE_START; thermistorsSelfTestState = THERMISTORS_SELF_TEST_START; dataPublishCounter = 0; } /*********************************************************************//** * @brief * The execThermistorsSelfTest function executes the thermistors self test. * @details Inputs: thermistorsSelfTestState * @details Outputs: thermistorsSelfTestState * @return Status of self test *************************************************************************/ SELF_TEST_STATUS_T execThermistorsSelfTest( void ) { switch ( thermistorsSelfTestState ) { case THERMISTORS_SELF_TEST_START: break; case THERMISTROS_SELF_TEST_ADC_CHECK: break; case THERMISTORS_SELF_TEST_COMPLETE: // Done with POST. Do nothing. break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_THERMISTORS_INVALID_SELF_TEST_STATE, thermistorsSelfTestState ); thermistorsSelfTestState = THERMISTORS_SELF_TEST_COMPLETE; break; } return thermistorsSelfTestReslt; } /*********************************************************************//** * @brief * The execThermistors function executes the thermistors exec states. * @details Inputs: thermistorsExecState * @details Outputs: thermistorsExecState * @return none *************************************************************************/ void execThermistors( void ) { switch ( thermistorsExecState ) { case THERMISTORS_EXEC_STATE_START: thermistorsExecState = handleExecStart(); break; case THERMISTORS_EXEC_STATE_GET_ADC_VALUES: thermistorsExecState = handleExecGetADCValues(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_THERMISTORS_INVALID_EXEC_STATE, thermistorsExecState ); thermistorsExecState = THERMISTORS_EXEC_STATE_GET_ADC_VALUES; break; } // Check if it is time to publish any data publishThermistorsData(); } /*********************************************************************//** * @brief * The getThermistorTemperatureValue function returns the temperature of * a requested thermistor or temperature sensor. * @details Inputs: thermistorsStatus * @details Outputs: none * @param thermistor to get its temperature value * @return temperature of a thermistor or temperature sensor *************************************************************************/ F32 getThermistorTemperatureValue( THERMISTORS_TEMP_SENSORS_T thermistor ) { F32 temperature = 0.0; // Check if the thermistor of sensor is in range if ( thermistor < NUM_OF_THERMISTORS ) { if ( thermistorsStatus[ thermistor ].temperatureValue.override == OVERRIDE_KEY ) { temperature = thermistorsStatus[ thermistor ].temperatureValue.ovData; } else { temperature = thermistorsStatus[ thermistor ].temperatureValue.data; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_THERMISTOR_SELECTED, thermistor ); } return temperature; } /*********************************************************************//** * @brief * The handleExecStart function handles the start state of the exec state * machine. * @details Inputs: adcReadCounter * @details Outputs: adcReadCounter * @return next state of the exec state machine *************************************************************************/ static THERMISTORS_EXEC_STATES_T handleExecStart( void ) { THERMISTORS_EXEC_STATES_T state = THERMISTORS_EXEC_STATE_START; // Give a short time for FPGA to boot up and start sending the ADC reads if ( ++adcReadCounter > ADC_FPGA_READ_DELAY_COUNT ) { state = THERMISTORS_EXEC_STATE_GET_ADC_VALUES; adcReadCounter = 0; } return state; } /*********************************************************************//** * @brief * The handleExecGetADCValues function handles the get ADC values state * of the exec state machine. * @details Inputs: adcReadCounter, thermistorsStatus * @details Outputs: adcReadCounter, thermistorsStatus * @return next state of the exec machine *************************************************************************/ static THERMISTORS_EXEC_STATES_T handleExecGetADCValues( void ) { THERMISTORS_EXEC_STATES_T state = THERMISTORS_EXEC_STATE_GET_ADC_VALUES; // If time has elapsed to read the ADCs, read them all if ( ++adcReadCounter >= THERMISTORS_ADC_READ_INTERVAL ) { // Get all the raw readings in ADC thermistorsStatus[ THERMISTOR_ONBOARD_NTC ].rawADCRead = getIntADCReading( INT_ADC_BOARD_THERMISTOR ); thermistorsStatus[ TEMPSENSOR_FPGA_SENSOR ].rawADCRead = getFPGABoardTemp(); thermistorsStatus[ TEMPSENSOR_LOAD_CELL_A1 ].rawADCRead = getFPGALoadCellsA1B1Temp(); thermistorsStatus[ TEMPSENSOR_LOAD_CELL_A2 ].rawADCRead = getFPGALoadCellsA2B2Temp(); thermistorsStatus[ TEMPSENSOR_THDO_RTD ].rawADCRead = getFPGATHDoInternalTemp(); thermistorsStatus[ TEMPSENSOR_TDI_RTD ].rawADCRead = getFPGATDiInternalTemp(); thermistorsStatus[ TEMPSENSOR_CONDUCTIVITY ].rawADCRead = getFPGAConductivitySnsrInternalTemp(); // Zero the counter for the next round of reading adcReadCounter = 0; // Convert all the latest ADCs to temperature convertADCtoTemperature(); // Monitor the values for a gross range check monitorThermistors(); } return state; } /*********************************************************************//** * @brief * The monitorThermistors function monitors the thermistors and sensors * for gross temperature range check. * @details Inputs: none * @details Outputs: none * @return none *************************************************************************/ static void monitorThermistors( void ) { THERMISTORS_TEMP_SENSORS_T thermistor; F32 temperature; for ( thermistor = THERMISTOR_ONBOARD_NTC; thermistor < NUM_OF_THERMISTORS; thermistor++ ) { temperature = getThermistorTemperatureValue( thermistor ); // If the thermisotrs and sensors read temperature out of range, raise an alarm if ( temperature < MIN_ALLOWED_TEMPERATURE || temperature >= MAX_ALLOWED_TEMPERATURE ) { SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DG_THERMISOTRS_TEMPERATURE_OUT_OF_RANGE, thermistor, temperature ); } } } /*********************************************************************//** * @brief * The convertADCtoTemperature function converts the ADC values of different * thermistors and temperature sensors to temperature value. * @details Inputs: thermistorsStatus * @details Outputs: thermistorsStatus * @param thermistor (also covers the temperature sensors) to convert its value * @return none *************************************************************************/ static void convertADCtoTemperature( void ) { THERMISTORS_TEMP_SENSORS_T thermistor; F32 temperature; U32 rawADC; // Loop through the list and update the temperature values for ( thermistor = THERMISTOR_ONBOARD_NTC; thermistor < NUM_OF_THERMISTORS; thermistor++ ) { rawADC = thermistorsStatus[ thermistor ].rawADCRead; // Each of the sensors/thermistors have different equations to convert ADC read to temperature switch ( thermistor ) { case THERMISTOR_ONBOARD_NTC: temperature = calculateOnBoardThemristorTemperature( rawADC ); thermistorsStatus[ THERMISTOR_ONBOARD_NTC ].temperatureValue.data = temperature; break; case TEMPSENSOR_FPGA_SENSOR: temperature = ( rawADC * fpgaBoardTempSensorConvCoeff ) - CELSIUS_TO_KELVIN_CONVERSION; thermistorsStatus[ TEMPSENSOR_FPGA_SENSOR ].temperatureValue.data = temperature; break; // All these sensors have the same conversion procedure case TEMPSENSOR_LOAD_CELL_A1: case TEMPSENSOR_LOAD_CELL_A2: case TEMPSENSOR_THDO_RTD: case TEMPSENSOR_TDI_RTD: case TEMPSENSOR_CONDUCTIVITY: temperature = ( ( rawADC * adcTempSensorsConversionCoeff1 ) - adcTempSensorsConversionCoeff2 ) - ADC_TEMP_SENSORS_CONVERSION_CONST; thermistorsStatus[ thermistor ].temperatureValue.data = temperature; break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_THERMISTOR_SELECTED, thermistor ); break; } } } /*********************************************************************//** * @brief * The calcualteOnBoardThemristorTemperature function converts the ADC value * of the onboard thermistor into temperature in C * @details Inputs: none * @details Outputs: none * @param ADC value to be converted into temperature in C * @return calculated temperature in C *************************************************************************/ static F32 calculateOnBoardThemristorTemperature( U32 adcValue ) { //TODO add comments F32 thermistorVoltage = ( adcValue * ONBOARD_THERMISTOR_SOURCE_VOLTAGE ) / TWELVE_BIT_RESOLUTION; F32 thermistorResistor = ( ( ONBOARD_THERMISTOR_REFERENCE_RESISTOR * ONBOARD_THERMISTOR_SOURCE_VOLTAGE ) - ( ONBOARD_THERMISTOR_REFERENCE_RESISTOR * thermistorVoltage ) ) / thermistorVoltage; F32 InvTemperature = ( logf(thermistorResistor/ONBOARD_THERMISTOR_REFERENCE_RESISTOR) / ONBOARD_THERMISTOR_BETA_VALUE ) + ( 1 / ONBOARD_THERMISTOR_REFERENCE_TEMPERATURE); F32 temperature = 1 / InvTemperature; return temperature; } /*********************************************************************//** * @brief * The getPublishThermistorsDataInterval function gets the thermistors * data publish interval. * @details Inputs: thermistorsPublishInterval * @details Outputs: none * @return data publish time interval in counts *************************************************************************/ static U32 getPublishThermistorsDataInterval( void ) { U32 result = thermistorsPublishInterval.data; if ( OVERRIDE_KEY == thermistorsPublishInterval.override ) { result = thermistorsPublishInterval.ovData; } return result; } /*********************************************************************//** * @brief * The publishThermistorsData function publishes the thermistors and * temperature sensors data at the specified time interval. * @details Inputs: dataPublishCounter * @details Outputs: dataPublishCounter * @return none *************************************************************************/ static void publishThermistorsData( void ) { if ( ++dataPublishCounter > getPublishThermistorsDataInterval() ) { THERMISTORS_DATA_T sensorsData; // Get all the sensors/thermistors values for publication sensorsData.onboardThermistor = getThermistorTemperatureValue( THERMISTOR_ONBOARD_NTC ); sensorsData.fpgaBoardTempSensor = getFPGABoardTemp(); sensorsData.loadCellA1TempSensor = getFPGALoadCellsA1B1Temp(); sensorsData.loadCellA2TempSensor = getFPGALoadCellsA2B2Temp(); sensorsData.rtdInternalTempSensor = getFPGATHDoInternalTemp(); sensorsData.rtdTDiInternalTempSensor = getFPGATDiInternalTemp(); sensorsData.conductivityTempSensor = getFPGAConductivitySnsrInternalTemp(); // Broadcast the thermistors data broadcastThermistorsData( &sensorsData ); // Reset the counter dataPublishCounter = 0; } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testSetThermistorPublishIntervalOverride function overrides the * thermistors data publish interval. * @details Inputs: thermistorsPublishInterval * @details Outputs: thermistorsPublishInterval * @param value which is override value for the thermistors data publish * interval * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetThermistorPublishIntervalOverride( U32 value ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { U32 intvl = value / TASK_GENERAL_INTERVAL; result = TRUE; thermistorsPublishInterval.ovData = intvl; thermistorsPublishInterval.override = OVERRIDE_KEY; } return result; } /*********************************************************************//** * @brief * The testResetThermistorPublishIntervalOverride function resets the override * of the thermistors publish interval. * @details Inputs: thermistorsPublishInterval * @details Outputs: thermistorsPublishInterval * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetThermistorPublishIntervalOverride( void ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { result = TRUE; thermistorsPublishInterval.override = OVERRIDE_RESET; thermistorsPublishInterval.ovData = thermistorsPublishInterval.ovInitData; } return result; } /*********************************************************************//** * @brief * The testSetMeasuredThermistorOverride function overrides the value of * a thermistor. * @details Inputs: thermistorsStatus * @details Outputs: thermistorsStatus * @param thermistor which its value is overridden * @param temperature value to override * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetMeasuredThermistorOverride( U32 thermistor, F32 temperature ) { BOOL result = FALSE; if ( thermistor < NUM_OF_THERMISTORS && isTestingActivated() ) { // There is a temperature range that the thermistors can be set to, otherwise, the driver // will throw an alarm. Also, the fans driver depends on these values to continuously control the fans if ( temperature >= MIN_ALLOWED_TEMPERATURE && temperature < MAX_ALLOWED_TEMPERATURE ) { result = TRUE; thermistorsStatus[ thermistor ].temperatureValue.ovData = temperature; thermistorsStatus[ thermistor ].temperatureValue.override = OVERRIDE_KEY; } } return result; } /*********************************************************************//** * @brief * The testResetMeasuredThermistorOverride function resets the override * value of a thermistor. * @details Inputs: thermistorsStatus * @details Outputs: thermistorsStatus * @param thermistor which its value is overridden * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testResetMeasuredThermistorOverride( U32 thermistor ) { BOOL result = FALSE; if ( thermistor < NUM_OF_THERMISTORS ) { if ( isTestingActivated() ) { result = TRUE; thermistorsStatus[ thermistor ].temperatureValue.override = OVERRIDE_RESET; thermistorsStatus[ thermistor ].temperatureValue.ovData = thermistorsStatus[ thermistor ].temperatureValue.ovInitData; } } return result; } /**@}*/