diff --git a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts index ca9c4f5..e7b1c86 100755 --- a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts +++ b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts @@ -69,35 +69,35 @@ }; }; - v-low { - compatible = "adc-gain"; - io-channels = <&adc1 0>; - numerator = <105600>; - denominator = <5600>; - }; + adc-inputs { + compatible = "adc-inputs"; - v-high { - compatible = "adc-gain"; - io-channels = <&adc1 1>; - numerator = <105600>; - denominator = <5600>; - enable-gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>; - }; + v-low { + io-channels = <&adc1 0>; + multiplier = <105600>; + divider = <5600>; + }; - i-load { - compatible = "adc-gain"; - io-channels = <&adc1 5>; - // amp gain: 50, resistor: 3 mOhm - numerator = <1000>; - denominator = <150>; // 3*50 - }; + v-high { + io-channels = <&adc1 1>; + multiplier = <105600>; + divider = <5600>; + enable-gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>; + }; - i-dcdc { - compatible = "adc-gain"; - io-channels = <&adc1 6>; - // amp gain: 50, resistor: 3 mOhm - numerator = <1000>; - denominator = <150>; // 3*50 + i-load { + io-channels = <&adc1 5>; + // amp gain: 50, resistor: 3 mOhm + multiplier = <1000>; + divider = <150>; // 3*50 + }; + + i-dcdc { + io-channels = <&adc1 6>; + // amp gain: 50, resistor: 3 mOhm + multiplier = <1000>; + divider = <150>; // 3*50 + }; }; aliases { diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index 3ce3676..2b1617f 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -63,50 +63,48 @@ }; }; - v-low { - compatible = "adc-gain"; - io-channels = <&adc1 12>; - numerator = <105600>; - denominator = <5600>; - }; + adc-inputs { + compatible = "adc-inputs"; - v-high { - compatible = "adc-gain"; - io-channels = <&adc1 15>; - numerator = <102200>; - denominator = <2200>; - }; + v-low { + io-channels = <&adc1 12>; + multiplier = <105600>; + divider = <5600>; + }; - v-pwm { - compatible = "adc-gain"; - io-channels = <&adc1 4>; - numerator = <25224>; // (12*8.2 + 120*8.2 + 120*12) * 10 - denominator = <984>; // (8.2*12) * 10 - raw-offset = <2338>; // 4096 / (1 + 8.2/120 + 8.2/12) - }; + v-high { + io-channels = <&adc1 15>; + multiplier = <102200>; + divider = <2200>; + }; - i-dcdc { - compatible = "adc-gain"; - io-channels = <&adc2 1>; - // amp gain: 20, resistor: 2 mOhm - numerator = <1000>; // 1000 - denominator = <40>; // 2*20 - }; + v-pwm { + io-channels = <&adc1 4>; + multiplier = <25224>; // (12*8.2 + 120*8.2 + 120*12) * 10 + divider = <984>; // (8.2*12) * 10 + offset = <2338>; // 4096 / (1 + 8.2/120 + 8.2/12) + }; - i-load { - compatible = "adc-gain"; - io-channels = <&adc2 2>; - // amp gain: 68/2.2, resistor: 2 mOhm - numerator = <2200>; // 1000*2.2 - denominator = <136>; // 2*68 - }; + i-dcdc { + io-channels = <&adc2 1>; + // amp gain: 20, resistor: 2 mOhm + multiplier = <1000>; // 1000 + divider = <40>; // 2*20 + }; - i-pwm { - compatible = "adc-gain"; - io-channels = <&adc2 5>; - // amp gain: 68/2.2, resistor: 2 mOhm - numerator = <2200>; // 1000*2.2 - denominator = <136>; // 2*68 + i-load { + io-channels = <&adc2 2>; + // amp gain: 68/2.2, resistor: 2 mOhm + multiplier = <2200>; // 1000*2.2 + divider = <136>; // 2*68 + }; + + i-pwm { + io-channels = <&adc2 5>; + // amp gain: 68/2.2, resistor: 2 mOhm + multiplier = <2200>; // 1000*2.2 + divider = <136>; // 2*68 + }; }; soc { diff --git a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts index 725149a..6af6264 100755 --- a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts +++ b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts @@ -73,34 +73,34 @@ }; }; - v-low { - compatible = "adc-gain"; - io-channels = <&adc1 0>; - numerator = <110000>; - denominator = <10000>; - }; + adc-inputs { + compatible = "adc-inputs"; - v-high { - compatible = "adc-gain"; - io-channels = <&adc1 1>; - numerator = <105600>; - denominator = <5600>; - }; + v-low { + io-channels = <&adc1 0>; + multiplier = <110000>; + divider = <10000>; + }; - i-load { - compatible = "adc-gain"; - io-channels = <&adc1 5>; - // amp gain: 150/2.2, resistor: 2 mOhm - numerator = <2200>; // 1000*2.2 - denominator = <300>; // 2*150 - }; + v-high { + io-channels = <&adc1 1>; + multiplier = <105600>; + divider = <5600>; + }; - i-dcdc { - compatible = "adc-gain"; - io-channels = <&adc1 6>; - // amp gain: 150/2.2, resistor: 2 mOhm - numerator = <2200>; // 1000*2.2 - denominator = <300>; // 2*150 + i-load { + io-channels = <&adc1 5>; + // amp gain: 150/2.2, resistor: 2 mOhm + multiplier = <2200>; // 1000*2.2 + divider = <300>; // 2*150 + }; + + i-dcdc { + io-channels = <&adc1 6>; + // amp gain: 150/2.2, resistor: 2 mOhm + multiplier = <2200>; // 1000*2.2 + divider = <300>; // 2*150 + }; }; aliases { diff --git a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts index 4d80c64..b910a9c 100644 --- a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts +++ b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts @@ -44,26 +44,27 @@ }; }; - v-low { - compatible = "adc-gain"; - io-channels = <&adc1 12>; - numerator = <105600>; - denominator = <5600>; - }; + adc-inputs { + compatible = "adc-inputs"; - v-high { - compatible = "adc-gain"; - io-channels = <&adc1 15>; - numerator = <102200>; - denominator = <2200>; - }; + v-low { + io-channels = <&adc1 12>; + multiplier = <105600>; + divider = <5600>; + }; - i-dcdc { - compatible = "adc-gain"; - io-channels = <&adc2 1>; - // amp gain: 68/2.2, resistor: 2 mOhm - numerator = <2200>; // 1000*2.2 - denominator = <136>; // 2*68 + v-high { + io-channels = <&adc1 15>; + multiplier = <102200>; + divider = <2200>; + }; + + i-dcdc { + io-channels = <&adc2 1>; + // amp gain: 68/2.2, resistor: 2 mOhm + multiplier = <2200>; // 1000*2.2 + divider = <136>; // 2*68 + }; }; soc { diff --git a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts index 8ebd117..2ed8f1f 100644 --- a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts +++ b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts @@ -75,36 +75,36 @@ }; }; - v-low { - compatible = "adc-gain"; - io-channels = <&adc1 0>; - numerator = <132000>; - denominator = <12000>; - }; + adc-inputs { + compatible = "adc-inputs"; - v-pwm { - compatible = "adc-gain"; - io-channels = <&adc1 1>; - numerator = <25224>; // (12*8.2 + 120*8.2 + 120*12) * 10 - denominator = <984>; // (8.2*12) * 10 - raw-offset = <2338>; // 4096 / (1 + 8.2/120 + 8.2/12) - }; + v-low { + io-channels = <&adc1 0>; + multiplier = <132000>; + divider = <12000>; + }; - i-load { - compatible = "adc-gain"; - io-channels = <&adc1 5>; - // amp gain: 68/2.2, resistor: 2 mOhm - // fix for hardware bug in overcurrent comparator voltage divider wiring - numerator = <2931>; // 1000 * (39+12+8.2) / (12+8.2) = 2930.69 - denominator = <62>; // 2*68/2.2 = 61.8181 - }; + v-pwm { + io-channels = <&adc1 1>; + multiplier = <25224>; // (12*8.2 + 120*8.2 + 120*12) * 10 + divider = <984>; // (8.2*12) * 10 + offset = <2338>; // 4096 / (1 + 8.2/120 + 8.2/12) + }; - i-pwm { - compatible = "adc-gain"; - io-channels = <&adc1 6>; - // amp gain: 68/2.2, resistor: 2 mOhm - numerator = <2200>; // 1000*2.2 - denominator = <136>; // 2*68 + i-load { + io-channels = <&adc1 5>; + // amp gain: 68/2.2, resistor: 2 mOhm + // fix for hardware bug in overcurrent comparator voltage divider wiring + multiplier = <2931>; // 1000 * (39+12+8.2) / (12+8.2) = 2930.69 + divider = <62>; // 2*68/2.2 = 61.8181 + }; + + i-pwm { + io-channels = <&adc1 6>; + // amp gain: 68/2.2, resistor: 2 mOhm + multiplier = <2200>; // 1000*2.2 + divider = <136>; // 2*68 + }; }; aliases { diff --git a/src/daq.cpp b/src/daq.cpp index 653ce14..6f6a958 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -162,7 +162,7 @@ void daq_update() #if DT_OUTPUTS_PWM_SWITCH_PRESENT pwm_switch.ext_voltage = lv_bus.voltage - - adc_scaled(ADC_POS_V_PWM, vref, ADC_GAIN(V_PWM), DT_ADC_GAIN_V_PWM_RAW_OFFSET); + adc_scaled(ADC_POS_V_PWM, vref, ADC_GAIN(V_PWM), DT_ADC_INPUTS_V_PWM_OFFSET); #endif #if DT_OUTPUTS_LOAD_PRESENT diff --git a/src/daq.h b/src/daq.h index c56c44f..81b42a1 100644 --- a/src/daq.h +++ b/src/daq.h @@ -34,7 +34,7 @@ extern "C" { #define VREF (VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU)) #endif -#define ADC_GAIN(name) ((float)DT_ADC_GAIN_##name##_NUMERATOR / DT_ADC_GAIN_##name##_DENOMINATOR) +#define ADC_GAIN(name) ((float)DT_ADC_INPUTS_##name##_MULTIPLIER / DT_ADC_INPUTS_##name##_DIVIDER) /** * Struct to definie upper and lower limit alerts for any ADC channel diff --git a/src/daq_driver.c b/src/daq_driver.c index dcfdab4..55bc844 100644 --- a/src/daq_driver.c +++ b/src/daq_driver.c @@ -187,10 +187,10 @@ static void adc_init(ADC_TypeDef *adc) static void adc_setup() { -#ifdef DT_ADC_GAIN_V_HIGH_ENABLE_GPIOS_PIN - struct device *dev = device_get_binding(DT_ADC_GAIN_V_HIGH_ENABLE_GPIOS_CONTROLLER); - gpio_pin_configure(dev, DT_ADC_GAIN_V_HIGH_ENABLE_GPIOS_PIN, - DT_ADC_GAIN_V_HIGH_ENABLE_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); +#ifdef DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_PIN + struct device *dev = device_get_binding(DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_CONTROLLER); + gpio_pin_configure(dev, DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_PIN, + DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); #endif adc_init(ADC1); diff --git a/test/board.h b/test/board.h index 4ad5f4d..c587420 100644 --- a/test/board.h +++ b/test/board.h @@ -101,21 +101,21 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 8200.0 -#define DT_ADC_GAIN_V_HIGH_NUMERATOR 105600 -#define DT_ADC_GAIN_V_HIGH_DENOMINATOR 5600 -#define DT_ADC_GAIN_V_LOW_NUMERATOR 105600 -#define DT_ADC_GAIN_V_LOW_DENOMINATOR 5600 -#define DT_ADC_GAIN_V_PWM_NUMERATOR 25224 // see pwm_2420_lus.dtx -#define DT_ADC_GAIN_V_PWM_DENOMINATOR 984 -#define DT_ADC_GAIN_V_PWM_RAW_OFFSET 2338 +#define DT_ADC_INPUTS_V_HIGH_MULTIPLIER 105600 +#define DT_ADC_INPUTS_V_HIGH_DIVIDER 5600 +#define DT_ADC_INPUTS_V_LOW_MULTIPLIER 105600 +#define DT_ADC_INPUTS_V_LOW_DIVIDER 5600 +#define DT_ADC_INPUTS_V_PWM_MULTIPLIER 25224 // see pwm_2420_lus.dtx +#define DT_ADC_INPUTS_V_PWM_DIVIDER 984 +#define DT_ADC_INPUTS_V_PWM_OFFSET 2338 // amp gain: 50, resistor: 4 mOhm -#define DT_ADC_GAIN_I_LOAD_NUMERATOR 1000 -#define DT_ADC_GAIN_I_LOAD_DENOMINATOR (4 * 50) -#define DT_ADC_GAIN_I_DCDC_NUMERATOR 1000 -#define DT_ADC_GAIN_I_DCDC_DENOMINATOR (4 * 50) -#define DT_ADC_GAIN_I_PWM_NUMERATOR 1000 -#define DT_ADC_GAIN_I_PWM_DENOMINATOR (4 * 50) +#define DT_ADC_INPUTS_I_LOAD_MULTIPLIER 1000 +#define DT_ADC_INPUTS_I_LOAD_DIVIDER (4 * 50) +#define DT_ADC_INPUTS_I_DCDC_MULTIPLIER 1000 +#define DT_ADC_INPUTS_I_DCDC_DIVIDER (4 * 50) +#define DT_ADC_INPUTS_I_PWM_MULTIPLIER 1000 +#define DT_ADC_INPUTS_I_PWM_DIVIDER (4 * 50) // position in the array written by the DMA controller enum { diff --git a/zephyr/dts/bindings/adc-gain.yaml b/zephyr/dts/bindings/adc-inputs.yaml similarity index 67% rename from zephyr/dts/bindings/adc-gain.yaml rename to zephyr/dts/bindings/adc-inputs.yaml index c947306..6963b31 100644 --- a/zephyr/dts/bindings/adc-gain.yaml +++ b/zephyr/dts/bindings/adc-inputs.yaml @@ -1,33 +1,32 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2020 Martin Jäger / Libre Solar -# Based on official Zephyr binding "voltage-divider" by Peter Bigot +description: Description for an ADC input including gain (multiplier/divider) and offset -description: | - Description for a gain (voltage divider or amplifier) at an ADC input +compatible: "adc-inputs" -compatible: "adc-gain" - -properties: +child-binding: + description: ADC inputs child node + properties: io-channels: type: phandle-array required: true description: | - Channels available with this divider configuration. + ADC channel for this input. - numerator: + multiplier: type: int required: true description: | - Resistance of the lower leg of the voltage divider + Resistance of the lower leg of the voltage divider. - denominator: + divider: type: int required: true description: | Resistance of the full path through the voltage divider. - raw-offset: + offset: type: int required: false description: | diff --git a/zephyr/mppt_1210_hus.overlay_0v4 b/zephyr/mppt_1210_hus.overlay_0v4 index ff37e1e..cc76bff 100644 --- a/zephyr/mppt_1210_hus.overlay_0v4 +++ b/zephyr/mppt_1210_hus.overlay_0v4 @@ -51,19 +51,21 @@ /delete-node/ boot0; }; - i-load { - compatible = "adc-gain"; - io-channels = <&adc1 6>; - // amp gain: 50, resistor: 4 mOhm - numerator = <1000>; - denominator = <200>; // 4*50 - }; + adc-inputs { + compatible = "adc-inputs"; - i-dcdc { - compatible = "adc-gain"; - io-channels = <&adc1 7>; - // amp gain: 50, resistor: 4 mOhm - numerator = <1000>; - denominator = <200>; // 4*50 + i-load { + io-channels = <&adc1 6>; + // amp gain: 50, resistor: 4 mOhm + multiplier = <1000>; + divider = <200>; // 4*50 + }; + + i-dcdc { + io-channels = <&adc1 7>; + // amp gain: 50, resistor: 4 mOhm + multiplier = <1000>; + divider = <200>; // 4*50 + }; }; }; diff --git a/zephyr/pwm_2420_lus.overlay_0v2 b/zephyr/pwm_2420_lus.overlay_0v2 index 6f5e9ab..2001aaa 100644 --- a/zephyr/pwm_2420_lus.overlay_0v2 +++ b/zephyr/pwm_2420_lus.overlay_0v2 @@ -10,11 +10,14 @@ version-num = <2>; }; - i-load { - compatible = "adc-gain"; - io-channels = <&adc1 5>; - // amp gain: 68/2.2, resistor: 2 mOhm - numerator = <1000>; - denominator = <62>; // 2*68/2.2 = 61.8181 + adc-inputs { + compatible = "adc-inputs"; + + i-load { + io-channels = <&adc1 5>; + // amp gain: 68/2.2, resistor: 2 mOhm + multiplier = <1000>; + divider = <62>; // 2*68/2.2 = 61.8181 + }; }; };