/**********************************************************************//** * * Copyright (c) 2019-2020 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 * * @date 7-Apr-2020 * @author Dara Navaei * * @brief DG temperature sensors controller * **************************************************************************/ #include #include "TemperatureSensors.h" #include "FPGA.h" // TODO Clean up // TODO Add Doxygen comments // Private variables #define PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN 16U #define PRIMARY_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE 19600U #define PRIMARY_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE 1000U #define COND_SENSORS_TEMP_SENSOR_GAIN 16U #define COND_SENSORS_TEMP_SENSOR_REF_RESISTANCE 19600U #define COND_SENSORS_TEMP_SENSOR_0_DEGREE_RESISTANCE 1000U #define TRIMMER_HEATER_EXT_TEMP_SENSORS_GAIN 32U #define TRIMMER_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE 5110U #define TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE 100U #define TEMP_SENSORS_ADC_BITS 24U #define TEMP_SENSORS_ADC_MAX_COUNT (pow(2,TEMP_SENSORS_ADC_BITS)) #define TEMP_EQUATION_COEFF_A (3.9083 * pow(10,-3)) #define TEMP_EQUATION_COEFF_B (-5.775 * pow(10,-7)) // TODO Change this to 8 to add the internal heater sensors #define NUMBER_OF_TEMP_SENSORS 6U #define NUMBER_OF_ADC_READS 20U #define ADC_READ_FIRST_READ_INDEX 0U // The last index is calculated by having the number of ADC reads // plus 1 that is read current index #define ADC_READ_LAST_READ_INDEX (NUMBER_OF_ADC_READS - 1) #define ADC_READ_NEXT_INDEX_INDEX (ADC_READ_LAST_READ_INDEX + 1) #define ADC_READ_RUNNING_SUM_INDEX (ADC_READ_NEXT_INDEX_INDEX + 1) /// Temperature sensor self test states typedef enum tempSensors_Self_Test_States { TEMPSENSORS_SELF_TEST_START = 0, TEMPSENSORS_SELF_TEST_ADC_CHECK, TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK, TEMPSENSORS_SELF_TEST_COMPLETE, NUM_OF_TEMPSENSORS_SELF_TEST_STATES } TEMPSENSORS_SELF_TEST_STATES_T; /// temperature sensor exec states typedef enum tempSensors_Exec_States { TEMPSENSORS_EXEC_STATE_START = 0, TEMPSENSORS_EXEC_STATE_READ_SENSORS, NUM_OF_TEMPSENSORS_EXEC_STATES, } TEMPSENSORS_EXEC_STATES_T; static SELF_TEST_STATUS_T tempSensorsSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; ///< Self test result of the TemperatureSensors module static TEMPSENSORS_SELF_TEST_STATES_T tempSensorsSelfTestState = TEMPSENSORS_SELF_TEST_START; ///< TemperatureSensor self test state static TEMPSENSORS_EXEC_STATES_T tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; ///< TemperatureSensor exec state static U32 tempSensorsADCReads [ NUMBER_OF_TEMP_SENSORS ] [ NUMBER_OF_ADC_READS + 2 ] = {0}; ///< Number of ADC reads + the running sum static F32 tempSensorsAveragedValues [ NUMBER_OF_TEMP_SENSORS ]; static U32 initialADCReadIndex = 0; // Private functions prototypes static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestStart ( void ); static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestADCCheck ( void ); static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestConsistencyCheck ( void ); static TEMPSENSORS_EXEC_STATES_T handleExecStart ( void ); static TEMPSENSORS_EXEC_STATES_T handleExecReadSensors ( void ); static F32 getADC2TempConversion ( U32 adc, U32 gain, U32 refResistance, U32 zeroDegResistance ); static void processADCRead ( U32 sensorIndex, U32 adc ); static void processADCRead ( U32 sensorIndex, U32 adc ) { U32 sampleCount; U32 index = tempSensorsADCReads [ sensorIndex ] [ ADC_READ_NEXT_INDEX_INDEX ]; U32 runningSum = tempSensorsADCReads [ sensorIndex ] [ ADC_READ_RUNNING_SUM_INDEX ]; U32 indexValue = tempSensorsADCReads [ sensorIndex ] [ index ]; U32 nextIndex = INC_WRAP( index, ADC_READ_FIRST_READ_INDEX, ADC_READ_LAST_READ_INDEX ); runningSum = runningSum - indexValue + adc; tempSensorsADCReads [ sensorIndex ] [ index ] = adc; tempSensorsADCReads [ sensorIndex ] [ ADC_READ_RUNNING_SUM_INDEX ] = runningSum; tempSensorsADCReads [ sensorIndex ] [ ADC_READ_NEXT_INDEX_INDEX ] = nextIndex; if ( initialADCReadIndex < ADC_READ_LAST_READ_INDEX ) { sampleCount = initialADCReadIndex + 1; initialADCReadIndex++; } else { sampleCount = NUMBER_OF_ADC_READS; } F32 test = runningSum / sampleCount; // Remove this code tempSensorsAveragedValues [ sensorIndex ] = runningSum / sampleCount; } // Public functions /************************************************************************* * @brief initTemperatureSensors * The initTemperatureSensors initializes the module * @details * Inputs : none * Outputs : none * @param none * @return none *************************************************************************/ void initTemperatureSensors ( void ) { tempSensorsSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; tempSensorsSelfTestState = TEMPSENSORS_SELF_TEST_START; tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; initialADCReadIndex = 0; // TODO initialize the 2D and 1D arrays } /************************************************************************* * @brief execTemperatureSensorsSelfTest * The execTemperatureSensorsSelfTest runs the TemperatureSensors POST * during the self test * @details * Inputs : none * Outputs : SELF_TEST_STATUS_T * @param none * @return SELF_TEST_STATUS_T *************************************************************************/ SELF_TEST_STATUS_T execTemperatureSensorsSelfTest ( void ) { switch ( tempSensorsSelfTestState ) { case TEMPSENSORS_SELF_TEST_START: tempSensorsSelfTestState = handleSelfTestStart(); break; case TEMPSENSORS_SELF_TEST_ADC_CHECK: tempSensorsSelfTestState = handleSelfTestADCCheck(); break; case TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK: tempSensorsSelfTestState = handleSelfTestConsistencyCheck(); break; case TEMPSENSORS_SELF_TEST_COMPLETE: // Done with self test, do nothing break; default: //TODO add software fault tempSensorsSelfTestState = TEMPSENSORS_SELF_TEST_COMPLETE; } return tempSensorsSelfTestResult; } /************************************************************************* * @brief execTemperatureSensors * The execTemperatureSensors runs the TemperatureSensors main tasks * @details * Inputs : none * Outputs : none * @param none * @return none *************************************************************************/ void execTemperatureSensors ( void ) { // read the sensors all the time switch ( tempSensorsExecState ) { case TEMPSENSORS_SELF_TEST_START: tempSensorsExecState = handleExecStart(); break; case TEMPSENSORS_EXEC_STATE_READ_SENSORS: // overwrite structure for the sensors tempSensorsExecState = handleExecReadSensors(); break; default: // TODO add software fault tempSensorsExecState = TEMPSENSORS_EXEC_STATE_READ_SENSORS; } } // Private functions /************************************************************************* * @brief getADC2TempConversion * The getADC2TempConversion calculates the temperature from ADC read from * FPGA * @details * Inputs : U32 (adc, gain, refResistance) * Outputs : F32 (temperature in deg C) * @param none * @return F32 (temperature in deg C) *************************************************************************/ static F32 getADC2TempConversion ( U32 adc, U32 gain, U32 refResistance, U32 zeroDegResistance ) { //R(RTD) = R(ref) * (adc – 2^N-1) / (G *2^N-1); F32 resistance = (refResistance * (adc - pow(2,(TEMP_SENSORS_ADC_BITS - 1)))) / (gain * pow(2,(TEMP_SENSORS_ADC_BITS - 1))); // For testing in a separate workspace, loop through and do the equation, disable Irq and Fiq and enable Irq and // get the mstimer() //T=(-A+√(A^2-4B(1-R_T/R_0 )))/2B F32 secondSqrtPart = 4 * TEMP_EQUATION_COEFF_B * (1 - (resistance / zeroDegResistance)); F32 temperature = (-TEMP_EQUATION_COEFF_A + sqrt(pow(TEMP_EQUATION_COEFF_A, 2) - secondSqrtPart)) / (2 * TEMP_EQUATION_COEFF_B); return temperature; } static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestStart ( void ) { return TEMPSENSORS_SELF_TEST_ADC_CHECK; } static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestADCCheck ( void ) { TEMPSENSORS_SELF_TEST_STATES_T state = TEMPSENSORS_SELF_TEST_ADC_CHECK; // TODO: check ADC and stuff // REMOVE THIS CODE state = TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK; // REMOVE THIS CODEs return state; } static TEMPSENSORS_SELF_TEST_STATES_T handleSelfTestConsistencyCheck ( void ) { TEMPSENSORS_SELF_TEST_STATES_T state = TEMPSENSORS_SELF_TEST_CONSISTENCY_CHECK; //TODO steps to consistency check // REMOVE THIS CODE state = TEMPSENSORS_SELF_TEST_COMPLETE; // REMOVE THIS CODE return state; } static TEMPSENSORS_EXEC_STATES_T handleExecStart ( void ) { TEMPSENSORS_EXEC_STATES_T state = TEMPSENSORS_EXEC_STATE_READ_SENSORS; /*if ( tempSensorsSelfTestState == TEMPSENSORS_SELF_TEST_COMPLETE ) { // TODO FOR TESTING, REMOVE THIS CODE U32 testADC = 16327313; //F32 temp = getADC2TempConversion ( testADC, TRIMMER_HEATER_EXT_TEMP_SENSORS_GAIN, TRIMMER_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE, // TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE); F32 temp = getADC2TempConversion ( testADC, PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN, PRIMARY_HEATER_EXT_TEMP_SENSORS_REF_RESISTANCE, PRIMARY_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE); // TODO REMOVE THE ABOVE CODE state = TEMPSENSORS_EXEC_STATE_READ_SENSORS; }*/ return state; } static TEMPSENSORS_EXEC_STATES_T handleExecReadSensors ( void ) { TEMPSENSORS_EXEC_STATES_T state = TEMPSENSORS_EXEC_STATE_READ_SENSORS; // TODO REMOVE THIS CODE. FOR TESTING ONLY U32 test [20] = {16327313, 16330313, 16333313, 16336313, 16339313, 16342313, 16345313, 16348313, 16351313, 16354313, 16357313, 16360313, 16363313, 16366313, 16369313, 16372313, 16375313, 16378313, 16381313, 16384313}; U08 i; for ( i = 0; i