diff --git a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts index d08f32c..cd81f4a 100644 --- a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts +++ b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts @@ -21,14 +21,7 @@ hs-voltage-max = <40>; ls-voltage-max = <16>; - }; - - dcdc { - compatible = "dcdc"; - timer = <&timers3>; - pwm-frequency = <50000>; - pwm-deadtime = <230>; - current-max = <10>; + dcdc-current-max = <10>; }; chosen { @@ -186,7 +179,13 @@ &timers3 { status = "okay"; - pinctrl-0 = <&tim3_ch3_pb0 &tim3_ch4_pb1>; + + halfbridge { + compatible = "half-bridge"; + pinctrl-0 = <&tim3_ch3_pb0 &tim3_ch4_pb1>; + frequency = <50000>; + deadtime = <230>; + }; }; &eeprom { diff --git a/boards/arm/mppt_2420_hc/mppt_2420_hc.dts b/boards/arm/mppt_2420_hc/mppt_2420_hc.dts index e18450e..1920fef 100644 --- a/boards/arm/mppt_2420_hc/mppt_2420_hc.dts +++ b/boards/arm/mppt_2420_hc/mppt_2420_hc.dts @@ -21,14 +21,7 @@ hs-voltage-max = <80>; ls-voltage-max = <32>; - }; - - dcdc { - compatible = "dcdc"; - timer = <&timers1>; - pwm-frequency = <70000>; - pwm-deadtime = <300>; - current-max = <20>; + dcdc-current-max = <20>; }; chosen { @@ -184,7 +177,13 @@ &timers1 { status = "okay"; - pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + + halfbridge { + compatible = "half-bridge"; + pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + frequency = <70000>; + deadtime = <300>; + }; }; /* TIM8 (CH2) used for charge pump */ diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index e0084ad..989b908 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -21,14 +21,7 @@ hs-voltage-max = <80>; ls-voltage-max = <32>; - }; - - dcdc { - compatible = "dcdc"; - timer = <&timers1>; - pwm-frequency = <70000>; - pwm-deadtime = <300>; - current-max = <20>; + dcdc-current-max = <20>; }; chosen { @@ -202,7 +195,13 @@ &timers1 { status = "okay"; - pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + + halfbridge { + compatible = "half-bridge"; + pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + frequency = <70000>; + deadtime = <300>; + }; }; /* TIM3 (CH2) used for PWM power switch */ diff --git a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts index c31747e..1fc662c 100644 --- a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts +++ b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts @@ -21,14 +21,7 @@ hs-voltage-max = <55>; ls-voltage-max = <32>; - }; - - dcdc { - compatible = "dcdc"; - timer = <&timers1>; - pwm-frequency = <70000>; - pwm-deadtime = <300>; - current-max = <20>; + dcdc-current-max = <20>; }; chosen { @@ -201,7 +194,13 @@ &timers1 { status = "okay"; - pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + + halfbridge { + compatible = "half-bridge"; + pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + frequency = <70000>; + deadtime = <300>; + }; }; &can1 { diff --git a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts index 5284639..95fb013 100644 --- a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts +++ b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts @@ -21,14 +21,7 @@ hs-voltage-max = <80>; ls-voltage-max = <32>; - }; - - dcdc { - compatible = "dcdc"; - timer = <&timers1>; - pwm-frequency = <70000>; - pwm-deadtime = <300>; - current-max = <20>; + dcdc-current-max = <20>; }; chosen { @@ -142,7 +135,13 @@ &timers1 { status = "okay"; - pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + + halfbridge { + compatible = "half-bridge"; + pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>; + frequency = <70000>; + deadtime = <300>; + }; }; &flash0 { diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index a2e89f3..3ce082f 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -27,7 +27,7 @@ extern LoadOutput load; #if BOARD_HAS_LOAD_OUTPUT #define DISCHARGE_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max) #else -#define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(dcdc), current_max) +#define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(pcb), dcdc_current_max) #endif void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capacity) diff --git a/src/board.h b/src/board.h index d4a880f..093f780 100644 --- a/src/board.h +++ b/src/board.h @@ -15,7 +15,7 @@ #include -#define BOARD_HAS_DCDC DT_NODE_EXISTS(DT_PATH(dcdc)) +#define BOARD_HAS_DCDC DT_HAS_COMPAT_STATUS_OKAY(half_bridge) #define BOARD_HAS_PWM_PORT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) #define BOARD_HAS_LOAD_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) #define BOARD_HAS_USB_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) diff --git a/src/dcdc.cpp b/src/dcdc.cpp index c3d7124..5f55d45 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -40,7 +40,7 @@ Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode) mode = op_mode; enable = true; state = DCDC_CONTROL_OFF; - inductor_current_max = DT_PROP(DT_PATH(dcdc), current_max); + inductor_current_max = DT_PROP(DT_PATH(pcb), dcdc_current_max); hs_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max); ls_voltage_max = DT_PROP(DT_PATH(pcb), ls_voltage_max); ls_voltage_min = 9.0; @@ -50,8 +50,8 @@ Dcdc::Dcdc(DcBus *high, DcBus *low, 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(DT_PROP(DT_PATH(dcdc), pwm_frequency) / 1000, - DT_PROP(DT_PATH(dcdc), pwm_deadtime), 12 / hs_voltage_max, 0.97); + half_bridge_init(DT_PROP(DT_INST(0, half_bridge), frequency) / 1000, + DT_PROP(DT_INST(0, half_bridge), deadtime), 12 / hs_voltage_max, 0.97); } int Dcdc::perturb_observe_controller() diff --git a/src/half_bridge.cpp b/src/half_bridge.cpp index 1f9162f..8b6a65f 100644 --- a/src/half_bridge.cpp +++ b/src/half_bridge.cpp @@ -16,12 +16,15 @@ #ifndef UNIT_TEST #include #include - -#define DT_DRV_COMPAT st_stm32_timers #endif #if BOARD_HAS_DCDC +#define DT_DRV_COMPAT half_bridge + +// Get address of used timer from board dts +#define TIMER_ADDR DT_REG_ADDR(DT_PARENT(DT_DRV_INST(0))) + static uint16_t tim_ccr_min; // capture/compare register min/max static uint16_t tim_ccr_max; static uint16_t tim_dt_clocks = 0; @@ -42,16 +45,13 @@ static uint16_t clamp_ccr(uint16_t ccr_target) #ifndef UNIT_TEST -// Get address of used timer from board dts -#define PWM_TIMER_ADDR DT_REG_ADDR(DT_PHANDLE(DT_PATH(dcdc), timer)) +static const struct soc_gpio_pinctrl tim_pinctrl[] = ST_STM32_DT_INST_PINCTRL(0, 0); -#if PWM_TIMER_ADDR == TIM3_BASE - -static const struct soc_gpio_pinctrl tim_pinctrl[] = ST_STM32_DT_PINCTRL(timers3, 0); +#if TIMER_ADDR == TIM3_BASE static void tim_init_registers(int freq_kHz) { - stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), PWM_TIMER_ADDR); + stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), TIMER_ADDR); // Enable TIM3 clock RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; @@ -124,13 +124,11 @@ bool half_bridge_enabled() return TIM3->CCER & TIM_CCER_CC3E; } -#elif PWM_TIMER_ADDR == TIM1_BASE - -static const struct soc_gpio_pinctrl tim_pinctrl[] = ST_STM32_DT_PINCTRL(timers1, 0); +#elif TIMER_ADDR == TIM1_BASE static void tim_init_registers(int freq_kHz) { - stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), PWM_TIMER_ADDR); + stm32_dt_pinctrl_configure(tim_pinctrl, ARRAY_SIZE(tim_pinctrl), TIMER_ADDR); // Enable TIM1 clock RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; diff --git a/test/devicetree.h b/test/devicetree.h index 6936e25..87b8edb 100644 --- a/test/devicetree.h +++ b/test/devicetree.h @@ -39,8 +39,13 @@ #define DT_PROP_BY_PHANDLE(node_id, ph, prop) \ DT_PROP_BY_PHANDLE_IDX(node_id, ph, 0, prop) +#define DT_HAS_COMPAT_STATUS_OKAY(compat) \ + IS_ENABLED(DT_CAT(DT_COMPAT_HAS_OKAY_, compat)) + /* * Simplified mocks for certain macros from devicetree.h */ +#define DT_INST(inst, compat) UTIL_CAT(DT_N_INST_, UTIL_CAT(inst, UTIL_CAT(_, compat))) + #define DT_PATH(node) UTIL_CAT(DT_N_S_, node) diff --git a/test/devicetree_unfixed.h b/test/devicetree_unfixed.h index 2ff7b31..e04a4cb 100644 --- a/test/devicetree_unfixed.h +++ b/test/devicetree_unfixed.h @@ -18,10 +18,13 @@ #define DT_N_S_pcb_P_mosfets_tj_max 120 #define DT_N_S_pcb_P_mosfets_tau_ja 5 -#define DT_N_S_dcdc_EXISTS 1 -#define DT_N_S_dcdc_P_pwm_frequency 70000 -#define DT_N_S_dcdc_P_pwm_deadtime 300 -#define DT_N_S_dcdc_P_current_max 20 +#define DT_N_S_pcb_P_dcdc_current_max 20 +#define DT_N_S_pcb_P_dcdc_current_max_EXISTS 1 + +#define DT_COMPAT_HAS_OKAY_half_bridge 1 +#define DT_N_INST_0_half_bridge DT_N_S_soc_S_timers_40016800_S_halfbridge +#define DT_N_S_soc_S_timers_40016800_S_halfbridge_P_frequency 70000 +#define DT_N_S_soc_S_timers_40016800_S_halfbridge_P_deadtime 300 #define DT_N_S_outputs_S_pwm_switch_EXISTS 1 #define DT_N_S_outputs_S_pwm_switch_P_current_max 20 diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 5c94233..930d2e9 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -37,7 +37,7 @@ menu "Charge Controller Setup" choice prompt "High-voltage terminal" - depends on $(dt_compat_enabled,dcdc) + depends on $(dt_compat_enabled,half-bridge) help Select the device connected to the high-voltage side terminal of the DC/DC converter. @@ -443,7 +443,7 @@ config CUSTOM_DATA_NODES_FILE config CUSTOM_DCDC_CONTROLLER bool "Custom implementation of DC/DC controller" - depends on $(dt_compat_enabled,dcdc) + depends on $(dt_compat_enabled,half-bridge) default n help Use a custom implementation instead of the default perturb & observe diff --git a/zephyr/dts/bindings/charge-controller.yaml b/zephyr/dts/bindings/charge-controller.yaml index 3cbf9f9..8d8c2b3 100644 --- a/zephyr/dts/bindings/charge-controller.yaml +++ b/zephyr/dts/bindings/charge-controller.yaml @@ -26,6 +26,9 @@ properties: type: int required: true description: Maximum voltage at low-side (typically battery) in volts + dcdc-current-max: + type: int + description: Maximum inductor current in amps (if existing in the charge controller) internal-tref-max: type: int default: 50 diff --git a/zephyr/dts/bindings/dcdc.yaml b/zephyr/dts/bindings/dcdc.yaml deleted file mode 100644 index 3c32903..0000000 --- a/zephyr/dts/bindings/dcdc.yaml +++ /dev/null @@ -1,24 +0,0 @@ -# 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: - timer: - type: phandle - required: true - description: Timer used for PWM signal generation - 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/half-bridge.yaml b/zephyr/dts/bindings/half-bridge.yaml new file mode 100644 index 0000000..d210568 --- /dev/null +++ b/zephyr/dts/bindings/half-bridge.yaml @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2021 Martin Jäger / Libre Solar + +description: Half bridge, consisting of two coupled PWM outputs + +compatible: "half-bridge" + +include: base.yaml + +properties: + frequency: + type: int + required: true + description: PWM frequency in Hz + + deadtime: + type: int + required: true + description: Dead time for synchronous PWM generation in nanoseconds + + pinctrl-0: + type: phandles + required: false + description: | + GPIO pin configuration for PWM signal/s. We expect that the phandles + will reference pinctrl nodes, e.g. + + pinctrl-0 = <&tim1_ch1_pwm_pa8 &tim1_ch2_pwm_pa9>;