Index: firmware/App/Drivers/InternalADC.c =================================================================== diff -u -r8cbddbe34a4ffed5a4d9fac07a065799c5862611 -r56100135135bb715d316b5fd002a4a4951b9334a --- firmware/App/Drivers/InternalADC.c (.../InternalADC.c) (revision 8cbddbe34a4ffed5a4d9fac07a065799c5862611) +++ firmware/App/Drivers/InternalADC.c (.../InternalADC.c) (revision 56100135135bb715d316b5fd002a4a4951b9334a) @@ -14,47 +14,47 @@ * @date (original) 07-Aug-2024 * ***************************************************************************/ - + #include "adc.h" -#include "InternalADC.h" - +#include "InternalADC.h" + /** * @addtogroup InternalADC * @{ */ -// ********** private definitions ********** - -#define MAX_ADC_CHANNELS 24 ///< ADC supports up to 24 channels. -#define SIZE_OF_ROLLING_AVG 8 ///< Samples in rolling average calculations. -#define ROLLING_AVG_SHIFT_DIVIDER 3 ///< Rolling average shift divider. +// ********** private definitions ********** -/// Mapping from enumerated used ADC channel to processor channel ID. -const INT_ADC_CHANNEL_T ADC_CHANNEL_NUM_TO_CHANNEL_ID[ MAX_ADC_CHANNELS ] = -{ - INT_ADC_NOT_USED, // 0 +#define MAX_ADC_CHANNELS 24 ///< ADC supports up to 24 channels. +#define SIZE_OF_ROLLING_AVG 8 ///< Samples in rolling average calculations. +#define ROLLING_AVG_SHIFT_DIVIDER 3 ///< Rolling average shift divider. + +/// Mapping from enumerated used ADC channel to processor channel ID. +const INT_ADC_CHANNEL_T ADC_CHANNEL_NUM_TO_CHANNEL_ID[ MAX_ADC_CHANNELS ] = +{ + INT_ADC_NOT_USED, // 0 INT_ADC_PS_THERMISTOR, // 1 INT_ADC_PBA_ADC_REF, // 2 INT_ADC_24V_ACTUATORS_REG, // 3 INT_ADC_1_2V_PROCESSOR, // 4 INT_ADC_5V_SENSORS, // 5 - INT_ADC_NOT_USED, // 6 - INT_ADC_NOT_USED, // 7 + INT_ADC_NOT_USED, // 6 + INT_ADC_NOT_USED, // 7 INT_ADC_PRIMARY_ALARM_CURRENT_HG, // 8 - INT_ADC_NOT_USED, // 9 + INT_ADC_NOT_USED, // 9 INT_ADC_BOARD_THERMISTOR, // 10 INT_ADC_1_25_FPGA_ADC_REF, // 11 INT_ADC_3_3V, // 12 INT_ADC_5V_LOGIC, // 13 INT_ADC_PRIMARY_ALARM_CURRENT_LG, // 14 - INT_ADC_NOT_USED, // 15 - INT_ADC_BLOOD_PUMP_SPEED, // 16 - INT_ADC_BLOOD_PUMP_MOTOR_CURRENT, // 17 - INT_ADC_BACKUP_V, // 18 - INT_ADC_NOT_USED, // 19 - INT_ADC_NOT_USED, // 20 - INT_ADC_NOT_USED, // 21 + INT_ADC_NOT_USED, // 15 + INT_ADC_BLOOD_PUMP_SPEED, // 16 + INT_ADC_BLOOD_PUMP_MOTOR_CURRENT, // 17 + INT_ADC_BACKUP_V, // 18 + INT_ADC_NOT_USED, // 19 + INT_ADC_NOT_USED, // 20 + INT_ADC_NOT_USED, // 21 INT_ADC_24V_ACTUATORS, // 22 INT_ADC_NOT_USED // 23 }; @@ -77,130 +77,129 @@ 0.073240, // mA - INT_ADC_PRIMARY_ALARM_CURRENT_LG 0.007106, // V - INT_ADC_24V_ACTUATORS 0.007106, // V - INT_ADC_BACKUP_V - 0.000000, // V - INT_ADC_DUMMY -}; - -// ********** private data ********** - -static adcData_t adcRawReadings[ NUM_OF_INT_ADC_CHANNELS ]; ///< Buffer holds latest adc channel readings. -static U32 adcRawReadingsCount = 0; ///< Readings count for raw readings buffer. - -static U16 adcReadings[ NUM_OF_INT_ADC_CHANNELS ][ SIZE_OF_ROLLING_AVG ]; ///< Holds samples for each channel for a rolling average. -static U32 adcReadingsIdx[ NUM_OF_INT_ADC_CHANNELS ]; ///< Index for next reading in each rolling average array. -static U32 adcReadingsTotals[ NUM_OF_INT_ADC_CHANNELS ]; ///< Rolling total for each channel - used to calc average. -static U32 adcReadingsAvgs[ NUM_OF_INT_ADC_CHANNELS ]; ///< Rolling average for each channel. - -// ********** private function prototypes ********** - -/*********************************************************************//** - * @brief - * The initInternalADC function initializes the InternalADC unit. - * @details \b Inputs: none - * @details \b Outputs: InternalADC unit is initialized. - * @return none - *************************************************************************/ -void initInternalADC( void ) -{ - U32 c,r; - - // Zero all adc values and stats - adcRawReadingsCount = 0; - for ( c = 0; c < NUM_OF_INT_ADC_CHANNELS; c++ ) - { - adcRawReadings[ c ].id = 0; - adcRawReadings[ c ].value = 0; - adcReadingsIdx[ c ] = 0; - adcReadingsTotals[ c ] = 0; - adcReadingsAvgs[ c ] = 0; - for ( r = 0; r < SIZE_OF_ROLLING_AVG; r++ ) - { - adcReadings[ c ][ r ] = 0; - } - } - - // Enable interrupt when all channels converted - adcEnableNotification( adcREG1, adcGROUP1 ); -} - -/*********************************************************************//** - * @brief - * The adcNotification function handles an ADC conversion complete interrupt. - * All channel readings in the FIFO are retrieved. - * @details \b Inputs: ADC FIFO - * @details \b Outputs: adcRawReadingsCount, adcRawReadings[] - * @param adc pointer to the ADC1 controller - * @param group ADC channel group ID - * @return none - *************************************************************************/ -void adcNotification( adcBASE_t *adc, uint32 group ) -{ - if ( adcGROUP1 == group ) - { - adcRawReadingsCount = adcGetData( adcREG1, adcGROUP1, adcRawReadings ); - } -} - -/*********************************************************************//** - * @brief - * The execInternalADC function processes the last set of raw ADC channel +}; + +// ********** private data ********** + +static adcData_t adcRawReadings[ NUM_OF_INT_ADC_CHANNELS ]; ///< Buffer holds latest adc channel readings. +static U32 adcRawReadingsCount = 0; ///< Readings count for raw readings buffer. + +static U16 adcReadings[ NUM_OF_INT_ADC_CHANNELS ][ SIZE_OF_ROLLING_AVG ]; ///< Holds samples for each channel for a rolling average. +static U32 adcReadingsIdx[ NUM_OF_INT_ADC_CHANNELS ]; ///< Index for next reading in each rolling average array. +static U32 adcReadingsTotals[ NUM_OF_INT_ADC_CHANNELS ]; ///< Rolling total for each channel - used to calc average. +static U32 adcReadingsAvgs[ NUM_OF_INT_ADC_CHANNELS ]; ///< Rolling average for each channel. + +// ********** private function prototypes ********** + +/*********************************************************************//** + * @brief + * The initInternalADC function initializes the InternalADC unit. + * @details \b Inputs: none + * @details \b Outputs: InternalADC unit is initialized. + * @return none + *************************************************************************/ +void initInternalADC( void ) +{ + U32 c,r; + + // Zero all adc values and stats + adcRawReadingsCount = 0; + for ( c = 0; c < NUM_OF_INT_ADC_CHANNELS; c++ ) + { + adcRawReadings[ c ].id = 0; + adcRawReadings[ c ].value = 0; + adcReadingsIdx[ c ] = 0; + adcReadingsTotals[ c ] = 0; + adcReadingsAvgs[ c ] = 0; + for ( r = 0; r < SIZE_OF_ROLLING_AVG; r++ ) + { + adcReadings[ c ][ r ] = 0; + } + } + + // Enable interrupt when all channels converted + adcEnableNotification( adcREG1, adcGROUP1 ); +} + +/*********************************************************************//** + * @brief + * The adcNotification function handles an ADC conversion complete interrupt. + * All channel readings in the FIFO are retrieved. + * @details \b Inputs: ADC FIFO + * @details \b Outputs: adcRawReadingsCount, adcRawReadings[] + * @param adc pointer to the ADC1 controller + * @param group ADC channel group ID + * @return none + *************************************************************************/ +void adcNotification( adcBASE_t *adc, uint32 group ) +{ + if ( adcGROUP1 == group ) + { + adcRawReadingsCount = adcGetData( adcREG1, adcGROUP1, adcRawReadings ); + } +} + +/*********************************************************************//** + * @brief + * The execInternalADC function processes the last set of raw ADC channel * readings and kicks off the next conversion of ADC channels. * @details \b Alarm: ALARM_ID_TD_SOFTWARE_FAULT if raw readings count exceeds - * ADC channel count - * @details \b Inputs: adcRawReadingsCount, adcRawReadings[] - * @details \b Outputs: adcReadings[][], adcReadingsIdx[], adcReadingsTotals[], adcReadingsAvgs[] - * @return none - *************************************************************************/ -void execInternalADC( void ) -{ - U32 i; - - if ( adcRawReadingsCount < NUM_OF_INT_ADC_CHANNELS ) - { - // Process readings from last conversion - for ( i = 0; i < adcRawReadingsCount; i++ ) - { - U32 ch = ADC_CHANNEL_NUM_TO_CHANNEL_ID[ adcRawReadings[ i ].id ]; - - adcReadingsTotals[ ch ] -= adcReadings[ ch ][ adcReadingsIdx[ ch ] ]; - adcReadings[ ch ][ adcReadingsIdx[ ch ] ] = adcRawReadings[i].value; - adcReadingsTotals[ ch ] += adcRawReadings[ i ].value; - adcReadingsAvgs[ ch ] = adcReadingsTotals[ ch ] >> ROLLING_AVG_SHIFT_DIVIDER; - adcReadingsIdx[ ch ] = INC_WRAP( adcReadingsIdx[ ch ], 0, SIZE_OF_ROLLING_AVG - 1 ); - } - } - else - { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_INT_ADC_DATA_OVERRUN, adcRawReadingsCount ) - } - - // Start an adc channel group conversion - adcStartConversion( adcREG1, adcGROUP1 ); -} - -/*********************************************************************//** - * @brief - * The getIntADCReading function gets the latest average reading for a given + * ADC channel count + * @details \b Inputs: adcRawReadingsCount, adcRawReadings[] + * @details \b Outputs: adcReadings[][], adcReadingsIdx[], adcReadingsTotals[], adcReadingsAvgs[] + * @return none + *************************************************************************/ +void execInternalADC( void ) +{ + U32 i; + + if ( adcRawReadingsCount < NUM_OF_INT_ADC_CHANNELS ) + { + // Process readings from last conversion + for ( i = 0; i < adcRawReadingsCount; i++ ) + { + U32 ch = ADC_CHANNEL_NUM_TO_CHANNEL_ID[ adcRawReadings[ i ].id ]; + + adcReadingsTotals[ ch ] -= adcReadings[ ch ][ adcReadingsIdx[ ch ] ]; + adcReadings[ ch ][ adcReadingsIdx[ ch ] ] = adcRawReadings[i].value; + adcReadingsTotals[ ch ] += adcRawReadings[ i ].value; + adcReadingsAvgs[ ch ] = adcReadingsTotals[ ch ] >> ROLLING_AVG_SHIFT_DIVIDER; + adcReadingsIdx[ ch ] = INC_WRAP( adcReadingsIdx[ ch ], 0, SIZE_OF_ROLLING_AVG - 1 ); + } + } + else + { + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_INT_ADC_DATA_OVERRUN, adcRawReadingsCount ) + } + + // Start an adc channel group conversion + adcStartConversion( adcREG1, adcGROUP1 ); +} + +/*********************************************************************//** + * @brief + * The getIntADCReading function gets the latest average reading for a given * channel. - * @details \b Alarm: ALARM_ID_TD_SOFTWARE_FAULT if given channel invalid. - * @details \b Inputs: adcReadingsAvgs[] - * @details \b Outputs: none - * @param channel adc channel to retrieve a reading for - * @return The rolling averaged reading for the given channel - *************************************************************************/ -U16 getIntADCReading( INT_ADC_CHANNEL_T channel ) -{ - U16 result = 0; - - if ( channel < NUM_OF_INT_ADC_CHANNELS ) - { - result = adcReadingsAvgs[ channel ]; - } - else - { - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_INT_ADC_INVALID_CHANNEL_REQUESTED1, channel ) - } - - return result; + * @details \b Alarm: ALARM_ID_TD_SOFTWARE_FAULT if given channel invalid. + * @details \b Inputs: adcReadingsAvgs[] + * @details \b Outputs: none + * @param channel adc channel to retrieve a reading for + * @return The rolling averaged reading for the given channel + *************************************************************************/ +U16 getIntADCReading( INT_ADC_CHANNEL_T channel ) +{ + U16 result = 0; + + if ( channel < NUM_OF_INT_ADC_CHANNELS ) + { + result = adcReadingsAvgs[ channel ]; + } + else + { + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_INT_ADC_INVALID_CHANNEL_REQUESTED1, channel ) + } + + return result; } /*********************************************************************//** @@ -228,5 +227,5 @@ return result; } - + /**@}*/