Move ADC peripheral setup to devicetree

Remove the ADC channel configuration from the board.h files and use new
DT macros to configure the ADC peripheral based on board.dts only.
This commit is contained in:
Martin Jäger
2020-07-20 09:07:31 +02:00
parent 1b6c56d25b
commit d283436747
21 changed files with 337 additions and 261 deletions
+1 -62
View File
@@ -35,7 +35,7 @@ extern const char *led_ports[NUM_LED_PINS];
extern const int led_pins[NUM_LED_PINS];
#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4
const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
{ PIN_HIGH, PIN_LOW, PIN_LOW, PIN_LOW, PIN_LOW }, // LED1
{ PIN_LOW, PIN_HIGH, PIN_LOW, PIN_LOW, PIN_LOW }, // LED2
{ PIN_LOW, PIN_LOW, PIN_HIGH, PIN_LOW, PIN_LOW }, // LED3
@@ -52,70 +52,9 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
};
#endif
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4
#define PIN_ADC_TEMP_FETS PA_5
#else
#define PIN_ADC_TEMP_BAT
#endif
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
// position in the array written by the DMA controller
#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4
enum {
ADC_POS_V_LOW, // ADC 0 (PA_0)
ADC_POS_V_HIGH, // ADC 1 (PA_1)
ADC_POS_TEMP_FETS, // ADC 5 (PA_5)
ADC_POS_I_LOAD, // ADC 6 (PA_6)
ADC_POS_I_DCDC, // ADC 7 (PA_7)
#if defined(CONFIG_SOC_SERIES_STM32F0X)
ADC_POS_TEMP_MCU, // ADC 16
ADC_POS_VREF_MCU, // ADC 17
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
ADC_POS_VREF_MCU, // ADC 17
ADC_POS_TEMP_MCU, // ADC 18
#endif
NUM_ADC_CH // trick to get the number of elements
};
#else
enum {
ADC_POS_V_LOW, // ADC 0 (PA_0)
ADC_POS_V_HIGH, // ADC 1 (PA_1)
ADC_POS_I_LOAD, // ADC 5 (PA_5)
ADC_POS_I_DCDC, // ADC 6 (PA_6)
ADC_POS_TEMP_BAT, // ADC 7 (PA_7)
ADC_POS_VREF_MCU, // ADC 17
ADC_POS_TEMP_MCU, // ADC 18
NUM_ADC_CH // trick to get the number of elements
};
#endif
#define NUM_ADC_1_CH NUM_ADC_CH
// selected ADC channels (has to match with above enum)
#if defined(CONFIG_SOC_SERIES_STM32F0X)
#define ADC_CHSEL ( \
ADC_CHSELR_CHSEL0 | \
ADC_CHSELR_CHSEL1 | \
ADC_CHSELR_CHSEL5 | \
ADC_CHSELR_CHSEL6 | \
ADC_CHSELR_CHSEL7 | \
ADC_CHSELR_CHSEL16 | \
ADC_CHSELR_CHSEL17 \
)
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
#define ADC_CHSEL ( \
ADC_CHSELR_CHSEL0 | \
ADC_CHSELR_CHSEL1 | \
ADC_CHSELR_CHSEL5 | \
ADC_CHSELR_CHSEL6 | \
ADC_CHSELR_CHSEL7 | \
ADC_CHSELR_CHSEL17 | \
ADC_CHSELR_CHSEL18 \
)
#endif
#endif
@@ -98,6 +98,24 @@
multiplier = <1000>;
divider = <150>; // 3*50
};
temp-bat {
io-channels = <&adc1 7>;
multiplier = <1>;
divider = <1>;
};
vref-mcu {
io-channels = <&adc1 17>;
multiplier = <1>;
divider = <1>;
};
temp-mcu {
io-channels = <&adc1 18>;
multiplier = <1>;
divider = <1>;
};
};
aliases {
+3 -3
View File
@@ -28,12 +28,12 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
};
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
#define PIN_ADC_TEMP_FETS PA_5
//#define PIN_ADC_TEMP_FETS PA_5
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
/*
// position in the array written by the DMA controller
enum {
// ADC1
@@ -70,5 +70,5 @@ static const uint32_t adc_2_sequence[] = {
LL_ADC_CHANNEL_2, // I_LOAD
LL_ADC_CHANNEL_5, // I_PWM
};
*/
#endif
@@ -78,6 +78,12 @@
divider = <2200>;
};
hv-ext-sense {
io-channels = <&adc1 11>;
multiplier = <1>;
divider = <1>;
};
v-pwm {
io-channels = <&adc1 4>;
multiplier = <25224>; // (12*8.2 + 120*8.2 + 120*12) * 10
@@ -85,6 +91,24 @@
offset = <2338>; // 4096 / (1 + 8.2/120 + 8.2/12)
};
temp-fets {
io-channels = <&adc1 6>;
multiplier = <1>;
divider = <1>;
};
vref-mcu {
io-channels = <&adc1 18>;
multiplier = <1>;
divider = <1>;
};
temp-mcu {
io-channels = <&adc1 16>;
multiplier = <1>;
divider = <1>;
};
i-dcdc {
io-channels = <&adc2 1>;
// amp gain: 20, resistor: 2 mOhm
-33
View File
@@ -29,41 +29,8 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
{ PIN_LOW, PIN_HIGH } // LED2
};
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
#define PIN_ADC_TEMP_BAT PA_0
#define PIN_ADC_TEMP_FETS PA_1
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
// position in the array written by the DMA controller
enum {
ADC_POS_TEMP_BAT, // ADC 0 (PA_0)
ADC_POS_TEMP_FETS, // ADC 1 (PA_1)
ADC_POS_V_REF, // ADC 5 (PA_5)
ADC_POS_V_LOW, // ADC 6 (PA_6)
ADC_POS_V_HIGH, // ADC 7 (PA_7)
ADC_POS_I_LOAD, // ADC 8 (PB_0)
ADC_POS_I_DCDC, // ADC 9 (PB_1)
ADC_POS_TEMP_MCU, // ADC 16
ADC_POS_VREF_MCU, // ADC 17
NUM_ADC_CH // trick to get the number of enums
};
#define NUM_ADC_1_CH NUM_ADC_CH
// selected ADC channels (has to match with above enum)
#define ADC_CHSEL ( \
ADC_CHSELR_CHSEL0 | \
ADC_CHSELR_CHSEL1 | \
ADC_CHSELR_CHSEL5 | \
ADC_CHSELR_CHSEL6 | \
ADC_CHSELR_CHSEL7 | \
ADC_CHSELR_CHSEL8 | \
ADC_CHSELR_CHSEL9 | \
ADC_CHSELR_CHSEL16 | \
ADC_CHSELR_CHSEL17 \
)
#endif
+34 -4
View File
@@ -76,31 +76,61 @@
adc-inputs {
compatible = "adc-inputs";
v-low {
temp-bat {
io-channels = <&adc1 0>;
multiplier = <1>;
divider = <1>;
};
temp-fets {
io-channels = <&adc1 1>;
multiplier = <1>;
divider = <1>;
};
vref {
io-channels = <&adc1 5>;
multiplier = <1>;
divider = <1>;
};
v-low {
io-channels = <&adc1 6>;
multiplier = <110000>;
divider = <10000>;
};
v-high {
io-channels = <&adc1 1>;
io-channels = <&adc1 7>;
multiplier = <105600>;
divider = <5600>;
};
i-load {
io-channels = <&adc1 5>;
io-channels = <&adc1 8>;
// amp gain: 150/2.2, resistor: 2 mOhm
multiplier = <2200>; // 1000*2.2
divider = <300>; // 2*150
};
i-dcdc {
io-channels = <&adc1 6>;
io-channels = <&adc1 9>;
// amp gain: 150/2.2, resistor: 2 mOhm
multiplier = <2200>; // 1000*2.2
divider = <300>; // 2*150
};
temp-mcu {
io-channels = <&adc1 16>;
multiplier = <1>;
divider = <1>;
};
vref-mcu {
io-channels = <&adc1 17>;
multiplier = <1>;
divider = <1>;
};
};
aliases {
-35
View File
@@ -27,43 +27,8 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
{ PIN_HIGH }, // LED1
};
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
#define PIN_ADC_TEMP_FETS PA_1
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
// position in the array written by the DMA controller
enum {
// ADC1
ADC_POS_V_LOW,
ADC_POS_V_HIGH,
ADC_POS_V_SC_SENSE,
ADC_POS_TEMP_FETS,
ADC_POS_VREF_MCU,
ADC_POS_TEMP_MCU,
// ADC2
ADC_POS_I_DCDC,
// trick to get the total number of elements
NUM_ADC_CH
};
// selected ADC channels (has to match with below arrays)
#define NUM_ADC_1_CH 6
#define NUM_ADC_2_CH 1
static const uint32_t adc_1_sequence[] = {
LL_ADC_CHANNEL_12, // V_LOW
LL_ADC_CHANNEL_15, // V_HIGH
LL_ADC_CHANNEL_11, // V_SC_SENSE
LL_ADC_CHANNEL_2, // TEMP_FETS
LL_ADC_CHANNEL_VREFINT, // VREF_MCU
LL_ADC_CHANNEL_TEMPSENSOR_ADC1, // TEMP_MCU
};
static const uint32_t adc_2_sequence[] = {
LL_ADC_CHANNEL_1, // I_DCDC
};
#endif
+24
View File
@@ -59,6 +59,30 @@
divider = <2200>;
};
hv-ext-sense {
io-channels = <&adc1 11>;
multiplier = <1>;
divider = <1>;
};
temp-fets {
io-channels = <&adc1 2>;
multiplier = <1>;
divider = <1>;
};
vref-mcu {
io-channels = <&adc1 18>;
multiplier = <1>;
divider = <1>;
};
temp-mcu {
io-channels = <&adc1 16>;
multiplier = <1>;
divider = <1>;
};
i-dcdc {
io-channels = <&adc2 1>;
// amp gain: 68/2.2, resistor: 2 mOhm
-28
View File
@@ -48,36 +48,8 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
};
#endif
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
#define PIN_ADC_TEMP_BAT
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 8200.0
// position in the array written by the DMA controller
enum {
ADC_POS_V_LOW, // ADC 0 (PA_0)
ADC_POS_V_PWM, // ADC 1 (PA_1)
ADC_POS_I_LOAD, // ADC 5 (PA_5)
ADC_POS_I_PWM, // ADC 6 (PA_6)
ADC_POS_TEMP_BAT, // ADC 7 (PA_7)
ADC_POS_VREF_MCU, // ADC 17
ADC_POS_TEMP_MCU, // ADC 18
NUM_ADC_CH // trick to get the number of elements
};
#define NUM_ADC_1_CH NUM_ADC_CH
// selected ADC channels (has to match with above enum)
#define ADC_CHSEL ( \
ADC_CHSELR_CHSEL0 | \
ADC_CHSELR_CHSEL1 | \
ADC_CHSELR_CHSEL5 | \
ADC_CHSELR_CHSEL6 | \
ADC_CHSELR_CHSEL7 | \
ADC_CHSELR_CHSEL17 | \
ADC_CHSELR_CHSEL18 \
)
#endif
+18
View File
@@ -105,6 +105,24 @@
multiplier = <2200>; // 1000*2.2
divider = <136>; // 2*68
};
temp-bat {
io-channels = <&adc1 7>;
multiplier = <1>;
divider = <1>;
};
vref-mcu {
io-channels = <&adc1 17>;
multiplier = <1>;
divider = <1>;
};
temp-mcu {
io-channels = <&adc1 18>;
multiplier = <1>;
divider = <1>;
};
};
aliases {
+33 -33
View File
@@ -48,7 +48,7 @@ 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 board.h
* @param channel valid ADC channel position using ADC_POS() macro
* @param vref reference voltage in millivolts
* @param offset offset substracted from raw ADC value before applying gain
*
@@ -62,7 +62,7 @@ static inline float adc_voltage(uint32_t channel, int32_t vref, int32_t offset =
/**
* Measured current/voltage for ADC channel after average and scaling
*
* @param channel valid ADC channel pos ADC_POS_..., see board.h
* @param channel valid ADC channel position using ADC_POS() macro
* @param vref reference voltage in millivolts
* @param offset offset added to raw ADC value before applying gain
*
@@ -89,13 +89,13 @@ 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
#if DT_OUTPUTS_LOAD_PRESENT
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));
#endif
}
@@ -106,7 +106,7 @@ void adc_update_value(unsigned int pos)
// see also here: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
if (pos == ADC_POS_V_PWM || pos == ADC_POS_I_PWM) {
if (pos == ADC_POS(v_pwm) || pos == ADC_POS(i_pwm)) {
// only read input voltage and current when switch is on or permanently off
if (pwm_switch.signal_high() || pwm_switch.active() == false) {
adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST);
@@ -154,19 +154,19 @@ 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 -
adc_scaled(ADC_POS_V_PWM, vref, ADC_GAIN(V_PWM), DT_ADC_INPUTS_V_PWM_OFFSET);
adc_scaled(ADC_POS(v_pwm), vref, ADC_GAIN(v_pwm), ADC_OFFSET(v_pwm));
#endif
#if DT_OUTPUTS_LOAD_PRESENT
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;
float load_current = load.current;
#else
float load_current = 0; // value used below, so we still need to define the variable
@@ -176,7 +176,7 @@ void daq_update()
// 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;
@@ -185,7 +185,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;
@@ -200,9 +200,9 @@ void daq_update()
load.power = load.bus->voltage * load.current;
#endif
#ifdef PIN_ADC_TEMP_BAT
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_bat))
// battery temperature calculation
float bat_temp = ntc_temp(ADC_POS_TEMP_BAT, vref);
float bat_temp = ntc_temp(ADC_POS(temp_bat), vref);
if (bat_temp > -50) {
// external sensor connected: take measured value
@@ -216,13 +216,13 @@ void daq_update()
}
#endif
#if DT_COMPAT_DCDC && defined(PIN_ADC_TEMP_FETS)
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_fets))
// MOSFET temperature calculation
dcdc.temp_mosfets = ntc_temp(ADC_POS_TEMP_FETS, vref);
dcdc.temp_mosfets = ntc_temp(ADC_POS(temp_fets), vref);
#endif
// internal MCU temperature
uint16_t adcval = adc_value(ADC_POS_TEMP_MCU) * vref / VREFINT_VALUE;
uint16_t adcval = adc_value(ADC_POS(temp_mcu)) * vref / VREFINT_VALUE;
dev_stat.internal_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) /
(TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE;
@@ -251,7 +251,7 @@ void high_voltage_alert()
dev_stat.set_error(ERR_BAT_OVERVOLTAGE);
print_error("High voltage alert, ADC reading: %d limit: %d\n",
adc_readings[ADC_POS_V_LOW], adc_alerts_upper[ADC_POS_V_LOW].limit);
adc_readings[ADC_POS(v_low)], adc_alerts_upper[ADC_POS(v_low)].limit);
}
void low_voltage_alert()
@@ -262,7 +262,7 @@ void low_voltage_alert()
#endif
print_error("Low voltage alert, ADC reading: %d limit: %d\n",
adc_readings[ADC_POS_V_LOW], adc_alerts_lower[ADC_POS_V_LOW].limit);
adc_readings[ADC_POS(v_low)], adc_alerts_lower[ADC_POS(v_low)].limit);
}
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms)
@@ -285,15 +285,15 @@ 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);
adc_alerts_upper[ADC_POS_V_LOW].callback = high_voltage_alert;
adc_alerts_upper[ADC_POS(v_low)].limit = adc_get_alert_limit(scale, upper);
adc_alerts_upper[ADC_POS(v_low)].callback = high_voltage_alert;
// LV side (battery) undervoltage alert
adc_alerts_lower[ADC_POS_V_LOW].limit = adc_get_alert_limit(scale, lower);
adc_alerts_lower[ADC_POS_V_LOW].callback = low_voltage_alert;
adc_alerts_lower[ADC_POS(v_low)].limit = adc_get_alert_limit(scale, lower);
adc_alerts_lower[ADC_POS(v_low)].callback = low_voltage_alert;
}
#if defined(UNIT_TEST)
@@ -302,15 +302,15 @@ 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(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;
}
void prepare_adc_filtered()
+27 -2
View File
@@ -20,6 +20,8 @@
extern "C" {
#endif
#include <zephyr.h>
#include <stdint.h>
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
@@ -31,10 +33,33 @@ extern "C" {
#define VREF (3300)
#else
// internal STM reference voltage
#define VREF (VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU))
#define VREF (VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS(vref_mcu)))
#endif
#define ADC_GAIN(name) ((float)DT_ADC_INPUTS_##name##_MULTIPLIER / DT_ADC_INPUTS_##name##_DIVIDER)
#define ADC_GAIN(name) ((float)DT_PROP(DT_CHILD(DT_PATH(adc_inputs), name), multiplier) / \
DT_PROP(DT_CHILD(DT_PATH(adc_inputs), name), divider))
#define ADC_OFFSET(name) (DT_PROP(DT_CHILD(DT_PATH(adc_inputs), name), offset))
/*
* Find out the position in the ADC reading array for a channel identified by its Devicetree node
*/
#define ADC_POS(node) DT_N_S_adc_inputs_S_##node##_ADC_POS
/*
* Creates a unique name for below enum
*/
#define ADC_ENUM(node) node##_ADC_POS,
/*
* Enum for numbering of ADC channels as they are written by the DMA controller
*
* The channels must be specified in ascending order in the board.dts file.
*/
enum {
DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_ENUM)
NUM_ADC_CH // trick to get the number of elements
};
/**
* Struct to definie upper and lower limit alerts for any ADC channel
+74 -15
View File
@@ -38,7 +38,56 @@
#include "board.h"
#include "debug.h"
#if !defined(CONFIG_SOC_SERIES_STM32F0X) && !defined(CONFIG_SOC_SERIES_STM32L0X)
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X)
// automatich 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)
int num_adc1_ch = NUM_ADC_CH;
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
// create array with selected channel for each ADC input
#define ADC_CH_NUM_(node_id) DT_PHA(node_id, io_channels, input),
static const uint32_t adc_ch_numbers[] = {
DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_CH_NUM_)
};
// create array with ADC instance register address for each ADC input
#define ADC_REG_(node_id) DT_REG_ADDR_BY_IDX(DT_PHANDLE(node_id, io_channels), 0),
static const uint32_t adc_registers[] = {
DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_REG_)
};
// number of channels per ADC peripheral will be calculated in adc_init function
int num_adc1_ch = 0;
int num_adc2_ch = 0;
// Depending on the channel, not just a single bit has to be set in the right location. So we need
// a map to get correct bit settings for each channel.
static const uint32_t table_channel[] = {
LL_ADC_CHANNEL_0,
LL_ADC_CHANNEL_1,
LL_ADC_CHANNEL_2,
LL_ADC_CHANNEL_3,
LL_ADC_CHANNEL_4,
LL_ADC_CHANNEL_5,
LL_ADC_CHANNEL_6,
LL_ADC_CHANNEL_7,
LL_ADC_CHANNEL_8,
LL_ADC_CHANNEL_9,
LL_ADC_CHANNEL_10,
LL_ADC_CHANNEL_11,
LL_ADC_CHANNEL_12,
LL_ADC_CHANNEL_13,
LL_ADC_CHANNEL_14,
LL_ADC_CHANNEL_15,
LL_ADC_CHANNEL_TEMPSENSOR_ADC1,
LL_ADC_CHANNEL_VBAT,
LL_ADC_CHANNEL_VREFINT,
};
// Rank and sequence definitions for STM32G4 from adc_stm32.c Zephyr driver
#define RANK(n) LL_ADC_REG_RANK_##n
@@ -80,7 +129,8 @@ static const uint32_t table_seq_len[] = {
SEQ_LEN(15),
SEQ_LEN(16),
};
#endif /* !STM32F0X && !STM32L0X */
#endif /* STM32G4X */
// for ADC and DMA
extern uint16_t adc_readings[];
@@ -160,15 +210,24 @@ static void adc_init(ADC_TypeDef *adc)
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL);
LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_160CYCLES_5);
#else // Others including CONFIG_SOC_SERIES_STM32G4X
const uint32_t *adc_seq = (adc == ADC1) ? adc_1_sequence : adc_2_sequence;
int num_ch = (adc == ADC1) ? NUM_ADC_1_CH : NUM_ADC_2_CH;
for (int i = 0; i < num_ch; i++) {
LL_ADC_REG_SetSequencerRanks(adc, table_rank[i], adc_seq[i]);
LL_ADC_SetChannelSamplingTime(adc, adc_seq[i], LL_ADC_SAMPLINGTIME_47CYCLES_5);
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
// more complicated sequencer allows to define the sequence independent
// of the channel number (using ranks)
int *num_ch = (adc == ADC1) ? &num_adc1_ch : &num_adc2_ch;
for (int i = 0; i < NUM_ADC_CH; i++) {
if (adc_registers[i] == (uint32_t)adc) {
uint32_t ch_number = adc_ch_numbers[i];
if (ch_number < sizeof(table_channel)) {
LL_ADC_REG_SetSequencerRanks(adc,
table_rank[*num_ch], table_channel[ch_number]);
LL_ADC_SetChannelSamplingTime(adc,
table_channel[ch_number], LL_ADC_SAMPLINGTIME_47CYCLES_5);
(*num_ch)++;
}
}
}
LL_ADC_REG_SetSequencerLength(adc, table_seq_len[num_ch - 1]);
#endif // defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X)
LL_ADC_REG_SetSequencerLength(adc, table_seq_len[*num_ch - 1]);
#endif
LL_ADC_SetDataAlignment(adc, LL_ADC_DATA_ALIGN_LEFT);
LL_ADC_SetResolution(adc, LL_ADC_RESOLUTION_12B);
@@ -211,7 +270,7 @@ static void DMA1_Channel1_IRQHandler(void *args)
{
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1
{
for (unsigned int i = 0; i < NUM_ADC_1_CH; i++) {
for (unsigned int i = 0; i < num_adc1_ch; i++) {
adc_update_value(i);
}
}
@@ -222,7 +281,7 @@ static void DMA1_Channel1_IRQHandler(void *args)
static void DMA2_Channel1_IRQHandler(void *args)
{
if ((DMA2->ISR & DMA_ISR_TCIF1) != 0) { // Test if transfer completed on DMA channel 2
for (unsigned int i = NUM_ADC_1_CH; i < NUM_ADC_1_CH + NUM_ADC_2_CH; i++) {
for (unsigned int i = num_adc1_ch; i < num_adc1_ch + num_adc2_ch; i++) {
adc_update_value(i);
}
}
@@ -258,7 +317,7 @@ static void dma_init(DMA_TypeDef *dma)
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
// Configure the number of DMA transfers (data length in multiples of size per transfer)
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, NUM_ADC_1_CH);
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, num_adc1_ch);
}
#if defined(CONFIG_SOC_SERIES_STM32G4X)
else if (dma == DMA2) {
@@ -268,11 +327,11 @@ static void dma_init(DMA_TypeDef *dma)
LL_DMA_ConfigAddresses(dma, LL_DMA_CHANNEL_1,
LL_ADC_DMA_GetRegAddr(ADC2, LL_ADC_DMA_REG_REGULAR_DATA), // source address
// destination address = position behind ADC_1 values
(uint32_t)(&(adc_readings[NUM_ADC_1_CH])),
(uint32_t)(&(adc_readings[num_adc1_ch])),
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
// Configure the number of DMA transfers (data length in multiples of size per transfer)
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, NUM_ADC_2_CH);
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, num_adc2_ch);
}
#endif // CONFIG_SOC_SERIES_STM32G4X
+1 -1
View File
@@ -260,7 +260,7 @@ static DataNode data_nodes[] = {
TS_NODE_FLOAT(0x78, "Int_degC", &dev_stat.internal_temp, 1,
ID_OUTPUT, TS_ANY_R, 0),
#if defined(PIN_ADC_TEMP_FETS) && DT_COMPAT_DCDC
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_fets))
TS_NODE_FLOAT(0x79, "Mosfet_degC", &dcdc.temp_mosfets, 1,
ID_OUTPUT, TS_ANY_R, 0),
#endif
+2 -2
View File
@@ -53,7 +53,7 @@ void PwmSwitch::test()
}
else if (!pwm_active() && enable == true) {
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
adc_upper_alert_inhibit(ADC_POS_V_LOW, 50);
adc_upper_alert_inhibit(ADC_POS(v_low), 50);
pwm_signal_start(0.9);
print_info("PWM test mode start.\n");
}
@@ -118,7 +118,7 @@ void PwmSwitch::control()
&& enable == true)
{
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
adc_upper_alert_inhibit(ADC_POS_V_LOW, 50);
adc_upper_alert_inhibit(ADC_POS(v_low), 50);
pwm_signal_start(1);
power_good_timestamp = uptime();
print_info("PWM charger start.\n");
+1 -1
View File
@@ -72,7 +72,7 @@ static void TIM3_IRQHandler(void *args)
if ((int)LL_TIM_OC_GetCompare(tim) < _pwm_resolution) {
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms
// at each rising edge if switch is not continuously on
adc_upper_alert_inhibit(ADC_POS_V_LOW, 10);
adc_upper_alert_inhibit(ADC_POS(v_low), 10);
}
}
-32
View File
@@ -101,36 +101,4 @@ 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_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_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 {
ADC_POS_V_LOW, // ADC 0 (PA_0)
ADC_POS_V_HIGH, // ADC 1 (PA_1)
ADC_POS_V_PWM, // ADC 3 (PA_2)
ADC_POS_I_PWM, // ADC 4 (PA_4)
ADC_POS_I_LOAD, // ADC 5 (PA_5)
ADC_POS_I_DCDC, // ADC 6 (PA_6)
ADC_POS_TEMP_BAT, // ADC 7 (PA_7)
ADC_POS_VREF_MCU, // ADC 17
ADC_POS_TEMP_MCU, // ADC 18
NUM_ADC_CH // trick to get the number of elements
};
#define NUM_ADC_1_CH NUM_ADC_CH
#endif
+63
View File
@@ -0,0 +1,63 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2020 Martin Jäger / Libre Solar
*/
/*
* Generated defines (from devicetree_unfixed.h)
*/
#define DT_N_S_adc_inputs_S_temp_bat_EXISTS 1
#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_low_P_multiplier 105600
#define DT_N_S_adc_inputs_S_v_low_P_divider 5600
#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
// 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_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_pwm_P_multiplier 1000
#define DT_N_S_adc_inputs_S_i_pwm_P_divider (4 * 50)
/*
* Macros copied from devicetree.h
*/
#define DT_CAT(node_id, prop_suffix) node_id##prop_suffix
#define DT_PROP(node_id, prop) DT_CAT(node_id, _P_##prop)
#define DT_NODE_EXISTS(name) DT_CAT(name, _EXISTS)
#define DT_CHILD(node_id, child) UTIL_CAT(node_id, DT_S_PREFIX(child))
#define DT_CAT(node_id, prop_suffix) node_id##prop_suffix
#define DT_S_PREFIX(name) _S_##name
#define UTIL_CAT(a, ...) UTIL_PRIMITIVE_CAT(a, __VA_ARGS__)
#define UTIL_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__
/*
* Simplified mocks for certain macros from devicetree.h
*/
#define DT_PATH(node) DT_N_S_##node
#define DT_FOREACH_CHILD(dummy, fn) \
fn(DT_N_S_adc_inputs_S_v_low) \
fn(DT_N_S_adc_inputs_S_v_high) \
fn(DT_N_S_adc_inputs_S_v_pwm) \
fn(DT_N_S_adc_inputs_S_i_dcdc) \
fn(DT_N_S_adc_inputs_S_i_load) \
fn(DT_N_S_adc_inputs_S_i_pwm) \
fn(DT_N_S_adc_inputs_S_temp_bat) \
fn(DT_N_S_adc_inputs_S_vref_mcu) \
fn(DT_N_S_adc_inputs_S_temp_mcu)
+10 -10
View File
@@ -28,14 +28,14 @@ void check_filtering()
}
uint32_t adc_filtered_bak[NUM_ADC_CH];
adc_filtered_bak[ADC_POS_VREF_MCU] = get_adc_filtered(ADC_POS_VREF_MCU);
adc_filtered_bak[ADC_POS_V_HIGH] = get_adc_filtered(ADC_POS_V_HIGH);
adc_filtered_bak[ADC_POS(vref_mcu)] = get_adc_filtered(ADC_POS(vref_mcu));
adc_filtered_bak[ADC_POS(v_high)] = get_adc_filtered(ADC_POS(v_high));
// overwrite filtered values
prepare_adc_filtered();
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS_VREF_MCU), adc_filtered_bak[ADC_POS_VREF_MCU]);
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS_V_HIGH), adc_filtered_bak[ADC_POS_V_HIGH]);
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS(vref_mcu)), adc_filtered_bak[ADC_POS(vref_mcu)]);
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS(v_high)), adc_filtered_bak[ADC_POS(v_high)]);
}
void check_solar_terminal_readings()
@@ -75,14 +75,14 @@ void adc_alert_undervoltage_triggering()
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
daq_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
prepare_adc_filtered();
adc_update_value(ADC_POS_V_LOW);
adc_update_value(ADC_POS(v_low));
// undervoltage test
adcval.battery_voltage = bat_conf.voltage_absolute_min - 0.1;
prepare_adc_readings(adcval);
adc_update_value(ADC_POS_V_LOW);
adc_update_value(ADC_POS(v_low));
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP));
adc_update_value(ADC_POS_V_LOW);
adc_update_value(ADC_POS(v_low));
load.control();
TEST_ASSERT_EQUAL(true, flags_check(&load.error_flags, ERR_LOAD_VOLTAGE_DIP));
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load.state);
@@ -103,16 +103,16 @@ void adc_alert_overvoltage_triggering()
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
daq_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
prepare_adc_filtered();
adc_update_value(ADC_POS_V_LOW);
adc_update_value(ADC_POS(v_low));
dcdc.state = DCDC_STATE_MPPT;
// overvoltage test
adcval.battery_voltage = bat_conf.voltage_absolute_max + 0.1;
prepare_adc_readings(adcval);
adc_update_value(ADC_POS_V_LOW);
adc_update_value(ADC_POS(v_low));
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
adc_update_value(ADC_POS_V_LOW);
adc_update_value(ADC_POS(v_low));
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
TEST_ASSERT_EQUAL(false, pwm_switch.active());
TEST_ASSERT_EQUAL(DCDC_STATE_OFF, dcdc.state);
+2
View File
@@ -11,6 +11,8 @@
* for unit tests
*/
#include "devicetree_mock.h"
#ifndef __deprecated
#define __deprecated __attribute__((deprecated))
#endif
+2
View File
@@ -67,5 +67,7 @@
multiplier = <1000>;
divider = <200>; // 4*50
};
/delete-node/ temp-bat;
};
};