From 8b60ad83b4449b0519b4cbb68bdb52b6679b82ba Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Wed, 25 Nov 2020 10:26:53 +0100 Subject: [PATCH] Added sample averaging to MDx ADC driver --- platform/drivers/ADC/ADC1_MDx.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/platform/drivers/ADC/ADC1_MDx.c b/platform/drivers/ADC/ADC1_MDx.c index 61d33265..95bf5050 100644 --- a/platform/drivers/ADC/ADC1_MDx.c +++ b/platform/drivers/ADC/ADC1_MDx.c @@ -19,7 +19,19 @@ #include "hwconfig.h" #include "ADC1_MDx.h" -uint16_t measurements[4]; +/* + * Ringbuffer of samples to allow for value smoothing through averaging. This + * buffer is structured as follows: + * + * | vbat | rssi | vox | vol | vbat | rssi | vox | vol | ... + * + * thus it contains four samples of the four channels. Then, DMA is configured + * in circular mode with a rollover after 16 transfers, effectively managing + * this buffer as a ringbuffer for the measurements of the four channels. + * Then, in adc1_getMeasurement(), the average over the values contained in the + * ring buffer is performed and returned as the current channel value. + */ +uint16_t sampleRingBuf[16]; void adc1_init() { @@ -86,8 +98,8 @@ void adc1_init() * - no interrupts */ DMA2_Stream0->PAR = ((uint32_t) &(ADC1->DR)); - DMA2_Stream0->M0AR = ((uint32_t) &measurements); - DMA2_Stream0->NDTR = 4; + DMA2_Stream0->M0AR = ((uint32_t) &sampleRingBuf); + DMA2_Stream0->NDTR = 16; DMA2_Stream0->CR = DMA_SxCR_MSIZE_0 | DMA_SxCR_PSIZE_0 | DMA_SxCR_MINC @@ -109,6 +121,15 @@ void adc1_terminate() float adc1_getMeasurement(uint8_t ch) { if(ch > 3) return 0.0f; - float value = ((float) measurements[ch]); + + /* Return the average over the ring buffer */ + float value = 0.0f; + for(uint8_t i = 0; i < 16; i += 4) + { + value += ((float) sampleRingBuf[i + ch]); + } + value /= 4.0f; + + /* Return average value converted to mV */ return (value * 3300.0f)/4096.0f; }