#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. /// 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 // ********** private function prototypes ********** static THERMISTORS_EXEC_STATES_T handleExecStart( void ); static THERMISTORS_EXEC_STATES_T handleExecGetADCValues( void ); static void convertADCtoTemperature( THERMISTORS_TEMP_SENSORS_T thermistor ); static F32 calcualteOnBoardThemristorTemperature( 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_TEMP_SENSORS_T thermistor; 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 ) { thermistorsStatus[ THERMISTOR_ONBOARD_NTC ].rawADCRead = getIntADCReading( INT_ADC_BOARD_THERMISTOR ); // Zero the counter for the next round of reading adcReadCounter = 0; // Loop through the list and update the temperature values for ( thermistor = THERMISTOR_ONBOARD_NTC; thermistor < NUM_OF_THERMISTORS; thermistor++ ) { convertADCtoTemperature( thermistor ); } } return state; } /*********************************************************************//** * @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( THERMISTORS_TEMP_SENSORS_T thermistor ) { F32 temperature; // Each of the sensors/thermistors have different equations to convert ADC read to temperature switch ( thermistor ) { case THERMISTOR_ONBOARD_NTC: temperature = calcualteOnBoardThemristorTemperature( thermistorsStatus[ THERMISTOR_ONBOARD_NTC ].rawADCRead ); thermistorsStatus[ THERMISTOR_ONBOARD_NTC ].temperatureValue.data = temperature; break; case TEMPSENSOR_FPGA_SENSOR: //TODO do stuff break; case TEMPSENSOR_LOAD_CELL_A1: case TEMPSENSOR_LOAD_CELL_A2: case TEMPSENSOR_THDO_RTD: case TEMPSENSOR_TDI_RTD: case THERMISTOR_CONDUCTIVITY: // TODO fill up the case 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 calcualteOnBoardThemristorTemperature( 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; //TODO add the rest too sensorsData.OnboardThermistor = getThermistorTemperatureValue( THERMISTOR_ONBOARD_NTC ); broadcastThermistorsData( &sensorsData ); dataPublishCounter = 0; } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ BOOL testSetMeasuredThermistorOverride( U32 thermistor, F32 temperature ) { } BOOL testResetMeasuredThermistorOverride( U32 thermistor ) { } BOOL testSetThermistorPublishIntervalOverride( U32 value ) { } BOOL testResetThermistorPublishIntervalOverride( void ) { }