#include // For temperature calculations #include "FPGA.h" #include "InternalADC.h" #include "PersistentAlarm.h" #include "SystemCommMessages.h" #include "Temperatures.h" #include "TaskGeneral.h" /** * @addtogroup Temperatures * @{ */ // ********** private definitions ********** #define TEMPERATURES_DATA_PUBLISH_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Temperatures publish data time interval. #define TEMPERATURES_ADC_READ_INTERVAL ( MS_PER_SECOND / ( 2 * TASK_GENERAL_INTERVAL ) ) ///< Temperatures ADC read time interval. #define ADC_FPGA_READ_DELAY_COUNT 1.0 ///< FGPA read delay upon startup. #define TWELVE_BIT_RESOLUTION 4096U ///< 12 bit resolution conversion. #define THERMISTOR_REFERENCE_VOLTAGE 3.0 ///< Thermistors source voltage. #define THERMISTOR_REFERENCE_RESISTOR_AT_25 10000.0 ///< Thermistors reference resistor in ohms. #define THERMISTOR_REFERENCE_TEMPERATURE 298.0 ///< Thermistors reference temperature in kelvin. #define ONBOARD_THERMISTOR_BETA_VALUE 3380.0 ///< Onboard thermistor beta value. #define POWER_SUPPLY_THERMISTOR_BETA_VALUE 3345.0 ///< Power supply beta value. #define CELSIUS_TO_KELVIN_CONVERSION 273.15 ///< Celsius to Kelvin temperature conversion. #define MIN_ALLOWED_TEMPERATURE 0.0 ///< Thermistors/sensors minimum allowed temperature reading. #define MAX_ALLOWED_TEMPERATURE 80.0 ///< Thermistors/sensors maximum allowed temperature reading. #define MAX_ALLOWED_TEMP_OUT_OF_RANGE_PERIOD ( 5 * MS_PER_SECOND ) ///< Thermistors/sensors maximum allowed temperature out of range period. /// Temperatures exec states typedef enum thermistors_Exec_States { TEMPERATURES_EXEC_STATE_START_STATE = 0, ///< Temperatures exec state start state. TEMPERATURES_EXEC_STATE_GET_ADC_VALUES_STATE, ///< Temperatures exec state get ADC values state. NUM_OF_TEMPERATURES_EXEC_STATES, ///< Number of temperatures exec state. } TEMPERATURES_EXEC_STATES_T; /// Temperatures structure typedef struct { U32 rawADCRead; ///< Temperatures raw ADC read. OVERRIDE_F32_T temperatureValue; ///< Temperatures value. } TEMPERATURE_SENSORS_T; // ********** private data ********** static TEMPERATURES_EXEC_STATES_T temperaturesExecState; ///< Temperatures exec state. static TEMPERATURE_SENSORS_T temperaturesStatus[ NUM_OF_TEMPERATURES ]; ///< Temperature sensors status. static OVERRIDE_U32_T temperaturesPublishInterval = { TEMPERATURES_DATA_PUBLISH_INTERVAL, TEMPERATURES_DATA_PUBLISH_INTERVAL, 0, 0 }; ///< Temperatures publish time interval override. static U32 dataPublishCounter; ///< Temperatures data publish timer counter. static U32 adcReadCounter; ///< Temperatures ADC read counter. static const F32 THERMISTOR_VOLTAGE_CONV_COEFF = THERMISTOR_REFERENCE_VOLTAGE / (F32)TWELVE_BIT_RESOLUTION; ///< On board thermistor ADC to voltage conversion coefficient. static const F32 ON_BOARD_THERMISTOR_REF_TEMP_INV = 1 / THERMISTOR_REFERENCE_TEMPERATURE; ///< On board thermistor reference inverse. // ********** private function prototypes ********** static TEMPERATURES_EXEC_STATES_T handleExecStart( void ); static TEMPERATURES_EXEC_STATES_T handleExecGetADCValues( void ); static void monitorTemperatures( void ); static void convertADC2Temperature( void ); static F32 calculateTemperature( S32 adcValue ); static void publishTemperaturesData( void ); static U32 getPublishTemperaturesDataInterval( void ); /*********************************************************************//** * @brief * The initTemperatures function initializes the temperatures module. * @details Inputs: none * @details Outputs: temperaturesExecState, dataPublishCounter, temperaturesStatus * @return none *************************************************************************/ void initTemperatures( void ) { U08 i; temperaturesExecState = TEMPERATURES_EXEC_STATE_START_STATE; dataPublishCounter = 0; for ( i = 0; i < NUM_OF_TEMPERATURES; i++ ) { memset( &temperaturesStatus[ i ], 0x0, sizeof( TEMPERATURE_SENSORS_T ) ); } // Initialize a persistent alarm for temperatures temeprature out of range initPersistentAlarm( ALARM_ID_HD_TEMPERATURES_OUT_OF_RANGE, MAX_ALLOWED_TEMP_OUT_OF_RANGE_PERIOD, MAX_ALLOWED_TEMP_OUT_OF_RANGE_PERIOD ); } /*********************************************************************//** * @brief * The execTemperaturesSelfTest function runs the temperatures POST during * the self-test. * @details Inputs: none * @details Outputs: none * @return execTemperaturesSelfTest which is the status of the self test *************************************************************************/ SELF_TEST_STATUS_T execTemperaturesSelfTest( void ) { SELF_TEST_STATUS_T status = SELF_TEST_STATUS_IN_PROGRESS; // TODO implement the calibration processing function. // It returns a pass for now status = SELF_TEST_STATUS_PASSED; return status; } /*********************************************************************//** * @brief * The execTemperatures function executes the temperature sensors' state machine. * @details Inputs: temperaturesExecState * @details Outputs: temperaturesExecState * @return none *************************************************************************/ void execTemperatures( void ) { // Read the sensors all the time switch ( temperaturesExecState ) { case TEMPSENSORS_EXEC_STATE_START: temperaturesExecState = handleExecStart(); break; case TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES: temperaturesExecState = handleExecGetADCValues(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_TEMPERATURE_SENSORS_EXEC_INVALID_STATE, temperaturesExecState ); temperaturesExecState = TEMPSENSORS_EXEC_STATE_GET_ADC_VALUES; break; } // Publish the data getPublishTemperaturesDataInterval(); } /**@}*/