From ef9c8571ccb5095cc82730b00bd5b4e352ce007b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Thu, 17 Sep 2020 15:53:49 +0200 Subject: [PATCH] ADC filter time constant configurable per channel Add a filter-const prop to the adc_inputs devicetree binding and use it instead of the global ADC_FILTER_CONST in daq.cpp. --- src/daq.cpp | 28 +++++++++++++++++++--------- src/daq.h | 1 - test/devicetree_unfixed.h | 15 +++++++++++++++ zephyr/dts/bindings/adc-inputs.yaml | 5 +++++ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/daq.cpp b/src/daq.cpp index 8b23aa7..bf2ce7c 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -35,12 +35,22 @@ static uint16_t load_current_offset_raw; // 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 +// filtered raw readings left-shifted by additional adc_filter_const[channel] bits volatile uint32_t adc_filtered[NUM_ADC_CH] = {}; static volatile AdcAlert adc_alerts_upper[NUM_ADC_CH] = {}; static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {}; +/* + * Channel-specific ADC filter constant from devicetree + * + * multiplier = 1/(2^adc_filter_const[channel]) + */ +#define ADC_FILTER_CONST(node_id) DT_PROP(node_id, filter_const), +static const uint8_t adc_filter_const[NUM_ADC_CH] = { + DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_FILTER_CONST) +}; + /** * Average value for ADC channel * @@ -48,7 +58,7 @@ static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {}; */ static inline uint32_t adc_raw_filtered(uint32_t channel) { - return adc_filtered[channel] >> ADC_FILTER_CONST; + return adc_filtered[channel] >> adc_filter_const[channel]; } /** @@ -106,19 +116,19 @@ void adc_update_value(unsigned int pos) * Low-pass filtering of ADC raw readings * (see also: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf) * - * y(n) = c * x(n) + (1 - c) * y(n-1) with filter constant c = 1/(2^ADC_FILTER_CONST) + * y(n) = c * x(n) + (1 - c) * y(n-1) with filter constant c = 1/(2^adc_filter_const[pos]) * * Remarks regarding below implementation optimized for efficiency: * * 1. adc_readings has different fixed-point math format, so that it would have to be - * aligned with << ADC_FILTER_CONST before calculation. Not aligning it is equivalent - * to multiplication with c: - * adc_readings[pos] == c * (adc_readings[pos] << ADC_FILTER_CONST) + * aligned with << adc_filter_const[pos] before calculation. Not aligning it is + * equivalent to multiplication with c: + * adc_readings[pos] == c * (adc_readings[pos] << adc_filter_const[pos]) * - * 2. c * adc_filtered[pos] == adc_filtered[pos] >> ADC_FILTER_CONST + * 2. c * adc_filtered[pos] == adc_filtered[pos] >> adc_filter_const[pos] */ adc_filtered[pos] = (uint32_t)adc_readings[pos] + - adc_filtered[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST); + adc_filtered[pos] - (adc_filtered[pos] >> adc_filter_const[pos]); } // check upper alerts @@ -318,7 +328,7 @@ void prepare_adc_filtered() { // initialize also filtered values for (int i = 0; i < NUM_ADC_CH; i++) { - adc_filtered[i] = adc_readings[i] << ADC_FILTER_CONST; + adc_filtered[i] = adc_readings[i] << adc_filter_const[i]; } } diff --git a/src/daq.h b/src/daq.h index 89c1f23..28d2a53 100644 --- a/src/daq.h +++ b/src/daq.h @@ -28,7 +28,6 @@ extern "C" { #include -#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 diff --git a/test/devicetree_unfixed.h b/test/devicetree_unfixed.h index 5e19f8c..30e2486 100644 --- a/test/devicetree_unfixed.h +++ b/test/devicetree_unfixed.h @@ -38,22 +38,37 @@ #define DT_N_S_adc_inputs_S_temp_bat_EXISTS 1 #define DT_N_S_adc_inputs_S_temp_bat_P_multiplier 10000 #define DT_N_S_adc_inputs_S_temp_bat_P_divider 1 +#define DT_N_S_adc_inputs_S_temp_bat_P_filter_const 5 #define DT_N_S_adc_inputs_S_v_high_P_multiplier 105600 #define DT_N_S_adc_inputs_S_v_high_P_divider 5600 +#define DT_N_S_adc_inputs_S_v_high_P_filter_const 5 + #define DT_N_S_adc_inputs_S_v_low_P_multiplier 105600 #define DT_N_S_adc_inputs_S_v_low_P_divider 5600 +#define DT_N_S_adc_inputs_S_v_low_P_filter_const 5 + #define DT_N_S_adc_inputs_S_v_pwm_P_multiplier 25224 // see pwm_2420_lus.dts #define DT_N_S_adc_inputs_S_v_pwm_P_divider 984 #define DT_N_S_adc_inputs_S_v_pwm_P_offset 2338 +#define DT_N_S_adc_inputs_S_v_pwm_P_filter_const 5 // amp gain: 50, resistor: 4 mOhm #define DT_N_S_adc_inputs_S_i_load_P_multiplier 1000 #define DT_N_S_adc_inputs_S_i_load_P_divider (4 * 50) +#define DT_N_S_adc_inputs_S_i_load_P_filter_const 5 + #define DT_N_S_adc_inputs_S_i_dcdc_P_multiplier 1000 #define DT_N_S_adc_inputs_S_i_dcdc_P_divider (4 * 50) +#define DT_N_S_adc_inputs_S_i_dcdc_P_filter_const 5 + #define DT_N_S_adc_inputs_S_i_pwm_P_multiplier 1000 #define DT_N_S_adc_inputs_S_i_pwm_P_divider (4 * 50) +#define DT_N_S_adc_inputs_S_i_pwm_P_filter_const 5 + +#define DT_N_S_adc_inputs_S_vref_mcu_P_filter_const 5 + +#define DT_N_S_adc_inputs_S_temp_mcu_P_filter_const 5 #define DT_N_S_adc_inputs_FOREACH_CHILD(fn) \ fn(DT_N_S_adc_inputs_S_v_low) \ diff --git a/zephyr/dts/bindings/adc-inputs.yaml b/zephyr/dts/bindings/adc-inputs.yaml index 6963b31..b8234e3 100644 --- a/zephyr/dts/bindings/adc-inputs.yaml +++ b/zephyr/dts/bindings/adc-inputs.yaml @@ -32,6 +32,11 @@ child-binding: description: | Offset to be substracted from ADC raw reading before applying the gain + filter-const: + type: int + default: 5 + description: Low-pass filter multiplier 1/(2^filter-const) + enable-gpios: type: phandle-array required: false