Added sample averaging to MDx ADC driver

replace/d9d9849da051909ed2fc6c1c61a0275a43a88f48
Silvano Seva 2020-11-25 10:26:53 +01:00
rodzic 50771c8bbe
commit 8b60ad83b4
1 zmienionych plików z 25 dodań i 4 usunięć

Wyświetl plik

@ -19,7 +19,19 @@
#include "hwconfig.h" #include "hwconfig.h"
#include "ADC1_MDx.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() void adc1_init()
{ {
@ -86,8 +98,8 @@ void adc1_init()
* - no interrupts * - no interrupts
*/ */
DMA2_Stream0->PAR = ((uint32_t) &(ADC1->DR)); DMA2_Stream0->PAR = ((uint32_t) &(ADC1->DR));
DMA2_Stream0->M0AR = ((uint32_t) &measurements); DMA2_Stream0->M0AR = ((uint32_t) &sampleRingBuf);
DMA2_Stream0->NDTR = 4; DMA2_Stream0->NDTR = 16;
DMA2_Stream0->CR = DMA_SxCR_MSIZE_0 DMA2_Stream0->CR = DMA_SxCR_MSIZE_0
| DMA_SxCR_PSIZE_0 | DMA_SxCR_PSIZE_0
| DMA_SxCR_MINC | DMA_SxCR_MINC
@ -109,6 +121,15 @@ void adc1_terminate()
float adc1_getMeasurement(uint8_t ch) float adc1_getMeasurement(uint8_t ch)
{ {
if(ch > 3) return 0.0f; 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; return (value * 3300.0f)/4096.0f;
} }