diff --git a/components/bms_boss/HW/HW_adc.c b/components/bms_boss/HW/HW_adc_componentSpecific.c similarity index 82% rename from components/bms_boss/HW/HW_adc.c rename to components/bms_boss/HW/HW_adc_componentSpecific.c index e87ce0b9..727f4139 100644 --- a/components/bms_boss/HW/HW_adc.c +++ b/components/bms_boss/HW/HW_adc_componentSpecific.c @@ -22,7 +22,6 @@ #include "IO.h" #include "SystemConfig.h" - /****************************************************************************** * D E F I N E S ******************************************************************************/ @@ -30,9 +29,6 @@ #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES 2U #define ADC_CALIBRATION_TIMEOUT 10U -#define ADC_MAX_COUNT 4095 -#define ADC_REF_VOLTAGE 3.0F - #define ADC_N_CHANNEL ADC_CHANNEL_0 #define ADC_P_CHANNEL ADC_CHANNEL_1 @@ -44,13 +40,11 @@ ADC_HandleTypeDef hadc1; ADC_HandleTypeDef hadc2; DMA_HandleTypeDef hdma_adc1; - /****************************************************************************** * P R I V A T E F U N C T I O N P R O T O T Y P E S ******************************************************************************/ -void HW_ADC_unpackBuffer(bufferHalf_E half); - +void HW_ADC_unpackBuffer(HW_adc_bufferHalf_E half); /****************************************************************************** * P U B L I C F U N C T I O N S @@ -59,7 +53,7 @@ void HW_ADC_unpackBuffer(bufferHalf_E half); /** * @brief Init function for ADC firmware */ -void HW_ADC_init(void) +HW_StatusTypeDef_E HW_ADC_init(void) { ADC_MultiModeTypeDef multimode = { 0 }; ADC_ChannelConfTypeDef sConfig = { 0 }; @@ -130,6 +124,8 @@ void HW_ADC_init(void) } HAL_ADC_Start(&hadc2); + + return HW_OK; } /** @@ -191,34 +187,3 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) __HAL_RCC_ADC2_CLK_DISABLE(); } } - - -/****************************************************************************** - * P U B L I C F U N C T I O N S - ******************************************************************************/ - -/** - * @brief Firmware function to initiate ADC calibration. - * - * @param hadc Pointer to ADC peripheral - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc) -{ - return HAL_ADCEx_Calibration_Start(hadc) == HAL_OK; -} - -/** - * @brief Firmware function to start DMA transfer - * - * @param hadc Pointer to ADC peripheral - * @param data Pointer to memory start address - * @param size Size of buffer - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size) -{ - return HAL_ADCEx_MultiModeStart_DMA(hadc, data, size) == HAL_OK; -} diff --git a/components/bms_boss/HW/include/HW_adc_componentSpecific.h b/components/bms_boss/HW/include/HW_adc_componentSpecific.h new file mode 100644 index 00000000..9cc6a86c --- /dev/null +++ b/components/bms_boss/HW/include/HW_adc_componentSpecific.h @@ -0,0 +1,26 @@ +/** + * @file HW_adc_componentSpecific.h + * @brief Header file for ADC firmware + */ + +#pragma once + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +#include "HW.h" + +/****************************************************************************** + * D E F I N E S + ******************************************************************************/ + +#define ADC_REF_VOLTAGE 3.0f + +/****************************************************************************** + * E X T E R N S + ******************************************************************************/ + +extern ADC_HandleTypeDef hadc1; +extern ADC_HandleTypeDef hadc2; +extern DMA_HandleTypeDef hdma_adc1; diff --git a/components/bms_boss/SConscript b/components/bms_boss/SConscript index ec600d69..eab24b58 100644 --- a/components/bms_boss/SConscript +++ b/components/bms_boss/SConscript @@ -150,11 +150,13 @@ project_source_files = [ SRC_DIR.File("Utility.c"), SRC_DIR.File("UDS.c"), SHARED_LIBS.File("LIB_app.c"), + SHARED_LIBS.File("LIB_simpleFilter.c"), ] project_hw_files = [ SHARED_HW.File("HW.c"), - HW_DIR.File("HW_adc.c"), + SHARED_HW.File("HW_adc.c"), + HW_DIR.File("HW_adc_componentSpecific.c"), HW_DIR.File("HW_can_componentSpecific.c"), HW_DIR.File("HW_gpio_componentSpecific.c"), HW_DIR.File("HW_dma_componentSpecific.c"), diff --git a/components/bms_boss/src/IO.c b/components/bms_boss/src/IO.c index 9eb01460..772761eb 100644 --- a/components/bms_boss/src/IO.c +++ b/components/bms_boss/src/IO.c @@ -25,7 +25,7 @@ /**< Other Includes */ #include "ModuleDesc.h" #include "Utility.h" - +#include "LIB_simpleFilter.h" /****************************************************************************** * D E F I N E S @@ -56,9 +56,9 @@ _Static_assert((IO_ADC_BUF_LEN / 2) % ADC_CHANNEL_COUNT == 0, "ADC Buffer Length typedef struct { - adcState_E adcState; - uint32_t adcBuffer[IO_ADC_BUF_LEN]; - simpleFilter_S adcData[ADC_CHANNEL_COUNT]; + adcState_E adcState; + uint32_t adcBuffer[IO_ADC_BUF_LEN]; + LIB_simpleFilter_S adcData[ADC_CHANNEL_COUNT]; } io_S; @@ -150,27 +150,23 @@ void IO_unpackADCBuffer(void) { for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) { - io.adcData[i].raw = 0; - io.adcData[i].count = 0; + LIB_simpleFilter_clear(&io.adcData[i]); } for (uint16_t i = 0; i < IO_ADC_BUF_LEN; i++) { if (i % 2 == 0) { - io.adcData[ADC_CURRENT_N].raw += io.adcBuffer[i] & 0xffff; - io.adcData[ADC_CURRENT_P].raw += (io.adcBuffer[i]) >> 16; - io.adcData[ADC_CURRENT_N].count++; - io.adcData[ADC_CURRENT_P].count++; + LIB_simpleFilter_increment(&io.adcData[ADC_CURRENT_N], (float32_t)(io.adcBuffer[i] & 0xffff)); + LIB_simpleFilter_increment(&io.adcData[ADC_CURRENT_P], (float32_t)(io.adcBuffer[i] >> 16U)); } else { - io.adcData[ADC_MCU_TEMP].raw += io.adcBuffer[i] & 0xffff; - io.adcData[ADC_MCU_TEMP].count++; + LIB_simpleFilter_increment(&io.adcData[ADC_MCU_TEMP], (float32_t)(io.adcBuffer[i] & 0xffff)); } } - io.adcData[ADC_CURRENT_N].value = (float32_t)io.adcData[ADC_CURRENT_N].raw / io.adcData[ADC_CURRENT_N].count; - io.adcData[ADC_CURRENT_P].value = (float32_t)io.adcData[ADC_CURRENT_P].raw / io.adcData[ADC_CURRENT_P].count; - io.adcData[ADC_MCU_TEMP].value = (float32_t)io.adcData[ADC_MCU_TEMP].raw / io.adcData[ADC_MCU_TEMP].count; + LIB_simpleFilter_average(&io.adcData[ADC_CURRENT_N]); + LIB_simpleFilter_average(&io.adcData[ADC_CURRENT_P]); + LIB_simpleFilter_average(&io.adcData[ADC_MCU_TEMP]); } diff --git a/components/bms_worker/HW/HW_adc.c b/components/bms_worker/HW/HW_adc_componentSpecific.c similarity index 83% rename from components/bms_worker/HW/HW_adc.c rename to components/bms_worker/HW/HW_adc_componentSpecific.c index 3b9b6360..3a0ddc1c 100644 --- a/components/bms_worker/HW/HW_adc.c +++ b/components/bms_worker/HW/HW_adc_componentSpecific.c @@ -1,5 +1,5 @@ /** - * @file HW_adc.c + * @file HW_adc_componentSpecific.c * @brief Source code for ADC firmware */ @@ -43,13 +43,11 @@ ADC_HandleTypeDef hadc1; ADC_HandleTypeDef hadc2; DMA_HandleTypeDef hdma_adc1; - /****************************************************************************** * P R I V A T E F U N C T I O N P R O T O T Y P E S ******************************************************************************/ -void HW_ADC_unpackBuffer(bufferHalf_E half); - +void HW_ADC_unpackBuffer(HW_adc_bufferHalf_E half); /****************************************************************************** * P U B L I C F U N C T I O N S @@ -249,45 +247,3 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) if (hadc->Instance == ADC1) {} } - -/****************************************************************************** - * P U B L I C F U N C T I O N S - ******************************************************************************/ - -/** - * @brief Firmware function to initiate ADC calibration. - * - * @param hadc Pointer to ADC peripheral - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc) -{ - return HAL_ADCEx_Calibration_Start(hadc) == HAL_OK; -} - -/** - * @brief Firmware function to start DMA transfer - * - * @param hadc Pointer to ADC peripheral - * @param data Pointer to memory start address - * @param size Size of buffer - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size) -{ - return HAL_ADCEx_MultiModeStart_DMA(hadc, data, size) == HAL_OK; -} - -/** - * @brief Get analog input voltage in 0.1mV from ADC count - * - * @param cnt ADC count - * - * @retval unit: 0.01mV - */ -float32_t HW_ADC_getVFromCount(uint16_t cnt) -{ - return ((float32_t)cnt) * ADC_REF_VOLTAGE / ADC_MAX_COUNT; -} diff --git a/components/bms_worker/HW/include/HW_adc.h b/components/bms_worker/HW/include/HW_adc.h deleted file mode 100644 index 3776d73d..00000000 --- a/components/bms_worker/HW/include/HW_adc.h +++ /dev/null @@ -1,60 +0,0 @@ -/** - * @file HW_adc.h - * @brief Header file for ADC firmware - */ - -#pragma once - -/****************************************************************************** - * I N C L U D E S - ******************************************************************************/ - -#include "HW.h" - -#include "SystemConfig.h" -#include "stdbool.h" -#include "FloatTypes.h" - - -/****************************************************************************** - * T Y P E D E F S - ******************************************************************************/ - -typedef enum -{ - BUFFER_HALF_LOWER = 0U, - BUFFER_HALF_UPPER, -} bufferHalf_E; - -typedef struct -{ - uint32_t raw; - float32_t value; - uint16_t count; -} simpleFilter_S; - -/****************************************************************************** - * D E F I N E S - ******************************************************************************/ - -#define ADC_MAX_COUNT 4095 -#define ADC_REF_VOLTAGE 3.0f - -/****************************************************************************** - * E X T E R N S - ******************************************************************************/ - -extern ADC_HandleTypeDef hadc1; -extern ADC_HandleTypeDef hadc2; -extern DMA_HandleTypeDef hdma_adc1; - - -/****************************************************************************** - * P U B L I C F U N C T I O N P R O T O T Y P E S - ******************************************************************************/ - -HW_StatusTypeDef_E HW_ADC_init(void); -HW_StatusTypeDef_E HW_ADC_deInit(void); -bool HW_ADC_calibrate(ADC_HandleTypeDef *hadc); -bool HW_ADC_startDMA(ADC_HandleTypeDef*, uint32_t*, uint32_t); -float32_t HW_ADC_getVFromCount(uint16_t cnt); diff --git a/components/bms_worker/HW/include/HW_adc_componentSpecific.h b/components/bms_worker/HW/include/HW_adc_componentSpecific.h new file mode 100644 index 00000000..9cc6a86c --- /dev/null +++ b/components/bms_worker/HW/include/HW_adc_componentSpecific.h @@ -0,0 +1,26 @@ +/** + * @file HW_adc_componentSpecific.h + * @brief Header file for ADC firmware + */ + +#pragma once + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +#include "HW.h" + +/****************************************************************************** + * D E F I N E S + ******************************************************************************/ + +#define ADC_REF_VOLTAGE 3.0f + +/****************************************************************************** + * E X T E R N S + ******************************************************************************/ + +extern ADC_HandleTypeDef hadc1; +extern ADC_HandleTypeDef hadc2; +extern DMA_HandleTypeDef hdma_adc1; diff --git a/components/bms_worker/SConscript b/components/bms_worker/SConscript index f7cb2d19..becca07c 100644 --- a/components/bms_worker/SConscript +++ b/components/bms_worker/SConscript @@ -161,11 +161,13 @@ src_files = [ THERMS_DIR.File("MF52C1103F3380.c"), SHARED_LIBS.File("LIB_app.c"), SRC_DIR.File("CANIO_componentSpecific.c"), + SHARED_LIBS.File("LIB_simpleFilter.c"), ] hw_files = [ SHARED_HW.File("HW.c"), - HW_DIR.File("HW_adc.c"), + SHARED_HW.File("HW_adc.c"), + HW_DIR.File("HW_adc_componentSpecific.c"), HW_DIR.File("HW_can_componentSpecific.c"), HW_DIR.File("HW_dma_componentSpecific.c"), HW_DIR.File("HW_gpio_componentSpecific.c"), diff --git a/components/bms_worker/src/IO.c b/components/bms_worker/src/IO.c index 6061e17d..e6648af9 100644 --- a/components/bms_worker/src/IO.c +++ b/components/bms_worker/src/IO.c @@ -27,7 +27,7 @@ #include "ModuleDesc.h" #include "Utility.h" #include "FeatureDefines_generated.h" - +#include "LIB_simpleFilter.h" /****************************************************************************** * D E F I N E S @@ -35,7 +35,6 @@ #define ADC_VOLTAGE_DIVISION 2U /**< Voltage division for cell voltage output */ - /****************************************************************************** * T Y P E D E F S ******************************************************************************/ @@ -64,10 +63,10 @@ _Static_assert((IO_ADC_BUF_LEN / 2) % ADC_CHANNEL_COUNT == 0, "ADC Buffer Length typedef struct { - adcState_E adcState; - uint32_t adcBuffer[IO_ADC_BUF_LEN]; - simpleFilter_S adcData[ADC_CHANNEL_COUNT]; - simpleFilter_S bmsData; + adcState_E adcState; + uint32_t adcBuffer[IO_ADC_BUF_LEN]; + LIB_simpleFilter_S adcData[ADC_CHANNEL_COUNT]; + LIB_simpleFilter_S bmsData; } io_S; @@ -239,14 +238,12 @@ void IO_Temps_unpackADCBuffer(void) { for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) { - io.adcData[i].raw = 0; - io.adcData[i].count = 0; + LIB_simpleFilter_clear(&io.adcData[i]); } for (uint16_t i = 0; i < IO_ADC_BUF_LEN; i++) { - io.adcData[i % ADC_CHANNEL_COUNT].raw += io.adcBuffer[i] & 0xffff; - io.adcData[i % ADC_CHANNEL_COUNT].count++; + LIB_simpleFilter_increment(&io.adcData[i % ADC_CHANNEL_COUNT], (float32_t)(io.adcBuffer[i] & 0xffff)); } } @@ -255,12 +252,10 @@ void IO_Temps_unpackADCBuffer(void) */ void IO_Cells_unpackADCBuffer(void) { - io.bmsData.raw = 0; - io.bmsData.count = 0; + LIB_simpleFilter_clear(&io.bmsData); for (uint16_t i = 0; i < IO_ADC_BUF_LEN; i++) { - io.bmsData.raw += io.adcBuffer[i] >> 16; - io.bmsData.count++; + LIB_simpleFilter_increment(&io.bmsData, (float32_t)(io.adcBuffer[i] >> 16U)); } } diff --git a/components/shared/code/HW/HW_adc.c b/components/shared/code/HW/HW_adc.c new file mode 100644 index 00000000..cc4e843a --- /dev/null +++ b/components/shared/code/HW/HW_adc.c @@ -0,0 +1,56 @@ +/** + * @file HW_adc.c + * @brief Source code for ADC firmware + */ + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +// System Inlcudes +#include "string.h" + +// Firmware Includes +#include "HW_adc.h" + +/****************************************************************************** + * P U B L I C F U N C T I O N S + ******************************************************************************/ + +/** + * @brief Firmware function to initiate ADC calibration. + * + * @param hadc Pointer to ADC peripheral + * + * @retval true = Success, false = Failure + */ +HW_StatusTypeDef_E HW_ADC_calibrate(ADC_HandleTypeDef* hadc) +{ + return HAL_ADCEx_Calibration_Start(hadc) == HAL_OK ? HW_OK : HW_ERROR; +} + +/** + * @brief Firmware function to start DMA transfer + * + * @param hadc Pointer to ADC peripheral + * @param data Pointer to memory start address + * @param size Size of buffer + * + * @retval true = Success, false = Failure + */ +HW_StatusTypeDef_E HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size) +{ + return HAL_ADCEx_MultiModeStart_DMA(hadc, data, size) == HAL_OK ? HW_OK : HW_ERROR; +} + +/** + * @brief Get analog input voltage in 0.1mV from ADC count + * + * @param cnt ADC count + * + * @retval unit: 0.01mV + */ +float32_t HW_ADC_getVFromCount(uint16_t cnt) +{ + return ((float32_t)cnt) * (ADC_REF_VOLTAGE / (float32_t)ADC_MAX_COUNT); +} diff --git a/components/bms_boss/HW/include/HW_adc.h b/components/shared/code/HW/HW_adc.h similarity index 61% rename from components/bms_boss/HW/include/HW_adc.h rename to components/shared/code/HW/HW_adc.h index a175418d..963570af 100644 --- a/components/bms_boss/HW/include/HW_adc.h +++ b/components/shared/code/HW/HW_adc.h @@ -9,10 +9,15 @@ * I N C L U D E S ******************************************************************************/ -// System Includes -#include "SystemConfig.h" -#include "stdbool.h" +#include "HW.h" +#include "LIB_Types.h" +#include "HW_adc_componentSpecific.h" +/****************************************************************************** + * D E F I N E S + ******************************************************************************/ + +#define ADC_MAX_COUNT 4095U // Max integer value of ADC reading (2^12 for this chip) /****************************************************************************** * T Y P E D E F S @@ -22,30 +27,14 @@ typedef enum { BUFFER_HALF_LOWER = 0U, BUFFER_HALF_UPPER, -} bufferHalf_E; - -typedef struct -{ - uint32_t raw; - float32_t value; - uint16_t count; -} simpleFilter_S; - - -/****************************************************************************** - * E X T E R N S - ******************************************************************************/ - -extern ADC_HandleTypeDef hadc1; -extern ADC_HandleTypeDef hadc2; -extern DMA_HandleTypeDef hdma_adc1; - +} HW_adc_bufferHalf_E; /****************************************************************************** * P U B L I C F U N C T I O N P R O T O T Y P E S ******************************************************************************/ -void HW_ADC_init(void); -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc); -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size); -uint16_t HW_ADC_getVFromCount(uint16_t cnt); +HW_StatusTypeDef_E HW_ADC_init(void); +HW_StatusTypeDef_E HW_ADC_deInit(void); +HW_StatusTypeDef_E HW_ADC_calibrate(ADC_HandleTypeDef* hadc); +HW_StatusTypeDef_E HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size); +float32_t HW_ADC_getVFromCount(uint16_t cnt); diff --git a/components/shared/code/libs/LIB_simpleFilter.c b/components/shared/code/libs/LIB_simpleFilter.c new file mode 100644 index 00000000..c0388ca5 --- /dev/null +++ b/components/shared/code/libs/LIB_simpleFilter.c @@ -0,0 +1,45 @@ +/** + * @file LIB_simpleFilter.c + * @brief Source file for simple filters + */ + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +#include "LIB_simpleFilter.h" + +/****************************************************************************** + * P U B L I C F U N C T I O N S + ******************************************************************************/ + +/** + * @brief Clear a simple averaging filter + * @param filter Filter to clear + */ +void LIB_simpleFilter_clear(LIB_simpleFilter_S* filter) +{ + filter->raw = 0U; + filter->count = 0U; +} + +/** + * @brief Add value to simple filter + * @param filter Filter to increment + * @param value Value to increment by + */ +void LIB_simpleFilter_increment(LIB_simpleFilter_S* filter, float32_t value) +{ + filter->raw += value; + filter->count++; +} + +/** + * @brief Calculate value of simple filter + * @param filter Filter to average + */ +float32_t LIB_simpleFilter_average(LIB_simpleFilter_S* filter) +{ + filter->value = (float32_t)filter->raw / (float32_t)filter->count; + return filter->value; +} diff --git a/components/shared/code/libs/LIB_simpleFilter.h b/components/shared/code/libs/LIB_simpleFilter.h new file mode 100644 index 00000000..02fe3d8f --- /dev/null +++ b/components/shared/code/libs/LIB_simpleFilter.h @@ -0,0 +1,31 @@ +/** + * @file LIB_simpleFilter.h + * @brief Header file for simple filters + */ + +#pragma once + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +#include "LIB_Types.h" + +/****************************************************************************** + * T Y P E D E F S + ******************************************************************************/ + +typedef struct +{ + float32_t raw; + float32_t value; + uint16_t count; +} LIB_simpleFilter_S; + +/****************************************************************************** + * P U B L I C F U N C T I O N P R O T O T Y P E S + ******************************************************************************/ + +void LIB_simpleFilter_clear(LIB_simpleFilter_S* filter); +void LIB_simpleFilter_increment(LIB_simpleFilter_S* filter, float32_t value); +float32_t LIB_simpleFilter_average(LIB_simpleFilter_S* filter); diff --git a/components/vc/front/SConscript b/components/vc/front/SConscript index c302f66c..e00e50b2 100644 --- a/components/vc/front/SConscript +++ b/components/vc/front/SConscript @@ -146,11 +146,13 @@ project_source_files = [ SRC_DIR.File("Utility.c"), SRC_DIR.File("UDS.c"), SHARED_LIBS.File("LIB_app.c"), + SHARED_LIBS.File("LIB_simpleFilter.c"), ] project_hw_files = [ SHARED_HW.File("HW.c"), - HW_DIR.File("HW_adc.c"), + SHARED_HW.File("HW_adc.c"), + HW_DIR.File("HW_adc_componentSpecific.c"), HW_DIR.File("HW_can_componentSpecific.c"), HW_DIR.File("HW_gpio_componentSpecific.c"), HW_DIR.File("HW_dma_componentSpecific.c"), diff --git a/components/vc/front/include/HW/HW_adc.h b/components/vc/front/include/HW/HW_adc.h deleted file mode 100644 index ccb41fd7..00000000 --- a/components/vc/front/include/HW/HW_adc.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * @file HW_adc.h - * @brief Header file for ADC firmware - */ - -#pragma once - -/****************************************************************************** - * I N C L U D E S - ******************************************************************************/ - -// System Includes -#include "SystemConfig.h" -#include "stdbool.h" -#include "LIB_Types.h" - -/****************************************************************************** - * D E F I N E S - ******************************************************************************/ - -#define ADC_MAX_VAL 4095U // Max integer value of ADC reading (2^12 for this chip) -#define ADC_REF_VOLTAGE 3.0F - -/****************************************************************************** - * T Y P E D E F S - ******************************************************************************/ - -typedef enum -{ - BUFFER_HALF_LOWER = 0U, - BUFFER_HALF_UPPER, -} bufferHalf_E; - -typedef struct -{ - uint32_t raw; - float32_t value; - uint16_t count; -} simpleFilter_S; - - -/****************************************************************************** - * E X T E R N S - ******************************************************************************/ - -extern ADC_HandleTypeDef hadc1; -extern ADC_HandleTypeDef hadc2; -extern DMA_HandleTypeDef hdma_adc1; - - -/****************************************************************************** - * P U B L I C F U N C T I O N P R O T O T Y P E S - ******************************************************************************/ - -void HW_ADC_init(void); -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc); -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size); -uint16_t HW_ADC_getVFromCount(uint16_t cnt); diff --git a/components/vc/front/include/HW/HW_adc_componentSpecific.h b/components/vc/front/include/HW/HW_adc_componentSpecific.h new file mode 100644 index 00000000..da514867 --- /dev/null +++ b/components/vc/front/include/HW/HW_adc_componentSpecific.h @@ -0,0 +1,26 @@ +/** + * @file HW_adc_componentSpecific.h + * @brief Header file for ADC firmware + */ + +#pragma once + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +#include "HW.h" + +/****************************************************************************** + * D E F I N E S + ******************************************************************************/ + +#define ADC_REF_VOLTAGE 2.5F + +/****************************************************************************** + * E X T E R N S + ******************************************************************************/ + +extern ADC_HandleTypeDef hadc1; +extern ADC_HandleTypeDef hadc2; +extern DMA_HandleTypeDef hdma_adc1; diff --git a/components/vc/front/include/IO.h b/components/vc/front/include/IO.h index c2dae852..8f7675fa 100644 --- a/components/vc/front/include/IO.h +++ b/components/vc/front/include/IO.h @@ -20,8 +20,6 @@ * D E F I N E S ******************************************************************************/ -#define VREF ADC_REF_VOLTAGE /**< Shunt Diode reference voltage */ - #define IO_ADC_BUF_LEN 192U /**< To fit the number of measurements into a time less than 100us \ the buffer length must be less than 100us / (ADC clock freq * \ cycles per conversion). For this firmware, there is 14 cycles \ diff --git a/components/vc/pdu/src/HW/HW_adc.c b/components/vc/front/src/HW/HW_adc_componentSpecific.c similarity index 80% rename from components/vc/pdu/src/HW/HW_adc.c rename to components/vc/front/src/HW/HW_adc_componentSpecific.c index 73c7785a..4e0f0946 100644 --- a/components/vc/pdu/src/HW/HW_adc.c +++ b/components/vc/front/src/HW/HW_adc_componentSpecific.c @@ -1,5 +1,5 @@ /** - * @file HW_adc.c + * @file HW_adc_componentSpecific.c * @brief Source code for ADC firmware */ @@ -21,7 +21,6 @@ #include "IO.h" #include "SystemConfig.h" - /****************************************************************************** * D E F I N E S ******************************************************************************/ @@ -29,8 +28,6 @@ #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES 2U #define ADC_CALIBRATION_TIMEOUT 10U -#define ADC_MAX_COUNT 4095 - /****************************************************************************** * P U B L I C V A R S ******************************************************************************/ @@ -39,13 +36,11 @@ ADC_HandleTypeDef hadc1; ADC_HandleTypeDef hadc2; DMA_HandleTypeDef hdma_adc1; - /****************************************************************************** * P R I V A T E F U N C T I O N P R O T O T Y P E S ******************************************************************************/ -void HW_ADC_unpackBuffer(bufferHalf_E half); - +void HW_ADC_unpackBuffer(HW_adc_bufferHalf_E half); /****************************************************************************** * P U B L I C F U N C T I O N S @@ -54,7 +49,7 @@ void HW_ADC_unpackBuffer(bufferHalf_E half); /** * @brief Init function for ADC firmware */ -void HW_ADC_init(void) +HW_StatusTypeDef_E HW_ADC_init(void) { ADC_MultiModeTypeDef multimode = { 0 }; ADC_ChannelConfTypeDef sConfig = { 0 }; @@ -109,6 +104,8 @@ void HW_ADC_init(void) } HAL_ADC_Start(&hadc2); + + return HW_OK; } /** @@ -170,34 +167,3 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) __HAL_RCC_ADC2_CLK_DISABLE(); } } - - -/****************************************************************************** - * P U B L I C F U N C T I O N S - ******************************************************************************/ - -/** - * @brief Firmware function to initiate ADC calibration. - * - * @param hadc Pointer to ADC peripheral - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc) -{ - return HAL_ADCEx_Calibration_Start(hadc) == HAL_OK; -} - -/** - * @brief Firmware function to start DMA transfer - * - * @param hadc Pointer to ADC peripheral - * @param data Pointer to memory start address - * @param size Size of buffer - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size) -{ - return HAL_ADCEx_MultiModeStart_DMA(hadc, data, size) == HAL_OK; -} diff --git a/components/vc/front/src/IO.c b/components/vc/front/src/IO.c index c875dc70..ae8fd50d 100644 --- a/components/vc/front/src/IO.c +++ b/components/vc/front/src/IO.c @@ -23,7 +23,7 @@ /**< Other Includes */ #include "ModuleDesc.h" #include "Utility.h" - +#include "LIB_simpleFilter.h" /****************************************************************************** * D E F I N E S @@ -53,9 +53,9 @@ _Static_assert((IO_ADC_BUF_LEN / 2) % ADC_CHANNEL_COUNT == 0, "ADC Buffer Length typedef struct { - adcState_E adcState; - uint32_t adcBuffer[IO_ADC_BUF_LEN]; - simpleFilter_S adcData[ADC_CHANNEL_COUNT]; + adcState_E adcState; + uint32_t adcBuffer[IO_ADC_BUF_LEN]; + LIB_simpleFilter_S adcData[ADC_CHANNEL_COUNT]; } io_S; @@ -118,7 +118,7 @@ static void IO10Hz_PRD(void) else if (io.adcState == ADC_STATE_RUNNING) { IO_unpackADCBuffer(); - IO.mcu_temp = (io.adcData[ADC_MCU_TEMP].value / ADC_MAX_VAL) * VREF; + IO.mcu_temp = HW_ADC_getVFromCount((uint16_t)io.adcData[ADC_MCU_TEMP].value); } } @@ -141,21 +141,16 @@ void IO_unpackADCBuffer(void) { for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) { - io.adcData[i].raw = 0; - io.adcData[i].count = 0; + LIB_simpleFilter_clear(&io.adcData[i]); } for (uint16_t i = 0; i < IO_ADC_BUF_LEN; i++) { - if (i % 2 == 0) - { - } - else - { - io.adcData[ADC_MCU_TEMP].raw += io.adcBuffer[i] & 0xffff; - io.adcData[ADC_MCU_TEMP].count++; - } + LIB_simpleFilter_increment(&io.adcData[i % ADC_CHANNEL_COUNT], (float32_t)(io.adcBuffer[i] & 0xffff)); } - io.adcData[ADC_MCU_TEMP].value = (float32_t)io.adcData[ADC_MCU_TEMP].raw / io.adcData[ADC_MCU_TEMP].count; + for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) + { + LIB_simpleFilter_average(&io.adcData[i]); + } } diff --git a/components/vc/pdu/SConscript b/components/vc/pdu/SConscript index 9d2cbfb6..731d8543 100644 --- a/components/vc/pdu/SConscript +++ b/components/vc/pdu/SConscript @@ -146,11 +146,13 @@ project_source_files = [ SRC_DIR.File("Utility.c"), SRC_DIR.File("UDS.c"), SHARED_LIBS.File("LIB_app.c"), + SHARED_LIBS.File("LIB_simpleFilter.c"), ] project_hw_files = [ SHARED_HW.File("HW.c"), - HW_DIR.File("HW_adc.c"), + SHARED_HW.File("HW_adc.c"), + HW_DIR.File("HW_adc_componentSpecific.c"), HW_DIR.File("HW_can_componentSpecific.c"), HW_DIR.File("HW_gpio_componentSpecific.c"), HW_DIR.File("HW_dma_componentSpecific.c"), diff --git a/components/vc/pdu/include/HW/HW_adc.h b/components/vc/pdu/include/HW/HW_adc.h deleted file mode 100644 index 596dde6b..00000000 --- a/components/vc/pdu/include/HW/HW_adc.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * @file HW_adc.h - * @brief Header file for ADC firmware - */ - -#pragma once - -/****************************************************************************** - * I N C L U D E S - ******************************************************************************/ - -// System Includes -#include "SystemConfig.h" -#include "stdbool.h" -#include "LIB_Types.h" - -/****************************************************************************** - * D E F I N E S - ******************************************************************************/ - -#define ADC_MAX_VAL 4095U // Max integer value of ADC reading (2^12 for this chip) -#define ADC_REF_VOLTAGE 2.5F - -/****************************************************************************** - * T Y P E D E F S - ******************************************************************************/ - -typedef enum -{ - BUFFER_HALF_LOWER = 0U, - BUFFER_HALF_UPPER, -} bufferHalf_E; - -typedef struct -{ - uint32_t raw; - float32_t value; - uint16_t count; -} simpleFilter_S; - - -/****************************************************************************** - * E X T E R N S - ******************************************************************************/ - -extern ADC_HandleTypeDef hadc1; -extern ADC_HandleTypeDef hadc2; -extern DMA_HandleTypeDef hdma_adc1; - - -/****************************************************************************** - * P U B L I C F U N C T I O N P R O T O T Y P E S - ******************************************************************************/ - -void HW_ADC_init(void); -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc); -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size); -uint16_t HW_ADC_getVFromCount(uint16_t cnt); diff --git a/components/vc/pdu/include/HW/HW_adc_componentSpecific.h b/components/vc/pdu/include/HW/HW_adc_componentSpecific.h new file mode 100644 index 00000000..da514867 --- /dev/null +++ b/components/vc/pdu/include/HW/HW_adc_componentSpecific.h @@ -0,0 +1,26 @@ +/** + * @file HW_adc_componentSpecific.h + * @brief Header file for ADC firmware + */ + +#pragma once + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +#include "HW.h" + +/****************************************************************************** + * D E F I N E S + ******************************************************************************/ + +#define ADC_REF_VOLTAGE 2.5F + +/****************************************************************************** + * E X T E R N S + ******************************************************************************/ + +extern ADC_HandleTypeDef hadc1; +extern ADC_HandleTypeDef hadc2; +extern DMA_HandleTypeDef hdma_adc1; diff --git a/components/vc/pdu/include/IO.h b/components/vc/pdu/include/IO.h index c2dae852..8f7675fa 100644 --- a/components/vc/pdu/include/IO.h +++ b/components/vc/pdu/include/IO.h @@ -20,8 +20,6 @@ * D E F I N E S ******************************************************************************/ -#define VREF ADC_REF_VOLTAGE /**< Shunt Diode reference voltage */ - #define IO_ADC_BUF_LEN 192U /**< To fit the number of measurements into a time less than 100us \ the buffer length must be less than 100us / (ADC clock freq * \ cycles per conversion). For this firmware, there is 14 cycles \ diff --git a/components/vc/front/src/HW/HW_adc.c b/components/vc/pdu/src/HW/HW_adc_componentSpecific.c similarity index 80% rename from components/vc/front/src/HW/HW_adc.c rename to components/vc/pdu/src/HW/HW_adc_componentSpecific.c index 73c7785a..7d62f518 100644 --- a/components/vc/front/src/HW/HW_adc.c +++ b/components/vc/pdu/src/HW/HW_adc_componentSpecific.c @@ -1,5 +1,5 @@ /** - * @file HW_adc.c + * @file HW_adc_componentSpecific.c * @brief Source code for ADC firmware */ @@ -29,8 +29,6 @@ #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES 2U #define ADC_CALIBRATION_TIMEOUT 10U -#define ADC_MAX_COUNT 4095 - /****************************************************************************** * P U B L I C V A R S ******************************************************************************/ @@ -39,13 +37,11 @@ ADC_HandleTypeDef hadc1; ADC_HandleTypeDef hadc2; DMA_HandleTypeDef hdma_adc1; - /****************************************************************************** * P R I V A T E F U N C T I O N P R O T O T Y P E S ******************************************************************************/ -void HW_ADC_unpackBuffer(bufferHalf_E half); - +void HW_ADC_unpackBuffer(HW_adc_bufferHalf_E half); /****************************************************************************** * P U B L I C F U N C T I O N S @@ -54,7 +50,7 @@ void HW_ADC_unpackBuffer(bufferHalf_E half); /** * @brief Init function for ADC firmware */ -void HW_ADC_init(void) +HW_StatusTypeDef_E HW_ADC_init(void) { ADC_MultiModeTypeDef multimode = { 0 }; ADC_ChannelConfTypeDef sConfig = { 0 }; @@ -109,6 +105,8 @@ void HW_ADC_init(void) } HAL_ADC_Start(&hadc2); + + return HW_OK; } /** @@ -170,34 +168,3 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) __HAL_RCC_ADC2_CLK_DISABLE(); } } - - -/****************************************************************************** - * P U B L I C F U N C T I O N S - ******************************************************************************/ - -/** - * @brief Firmware function to initiate ADC calibration. - * - * @param hadc Pointer to ADC peripheral - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc) -{ - return HAL_ADCEx_Calibration_Start(hadc) == HAL_OK; -} - -/** - * @brief Firmware function to start DMA transfer - * - * @param hadc Pointer to ADC peripheral - * @param data Pointer to memory start address - * @param size Size of buffer - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size) -{ - return HAL_ADCEx_MultiModeStart_DMA(hadc, data, size) == HAL_OK; -} diff --git a/components/vc/pdu/src/IO.c b/components/vc/pdu/src/IO.c index c875dc70..ae8fd50d 100644 --- a/components/vc/pdu/src/IO.c +++ b/components/vc/pdu/src/IO.c @@ -23,7 +23,7 @@ /**< Other Includes */ #include "ModuleDesc.h" #include "Utility.h" - +#include "LIB_simpleFilter.h" /****************************************************************************** * D E F I N E S @@ -53,9 +53,9 @@ _Static_assert((IO_ADC_BUF_LEN / 2) % ADC_CHANNEL_COUNT == 0, "ADC Buffer Length typedef struct { - adcState_E adcState; - uint32_t adcBuffer[IO_ADC_BUF_LEN]; - simpleFilter_S adcData[ADC_CHANNEL_COUNT]; + adcState_E adcState; + uint32_t adcBuffer[IO_ADC_BUF_LEN]; + LIB_simpleFilter_S adcData[ADC_CHANNEL_COUNT]; } io_S; @@ -118,7 +118,7 @@ static void IO10Hz_PRD(void) else if (io.adcState == ADC_STATE_RUNNING) { IO_unpackADCBuffer(); - IO.mcu_temp = (io.adcData[ADC_MCU_TEMP].value / ADC_MAX_VAL) * VREF; + IO.mcu_temp = HW_ADC_getVFromCount((uint16_t)io.adcData[ADC_MCU_TEMP].value); } } @@ -141,21 +141,16 @@ void IO_unpackADCBuffer(void) { for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) { - io.adcData[i].raw = 0; - io.adcData[i].count = 0; + LIB_simpleFilter_clear(&io.adcData[i]); } for (uint16_t i = 0; i < IO_ADC_BUF_LEN; i++) { - if (i % 2 == 0) - { - } - else - { - io.adcData[ADC_MCU_TEMP].raw += io.adcBuffer[i] & 0xffff; - io.adcData[ADC_MCU_TEMP].count++; - } + LIB_simpleFilter_increment(&io.adcData[i % ADC_CHANNEL_COUNT], (float32_t)(io.adcBuffer[i] & 0xffff)); } - io.adcData[ADC_MCU_TEMP].value = (float32_t)io.adcData[ADC_MCU_TEMP].raw / io.adcData[ADC_MCU_TEMP].count; + for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) + { + LIB_simpleFilter_average(&io.adcData[i]); + } } diff --git a/components/vc/rear/SConscript b/components/vc/rear/SConscript index 367807a5..7ed9cddb 100644 --- a/components/vc/rear/SConscript +++ b/components/vc/rear/SConscript @@ -146,11 +146,13 @@ project_source_files = [ SRC_DIR.File("Utility.c"), SRC_DIR.File("UDS.c"), SHARED_LIBS.File("LIB_app.c"), + SHARED_LIBS.File("LIB_simpleFilter.c"), ] project_hw_files = [ SHARED_HW.File("HW.c"), - HW_DIR.File("HW_adc.c"), + SHARED_HW.File("HW_adc.c"), + HW_DIR.File("HW_adc_componentSpecific.c"), HW_DIR.File("HW_can_componentSpecific.c"), HW_DIR.File("HW_gpio_componentSpecific.c"), HW_DIR.File("HW_dma_componentSpecific.c"), diff --git a/components/vc/rear/include/HW/HW_adc.h b/components/vc/rear/include/HW/HW_adc.h deleted file mode 100644 index ccb41fd7..00000000 --- a/components/vc/rear/include/HW/HW_adc.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * @file HW_adc.h - * @brief Header file for ADC firmware - */ - -#pragma once - -/****************************************************************************** - * I N C L U D E S - ******************************************************************************/ - -// System Includes -#include "SystemConfig.h" -#include "stdbool.h" -#include "LIB_Types.h" - -/****************************************************************************** - * D E F I N E S - ******************************************************************************/ - -#define ADC_MAX_VAL 4095U // Max integer value of ADC reading (2^12 for this chip) -#define ADC_REF_VOLTAGE 3.0F - -/****************************************************************************** - * T Y P E D E F S - ******************************************************************************/ - -typedef enum -{ - BUFFER_HALF_LOWER = 0U, - BUFFER_HALF_UPPER, -} bufferHalf_E; - -typedef struct -{ - uint32_t raw; - float32_t value; - uint16_t count; -} simpleFilter_S; - - -/****************************************************************************** - * E X T E R N S - ******************************************************************************/ - -extern ADC_HandleTypeDef hadc1; -extern ADC_HandleTypeDef hadc2; -extern DMA_HandleTypeDef hdma_adc1; - - -/****************************************************************************** - * P U B L I C F U N C T I O N P R O T O T Y P E S - ******************************************************************************/ - -void HW_ADC_init(void); -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc); -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size); -uint16_t HW_ADC_getVFromCount(uint16_t cnt); diff --git a/components/vc/rear/include/HW/HW_adc_componentSpecific.h b/components/vc/rear/include/HW/HW_adc_componentSpecific.h new file mode 100644 index 00000000..40a85766 --- /dev/null +++ b/components/vc/rear/include/HW/HW_adc_componentSpecific.h @@ -0,0 +1,26 @@ +/** + * @file HW_adc_componentSpecific.h + * @brief Header file for ADC firmware + */ + +#pragma once + +/****************************************************************************** + * I N C L U D E S + ******************************************************************************/ + +#include "HW.h" + +/****************************************************************************** + * D E F I N E S + ******************************************************************************/ + +#define ADC_REF_VOLTAGE 3.0F + +/****************************************************************************** + * E X T E R N S + ******************************************************************************/ + +extern ADC_HandleTypeDef hadc1; +extern ADC_HandleTypeDef hadc2; +extern DMA_HandleTypeDef hdma_adc1; diff --git a/components/vc/rear/src/HW/HW_adc.c b/components/vc/rear/src/HW/HW_adc_componentSpecific.c similarity index 80% rename from components/vc/rear/src/HW/HW_adc.c rename to components/vc/rear/src/HW/HW_adc_componentSpecific.c index 73c7785a..7d62f518 100644 --- a/components/vc/rear/src/HW/HW_adc.c +++ b/components/vc/rear/src/HW/HW_adc_componentSpecific.c @@ -1,5 +1,5 @@ /** - * @file HW_adc.c + * @file HW_adc_componentSpecific.c * @brief Source code for ADC firmware */ @@ -29,8 +29,6 @@ #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES 2U #define ADC_CALIBRATION_TIMEOUT 10U -#define ADC_MAX_COUNT 4095 - /****************************************************************************** * P U B L I C V A R S ******************************************************************************/ @@ -39,13 +37,11 @@ ADC_HandleTypeDef hadc1; ADC_HandleTypeDef hadc2; DMA_HandleTypeDef hdma_adc1; - /****************************************************************************** * P R I V A T E F U N C T I O N P R O T O T Y P E S ******************************************************************************/ -void HW_ADC_unpackBuffer(bufferHalf_E half); - +void HW_ADC_unpackBuffer(HW_adc_bufferHalf_E half); /****************************************************************************** * P U B L I C F U N C T I O N S @@ -54,7 +50,7 @@ void HW_ADC_unpackBuffer(bufferHalf_E half); /** * @brief Init function for ADC firmware */ -void HW_ADC_init(void) +HW_StatusTypeDef_E HW_ADC_init(void) { ADC_MultiModeTypeDef multimode = { 0 }; ADC_ChannelConfTypeDef sConfig = { 0 }; @@ -109,6 +105,8 @@ void HW_ADC_init(void) } HAL_ADC_Start(&hadc2); + + return HW_OK; } /** @@ -170,34 +168,3 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) __HAL_RCC_ADC2_CLK_DISABLE(); } } - - -/****************************************************************************** - * P U B L I C F U N C T I O N S - ******************************************************************************/ - -/** - * @brief Firmware function to initiate ADC calibration. - * - * @param hadc Pointer to ADC peripheral - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_calibrate(ADC_HandleTypeDef* hadc) -{ - return HAL_ADCEx_Calibration_Start(hadc) == HAL_OK; -} - -/** - * @brief Firmware function to start DMA transfer - * - * @param hadc Pointer to ADC peripheral - * @param data Pointer to memory start address - * @param size Size of buffer - * - * @retval true = Success, false = Failure - */ -bool HW_ADC_startDMA(ADC_HandleTypeDef* hadc, uint32_t* data, uint32_t size) -{ - return HAL_ADCEx_MultiModeStart_DMA(hadc, data, size) == HAL_OK; -} diff --git a/components/vc/rear/src/IO.c b/components/vc/rear/src/IO.c index c875dc70..ae8fd50d 100644 --- a/components/vc/rear/src/IO.c +++ b/components/vc/rear/src/IO.c @@ -23,7 +23,7 @@ /**< Other Includes */ #include "ModuleDesc.h" #include "Utility.h" - +#include "LIB_simpleFilter.h" /****************************************************************************** * D E F I N E S @@ -53,9 +53,9 @@ _Static_assert((IO_ADC_BUF_LEN / 2) % ADC_CHANNEL_COUNT == 0, "ADC Buffer Length typedef struct { - adcState_E adcState; - uint32_t adcBuffer[IO_ADC_BUF_LEN]; - simpleFilter_S adcData[ADC_CHANNEL_COUNT]; + adcState_E adcState; + uint32_t adcBuffer[IO_ADC_BUF_LEN]; + LIB_simpleFilter_S adcData[ADC_CHANNEL_COUNT]; } io_S; @@ -118,7 +118,7 @@ static void IO10Hz_PRD(void) else if (io.adcState == ADC_STATE_RUNNING) { IO_unpackADCBuffer(); - IO.mcu_temp = (io.adcData[ADC_MCU_TEMP].value / ADC_MAX_VAL) * VREF; + IO.mcu_temp = HW_ADC_getVFromCount((uint16_t)io.adcData[ADC_MCU_TEMP].value); } } @@ -141,21 +141,16 @@ void IO_unpackADCBuffer(void) { for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) { - io.adcData[i].raw = 0; - io.adcData[i].count = 0; + LIB_simpleFilter_clear(&io.adcData[i]); } for (uint16_t i = 0; i < IO_ADC_BUF_LEN; i++) { - if (i % 2 == 0) - { - } - else - { - io.adcData[ADC_MCU_TEMP].raw += io.adcBuffer[i] & 0xffff; - io.adcData[ADC_MCU_TEMP].count++; - } + LIB_simpleFilter_increment(&io.adcData[i % ADC_CHANNEL_COUNT], (float32_t)(io.adcBuffer[i] & 0xffff)); } - io.adcData[ADC_MCU_TEMP].value = (float32_t)io.adcData[ADC_MCU_TEMP].raw / io.adcData[ADC_MCU_TEMP].count; + for (uint8_t i = 0; i < ADC_CHANNEL_COUNT; i++) + { + LIB_simpleFilter_average(&io.adcData[i]); + } }