Index: firmware/App/Controllers/TemperatureSensors.c =================================================================== diff -u -r3982a398c9c14a8f7688656b60055d4010328456 -r4ac01748d5c61f07470acfc6ae535d770f7e872d --- firmware/App/Controllers/TemperatureSensors.c (.../TemperatureSensors.c) (revision 3982a398c9c14a8f7688656b60055d4010328456) +++ firmware/App/Controllers/TemperatureSensors.c (.../TemperatureSensors.c) (revision 4ac01748d5c61f07470acfc6ae535d770f7e872d) @@ -18,6 +18,9 @@ #include "TemperatureSensors.h" #include "FPGA.h" +// TODO Clean up +// TODO Add Doxygen comments + // Private variables #define PRIMARY_HEATER_EXT_TEMP_SENSORS_GAIN 16U @@ -33,13 +36,23 @@ #define TRIMMER_HEATER_EXT_TEMP_SENSORS_0_DEGREE_RESISTANCE 100U -#define TEMPERATURE_SENSORS_ADC_BITS 24U -#define TEMPERATURE_SENSORS_ADC_MAX_COUNT (pow(2,TEMPERATURE_SENSORS_ADC_BITS)) +#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 { @@ -53,27 +66,59 @@ /// temperature sensor exec states typedef enum tempSensors_Exec_States { - TEMPSENSORS_EXEC_STATE_WAIT_FOR_POST = 0, - TEMPSENSROS_EXEC_STATE_READ_SENSORS, + 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_WAIT_FOR_POST; ///< TemperatureSensor exec 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 handleExecWaitForPost ( 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 /************************************************************************* @@ -85,12 +130,14 @@ * @param none * @return none *************************************************************************/ + void initTemperatureSensors ( void ) { tempSensorsSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; tempSensorsSelfTestState = TEMPSENSORS_SELF_TEST_START; - tempSensorsExecState = TEMPSENSORS_EXEC_STATE_WAIT_FOR_POST; - + tempSensorsExecState = TEMPSENSORS_EXEC_STATE_START; + initialADCReadIndex = 0; + // TODO initialize the 2D and 1D arrays } /************************************************************************* @@ -145,22 +192,23 @@ *************************************************************************/ void execTemperatureSensors ( void ) { + // read the sensors all the time switch ( tempSensorsExecState ) { - case TEMPSENSORS_EXEC_STATE_WAIT_FOR_POST: + case TEMPSENSORS_SELF_TEST_START: - tempSensorsExecState = handleExecWaitForPost(); + tempSensorsExecState = handleExecStart(); break; - case TEMPSENSROS_EXEC_STATE_READ_SENSORS: + case TEMPSENSORS_EXEC_STATE_READ_SENSORS: // overwrite structure for the sensors tempSensorsExecState = handleExecReadSensors(); break; default: // TODO add software fault - tempSensorsExecState = TEMPSENSROS_EXEC_STATE_READ_SENSORS; + tempSensorsExecState = TEMPSENSORS_EXEC_STATE_READ_SENSORS; } } @@ -179,8 +227,9 @@ 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,(TEMPERATURE_SENSORS_ADC_BITS - 1)))) / (gain * pow(2,(TEMPERATURE_SENSORS_ADC_BITS - 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); @@ -214,11 +263,11 @@ return state; } -static TEMPSENSORS_EXEC_STATES_T handleExecWaitForPost ( void ) +static TEMPSENSORS_EXEC_STATES_T handleExecStart ( void ) { - TEMPSENSORS_EXEC_STATES_T state = TEMPSENSORS_EXEC_STATE_WAIT_FOR_POST; + TEMPSENSORS_EXEC_STATES_T state = TEMPSENSORS_EXEC_STATE_READ_SENSORS; - if ( tempSensorsSelfTestState == TEMPSENSORS_SELF_TEST_COMPLETE ) + /*if ( tempSensorsSelfTestState == TEMPSENSORS_SELF_TEST_COMPLETE ) { // TODO FOR TESTING, REMOVE THIS CODE U32 testADC = 16327313; @@ -231,14 +280,24 @@ // TODO REMOVE THE ABOVE CODE - state = TEMPSENSROS_EXEC_STATE_READ_SENSORS; - } + state = TEMPSENSORS_EXEC_STATE_READ_SENSORS; + }*/ return state; } static TEMPSENSORS_EXEC_STATES_T handleExecReadSensors ( void ) { - TEMPSENSORS_EXEC_STATES_T state = TEMPSENSROS_EXEC_STATE_READ_SENSORS; + 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