ADC: Use 16-bit raw values for calculations

Using 16-bit for calculation allows to increase resolution by oversampling.
The ADC was already configured to use left-aligned 12-bit readings, which can
be treated the same as 16-bit native readings.
This commit is contained in:
Martin Jäger
2020-09-21 13:29:31 +02:00
committed by Martin Jäger
parent 3bb4a86b5c
commit 4008a09f3b
4 changed files with 27 additions and 25 deletions
+7 -10
View File
@@ -32,7 +32,7 @@ static uint16_t pwm_current_offset_raw;
static uint16_t load_current_offset_raw;
#endif
// left-aligned 12-bit ADC raw readings (i.e. left-shifted by 4 bits)
// 16-bit ADC raw readings (actually left-aligned 12-bit, i.e. left-shifted by 4 bits)
volatile uint16_t adc_readings[NUM_ADC_CH] = {};
// filtered raw readings left-shifted by additional ADC_FILTER_CONST bits
@@ -48,8 +48,7 @@ static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {};
*/
static inline uint32_t adc_raw_filtered(uint32_t channel)
{
assert(channel < NUM_ADC_CH);
return adc_filtered[channel] >> (4 + ADC_FILTER_CONST);
return adc_filtered[channel] >> ADC_FILTER_CONST;
}
/**
@@ -225,8 +224,8 @@ void daq_update()
dcdc.temp_mosfets = ntc_temp(ADC_POS(temp_fets), vref, ADC_GAIN(temp_fets));
#endif
// internal MCU temperature
uint16_t adcval = adc_raw_filtered(ADC_POS(temp_mcu)) * vref / VREFINT_VALUE;
// internal MCU temperature (calibrated using 12-bit right-aligned readings)
uint16_t adcval = (adc_raw_filtered(ADC_POS(temp_mcu)) >> 4) * vref / VREFINT_VALUE;
dev_stat.internal_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) /
(TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE;
@@ -278,18 +277,16 @@ void adc_upper_alert_inhibit(int adc_pos, int timeout_ms)
uint16_t adc_get_alert_limit(float scale, float limit)
{
const float adclimit = (UINT16_MAX >> 4); // 12 bits ADC resolution
const float limit_scaled = limit * scale;
// even if we have a higher voltage limit, we must limit it
// to the max value the ADC will be able to deliver
return (limit_scaled > adclimit ? 0x0FFF : (uint16_t)(limit_scaled)) << 4;
// shift 4 bits left to generate left aligned 16bit value
const float limit_scaled = limit * scale;
return limit_scaled > (float)UINT16_MAX ? UINT16_MAX : (uint16_t)(limit_scaled);
}
void daq_set_lv_alerts(float upper, float lower)
{
int vref = VREF;
float scale = ((4096 * 1000) / ADC_GAIN(v_low)) / vref;
float scale = (((4096 << 4) * 1000) / ADC_GAIN(v_low)) / vref;
// LV side (battery) overvoltage alert
adc_alerts_upper[ADC_POS(v_low)].limit = adc_get_alert_limit(scale, upper);
+13 -8
View File
@@ -11,6 +11,10 @@
* Measurements are taken using ADC with DMA. The DAC is used to generate reference voltages
* for bi-directional current measurement. Data is stored in structs, which are afterwards
* used by the control algorithms
*
* All calculations assume a 16-bit ADC, even though the STM32 ADCs natively support 12-bit.
* Using 16-bit for calculation allows to increase resolution by oversampling. Left-aligned
* 12-bit ADC readings can be treated as 16-bit ADC readings.
*/
#ifndef DAQ_H_
@@ -25,6 +29,7 @@ extern "C" {
#include <stdint.h>
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
#define ADC_SCALE_FLOAT 65536.0F // 16-bit full scale
#ifdef CONFIG_SOC_SERIES_STM32G4X
// Using internal reference buffer at VREF+ pin, set to 2048 mV
@@ -72,29 +77,29 @@ typedef struct {
} AdcAlert;
/**
* Convert raw ADC reading to voltage
* Convert 16-bit raw ADC reading to voltage
*
* @param raw 12-bit ADC reading (right-aligned)
* @param raw 16-bit ADC reading
* @param vref_mV Reference voltage in millivolts
*
* @return Voltage in Volts
* @return Voltage in volts
*/
static inline float adc_raw_to_voltage(int32_t raw, int32_t vref_mV)
{
return (raw * vref_mV) / (4096.0F * 1000);
return (raw * vref_mV) / (ADC_SCALE_FLOAT * 1000);
}
/**
* Convert voltage to raw ADC reading
* Convert voltage to 16-bit raw ADC reading
*
* @param voltage Voltage in Volts
* @param voltage Voltage in volts
* @param vref_mV Reference voltage in millivolts
*
* @return 12-bit ADC reading (right-aligned)
* @return 16-bit ADC reading
*/
static inline int32_t adc_voltage_to_raw(float voltage, int32_t vref_mV)
{
return voltage / vref_mV * 4096.0F * 1000;
return voltage / vref_mV * ADC_SCALE_FLOAT * 1000;
}
/**
+1 -1
View File
@@ -38,7 +38,7 @@
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X)
// automatich channel selection based on devicetree settings
// automatic channel selection based on devicetree settings
#define ADC_CHSEL_FN(node_id) (1UL << DT_PHA(node_id, io_channels, input)) |
#define ADC_CHSEL (DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_CHSEL_FN) 0)
+6 -6
View File
@@ -22,10 +22,10 @@ void test_adc_voltage_to_raw()
TEST_ASSERT_EQUAL_INT32(0, raw);
raw = adc_voltage_to_raw(3.3, 3300);
TEST_ASSERT_EQUAL_INT32(4095, raw);
TEST_ASSERT_EQUAL_INT32(65535, raw);
raw = adc_voltage_to_raw(1.65, 3300);
TEST_ASSERT_EQUAL_INT32(2047, raw);
TEST_ASSERT_EQUAL_INT32(32767, raw);
}
void test_adc_raw_to_voltage()
@@ -35,10 +35,10 @@ void test_adc_raw_to_voltage()
voltage = adc_raw_to_voltage(0, 3300);
TEST_ASSERT_EQUAL_FLOAT(0.0, voltage);
voltage = adc_raw_to_voltage(4095, 3300);
voltage = adc_raw_to_voltage(65535, 3300);
TEST_ASSERT_FLOAT_WITHIN(0.01, 3.3, voltage);
voltage = adc_raw_to_voltage(2048, 3300);
voltage = adc_raw_to_voltage(32767, 3300);
TEST_ASSERT_FLOAT_WITHIN(0.01, 1.65, voltage);
}
@@ -159,8 +159,8 @@ void adc_alert_overvoltage_triggering()
void adc_alert_overflow_prevention()
{
// try to set an alert that overflows the 12-bit ADC resolution
uint16_t limit = adc_get_alert_limit(1, 4097);
TEST_ASSERT_EQUAL_HEX((uint16_t)(UINT16_MAX << 4), limit);
uint16_t limit = adc_get_alert_limit(1, UINT16_MAX + 1);
TEST_ASSERT_EQUAL_HEX(UINT16_MAX, limit);
}
/** Data acquisition tests