New adc-gain devicetree binding

This commit is contained in:
Martin Jäger
2020-05-06 12:53:26 +02:00
parent 4a70293375
commit a412117b5b
13 changed files with 287 additions and 81 deletions
-10
View File
@@ -63,16 +63,6 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
#define ADC_GAIN_V_LOW (105.6 / 5.6) // both voltage dividers: 100k + 5.6k
#define ADC_GAIN_V_HIGH (105.6 / 5.6)
#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4
#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm
#define ADC_GAIN_I_DCDC (1000 / 4 / 50)
#else
#define ADC_GAIN_I_LOAD (1000.0 / 3.0 / 50.0) // amp gain: 50, resistor: 3 mOhm
#define ADC_GAIN_I_DCDC (1000.0 / 3.0 / 50.0)
#endif
// position in the array written by the DMA controller
#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4
enum {
+31 -4
View File
@@ -63,16 +63,43 @@
label = "USB charging port enable";
gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>;
};
v-solar {
label = "Solar voltage measurement enable";
gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>;
};
boot0 {
label = "BOOT0 pin trigger";
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
};
};
v-low {
compatible = "adc-gain";
io-channels = <&adc1 0>;
numerator = <105600>;
denominator = <5600>;
};
v-high {
compatible = "adc-gain";
io-channels = <&adc1 1>;
numerator = <105600>;
denominator = <5600>;
enable-gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>;
};
i-load {
compatible = "adc-gain";
io-channels = <&adc1 5>;
// amp gain: 50, resistor: 3 mOhm
numerator = <1000>;
denominator = <150>; // 3*50
};
i-dcdc {
compatible = "adc-gain";
io-channels = <&adc1 6>;
// amp gain: 50, resistor: 3 mOhm
numerator = <1000>;
denominator = <150>; // 3*50
};
aliases {
uart-dbg = &usart1;
uart-uext = &usart2;
-13
View File
@@ -34,19 +34,6 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
#define ADC_GAIN_V_LOW (105.6 / 5.6)
#define ADC_GAIN_V_HIGH (102.2 / 2.2)
#define ADC_GAIN_V_PWM (1 + 120/12 + 120/8.2)
#define ADC_GAIN_I_DCDC (1000 / 2 / 20) // amp gain: 20, resistor: 2 mOhm
// op amp gain: 68/2.2, resistor: 2 mOhm
#define ADC_GAIN_I_LOAD (1000 / 2 / (68/2.2))
#define ADC_GAIN_I_PWM (1000 / 2 / (68/2.2))
// to be multiplied with VREF to get absolute voltage offset
#define ADC_OFFSET_V_PWM (-120.0 / 8.2)
// position in the array written by the DMA controller
enum {
// ADC1
@@ -63,6 +63,84 @@
};
};
v-low {
compatible = "adc-gain";
io-channels = <&adc1 12>;
numerator = <102200>;
denominator = <5600>;
};
v-high {
compatible = "adc-gain";
io-channels = <&adc1 15>;
numerator = <105600>;
denominator = <2200>;
};
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)
};
i-dcdc {
compatible = "adc-gain";
io-channels = <&adc2 1>;
// amp gain: 20, resistor: 2 mOhm
numerator = <1000>; // 1000
denominator = <40>; // 2*20
};
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-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
};
soc {
// ADC nodes not yet provided by Zephyr, but needed for adc-gain compatibles
adc1: adc@50000000 {
compatible = "st,stm32-adc";
reg = <0x50000000 0x400>;
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>;
interrupts = <18 0>;
status = "disabled";
label = "ADC_1";
#io-channel-cells = <1>;
};
adc2: adc@50000100 {
compatible = "st,stm32-adc";
reg = <0x50000100 0x400>;
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>;
interrupts = <18 0>;
status = "disabled";
label = "ADC_2";
#io-channel-cells = <1>;
};
dac1: dac@50000800 {
compatible = "st,stm32-dac";
reg = <0x50000800 0x400>;
clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00010000>;
status = "disabled";
label = "DAC_1";
#io-channel-cells = <1>;
};
};
aliases {
uart-dbg = &usart1;
uart-uext = &usart3;
-5
View File
@@ -37,11 +37,6 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
#define ADC_GAIN_V_LOW (110.0 / 10 ) // battery voltage divider 100k + 10k
#define ADC_GAIN_V_HIGH (105.6 / 5.6) // solar voltage divider: 100k + 5.6k
#define ADC_GAIN_I_LOAD (1000 / 2 / (1500.0 / 22.0)) // op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm
#define ADC_GAIN_I_DCDC (1000 / 2 / (1500.0 / 22.0))
// position in the array written by the DMA controller
enum {
ADC_POS_TEMP_BAT, // ADC 0 (PA_0)
+30
View File
@@ -73,6 +73,36 @@
};
};
v-low {
compatible = "adc-gain";
io-channels = <&adc1 0>;
numerator = <110000>;
denominator = <10000>;
};
v-high {
compatible = "adc-gain";
io-channels = <&adc1 1>;
numerator = <105600>;
denominator = <5600>;
};
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
};
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
};
aliases {
uart-dbg = &usart1;
uart-uext = &usart2;
-16
View File
@@ -55,22 +55,6 @@ 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 ADC_GAIN_V_LOW (132 / 12)
#define ADC_GAIN_V_PWM (1 + 120/12 + 120/8.2)
#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 2
// op amp gain: 68/2.2, resistor: 2 mOhm
#define ADC_GAIN_I_LOAD (1000 / 2 / (68/2.2))
#elif DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 3
// fix for hardware bug in overcurrent comparator voltage divider wiring
#define ADC_GAIN_I_LOAD (1000 / 2 / (68/2.2) * (39+12+8.2) / (12+8.2))
#endif
#define ADC_GAIN_I_PWM (1000 / 2 / (68/2.2))
#define ADC_OFFSET_V_PWM (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset
// position in the array written by the DMA controller
enum {
ADC_POS_V_LOW, // ADC 0 (PA_0)
+32
View File
@@ -75,6 +75,38 @@
};
};
v-low {
compatible = "adc-gain";
io-channels = <&adc1 0>;
numerator = <132000>;
denominator = <12000>;
};
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)
};
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
};
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
};
aliases {
uart-dbg = &usart1;
uart-uext = &usart2;
+35 -25
View File
@@ -42,9 +42,12 @@ extern DeviceStatus dev_stat;
#define VREF (VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU))
#endif
#define ADC_GAIN(name) (DT_ADC_GAIN_##name##_NUMERATOR / DT_ADC_GAIN_##name##_DENOMINATOR)
/**
* Average value for ADC channel
* @param channel valid ADC channel pos ADC_POS_..., see adc_h.c
*
* @param channel valid ADC channel pos ADC_POS_..., see board.h
*/
static inline uint32_t adc_value(uint32_t channel)
{
@@ -54,27 +57,30 @@ static inline uint32_t adc_value(uint32_t channel)
/**
* Measured voltage for ADC channel after average
* @param channel valid ADC channel pos ADC_POS_..., see adc_h.c
*
* @param channel valid ADC channel pos ADC_POS_..., see board.h
* @param vref reference voltage in millivolts
* @param offset offset substracted from raw ADC value before applying gain
*
* @return voltage in millivolts
*/
static inline float adc_voltage(uint32_t channel, int32_t vref)
static inline float adc_voltage(uint32_t channel, int32_t vref, int32_t offset = 0)
{
return (adc_value(channel) * vref) / 4096;
return (((int32_t)adc_value(channel) - offset) * vref) / 4096;
}
/**
* Measured current/voltage for ADC channel after average and scaling
* @param channel valid ADC channel pos ADC_POS_..., see adc_h.c
* @param vref reference voltage in millivolts
*
* @return scaled final value
* @param channel valid ADC channel pos ADC_POS_..., see board.h
* @param vref reference voltage in millivolts
* @param offset offset added to raw ADC value before applying gain
*
* @return scaled final value in volts/amps
*/
static inline float adc_scaled(uint32_t channel, int32_t vref, const float gain)
static inline float adc_scaled(uint32_t channel, int32_t vref, const float gain, int32_t offset = 0)
{
return adc_voltage(channel, vref) * (gain/1000.0);
return adc_voltage(channel, vref, offset) * (gain/1000.0);
}
static inline float ntc_temp(uint32_t channel, int32_t vref)
@@ -93,12 +99,12 @@ void calibrate_current_sensors()
{
int vref = VREF;
#if DT_COMPAT_DCDC
dcdc_current_offset = -adc_scaled(ADC_POS_I_DCDC, vref, ADC_GAIN_I_DCDC);
dcdc_current_offset = -adc_scaled(ADC_POS_I_DCDC, vref, ADC_GAIN(I_DCDC));
#endif
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
pwm_current_offset = -adc_scaled(ADC_POS_I_PWM, vref, ADC_GAIN_I_PWM);
pwm_current_offset = -adc_scaled(ADC_POS_I_PWM, vref, ADC_GAIN(I_PWM));
#endif
load_current_offset = -adc_scaled(ADC_POS_I_LOAD, vref, ADC_GAIN_I_LOAD);
load_current_offset = -adc_scaled(ADC_POS_I_LOAD, vref, ADC_GAIN(I_LOAD));
}
void adc_update_value(unsigned int pos)
@@ -156,24 +162,24 @@ void daq_update()
int vref = VREF;
// calculate lower voltage first, as it is needed for PWM terminal voltage calculation
lv_bus.voltage = adc_scaled(ADC_POS_V_LOW, vref, ADC_GAIN_V_LOW);
lv_bus.voltage = adc_scaled(ADC_POS_V_LOW, vref, ADC_GAIN(V_LOW));
#if DT_COMPAT_DCDC
hv_bus.voltage = adc_scaled(ADC_POS_V_HIGH, vref, ADC_GAIN_V_HIGH);
hv_bus.voltage = adc_scaled(ADC_POS_V_HIGH, vref, ADC_GAIN(V_HIGH));
#endif
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
pwm_switch.ext_voltage = lv_bus.voltage - vref * (ADC_OFFSET_V_PWM / 1000.0) -
adc_scaled(ADC_POS_V_PWM, vref, ADC_GAIN_V_PWM);
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);
#endif
load.current = adc_scaled(ADC_POS_I_LOAD, vref, ADC_GAIN_I_LOAD) + load_current_offset;
load.current = adc_scaled(ADC_POS_I_LOAD, vref, ADC_GAIN(I_LOAD)) + load_current_offset;
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power
// calculation
pwm_switch.current = -pwm_switch.get_duty_cycle() * (
adc_scaled(ADC_POS_I_PWM, vref, ADC_GAIN_I_PWM) + pwm_current_offset);
adc_scaled(ADC_POS_I_PWM, vref, ADC_GAIN(I_PWM)) + pwm_current_offset);
lv_terminal.current = -pwm_switch.current - load.current;
@@ -182,7 +188,7 @@ void daq_update()
#if DT_COMPAT_DCDC
dcdc_lv_port.current =
adc_scaled(ADC_POS_I_DCDC, vref, ADC_GAIN_I_DCDC) + dcdc_current_offset;
adc_scaled(ADC_POS_I_DCDC, vref, ADC_GAIN(I_DCDC)) + dcdc_current_offset;
lv_terminal.current = dcdc_lv_port.current - load.current;
@@ -277,7 +283,7 @@ uint16_t adc_get_alert_limit(float scale, float limit)
void daq_set_lv_alerts(float upper, float lower)
{
int vref = VREF;
float scale = ((4096* 1000) / (ADC_GAIN_V_LOW)) / vref;
float scale = ((4096 * 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);
@@ -295,10 +301,14 @@ void daq_set_lv_alerts(float upper, float lower)
void prepare_adc_readings(AdcValues values)
{
adc_readings[ADC_POS_VREF_MCU] = (uint16_t)(1.224 / 3.3 * 4096) << 4;
adc_readings[ADC_POS_V_HIGH] = (uint16_t)((values.solar_voltage / (ADC_GAIN_V_HIGH)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_V_LOW] = (uint16_t)((values.battery_voltage / (ADC_GAIN_V_LOW)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_I_DCDC] = (uint16_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_I_LOAD] = (uint16_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_V_HIGH] =
(uint16_t)((values.solar_voltage / ADC_GAIN(V_HIGH)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_V_LOW] =
(uint16_t)((values.battery_voltage / ADC_GAIN(V_LOW)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_I_DCDC] =
(uint16_t)((values.dcdc_current / ADC_GAIN(I_DCDC)) / 3.3 * 4096) << 4;
adc_readings[ADC_POS_I_LOAD] =
(uint16_t)((values.load_current / ADC_GAIN(I_LOAD)) / 3.3 * 4096) << 4;
}
void prepare_adc_filtered()
+15 -8
View File
@@ -34,7 +34,7 @@
#define DT_INST_0_DCDC_CURRENT_MAX 20 // A
#define DT_OUTPUTS_PWM_SWITCH_PRESENT 1
#define DT_OUTPUTS_PWM_SWITCH_PWMS_PERIOD 20*1000*1000
#define DT_OUTPUTS_PWM_SWITCH_PWMS_PERIOD (20*1000*1000)
#define DT_OUTPUTS_PWM_SWITCH_CURRENT_MAX 20 // A
#define DT_OUTPUTS_LOAD_PRESENT 1
@@ -98,14 +98,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 ADC_GAIN_V_LOW (105.6 / 5.6) // both voltage dividers: 100k + 5.6k
#define ADC_GAIN_V_HIGH (105.6 / 5.6)
#define ADC_GAIN_V_PWM (105.6 / 5.6)
#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm
#define ADC_GAIN_I_DCDC (1000 / 4 / 50)
#define ADC_GAIN_I_PWM (1000 / 4 / 50)
#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 ADC_OFFSET_V_PWM (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset
// 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)
// position in the array written by the DMA controller
enum {
+43
View File
@@ -0,0 +1,43 @@
# 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 a gain (voltage divider or amplifier) at an ADC input
compatible: "adc-gain"
properties:
io-channels:
type: phandle-array
required: true
description: |
Channels available with this divider configuration.
numerator:
type: int
required: true
description: |
Resistance of the lower leg of the voltage divider
denominator:
type: int
required: true
description: |
Resistance of the full path through the voltage divider.
raw-offset:
type: int
required: false
description: |
Offset to be substracted from ADC raw reading before applying the gain
enable-gpios:
type: phandle-array
required: false
description: |
Enable the voltage divider input or amplifier.
If present the corresponding GPIO must be set to an active level
to enable the divider input.
+15
View File
@@ -51,4 +51,19 @@
/delete-node/ boot0;
};
i-load {
compatible = "adc-gain";
io-channels = <&adc1 6>;
// amp gain: 50, resistor: 4 mOhm
numerator = <1000>;
denominator = <200>; // 4*50
};
i-dcdc {
compatible = "adc-gain";
io-channels = <&adc1 7>;
// amp gain: 50, resistor: 4 mOhm
numerator = <1000>;
denominator = <200>; // 4*50
};
};
+8
View File
@@ -9,4 +9,12 @@
version-str = "v0.2";
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
};
};