DC/DC and PWM switch configuration via DTS

As these settings are clearly hardware-specific, configuration was
moved from Kconfig (hidden symbols) to DTS. New bindings added.
This commit is contained in:
Martin Jäger
2020-05-06 12:53:26 +02:00
parent 64b771cd42
commit ab504e3dfa
31 changed files with 220 additions and 188 deletions
+16 -12
View File
@@ -6,6 +6,10 @@
#include "daq.h"
#ifndef UNIT_TEST
#include <zephyr.h>
#endif
#include <math.h> // log for thermistor calculation
#include <assert.h>
@@ -14,10 +18,10 @@
#include "debug.h"
#include "board.h" // contains defines for pins
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
static float dcdc_current_offset;
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
static float pwm_current_offset;
#endif
static float load_current_offset;
@@ -88,10 +92,10 @@ static inline float ntc_temp(uint32_t channel, int32_t vref)
void calibrate_current_sensors()
{
int vref = VREF;
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
dcdc_current_offset = -adc_scaled(ADC_POS_I_DCDC, vref, ADC_GAIN_I_DCDC);
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
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);
@@ -103,7 +107,7 @@ void adc_update_value(unsigned int pos)
// y(n) = c * x(n) + (c - 1) * y(n-1)
// see also here: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf
#if CONFIG_HAS_PWM_SWITCH == 1
#if DT_COMPAT_PWM_SWITCH
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) {
@@ -154,18 +158,18 @@ void daq_update()
// 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);
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
hv_bus.voltage = adc_scaled(ADC_POS_V_HIGH, vref, ADC_GAIN_V_HIGH);
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
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);
#endif
load.current = adc_scaled(ADC_POS_I_LOAD, vref, ADC_GAIN_I_LOAD) + load_current_offset;
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
// 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() * (
@@ -176,7 +180,7 @@ void daq_update()
pwm_switch.power = pwm_switch.bus->voltage * pwm_switch.current;
#endif
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
dcdc_lv_port.current =
adc_scaled(ADC_POS_I_DCDC, vref, ADC_GAIN_I_DCDC) + dcdc_current_offset;
@@ -206,7 +210,7 @@ void daq_update()
}
#endif
#ifdef PIN_ADC_TEMP_FETS
#if DT_COMPAT_DCDC && defined(PIN_ADC_TEMP_FETS)
// MOSFET temperature calculation
dcdc.temp_mosfets = ntc_temp(ADC_POS_TEMP_FETS, vref);
#endif
@@ -229,10 +233,10 @@ void daq_update()
void high_voltage_alert()
{
// disable any sort of input
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
dcdc.emergency_stop();
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
pwm_switch.emergency_stop();
#endif
// do not use enter_state function, as we don't want to wait entire recharge delay
+9 -5
View File
@@ -4,6 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef UNIT_TEST
#include <zephyr.h>
#endif
#include <stdio.h>
#include "setup.h"
@@ -190,12 +194,12 @@ const data_object_t data_objects[] = {
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#endif
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
TS_DATA_OBJ_BOOL(0x62, "DcdcEn", &dcdc.enable,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
TS_DATA_OBJ_BOOL(0x63, "PwmEn", &pwm_switch.enable,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#endif
@@ -247,7 +251,7 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_FLOAT(0x76, "Int_degC", &dev_stat.internal_temp, 1,
TS_OUTPUT, TS_READ_ALL),
#if defined(PIN_ADC_TEMP_FETS) && CONFIG_HAS_DCDC_CONVERTER
#if defined(PIN_ADC_TEMP_FETS) && DT_COMPAT_DCDC
TS_DATA_OBJ_FLOAT(0x77, "Mosfet_degC", &dcdc.temp_mosfets, 1,
TS_OUTPUT, TS_READ_ALL),
#endif
@@ -255,7 +259,7 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_UINT32(0x78, "ChgState", &charger.state,
TS_OUTPUT, TS_READ_ALL),
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
TS_DATA_OBJ_UINT16(0x79, "DCDCState", &dcdc.state,
TS_OUTPUT, TS_READ_ALL),
#endif
@@ -378,7 +382,7 @@ const data_object_t data_objects[] = {
// CALIBRATION DATA ///////////////////////////////////////////////////////
// using IDs >= 0xD0
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
TS_DATA_OBJ_FLOAT(0xD0, "DcdcMin_W", &dcdc.output_power_min, 1,
TS_CAL, TS_READ_ALL | TS_WRITE_MAKER),
+13 -8
View File
@@ -6,6 +6,10 @@
#include "dcdc.h"
#ifndef UNIT_TEST
#include <zephyr.h>
#endif
#include <math.h> // for fabs function
#include <stdlib.h> // for min/max function
#include <stdio.h>
@@ -19,7 +23,7 @@
extern DeviceStatus dev_stat;
#if CONFIG_HAS_DCDC_CONVERTER == 0
#if !DT_COMPAT_DCDC
Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) {}
@@ -32,11 +36,11 @@ Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode)
mode = op_mode;
enable = true;
state = DCDC_STATE_OFF;
lvs->neg_current_limit = -DCDC_CURRENT_MAX;
lvs->pos_current_limit = DCDC_CURRENT_MAX;
ls_current_max = DCDC_CURRENT_MAX;
hs_voltage_max = HIGH_SIDE_VOLTAGE_MAX;
ls_voltage_max = LOW_SIDE_VOLTAGE_MAX;
lvs->neg_current_limit = -DT_INST_0_DCDC_CURRENT_MAX;
lvs->pos_current_limit = DT_INST_0_DCDC_CURRENT_MAX;
ls_current_max = DT_INST_0_DCDC_CURRENT_MAX;
hs_voltage_max = DT_INST_0_CHARGE_CONTROLLER_HS_VOLTAGE_MAX;
ls_voltage_max = DT_INST_0_CHARGE_CONTROLLER_LS_VOLTAGE_MAX;
ls_voltage_min = 9.0;
output_power_min = 1; // switch off if power < 1 W
restart_interval = 60;
@@ -44,7 +48,8 @@ Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode)
pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction
// lower duty limit might have to be adjusted dynamically depending on LS voltage
half_bridge_init(PWM_FREQUENCY, PWM_DEADTIME, 12 / hs_voltage_max, 0.97);
half_bridge_init(DT_INST_0_DCDC_PWM_FREQUENCY / 1000, DT_INST_0_DCDC_PWM_DEADTIME,
12 / hs_voltage_max, 0.97);
}
int Dcdc::duty_cycle_delta()
@@ -283,4 +288,4 @@ void Dcdc::fuse_destruction()
counter++;
}
#endif /* CONFIG_HAS_DCDC_CONVERTER */
#endif /* DT_COMPAT_DCDC */
+1 -1
View File
@@ -84,7 +84,7 @@ void DeviceStatus::update_min_max_values()
}
#endif
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
if (dcdc.lvs->current > dcdc_current_max) {
dcdc_current_max = dcdc.lvs->current;
}
+2 -2
View File
@@ -253,9 +253,9 @@ uint32_t PWM_UT::m_ccr = 0;
#endif /* UNIT_TEST */
#if (PWM_TIM == 1)
#if defined(STM32F0) || defined(STM32G4)
typedef PWM_TIM1 PWM_TIM_HW;
#elif (PWM_TIM == 3)
#elif defined(STM32L0)
typedef PWM_TIM3 PWM_TIM_HW;
#else
typedef PWM_UT PWM_TIM_HW;
+13 -4
View File
@@ -6,6 +6,10 @@
#include "load.h"
#ifndef UNIT_TEST
#include <zephyr.h>
#endif
#include "board.h"
#include "leds.h"
#include "device_status.h"
@@ -45,7 +49,9 @@ void LoadOutput::control()
(CONFIG_MOSFET_MAX_JUNCTION_TEMP - CONFIG_INTERNAL_MAX_REFERENCE_TEMP)
) / (CONFIG_MOSFET_THERMAL_TIME_CONSTANT * CONFIG_CONTROL_FREQUENCY);
if (junction_temperature > CONFIG_MOSFET_MAX_JUNCTION_TEMP || current > LOAD_CURRENT_MAX * 2) {
if (junction_temperature > CONFIG_MOSFET_MAX_JUNCTION_TEMP ||
current > LOAD_CURRENT_MAX * 2)
{
flags_set(&error_flags, ERR_LOAD_OVERCURRENT);
oc_timestamp = uptime();
}
@@ -62,9 +68,12 @@ void LoadOutput::control()
// long-term overvoltage (overvoltage transients are detected as an ADC alert and switch
// off the solar input instead of the load output)
if (bus->voltage > overvoltage || bus->voltage > LOW_SIDE_VOLTAGE_MAX) {
if (bus->voltage > overvoltage || bus->voltage >
DT_INST_0_CHARGE_CONTROLLER_LS_VOLTAGE_MAX)
{
ov_debounce_counter++;
if (ov_debounce_counter > CONFIG_CONTROL_FREQUENCY) { // waited 1s before setting the flag
if (ov_debounce_counter > CONFIG_CONTROL_FREQUENCY) {
// waited 1s before setting the flag
flags_set(&error_flags, ERR_LOAD_OVERVOLTAGE);
}
}
@@ -99,7 +108,7 @@ void LoadOutput::control()
if (flags_check(&error_flags, ERR_LOAD_OVERVOLTAGE) &&
bus->voltage < (overvoltage - ov_hysteresis) &&
bus->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis))
bus->voltage < (DT_INST_0_CHARGE_CONTROLLER_LS_VOLTAGE_MAX - ov_hysteresis))
{
flags_clear(&error_flags, ERR_LOAD_OVERVOLTAGE);
}
+5 -5
View File
@@ -99,12 +99,12 @@ void control_thread()
lv_terminal.update_bus_current_margins();
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
//pwm_switch.control();
leds_set_charging(pwm_switch.active());
#endif
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
hv_terminal.update_bus_current_margins();
dcdc.control(); // control of DC/DC including MPPT algorithm
leds_set_charging(half_bridge_enabled());
@@ -123,11 +123,11 @@ void control_thread()
last_call = now;
// energy + soc calculation must be called exactly once per second
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
hv_terminal.energy_balance();
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
pwm_switch.energy_balance();
#endif
@@ -137,7 +137,7 @@ void control_thread()
dev_stat.update_min_max_values();
charger.update_soc(&bat_conf);
#if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && CONFIG_HAS_DCDC_CONVERTER
#if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && DT_COMPAT_DCDC
if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) {
dcdc.fuse_destruction();
}
+9 -5
View File
@@ -6,6 +6,10 @@
#include "pwm_switch.h"
#ifndef UNIT_TEST
#include <zephyr.h>
#endif
#include <stdio.h>
#include <time.h>
@@ -14,7 +18,7 @@
#include "debug.h"
#include "helper.h"
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
static bool _pwm_active;
@@ -160,7 +164,7 @@ PwmSwitch::PwmSwitch(DcBus *dc_bus) :
offset_voltage_start = 2.0; // V charging switched on if Vsolar > Vbat + offset
restart_interval = 60; // s When should we retry to start charging after low solar power cut-off?
pwm_signal_init_registers(PWM_FREQUENCY);
pwm_signal_init_registers(DT_INST_0_PWM_SWITCH_PWM_FREQUENCY);
enable = true; // enable charging in general
_pwm_active = false; // still disable actual switch
@@ -179,8 +183,8 @@ void PwmSwitch::control()
print_info("PWM charger stop.\n");
}
else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target
|| current < neg_current_limit // port current limit exceeded
|| current < -PWM_CURRENT_MAX) // PCB current limit exceeded
|| current < neg_current_limit // port current limit exceeded
|| current < -DT_INST_0_PWM_SWITCH_CURRENT_MAX) // PCB current limit exceeded
{
// decrease power, as limits were reached
@@ -238,4 +242,4 @@ float PwmSwitch::get_duty_cycle()
return pwm_signal_get_duty_cycle();
}
#endif /* CONFIG_HAS_PWM_SWITCH */
#endif /* DT_COMPAT_PWM_SWITCH */
+6 -2
View File
@@ -9,6 +9,10 @@
* @brief Setup of ports and other essential charge controller objects
*/
#ifndef UNIT_TEST
#include <zephyr.h>
#endif
#include "thingset.h" // handles access to internal data via communication interfaces
#include "board.h" // hardware-specific settings
@@ -28,7 +32,7 @@
DcBus lv_bus;
PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT)
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
DcBus hv_bus;
PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT)
PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter
@@ -41,7 +45,7 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK);
#endif // CONFIG_HV_TERMINAL
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
PwmSwitch pwm_switch(&lv_bus);
#endif
+6 -2
View File
@@ -4,6 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef UNIT_TEST
#include <zephyr.h>
#endif
#include <stdio.h>
#include "bat_charger.h"
@@ -17,14 +21,14 @@
extern DcBus lv_bus;
extern PowerPort lv_terminal;
#if CONFIG_HAS_DCDC_CONVERTER
#if DT_COMPAT_DCDC
extern DcBus hv_bus;
extern PowerPort hv_terminal;
extern PowerPort dcdc_lv_port;
extern Dcdc dcdc;
#endif
#if CONFIG_HAS_PWM_SWITCH
#if DT_COMPAT_PWM_SWITCH
extern PwmSwitch pwm_switch;
#endif