Skip to content

Commit

Permalink
Abstracted HW_adc and created a simple filter library
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLafleur committed Mar 2, 2025
1 parent 5328b8f commit bce0d93
Show file tree
Hide file tree
Showing 30 changed files with 365 additions and 545 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
#include "IO.h"
#include "SystemConfig.h"


/******************************************************************************
* D E F I N E S
******************************************************************************/

#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

Expand All @@ -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
Expand All @@ -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 };
Expand Down Expand Up @@ -130,6 +124,8 @@ void HW_ADC_init(void)
}

HAL_ADC_Start(&hadc2);

return HW_OK;
}

/**
Expand Down Expand Up @@ -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;
}
26 changes: 26 additions & 0 deletions components/bms_boss/HW/include/HW_adc_componentSpecific.h
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion components/bms_boss/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
26 changes: 11 additions & 15 deletions components/bms_boss/src/IO.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**< Other Includes */
#include "ModuleDesc.h"
#include "Utility.h"

#include "LIB_simpleFilter.h"

/******************************************************************************
* D E F I N E S
Expand Down Expand Up @@ -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;


Expand Down Expand Up @@ -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]);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file HW_adc.c
* @file HW_adc_componentSpecific.c
* @brief Source code for ADC firmware
*/

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
60 changes: 0 additions & 60 deletions components/bms_worker/HW/include/HW_adc.h

This file was deleted.

26 changes: 26 additions & 0 deletions components/bms_worker/HW/include/HW_adc_componentSpecific.h
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion components/bms_worker/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading

0 comments on commit bce0d93

Please sign in to comment.