Skip to content

Commit

Permalink
Merge pull request #3268 from shellixyz/adc_averaging_stm32f3
Browse files Browse the repository at this point in the history
ADC samples averaging with added STM32F30x support
  • Loading branch information
digitalentity authored May 31, 2018
2 parents 83bb1b8 + 15955b5 commit 1b0d888
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
26 changes: 25 additions & 1 deletion src/main/drivers/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@

#ifdef USE_ADC

#if defined(USE_ADC_AVERAGING)
static uint8_t activeChannelCount[ADCDEV_COUNT] = {0};
#endif

static int adcFunctionMap[ADC_FUNCTION_COUNT];
adc_config_t adcConfig[ADC_CHN_COUNT]; // index 0 is dummy for ADC_CHN_NONE
volatile uint16_t adcValues[ADCDEV_COUNT][ADC_CHN_COUNT];
volatile uint16_t adcValues[ADCDEV_COUNT][ADC_CHN_COUNT * ADC_AVERAGE_N_SAMPLES];

uint8_t adcChannelByTag(ioTag_t ioTag)
{
Expand Down Expand Up @@ -82,7 +86,15 @@ uint16_t adcGetChannel(uint8_t function)
return 0;

if (adcConfig[channel].adcDevice != ADCINVALID && adcConfig[channel].enabled) {
#if !defined(USE_ADC_AVERAGING)
return adcValues[adcConfig[channel].adcDevice][adcConfig[channel].dmaIndex];
#else
uint32_t acc = 0;
for (int i = 0; i < ADC_AVERAGE_N_SAMPLES; i++) {
acc += adcValues[adcConfig[channel].adcDevice][adcConfig[channel].dmaIndex + i * activeChannelCount[adcConfig[channel].adcDevice]];
}
return acc / ADC_AVERAGE_N_SAMPLES;
#endif
} else {
return 0;
}
Expand Down Expand Up @@ -128,6 +140,9 @@ void adcInit(drv_adc_config_t *init)
adcConfig[ADC_CHN_1].adcDevice = adcDeviceByInstance(ADC_CHANNEL_1_INSTANCE);
if (adcConfig[ADC_CHN_1].adcDevice != ADCINVALID) {
adcConfig[ADC_CHN_1].tag = IO_TAG(ADC_CHANNEL_1_PIN);
#if defined(USE_ADC_AVERAGING)
activeChannelCount[adcConfig[ADC_CHN_1].adcDevice] += 1;
#endif
}
}
#else
Expand All @@ -139,6 +154,9 @@ void adcInit(drv_adc_config_t *init)
adcConfig[ADC_CHN_2].adcDevice = adcDeviceByInstance(ADC_CHANNEL_2_INSTANCE);
if (adcConfig[ADC_CHN_2].adcDevice != ADCINVALID) {
adcConfig[ADC_CHN_2].tag = IO_TAG(ADC_CHANNEL_2_PIN);
#if defined(USE_ADC_AVERAGING)
activeChannelCount[adcConfig[ADC_CHN_2].adcDevice] += 1;
#endif
}
}
#else
Expand All @@ -150,6 +168,9 @@ void adcInit(drv_adc_config_t *init)
adcConfig[ADC_CHN_3].adcDevice = adcDeviceByInstance(ADC_CHANNEL_3_INSTANCE);
if (adcConfig[ADC_CHN_3].adcDevice != ADCINVALID) {
adcConfig[ADC_CHN_3].tag = IO_TAG(ADC_CHANNEL_3_PIN);
#if defined(USE_ADC_AVERAGING)
activeChannelCount[adcConfig[ADC_CHN_3].adcDevice] += 1;
#endif
}
}
#else
Expand All @@ -161,6 +182,9 @@ void adcInit(drv_adc_config_t *init)
adcConfig[ADC_CHN_4].adcDevice = adcDeviceByInstance(ADC_CHANNEL_4_INSTANCE);
if (adcConfig[ADC_CHN_4].adcDevice != ADCINVALID) {
adcConfig[ADC_CHN_4].tag = IO_TAG(ADC_CHANNEL_4_PIN);
#if defined(USE_ADC_AVERAGING)
activeChannelCount[adcConfig[ADC_CHN_4].adcDevice] += 1;
#endif
}
}
#else
Expand Down
4 changes: 4 additions & 0 deletions src/main/drivers/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ void adcInit(drv_adc_config_t *init);
uint16_t adcGetChannel(uint8_t channel);
bool adcIsFunctionAssigned(uint8_t function);
int adcGetFunctionChannelAllocation(uint8_t function);

#if !defined(USE_ADC_AVERAGING)
#define ADC_AVERAGE_N_SAMPLES 1
#endif
2 changes: 1 addition & 1 deletion src/main/drivers/adc_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ typedef struct adc_config_s {

extern const adcTagMap_t adcTagMap[ADC_TAG_MAP_COUNT];
extern adc_config_t adcConfig[ADC_CHN_COUNT];
extern volatile uint16_t adcValues[ADCDEV_COUNT][ADC_CHN_COUNT];
extern volatile uint16_t adcValues[ADCDEV_COUNT][ADC_CHN_COUNT * ADC_AVERAGE_N_SAMPLES];

void adcHardwareInit(drv_adc_config_t *init);
ADCDevice adcDeviceByInstance(ADC_TypeDef *instance);
Expand Down
4 changes: 2 additions & 2 deletions src/main/drivers/adc_stm32f30x.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ static void adcInstanceInit(ADCDevice adcDevice)
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&adc->ADCx->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues[adcDevice];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = adc->usedChannelCount;
DMA_InitStructure.DMA_BufferSize = adc->usedChannelCount * ADC_AVERAGE_N_SAMPLES;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = adc->usedChannelCount > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_MemoryInc = ((adc->usedChannelCount > 1) || (ADC_AVERAGE_N_SAMPLES > 1)) ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
Expand Down
4 changes: 2 additions & 2 deletions src/main/drivers/adc_stm32f4xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ static void adcInstanceInit(ADCDevice adcDevice)
DMA_InitStructure.DMA_Channel = adc->channel;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adcValues[adcDevice];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = adc->usedChannelCount;
DMA_InitStructure.DMA_BufferSize = adc->usedChannelCount * ADC_AVERAGE_N_SAMPLES;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = adc->usedChannelCount > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_MemoryInc = ((adc->usedChannelCount > 1) || (ADC_AVERAGE_N_SAMPLES > 1)) ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
Expand Down
5 changes: 5 additions & 0 deletions src/main/target/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#define USE_UNDERCLOCK
#endif

#if defined(STM32F3) || defined(STM32F4)
#define USE_ADC_AVERAGING
#define ADC_AVERAGE_N_SAMPLES 20
#endif

#define USE_64BIT_TIME
#define USE_BLACKBOX
#define USE_GPS
Expand Down

0 comments on commit 1b0d888

Please sign in to comment.