wideband/firmware/analog_input.cpp

49 lines
1.1 KiB
C++
Raw Normal View History

2020-09-19 21:39:29 -07:00
#include "analog_input.h"
2020-10-30 02:03:12 -07:00
#include "wideband_config.h"
2020-09-19 21:39:29 -07:00
#include "hal.h"
2020-10-26 12:53:45 -07:00
#define ADC_CHANNEL_COUNT 3
2020-09-19 21:39:29 -07:00
2020-10-26 12:53:45 -07:00
static adcsample_t adcBuffer[ADC_CHANNEL_COUNT * ADC_OVERSAMPLE];
2020-09-19 21:39:29 -07:00
ADCConversionGroup convGroup =
{
2020-10-26 12:53:45 -07:00
false,
ADC_CHANNEL_COUNT,
nullptr,
nullptr,
ADC_CFGR1_CONT | ADC_CFGR1_RES_12BIT, // CFGR1
2020-09-19 21:39:29 -07:00
ADC_TR(0, 0), // TR
2020-11-03 01:52:19 -08:00
ADC_SMPR_SMP_7P5, // SMPR
2020-09-19 21:39:29 -07:00
ADC_CHSELR_CHSEL0 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL2
};
static float AverageSamples(adcsample_t* buffer, size_t idx)
{
uint32_t sum = 0;
for (size_t i = 0; i < ADC_OVERSAMPLE; i++)
{
sum += buffer[idx];
2020-10-26 14:06:00 -07:00
idx += ADC_CHANNEL_COUNT;
2020-09-19 21:39:29 -07:00
}
2020-10-30 02:03:12 -07:00
constexpr float scale = VCC_VOLTS / (ADC_MAX_COUNT * ADC_OVERSAMPLE);
2020-09-20 13:39:14 -07:00
return (float)sum * scale;
2020-09-19 21:39:29 -07:00
}
AnalogResult AnalogSample()
{
adcConvert(&ADCD1, &convGroup, adcBuffer, ADC_OVERSAMPLE);
return
{
2020-10-30 02:03:12 -07:00
.NernstVoltage = AverageSamples(adcBuffer, 0) * NERNST_INPUT_GAIN,
2020-09-19 21:39:29 -07:00
.VirtualGroundVoltage = AverageSamples(adcBuffer, 1),
.PumpCurrentVoltage = AverageSamples(adcBuffer, 2),
};
}