diff --git a/boards/Kconfig.hidden b/boards/Kconfig.hidden index 8560c0a..89779a8 100644 --- a/boards/Kconfig.hidden +++ b/boards/Kconfig.hidden @@ -14,30 +14,6 @@ config HARDWARE_VERSION help Version of the PCB -config HAS_DCDC_CONVERTER - bool - help - Board has a DC/DC converter. - -config DCDC_PWM_FREQUENCY_KHZ - depends on HAS_DCDC_CONVERTER - int - range 30 100 - help - PWM frequency of DC/DC converter in kHz, range 30 to 100. - -config DCDC_PWM_DEADTIME_NS - depends on HAS_DCDC_CONVERTER - int - range 100 300 - help - Dead time for synchronous PWM generation in nanoseconds. - -config HAS_PWM_SWITCH - bool - help - PCB has PWM power switch. - config HAS_LOAD_OUTPUT bool help @@ -53,16 +29,6 @@ config NUM_LED_PINS help Number of LED pins for charlieplexing. -config LOW_SIDE_VOLTAGE_MAX - int - help - Low side (usually battery) voltage maximum. - -config HIGH_SIDE_VOLTAGE_MAX - int - help - High side (usually solar panel) voltage maximum. - config CONTROL_FREQUENCY int default 10 diff --git a/boards/Kconfig.user b/boards/Kconfig.user index b09359d..f2e2713 100644 --- a/boards/Kconfig.user +++ b/boards/Kconfig.user @@ -7,7 +7,7 @@ choice prompt "High-voltage terminal" - depends on HAS_DCDC_CONVERTER + depends on $(dt_compat_enabled,dcdc) help Select the device connected to the high-voltage side terminal of the DC/DC converter. @@ -44,7 +44,7 @@ endchoice choice prompt "PWM terminal" - depends on HAS_PWM_SWITCH + depends on $(dt_compat_enabled,pwm-switch) help Select the device connected to the PWM switch (currently only solar panel possible) diff --git a/boards/arm/mppt_1210_hus_0v4/Kconfig.defconfig b/boards/arm/mppt_1210_hus_0v4/Kconfig.defconfig index fb4fed3..e262c6b 100755 --- a/boards/arm/mppt_1210_hus_0v4/Kconfig.defconfig +++ b/boards/arm/mppt_1210_hus_0v4/Kconfig.defconfig @@ -6,15 +6,6 @@ if BOARD_MPPT_1210_HUS_0V4 config BOARD default "mppt_1210_hus_0v4" -config HAS_DCDC_CONVERTER - default y - -config DCDC_PWM_FREQUENCY_KHZ - default 50 - -config DCDC_PWM_DEADTIME_NS - default 230 - config HAS_LOAD_OUTPUT default y diff --git a/boards/arm/mppt_1210_hus_0v4/board.h b/boards/arm/mppt_1210_hus_0v4/board.h index 33aa45c..dd1b4c0 100644 --- a/boards/arm/mppt_1210_hus_0v4/board.h +++ b/boards/arm/mppt_1210_hus_0v4/board.h @@ -7,22 +7,11 @@ #ifndef MPPT_1210_HUS_0V4_H #define MPPT_1210_HUS_0V4_H -#include - #define DEVICE_TYPE "MPPT-1210-HUS" #define HARDWARE_VERSION "v0.4" -// DC/DC converter settings -#define PWM_FREQUENCY 50 // kHz 50 = better for cloud solar to increase efficiency -#define PWM_DEADTIME 230 // ns -#define PWM_TIM 3 // use TIM3 timer - -#define DCDC_CURRENT_MAX 10 // PCB maximum DCDC output current #define LOAD_CURRENT_MAX 10 // PCB maximum load switch current -#define LOW_SIDE_VOLTAGE_MAX 16 // Maximum voltage at battery port (V) -#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) - #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 #define PIN_UEXT_SCL PB_6 diff --git a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts index 62a7e79..a4191e2 100755 --- a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts +++ b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts @@ -11,6 +11,19 @@ model = "Libre Solar MPPT 1210 HUS v0.4"; compatible = "st,stm32l073"; + charge-controller { + compatible = "charge-controller"; + hs-voltage-max = <40>; + ls-voltage-max = <16>; + }; + + dcdc { + compatible = "dcdc"; + pwm-frequency = <50000>; + pwm-deadtime = <230>; + current-max = <10>; + }; + chosen { zephyr,console = &usart1; zephyr,shell-uart = &usart1; diff --git a/boards/arm/mppt_1210_hus_0v7/Kconfig.defconfig b/boards/arm/mppt_1210_hus_0v7/Kconfig.defconfig index 027a1ee..6975888 100755 --- a/boards/arm/mppt_1210_hus_0v7/Kconfig.defconfig +++ b/boards/arm/mppt_1210_hus_0v7/Kconfig.defconfig @@ -6,15 +6,6 @@ if BOARD_MPPT_1210_HUS_0V7 config BOARD default "mppt_1210_hus_0v7" -config HAS_DCDC_CONVERTER - default y - -config DCDC_PWM_FREQUENCY_KHZ - default 50 - -config DCDC_PWM_DEADTIME_NS - default 230 - config HAS_LOAD_OUTPUT default y diff --git a/boards/arm/mppt_1210_hus_0v7/board.h b/boards/arm/mppt_1210_hus_0v7/board.h index 1c9fc09..c3b3ce2 100644 --- a/boards/arm/mppt_1210_hus_0v7/board.h +++ b/boards/arm/mppt_1210_hus_0v7/board.h @@ -7,22 +7,11 @@ #ifndef MPPT_1210_HUS_0V7_H #define MPPT_1210_HUS_0V7_H -#include - #define DEVICE_TYPE "MPPT-1210-HUS" #define HARDWARE_VERSION "v0.7.1" -// DC/DC converter settings -#define PWM_FREQUENCY 50 // kHz 50 = better for cloud solar to increase efficiency -#define PWM_DEADTIME 230 // ns -#define PWM_TIM 3 // use TIM3 timer - -#define DCDC_CURRENT_MAX 10 // PCB maximum DCDC output current #define LOAD_CURRENT_MAX 10 // PCB maximum load switch current -#define LOW_SIDE_VOLTAGE_MAX 16 // Maximum voltage at battery port (V) -#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) - #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 #define PIN_UEXT_SCL PB_6 diff --git a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts index 0d8fc91..4f70769 100755 --- a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts +++ b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts @@ -11,6 +11,19 @@ model = "Libre Solar MPPT 1210 HUS v0.7"; compatible = "st,stm32l073"; + charge-controller { + compatible = "charge-controller"; + hs-voltage-max = <40>; + ls-voltage-max = <16>; + }; + + dcdc { + compatible = "dcdc"; + pwm-frequency = <50000>; + pwm-deadtime = <230>; + current-max = <10>; + }; + chosen { zephyr,console = &usart1; zephyr,shell-uart = &usart1; @@ -53,6 +66,7 @@ label = "BOOT0 pin trigger"; }; }; + aliases { uart-dbg = &usart1; uext-uart = &usart2; diff --git a/boards/arm/mppt_2420_hpx/Kconfig.defconfig b/boards/arm/mppt_2420_hpx/Kconfig.defconfig index e201b37..f6ea61b 100644 --- a/boards/arm/mppt_2420_hpx/Kconfig.defconfig +++ b/boards/arm/mppt_2420_hpx/Kconfig.defconfig @@ -6,21 +6,9 @@ if BOARD_MPPT_2420_HPX config BOARD default "mppt_2420_hpx" -config HAS_DCDC_CONVERTER - default y - -config DCDC_PWM_FREQUENCY_KHZ - default 70 - -config DCDC_PWM_DEADTIME_NS - default 300 - config HAS_LOAD_OUTPUT default y -config HAS_PWM_SWITCH - default y - config NUM_LED_PINS default 1 diff --git a/boards/arm/mppt_2420_hpx/board.h b/boards/arm/mppt_2420_hpx/board.h index 75ac4cf..12e315b 100644 --- a/boards/arm/mppt_2420_hpx/board.h +++ b/boards/arm/mppt_2420_hpx/board.h @@ -12,17 +12,7 @@ #define DEVICE_TYPE "MPPT-2420-HPX" #define HARDWARE_VERSION "v0.1" -// DC/DC converter settings -#define PWM_FREQUENCY 70 // kHz -#define PWM_DEADTIME 300 // ns -#define PWM_TIM 1 // use TIM1 timer - -#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current #define LOAD_CURRENT_MAX 20 // PCB maximum load switch current -#define PWM_CURRENT_MAX 20 // PCB maximum PWM switch (solar) current - -#define LOW_SIDE_VOLTAGE_MAX 16 // Maximum voltage at battery port (V) -#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index 6837bdc..6a5bd71 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -42,6 +42,25 @@ }; }; + charge-controller { + compatible = "charge-controller"; + hs-voltage-max = <80>; + ls-voltage-max = <32>; + }; + + dcdc { + compatible = "dcdc"; + pwm-frequency = <70000>; + pwm-deadtime = <300>; + current-max = <20>; + }; + + pwm-switch { + compatible = "pwm-switch"; + pwm-frequency = <50>; + current-max = <20>; + }; + chosen { zephyr,console = &usart1; zephyr,shell-uart = &usart1; diff --git a/boards/arm/mppt_2420_lc_0v10/Kconfig.defconfig b/boards/arm/mppt_2420_lc_0v10/Kconfig.defconfig index 479b250..cb98cad 100755 --- a/boards/arm/mppt_2420_lc_0v10/Kconfig.defconfig +++ b/boards/arm/mppt_2420_lc_0v10/Kconfig.defconfig @@ -6,15 +6,6 @@ if BOARD_MPPT_2420_LC_0V10 config BOARD default "mppt_2420_lc_0v10" -config HAS_DCDC_CONVERTER - default y - -config DCDC_PWM_FREQUENCY_KHZ - default 70 - -config DCDC_PWM_DEADTIME_NS - default 300 - config HAS_LOAD_OUTPUT default y diff --git a/boards/arm/mppt_2420_lc_0v10/board.h b/boards/arm/mppt_2420_lc_0v10/board.h index 46410c2..becfc0b 100644 --- a/boards/arm/mppt_2420_lc_0v10/board.h +++ b/boards/arm/mppt_2420_lc_0v10/board.h @@ -10,19 +10,8 @@ #define DEVICE_TYPE "MPPT-2420-LC" #define HARDWARE_VERSION "v0.10" -#include - -// DC/DC converter settings -#define PWM_FREQUENCY 70 // kHz 70 = good compromise between output ripple and efficiency -#define PWM_DEADTIME 300 // ns -#define PWM_TIM 1 // use TIM1 timer - -#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current #define LOAD_CURRENT_MAX 20 // PCB maximum load switch current -#define LOW_SIDE_VOLTAGE_MAX 32 // Maximum voltage at battery port (V) -#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) - #define PIN_UEXT_DIS PC_14 // starting from rev. 0.10 #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 diff --git a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts index 86f615d..f4f829b 100755 --- a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts +++ b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts @@ -11,6 +11,19 @@ model = "Libre Solar MPPT 2420 LC v0.10"; compatible = "st,stm32f072"; + charge-controller { + compatible = "charge-controller"; + hs-voltage-max = <55>; + ls-voltage-max = <32>; + }; + + dcdc { + compatible = "dcdc"; + pwm-frequency = <70000>; + pwm-deadtime = <300>; + current-max = <20>; + }; + chosen { zephyr,console = &usart1; zephyr,shell-uart = &usart1; diff --git a/boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig b/boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig index 4679d74..3408898 100644 --- a/boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig +++ b/boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig @@ -6,9 +6,6 @@ if BOARD_PWM_2420_LUS_0V3 config BOARD default "pwm_2420_lus_0v3" -config HAS_PWM_SWITCH - default y - config HAS_LOAD_OUTPUT default y diff --git a/boards/arm/pwm_2420_lus_0v3/board.h b/boards/arm/pwm_2420_lus_0v3/board.h index 753b171..84b5325 100644 --- a/boards/arm/pwm_2420_lus_0v3/board.h +++ b/boards/arm/pwm_2420_lus_0v3/board.h @@ -7,8 +7,6 @@ #ifndef PWM_2420_LUS_H #define PWM_2420_LUS_H -#include - #define DEVICE_TYPE "PWM-2420-LUS" #ifdef CONFIG_BOARD_PWM_2420_LUS_0V2 @@ -17,16 +15,8 @@ #define HARDWARE_VERSION "v0.3" #endif -#define PWM_TIM 3 // use TIM3 timer -#define PWM_FREQUENCY 50 // Hz - -// Current reduced to 15A. Increase to 20A PCB max values only if attached to a big heat sink. -#define PWM_CURRENT_MAX 15 // PCB maximum PWM switch (solar) current #define LOAD_CURRENT_MAX 15 // PCB maximum load switch current -#define LOW_SIDE_VOLTAGE_MAX 32 // Maximum voltage at battery port (V) -#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) - // The MCU, where internal temperature is measured, is close to the MOSFETs. Tests showed that // temperature at heat sink is only 10-20°C above measured internal temp. As PWM CC doesn't // use electrolytic cap for core charging function, internal temperature of 70°C can be allowed. diff --git a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts index 8ad7138..1964a0d 100644 --- a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts @@ -11,6 +11,20 @@ model = "Libre Solar PWM 2420 LUS v0.3"; compatible = "st,stm32l073"; + charge-controller { + compatible = "charge-controller"; + hs-voltage-max = <55>; + ls-voltage-max = <32>; + }; + + pwm-switch { + compatible = "pwm-switch"; + pwm-frequency = <50>; + // Current reduced to 15A. Increase to 20A PCB max values + // only if attached to a big heat sink. + current-max = <15>; + }; + chosen { zephyr,console = &usart1; zephyr,shell-uart = &usart1; diff --git a/src/daq.cpp b/src/daq.cpp index acb8ac3..1e613e1 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -6,6 +6,10 @@ #include "daq.h" +#ifndef UNIT_TEST +#include +#endif + #include // log for thermistor calculation #include @@ -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 diff --git a/src/data_objects.cpp b/src/data_objects.cpp index a9a8602..7f25691 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -4,6 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ +#ifndef UNIT_TEST +#include +#endif + #include #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), diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 3de0b76..a10afd4 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -6,6 +6,10 @@ #include "dcdc.h" +#ifndef UNIT_TEST +#include +#endif + #include // for fabs function #include // for min/max function #include @@ -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 */ diff --git a/src/device_status.cpp b/src/device_status.cpp index 6f15cc5..42afcc8 100644 --- a/src/device_status.cpp +++ b/src/device_status.cpp @@ -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; } diff --git a/src/half_bridge.cpp b/src/half_bridge.cpp index ffefd23..4558114 100644 --- a/src/half_bridge.cpp +++ b/src/half_bridge.cpp @@ -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; diff --git a/src/load.cpp b/src/load.cpp index 48008d5..d9023eb 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -6,6 +6,10 @@ #include "load.h" +#ifndef UNIT_TEST +#include +#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); } diff --git a/src/main.cpp b/src/main.cpp index b3fc4b4..482e210 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); } diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 1de268c..3f6e04b 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -6,6 +6,10 @@ #include "pwm_switch.h" +#ifndef UNIT_TEST +#include +#endif + #include #include @@ -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 */ diff --git a/src/setup.cpp b/src/setup.cpp index 93ed4db..f185832 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -9,6 +9,10 @@ * @brief Setup of ports and other essential charge controller objects */ +#ifndef UNIT_TEST +#include +#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 diff --git a/src/setup.h b/src/setup.h index 4cdd230..1c532ba 100644 --- a/src/setup.h +++ b/src/setup.h @@ -4,6 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ +#ifndef UNIT_TEST +#include +#endif + #include #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 diff --git a/test/board.h b/test/board.h index a905ace..495a9a9 100644 --- a/test/board.h +++ b/test/board.h @@ -8,10 +8,10 @@ #define PCB_STUB_H // PWM charge controller -#define CONFIG_PWM_TERMINAL_SOLAR CONFIG_HAS_PWM_SWITCH +#define CONFIG_PWM_TERMINAL_SOLAR DT_COMPAT_PWM_SWITCH // MPPT buck/boost or nanogrid mode -#define CONFIG_HV_TERMINAL_SOLAR CONFIG_HAS_DCDC_CONVERTER +#define CONFIG_HV_TERMINAL_SOLAR DT_COMPAT_DCDC #define CONFIG_HV_TERMINAL_NANOGRID 0 // battery always assumed to be at low-voltage terminal (might need to be changed for boost mode) @@ -28,8 +28,15 @@ #define HARDWARE_VERSION "v0.1" // specify features of charge controller -#define CONFIG_HAS_DCDC_CONVERTER 1 -#define CONFIG_HAS_PWM_SWITCH 1 +#define DT_COMPAT_DCDC 1 +#define DT_INST_0_DCDC_PWM_FREQUENCY 70000 // Hz +#define DT_INST_0_DCDC_PWM_DEADTIME 300 // ns +#define DT_INST_0_DCDC_CURRENT_MAX 20 // A + +#define DT_COMPAT_PWM_SWITCH 1 +#define DT_INST_0_PWM_SWITCH_PWM_FREQUENCY 50 // Hz +#define DT_INST_0_PWM_SWITCH_CURRENT_MAX 20 // A + #define CONFIG_HAS_LOAD_OUTPUT 1 #define CONFIG_HAS_USB_PWR_OUTPUT 1 @@ -39,15 +46,10 @@ #define CONFIG_INTERNAL_MAX_REFERENCE_TEMP 50 #define CONFIG_MOSFET_THERMAL_TIME_CONSTANT 5 -#define PWM_FREQUENCY 50 // kHz -#define PWM_DEADTIME 230 // ns - -#define PWM_CURRENT_MAX 15 // PCB maximum PWM switch (solar) current -#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current #define LOAD_CURRENT_MAX 20 // PCB maximum load switch current -#define LOW_SIDE_VOLTAGE_MAX 32 // Maximum voltage at battery port (V) -#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) +#define DT_INST_0_CHARGE_CONTROLLER_LS_VOLTAGE_MAX 32 // Maximum voltage at battery port (V) +#define DT_INST_0_CHARGE_CONTROLLER_HS_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) #define PIN_UEXT_TX 0 #define PIN_UEXT_RX 0 diff --git a/zephyr/dts/bindings/charge-controller.yaml b/zephyr/dts/bindings/charge-controller.yaml new file mode 100644 index 0000000..80d480a --- /dev/null +++ b/zephyr/dts/bindings/charge-controller.yaml @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +description: Generic charge controller device setup + +compatible: "charge-controller" + +properties: + hs-voltage-max: + type: int + required: true + description: Maximum voltage at high-side (typically PV input) in volts + ls-voltage-max: + type: int + required: true + description: Maximum voltage at low-side (typically battery) in volts diff --git a/zephyr/dts/bindings/dcdc.yaml b/zephyr/dts/bindings/dcdc.yaml new file mode 100644 index 0000000..0d74f75 --- /dev/null +++ b/zephyr/dts/bindings/dcdc.yaml @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +description: DC/DC converter as part of MPPT charge controllers + +compatible: "dcdc" + +properties: + pwm-frequency: + type: int + required: true + description: PWM frequency in Hz + pwm-deadtime: + type: int + required: true + description: Dead time for synchronous PWM generation in nanoseconds + current-max: + type: int + required: true + description: Maximum inductor current in amps diff --git a/zephyr/dts/bindings/pwm-switch.yaml b/zephyr/dts/bindings/pwm-switch.yaml new file mode 100644 index 0000000..19385cb --- /dev/null +++ b/zephyr/dts/bindings/pwm-switch.yaml @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +description: MOSFETs of PWM charge controllers + +compatible: "pwm-switch" + +properties: + pwm-frequency: + type: int + required: true + description: PWM frequency in Hz + current-max: + type: int + required: true + description: Maximum inductor current in amps