app: convert DC/DC control to C

The Dcdc class becomes struct dcdc with prefixed functions. Private
member functions become static functions. dcdc_check_start_conditions()
and dcdc_control() are still weak, so they can be overridden by custom
implementations.

The constructor is replaced by dcdc_init(), which is called from a
SYS_INIT hook at application level in setup.cpp. This corresponds to the
time the global C++ objects were constructed before, so the unit tests
(which don't run main()) still get initialized objects. The remaining
objects will be moved to this hook when they are converted.

The global device status is declared in device_status.h instead of
setup.h, so that C modules don't need to include the C++ setup header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-11-01 15:33:37 +01:00
co-authored by Claude Opus 5.5
parent a428892916
commit bef0ed576c
10 changed files with 606 additions and 590 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ target_sources(app PRIVATE
bat_charger.cpp
daq.cpp
device_status.cpp
dcdc.cpp
dcdc.c
hardware.cpp
leds.cpp
load.cpp
+2 -2
View File
@@ -219,7 +219,7 @@ static void lv_overvoltage_alert(const struct device *dev, int channel)
{
// disable any sort of input
#if BOARD_HAS_DCDC
dcdc.stop();
dcdc_stop(&dcdc);
#endif
#if BOARD_HAS_PWM_PORT
pwm_switch.stop();
@@ -254,7 +254,7 @@ void daq_set_lv_limits(float lv_overvoltage, float lv_undervoltage)
#if BOARD_HAS_DCDC
static void hv_overvoltage_alert(const struct device *dev, int channel)
{
dcdc.stop();
dcdc_stop(&dcdc);
// do not use enter_state function, as we don't want to wait entire recharge delay
charger.state = CHG_STATE_IDLE;
+452
View File
@@ -0,0 +1,452 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "dcdc.h"
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <math.h> // for fabs function
#include <stdio.h>
#include <stdlib.h> // for min/max function
#include "board.h"
#include "device_status.h"
#include "helper.h"
#include <libresolar/drivers/half_bridge.h>
#include <thingset.h>
#include <thingset/sdk.h>
#include <thingset/storage.h>
LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
#if BOARD_HAS_DCDC
#define BUCK_DUTY_POWER_DECREASE (-1)
#define BUCK_DUTY_POWER_INCREASE (1)
#define BOOST_DUTY_POWER_DECREASE (1)
#define BOOST_DUTY_POWER_INCREASE (-1)
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_out))
#define HV_OUT_NODE DT_CHILD(DT_PATH(outputs), hv_out)
#endif
static void output_hvs_enable(void);
static void output_hvs_disable(void);
void dcdc_init(struct dcdc *dcdc, const struct device *hb, struct dc_bus *high, struct dc_bus *low,
enum dcdc_operation_mode op_mode)
{
dcdc->half_bridge = hb;
dcdc->hvb = high;
dcdc->lvb = low;
dcdc->mode = op_mode;
dcdc->enable = true;
dcdc->state = DCDC_CONTROL_OFF;
dcdc->inductor_current_max = DT_PROP(DT_PATH(pcb), dcdc_current_max);
dcdc->hs_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max);
dcdc->ls_voltage_max = DT_PROP(DT_PATH(pcb), ls_voltage_max);
dcdc->ls_voltage_min = 9.0;
dcdc->output_power_min = 1; // switch off if power < 1 W
dcdc->restart_interval = 60;
dcdc->off_timestamp = -10000; // start immediately
if (!device_is_ready(dcdc->half_bridge)) {
LOG_ERR("Half bridge not ready");
dcdc->enable = false;
return;
}
// lower duty limit might have to be adjusted dynamically depending on LS voltage
half_bridge_set_duty_limits(dcdc->half_bridge, 12 / dcdc->hs_voltage_max, 0.97);
half_bridge_set_duty_cycle(dcdc->half_bridge, 0.97); // init with allowed value
}
static void perturb_observe_buck(struct dcdc *dcdc)
{
if (dcdc->power >= dcdc->output_power_min) {
dcdc->power_good_timestamp = uptime();
}
if ((uptime() - dcdc->power_good_timestamp > 10 || dcdc->power < -10.0f)
&& dcdc->mode != DCDC_MODE_AUTO)
{
// switch off after 10s low power or negative power (if not in nanogrid mode)
dcdc->pwm_direction = 0;
}
else if (dcdc->lvb->voltage > dc_bus_sink_control_voltage(dcdc->lvb, 0)) {
dcdc->state = DCDC_CONTROL_CV_LS;
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (dcdc->lvb->sink_current_margin < 0
|| dcdc->inductor_current > dcdc->inductor_current_max)
{
dcdc->state = DCDC_CONTROL_CC_LS;
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (dcdc->hvb->src_current_margin > 0) {
dcdc->state = DCDC_CONTROL_CC_HS;
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (dcdc->hvb->voltage < dc_bus_src_control_voltage(dcdc->hvb, 0)
&& dcdc->power > dcdc->output_power_min)
{
// input voltage below limit
dcdc->state = DCDC_CONTROL_CV_HS;
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (dcdc->temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
dcdc->state = DCDC_CONTROL_DERATING;
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (dcdc->power < dcdc->output_power_min
&& dcdc->lvb->voltage < dc_bus_src_control_voltage(dcdc->lvb, 0))
{
// no load condition (e.g. start-up of nanogrid) --> raise voltage
dcdc->pwm_direction = BUCK_DUTY_POWER_INCREASE;
}
else {
dcdc->state = DCDC_CONTROL_MPPT;
if (dcdc->power_prev > dcdc->power) {
dcdc->pwm_direction = -dcdc->pwm_direction;
}
}
dcdc->power_prev = dcdc->power;
}
static void perturb_observe_boost(struct dcdc *dcdc)
{
if (-dcdc->power >= dcdc->output_power_min) {
dcdc->power_good_timestamp = uptime();
}
if ((uptime() - dcdc->power_good_timestamp > 10 || -dcdc->power < -10.0f)
&& dcdc->mode != DCDC_MODE_AUTO)
{
// switch off after 10s low power or negative power (if not in nanogrid mode)
dcdc->pwm_direction = 0;
}
else if (dcdc->hvb->voltage > dc_bus_sink_control_voltage(dcdc->hvb, 0)) {
dcdc->state = DCDC_CONTROL_CV_HS;
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (dcdc->hvb->sink_current_margin < 0) {
dcdc->state = DCDC_CONTROL_CC_HS;
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (dcdc->lvb->src_current_margin > 0
|| -dcdc->inductor_current > dcdc->inductor_current_max)
{
dcdc->state = DCDC_CONTROL_CC_LS;
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (dcdc->lvb->voltage < dc_bus_src_control_voltage(dcdc->lvb, 0)
&& -dcdc->power > dcdc->output_power_min)
{
// input voltage below limit
dcdc->state = DCDC_CONTROL_CV_LS;
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (dcdc->temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
dcdc->state = DCDC_CONTROL_DERATING;
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (-dcdc->power < dcdc->output_power_min
&& dcdc->hvb->voltage < dc_bus_src_control_voltage(dcdc->hvb, 0))
{
// no load condition (e.g. start-up of nanogrid) --> raise voltage
dcdc->pwm_direction = BOOST_DUTY_POWER_INCREASE;
}
else {
dcdc->state = DCDC_CONTROL_MPPT;
if (-dcdc->power_prev > -dcdc->power) {
dcdc->pwm_direction = -dcdc->pwm_direction;
}
}
dcdc->power_prev = dcdc->power;
}
__weak enum dcdc_operation_mode dcdc_check_start_conditions(struct dcdc *dcdc)
{
if (dcdc->enable == false
|| dcdc->hvb->voltage
> dcdc->hs_voltage_max // also critical for buck mode because of ringing
|| dcdc->lvb->voltage > dcdc->ls_voltage_max || dcdc->lvb->voltage < dcdc->ls_voltage_min
|| device_status_has_error(&dev_stat, ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)
|| (int)uptime() < (dcdc->off_timestamp + (int)dcdc->restart_interval))
{
return DCDC_MODE_OFF;
}
if (dcdc->lvb->sink_current_margin > 0
&& dcdc->lvb->voltage < dc_bus_sink_control_voltage(dcdc->lvb, 0)
&& dcdc->hvb->src_current_margin < 0
&& dcdc->hvb->voltage > dc_bus_src_control_voltage(dcdc->hvb, 0)
&& dcdc->hvb->voltage * 0.85f > dcdc->lvb->voltage)
{
return DCDC_MODE_BUCK;
}
if (dcdc->hvb->sink_current_margin > 0
&& dcdc->hvb->voltage < dc_bus_sink_control_voltage(dcdc->hvb, 0)
&& dcdc->lvb->src_current_margin < 0
&& dcdc->lvb->voltage > dc_bus_src_control_voltage(dcdc->lvb, 0))
{
return DCDC_MODE_BOOST;
}
return DCDC_MODE_OFF;
}
static bool check_hs_mosfet_short(struct dcdc *dcdc)
{
static uint32_t first_time_detected;
if (half_bridge_is_enabled(dcdc->half_bridge) == false && dcdc->inductor_current > 0.5F
&& dcdc->lvb->voltage_filtered > dc_bus_sink_control_voltage(dcdc->lvb, 0))
{
// if there is current even though the DC/DC is switched off, the
// high-side MOSFET must be broken --> set flag and let main() decide
// what to do... (e.g. call dcdc_self_destruction)
uint32_t now = uptime();
if (first_time_detected == 0) {
first_time_detected = now;
}
else if (now - first_time_detected > 10) {
// waited approx 10s before setting the flag
device_status_set_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
}
}
else {
first_time_detected = 0;
}
return device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
}
static bool startup_inhibit(bool reset)
{
static uint32_t inhibit_start;
if (reset) {
inhibit_start = uptime();
return true;
}
else {
return (uptime() < inhibit_start + DCDC_STARTUP_INHIBIT_TIME);
}
}
__weak void dcdc_control(struct dcdc *dcdc)
{
if (device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT)) {
// DC/DC must not be operated anymore and the low-side switch might be forced on to blow
// the fuse, so don't touch the half bridge
return;
}
if (half_bridge_is_enabled(dcdc->half_bridge) == false) {
if (check_hs_mosfet_short(dcdc)) {
return;
}
int startup_mode = dcdc_check_start_conditions(dcdc);
if ((startup_mode == DCDC_MODE_BUCK && dcdc->mode == DCDC_MODE_BUCK)
|| (startup_mode == DCDC_MODE_BOOST && dcdc->mode == DCDC_MODE_BOOST)
|| (startup_mode != DCDC_MODE_OFF && dcdc->mode == DCDC_MODE_AUTO))
{
output_hvs_enable();
// startup allowed, but we need to wait until voltages settle
if (startup_inhibit(false)) {
return;
}
const char *mode_name = NULL;
if (startup_mode == DCDC_MODE_BUCK) {
mode_name = "buck";
dcdc->pwm_direction = BUCK_DUTY_POWER_INCREASE;
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush
// currents and stress on MOSFETs
half_bridge_set_duty_cycle(dcdc->half_bridge,
dcdc->lvb->voltage / (dcdc->hvb->voltage - 1));
}
else {
mode_name = "boost";
dcdc->pwm_direction = BOOST_DUTY_POWER_INCREASE;
// Will automatically start with max. duty (0.97) if connected to a
// nanogrid not yet started up (zero voltage)
half_bridge_set_duty_cycle(dcdc->half_bridge,
dcdc->lvb->voltage / (dcdc->hvb->voltage + 1));
}
half_bridge_start(dcdc->half_bridge);
dcdc->power_good_timestamp = uptime();
printf("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
(double)dcdc->hvb->voltage, (double)dcdc->lvb->voltage,
(double)half_bridge_get_duty_cycle(dcdc->half_bridge) * 100.0);
}
else {
startup_inhibit(true); // reset inhibit counter
}
}
else { // half bridge is on
const char *stop_reason = NULL;
if (dcdc->lvb->voltage > dcdc->ls_voltage_max || dcdc->hvb->voltage > dcdc->hs_voltage_max)
{
stop_reason = "emergency (voltage limits exceeded)";
}
else if (dcdc->enable == false) {
stop_reason = "disabled";
}
else {
if (dcdc->mode == DCDC_MODE_BUCK
|| (dcdc->mode == DCDC_MODE_AUTO && dcdc->inductor_current > 0.1f))
{
perturb_observe_buck(dcdc);
}
else {
perturb_observe_boost(dcdc);
}
if (dcdc->pwm_direction != 0) {
half_bridge_set_compare(dcdc->half_bridge,
half_bridge_get_compare(dcdc->half_bridge)
+ dcdc->pwm_direction * CONFIG_DCDC_DUTY_STEP_SIZE);
// requires floating point support with CONFIG_CBPRINTF_FP_SUPPORT=y
LOG_DBG("P %.2fW, inductor %.2fA, HS: %.2fV, %.2fA margin, "
"LS: %.2fV (target %.2fV), %.2fA margin, "
"PWM: %.1f, dcdc_state: %d, pwm_direction: %d",
(double)dcdc->power, (double)dcdc->inductor_current,
(double)dcdc->hvb->voltage, (double)dcdc->hvb->src_current_margin,
(double)dcdc->lvb->voltage, (double)dcdc->lvb->sink_voltage_intercept,
(double)dcdc->lvb->sink_current_margin,
(double)half_bridge_get_duty_cycle(dcdc->half_bridge) * 100.0, dcdc->state,
dcdc->pwm_direction);
}
else {
stop_reason = "low power";
}
}
if (stop_reason != NULL) {
dcdc_stop(dcdc);
printf("DC/DC Stop: %s.\n", stop_reason);
}
}
}
void dcdc_test(struct dcdc *dcdc)
{
if (half_bridge_is_enabled(dcdc->half_bridge)) {
const char *stop_reason = NULL;
if (dcdc->lvb->voltage > dcdc->ls_voltage_max || dcdc->hvb->voltage > dcdc->hs_voltage_max)
{
stop_reason = "emergency (voltage limits exceeded)";
}
else if (dcdc->enable == false) {
stop_reason = "disabled";
}
else {
if (half_bridge_get_duty_cycle(dcdc->half_bridge) > 0.50f) {
half_bridge_set_compare(dcdc->half_bridge,
half_bridge_get_compare(dcdc->half_bridge) - 1);
}
else {
half_bridge_set_compare(dcdc->half_bridge,
half_bridge_get_compare(dcdc->half_bridge) + 1);
}
}
if (stop_reason != NULL) {
dcdc_stop(dcdc);
printf("DC/DC Stop: %s.\n", stop_reason);
}
}
else {
if (dcdc_check_start_conditions(dcdc) != DCDC_MODE_OFF) {
// startup allowed, but we need to wait until voltages settle
if (startup_inhibit(false)) {
return;
}
half_bridge_set_duty_cycle(dcdc->half_bridge, dcdc->lvb->voltage / dcdc->hvb->voltage);
half_bridge_start(dcdc->half_bridge);
printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
(double)dcdc->hvb->voltage, (double)dcdc->lvb->voltage,
(double)half_bridge_get_duty_cycle(dcdc->half_bridge) * 100.0);
}
else {
startup_inhibit(true); // reset inhibit counter
}
}
}
void dcdc_stop(struct dcdc *dcdc)
{
half_bridge_stop(dcdc->half_bridge);
dcdc->state = DCDC_CONTROL_OFF;
dcdc->off_timestamp = uptime();
output_hvs_disable();
}
void dcdc_fuse_destruction(struct dcdc *dcdc)
{
static int counter = 0;
if (counter > 20) { // wait 20s to be able to send out data
if (counter == 21) {
LOG_ERR("Charge controller fuse destruction called!\n");
#ifdef CONFIG_THINGSET_STORAGE
thingset_storage_save();
#endif
}
// repeated in case the half bridge was stopped in the meantime
half_bridge_force_low_side(dcdc->half_bridge);
// now the fuse should be triggered and we disappear
}
counter++;
}
static void output_hvs_enable(void)
{
#ifdef HV_OUT_NODE
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
if (device_is_ready(hv_out)) {
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_ACTIVE);
}
#endif
}
static void output_hvs_disable(void)
{
#ifdef HV_OUT_NODE
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
if (device_is_ready(hv_out)) {
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_INACTIVE);
}
#endif
}
#else
void dcdc_init(struct dcdc *dcdc, const struct device *hb, struct dc_bus *high, struct dc_bus *low,
enum dcdc_operation_mode op_mode)
{}
#endif /* BOARD_HAS_DCDC */
-421
View File
@@ -1,421 +0,0 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "dcdc.h"
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <math.h> // for fabs function
#include <stdio.h>
#include <stdlib.h> // for min/max function
#include "device_status.h"
#include "helper.h"
#include "setup.h"
#include <libresolar/drivers/half_bridge.h>
#include <thingset.h>
#include <thingset/sdk.h>
#include <thingset/storage.h>
LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
#if BOARD_HAS_DCDC
#define BUCK_DUTY_POWER_DECREASE (-1)
#define BUCK_DUTY_POWER_INCREASE (1)
#define BOOST_DUTY_POWER_DECREASE (1)
#define BOOST_DUTY_POWER_INCREASE (-1)
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_out))
#define HV_OUT_NODE DT_CHILD(DT_PATH(outputs), hv_out)
#endif
extern struct device_status dev_stat;
Dcdc::Dcdc(const struct device *hb, struct dc_bus *high, struct dc_bus *low,
DcdcOperationMode op_mode)
{
half_bridge = hb;
hvb = high;
lvb = low;
mode = op_mode;
enable = true;
state = DCDC_CONTROL_OFF;
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;
output_power_min = 1; // switch off if power < 1 W
restart_interval = 60;
off_timestamp = -10000; // start immediately
if (!device_is_ready(half_bridge)) {
LOG_ERR("Half bridge not ready");
enable = false;
return;
}
// lower duty limit might have to be adjusted dynamically depending on LS voltage
half_bridge_set_duty_limits(half_bridge, 12 / hs_voltage_max, 0.97);
half_bridge_set_duty_cycle(half_bridge, 0.97); // init with allowed value
}
void Dcdc::perturb_observe_buck()
{
if (power >= output_power_min) {
power_good_timestamp = uptime();
}
if ((uptime() - power_good_timestamp > 10 || power < -10.0f) && mode != DCDC_MODE_AUTO) {
// switch off after 10s low power or negative power (if not in nanogrid mode)
pwm_direction = 0;
}
else if (lvb->voltage > dc_bus_sink_control_voltage(lvb, 0)) {
state = DCDC_CONTROL_CV_LS;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (lvb->sink_current_margin < 0 || inductor_current > inductor_current_max) {
state = DCDC_CONTROL_CC_LS;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (hvb->src_current_margin > 0) {
state = DCDC_CONTROL_CC_HS;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (hvb->voltage < dc_bus_src_control_voltage(hvb, 0) && power > output_power_min) {
// input voltage below limit
state = DCDC_CONTROL_CV_HS;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
state = DCDC_CONTROL_DERATING;
pwm_direction = BUCK_DUTY_POWER_DECREASE;
}
else if (power < output_power_min && lvb->voltage < dc_bus_src_control_voltage(lvb, 0)) {
// no load condition (e.g. start-up of nanogrid) --> raise voltage
pwm_direction = BUCK_DUTY_POWER_INCREASE;
}
else {
state = DCDC_CONTROL_MPPT;
if (power_prev > power) {
pwm_direction = -pwm_direction;
}
}
power_prev = power;
}
void Dcdc::perturb_observe_boost()
{
if (-power >= output_power_min) {
power_good_timestamp = uptime();
}
if ((uptime() - power_good_timestamp > 10 || -power < -10.0f) && mode != DCDC_MODE_AUTO) {
// switch off after 10s low power or negative power (if not in nanogrid mode)
pwm_direction = 0;
}
else if (hvb->voltage > dc_bus_sink_control_voltage(hvb, 0)) {
state = DCDC_CONTROL_CV_HS;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (hvb->sink_current_margin < 0) {
state = DCDC_CONTROL_CC_HS;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (lvb->src_current_margin > 0 || -inductor_current > inductor_current_max) {
state = DCDC_CONTROL_CC_LS;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (lvb->voltage < dc_bus_src_control_voltage(lvb, 0) && -power > output_power_min) {
// input voltage below limit
state = DCDC_CONTROL_CV_LS;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
state = DCDC_CONTROL_DERATING;
pwm_direction = BOOST_DUTY_POWER_DECREASE;
}
else if (-power < output_power_min && hvb->voltage < dc_bus_src_control_voltage(hvb, 0)) {
// no load condition (e.g. start-up of nanogrid) --> raise voltage
pwm_direction = BOOST_DUTY_POWER_INCREASE;
}
else {
state = DCDC_CONTROL_MPPT;
if (-power_prev > -power) {
pwm_direction = -pwm_direction;
}
}
power_prev = power;
}
__weak DcdcOperationMode Dcdc::check_start_conditions()
{
if (enable == false
|| hvb->voltage > hs_voltage_max // also critical for buck mode because of ringing
|| lvb->voltage > ls_voltage_max || lvb->voltage < ls_voltage_min
|| device_status_has_error(&dev_stat, ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)
|| (int)uptime() < (off_timestamp + (int)restart_interval))
{
return DCDC_MODE_OFF;
}
if (lvb->sink_current_margin > 0 && lvb->voltage < dc_bus_sink_control_voltage(lvb, 0)
&& hvb->src_current_margin < 0 && hvb->voltage > dc_bus_src_control_voltage(hvb, 0)
&& hvb->voltage * 0.85f > lvb->voltage)
{
return DCDC_MODE_BUCK;
}
if (hvb->sink_current_margin > 0 && hvb->voltage < dc_bus_sink_control_voltage(hvb, 0)
&& lvb->src_current_margin < 0 && lvb->voltage > dc_bus_src_control_voltage(lvb, 0))
{
return DCDC_MODE_BOOST;
}
return DCDC_MODE_OFF;
}
bool Dcdc::check_hs_mosfet_short()
{
static uint32_t first_time_detected;
if (half_bridge_is_enabled(half_bridge) == false && inductor_current > 0.5F
&& lvb->voltage_filtered > dc_bus_sink_control_voltage(lvb, 0))
{
// if there is current even though the DC/DC is switched off, the
// high-side MOSFET must be broken --> set flag and let main() decide
// what to do... (e.g. call dcdc_self_destruction)
uint32_t now = uptime();
if (first_time_detected == 0) {
first_time_detected = now;
}
else if (now - first_time_detected > 10) {
// waited approx 10s before setting the flag
device_status_set_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
}
}
else {
first_time_detected = 0;
}
return device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
}
bool Dcdc::startup_inhibit(bool reset)
{
static uint32_t inhibit_start;
if (reset) {
inhibit_start = uptime();
return true;
}
else {
return (uptime() < inhibit_start + DCDC_STARTUP_INHIBIT_TIME);
}
}
__weak void Dcdc::control()
{
if (device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT)) {
// DC/DC must not be operated anymore and the low-side switch might be forced on to blow
// the fuse, so don't touch the half bridge
return;
}
if (half_bridge_is_enabled(half_bridge) == false) {
if (check_hs_mosfet_short()) {
return;
}
int startup_mode = check_start_conditions();
if ((startup_mode == DCDC_MODE_BUCK && mode == DCDC_MODE_BUCK)
|| (startup_mode == DCDC_MODE_BOOST && mode == DCDC_MODE_BOOST)
|| (startup_mode != DCDC_MODE_OFF && mode == DCDC_MODE_AUTO))
{
output_hvs_enable();
// startup allowed, but we need to wait until voltages settle
if (startup_inhibit()) {
return;
}
const char *mode_name = NULL;
if (startup_mode == DCDC_MODE_BUCK) {
mode_name = "buck";
pwm_direction = BUCK_DUTY_POWER_INCREASE;
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush
// currents and stress on MOSFETs
half_bridge_set_duty_cycle(half_bridge, lvb->voltage / (hvb->voltage - 1));
}
else {
mode_name = "boost";
pwm_direction = BOOST_DUTY_POWER_INCREASE;
// Will automatically start with max. duty (0.97) if connected to a
// nanogrid not yet started up (zero voltage)
half_bridge_set_duty_cycle(half_bridge, lvb->voltage / (hvb->voltage + 1));
}
half_bridge_start(half_bridge);
power_good_timestamp = uptime();
printf("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
(double)hvb->voltage, (double)lvb->voltage,
(double)half_bridge_get_duty_cycle(half_bridge) * 100.0);
}
else {
startup_inhibit(true); // reset inhibit counter
}
}
else { // half bridge is on
const char *stop_reason = NULL;
if (lvb->voltage > ls_voltage_max || hvb->voltage > hs_voltage_max) {
stop_reason = "emergency (voltage limits exceeded)";
}
else if (enable == false) {
stop_reason = "disabled";
}
else {
if (mode == DCDC_MODE_BUCK || (mode == DCDC_MODE_AUTO && inductor_current > 0.1f)) {
perturb_observe_buck();
}
else {
perturb_observe_boost();
}
if (pwm_direction != 0) {
half_bridge_set_compare(half_bridge,
half_bridge_get_compare(half_bridge)
+ pwm_direction * CONFIG_DCDC_DUTY_STEP_SIZE);
// requires floating point support with CONFIG_CBPRINTF_FP_SUPPORT=y
LOG_DBG("P %.2fW, inductor %.2fA, HS: %.2fV, %.2fA margin, "
"LS: %.2fV (target %.2fV), %.2fA margin, "
"PWM: %.1f, dcdc_state: %d, pwm_direction: %d",
(double)power, (double)inductor_current, (double)hvb->voltage,
(double)hvb->src_current_margin, (double)lvb->voltage,
(double)lvb->sink_voltage_intercept, (double)lvb->sink_current_margin,
(double)half_bridge_get_duty_cycle(half_bridge) * 100.0, state,
pwm_direction);
}
else {
stop_reason = "low power";
}
}
if (stop_reason != NULL) {
stop();
printf("DC/DC Stop: %s.\n", stop_reason);
}
}
}
void Dcdc::test()
{
if (half_bridge_is_enabled(half_bridge)) {
const char *stop_reason = NULL;
if (lvb->voltage > ls_voltage_max || hvb->voltage > hs_voltage_max) {
stop_reason = "emergency (voltage limits exceeded)";
}
else if (enable == false) {
stop_reason = "disabled";
}
else {
if (half_bridge_get_duty_cycle(half_bridge) > 0.50f) {
half_bridge_set_compare(half_bridge, half_bridge_get_compare(half_bridge) - 1);
}
else {
half_bridge_set_compare(half_bridge, half_bridge_get_compare(half_bridge) + 1);
}
}
if (stop_reason != NULL) {
stop();
printf("DC/DC Stop: %s.\n", stop_reason);
}
}
else {
if (check_start_conditions() != DCDC_MODE_OFF) {
// startup allowed, but we need to wait until voltages settle
if (startup_inhibit()) {
return;
}
half_bridge_set_duty_cycle(half_bridge, lvb->voltage / hvb->voltage);
half_bridge_start(half_bridge);
printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
(double)hvb->voltage, (double)lvb->voltage,
(double)half_bridge_get_duty_cycle(half_bridge) * 100.0);
}
else {
startup_inhibit(true); // reset inhibit counter
}
}
}
void Dcdc::stop()
{
half_bridge_stop(half_bridge);
state = DCDC_CONTROL_OFF;
off_timestamp = uptime();
output_hvs_disable();
}
void Dcdc::fuse_destruction()
{
static int counter = 0;
if (counter > 20) { // wait 20s to be able to send out data
if (counter == 21) {
LOG_ERR("Charge controller fuse destruction called!\n");
#ifdef CONFIG_THINGSET_STORAGE
thingset_storage_save();
#endif
}
// repeated in case the half bridge was stopped in the meantime
half_bridge_force_low_side(half_bridge);
// now the fuse should be triggered and we disappear
}
counter++;
}
void Dcdc::output_hvs_enable()
{
#ifdef HV_OUT_NODE
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
if (device_is_ready(hv_out)) {
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_ACTIVE);
}
#endif
}
void Dcdc::output_hvs_disable()
{
#ifdef HV_OUT_NODE
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
if (device_is_ready(hv_out)) {
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_INACTIVE);
}
#endif
}
#else
Dcdc::Dcdc(const struct device *hb, struct dc_bus *high, struct dc_bus *low,
DcdcOperationMode op_mode)
{}
#endif /* BOARD_HAS_DCDC */
+75 -109
View File
@@ -21,10 +21,12 @@
#include <zephyr/device.h>
#ifdef __cplusplus
#include "power_port.h"
#ifdef __cplusplus
extern "C" {
#endif
#define DCDC_STARTUP_INHIBIT_TIME 3 // seconds
#define DCDC_MOSFETS_MAX_TEMP (80) // °C
@@ -34,7 +36,7 @@
*
* Defines which type of device is connected to the high side and low side ports
*/
enum DcdcOperationMode
enum dcdc_operation_mode
{
/**
* DC/DC converter switched off
@@ -71,7 +73,7 @@ enum DcdcOperationMode
*
* Allows to determine the current control state (off, CC, CV and MPPT)
*/
enum DcdcControlState
enum dcdc_control_state
{
DCDC_CONTROL_OFF, ///< DC/DC switched off (low input power or actively disabled)
DCDC_CONTROL_MPPT, ///< Maximum Power Point Tracking
@@ -83,69 +85,18 @@ enum DcdcControlState
};
/**
* DC/DC class
* DC/DC converter
*
* Contains all data belonging to the DC/DC sub-component of the PCB, incl.
* actual measurements and calibration parameters.
*/
class Dcdc
struct dcdc
{
public:
/**
* Initialize DC/DC and DC/DC port structs
*
* See http://libre.solar/docs/dcdc_control for detailed information
*
* @param half_bridge Half-bridge device generating the PWM signal
* @param high High voltage bus (e.g. solar input for MPPT buck)
* @param low Low voltage bus (e.g. battery output for MPPT buck)
* @param mode Operation mode (buck, boost or nanogrid)
*/
Dcdc(const struct device *half_bridge, struct dc_bus *high, struct dc_bus *low,
DcdcOperationMode mode);
/**
* Check for valid start conditions of the DC/DC converter
*
* @returns operation mode that is valid for starting up
*/
DcdcOperationMode check_start_conditions();
/**
* Main control function for the DC/DC converter
*
* If DC/DC is off, this function checks start conditions and starts conversion if possible.
*/
void control();
/**
* Test mode for DC/DC, ramping up/down to 50% duty cycle
*/
void test();
/**
* Fast stop function (bypassing control loop)
*
* May be called from an ISR which detected overvoltage / overcurrent conditions.
*
* DC/DC will be restarted automatically from control function if condtions are valid.
*/
void stop();
/**
* Prevent overcharging of battery in case of shorted HS MOSFET
*
* This function switches the LS MOSFET continuously on to blow the battery input fuse. The
* reason for self destruction should be logged and stored to EEPROM prior to calling this
* function, as the charge controller power supply will be cut after the fuse is destroyed.
*/
void fuse_destruction();
const struct device *half_bridge; ///< Half-bridge device generating the PWM signal
DcdcOperationMode mode; ///< DC/DC mode (buck, boost or nanogrid)
bool enable; ///< Can be used to disable the DC/DC power stage
uint16_t state; ///< Control state (off / MPPT / CC / CV)
enum dcdc_operation_mode mode; ///< DC/DC mode (buck, boost or nanogrid)
bool enable; ///< Can be used to disable the DC/DC power stage
uint16_t state; ///< Control state (off / MPPT / CC / CV)
// actual measurements
struct dc_bus *hvb; ///< Pointer to DC bus at high voltage side
@@ -161,62 +112,77 @@ public:
int32_t power_good_timestamp; ///< Last time the DC/DC reached above minimum output power
// maximum allowed values
float inductor_current_max = 0; ///< Maximum low-side (inductor) current
float hs_voltage_max = 0; ///< Maximum high-side voltage
float ls_voltage_max; ///< Maximum low-side voltage
float ls_voltage_min; ///< Minimum low-side voltage, e.g. for driver supply
float output_power_min; ///< Minimum output power (if lower, DC/DC is switched off)
float inductor_current_max; ///< Maximum low-side (inductor) current
float hs_voltage_max; ///< Maximum high-side voltage
float ls_voltage_max; ///< Maximum low-side voltage
float ls_voltage_min; ///< Minimum low-side voltage, e.g. for driver supply
float output_power_min; ///< Minimum output power (if lower, DC/DC is switched off)
// calibration parameters
uint32_t restart_interval; ///< Restart interval (s): When should we retry to start
///< charging after low output power cut-off?
private:
/**
* MPPT perturb & observe control (buck mode)
*
* @returns calculated pwm duty cycle delta or 0 if the DC/DC should be stopped
*/
void perturb_observe_buck();
/**
* MPPT perturb & observe control (boost mode)
*
* @returns calculated pwm duty cycle delta or 0 if the DC/DC should be stopped
*/
void perturb_observe_boost();
/**
* If manual control of the reverse polarity MOSFET on the high-side is available, this
* function enables it to use the high voltage side as output.
*/
void output_hvs_enable();
/**
* Disable the reverse polarity MOSFET on the high-side (if option available)
*/
void output_hvs_disable();
/**
* Check if the high-side MOSFET is shorted
*/
bool check_hs_mosfet_short();
/**
* Check if we need to wait for voltages to settle before starting the DC/DC
*
* @param reset Reset the inhibit counter if set to true
*
* @returns true if we need to wait
*/
bool startup_inhibit(bool reset = false);
};
#endif // __cplusplus
/**
* Initialize DC/DC struct and half bridge
*
* See http://libre.solar/docs/dcdc_control for detailed information
*
* @param dcdc DC/DC struct
* @param half_bridge Half-bridge device generating the PWM signal
* @param high High voltage bus (e.g. solar input for MPPT buck)
* @param low Low voltage bus (e.g. battery output for MPPT buck)
* @param mode Operation mode (buck, boost or nanogrid)
*/
void dcdc_init(struct dcdc *dcdc, const struct device *half_bridge, struct dc_bus *high,
struct dc_bus *low, enum dcdc_operation_mode mode);
#ifdef __cplusplus
extern "C" {
#endif
/**
* Check for valid start conditions of the DC/DC converter
*
* @param dcdc DC/DC struct
*
* @returns operation mode that is valid for starting up
*/
enum dcdc_operation_mode dcdc_check_start_conditions(struct dcdc *dcdc);
/**
* Main control function for the DC/DC converter
*
* If DC/DC is off, this function checks start conditions and starts conversion if possible.
*
* @param dcdc DC/DC struct
*/
void dcdc_control(struct dcdc *dcdc);
/**
* Test mode for DC/DC, ramping up/down to 50% duty cycle
*
* @param dcdc DC/DC struct
*/
void dcdc_test(struct dcdc *dcdc);
/**
* Fast stop function (bypassing control loop)
*
* May be called from an ISR which detected overvoltage / overcurrent conditions.
*
* DC/DC will be restarted automatically from control function if condtions are valid.
*
* @param dcdc DC/DC struct
*/
void dcdc_stop(struct dcdc *dcdc);
/**
* Prevent overcharging of battery in case of shorted HS MOSFET
*
* This function switches the LS MOSFET continuously on to blow the battery input fuse. The
* reason for self destruction should be logged and stored to EEPROM prior to calling this
* function, as the charge controller power supply will be cut after the fuse is destroyed.
*
* @param dcdc DC/DC struct
*/
void dcdc_fuse_destruction(struct dcdc *dcdc);
#ifdef CONFIG_CUSTOM_DCDC_CONTROLLER
+3
View File
@@ -142,6 +142,9 @@ struct device_status
float internal_temp; ///< Internal temperature (measured in MCU)
};
/** Global device status */
extern struct device_status dev_stat;
/**
* Updates the total energy counters for solar, battery and load bus
*
+2 -2
View File
@@ -112,7 +112,7 @@ int main(void)
#if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && BOARD_HAS_DCDC
if (device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT)) {
dcdc.fuse_destruction();
dcdc_fuse_destruction(&dcdc);
}
#endif
@@ -155,7 +155,7 @@ void control_thread()
#if BOARD_HAS_DCDC
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control(); // control of DC/DC including MPPT algorithm
dcdc_control(&dcdc); // control of DC/DC including MPPT algorithm
charging |= half_bridge_is_enabled(dcdc.half_bridge);
#endif
+19 -3
View File
@@ -34,12 +34,13 @@ struct power_port lv_terminal = { .bus = &lv_bus };
extern struct power_port hv_terminal;
struct dc_bus hv_bus = { .series_multiplier = 1, .ref_current = &hv_terminal.current };
struct power_port hv_terminal = { .bus = &hv_bus };
struct dcdc dcdc;
#if CONFIG_HV_TERMINAL_NANOGRID
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_AUTO);
#define DCDC_MODE DCDC_MODE_AUTO
#elif CONFIG_HV_TERMINAL_BATTERY
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_BOOST);
#define DCDC_MODE DCDC_MODE_BOOST
#else
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_BUCK);
#define DCDC_MODE DCDC_MODE_BUCK
#endif // CONFIG_HV_TERMINAL
#endif
@@ -88,6 +89,21 @@ struct device_status dev_stat;
// uint32_t considered large enough, so we avoid 64-bit math (overflow in year 2106)
uint32_t timestamp;
/*
* Initialize the charge controller objects after all devices are ready (also used by the unit
* tests, which don't call main())
*/
static int setup_objects(void)
{
#if BOARD_HAS_DCDC
dcdc_init(&dcdc, DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE);
#endif
return 0;
}
SYS_INIT(setup_objects, APPLICATION, 0);
#ifndef UNIT_TEST
#include <zephyr/kernel.h>
+1 -1
View File
@@ -23,7 +23,7 @@ extern struct power_port lv_terminal;
#if BOARD_HAS_DCDC
extern struct dc_bus hv_bus;
extern struct power_port hv_terminal;
extern Dcdc dcdc;
extern struct dcdc dcdc;
#endif
#if BOARD_HAS_PWM_PORT
+51 -51
View File
@@ -50,9 +50,9 @@ static void start_buck()
init_structs_buck();
half_bridge_set_duty_limits(dcdc.half_bridge, 12 / dcdc.hs_voltage_max, 0.97);
half_bridge_set_duty_cycle(dcdc.half_bridge, 0.97);
dcdc.control();
dcdc.control();
dcdc.control(); // call multiple times because of startup delay
dcdc_control(&dcdc);
dcdc_control(&dcdc);
dcdc_control(&dcdc); // call multiple times because of startup delay
zassert_equal(dcdc.state, DCDC_CONTROL_MPPT);
}
@@ -91,57 +91,57 @@ static void start_boost()
init_structs_boost();
half_bridge_set_duty_limits(dcdc.half_bridge, 12 / dcdc.hs_voltage_max, 0.97);
half_bridge_set_duty_cycle(dcdc.half_bridge, 0.97);
dcdc.control();
dcdc.control();
dcdc.control(); // call multiple times because of startup delay
dcdc_control(&dcdc);
dcdc_control(&dcdc);
dcdc_control(&dcdc); // call multiple times because of startup delay
zassert_equal(dcdc.state, DCDC_CONTROL_MPPT);
}
ZTEST(dcdc, test_start_valid_mppt_buck)
{
init_structs_buck();
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BUCK);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_BUCK);
}
ZTEST(dcdc, test_start_valid_mppt_buck_dual_battery)
{
init_structs_buck(2);
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BUCK);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_BUCK);
}
ZTEST(dcdc, test_start_valid_mppt_boost)
{
init_structs_boost();
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BOOST);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_BOOST);
}
ZTEST(dcdc, test_start_valid_mppt_boost_dual_battery)
{
init_structs_boost(2);
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BOOST);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_BOOST);
}
ZTEST(dcdc, test_no_start_before_restart_delay)
{
init_structs_buck();
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval + 1;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BUCK);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_BUCK);
}
ZTEST(dcdc, test_no_start_if_dcdc_disabled)
{
init_structs_buck();
dcdc.enable = false;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
ZTEST(dcdc, test_no_start_if_dcdc_lv_voltage_low)
{
init_structs_buck();
lv_terminal.bus->voltage = dcdc.ls_voltage_min - 0.5f;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
// buck
@@ -150,28 +150,28 @@ ZTEST(dcdc, test_no_buck_start_if_bat_voltage_high)
{
init_structs_buck();
lv_terminal.bus->voltage = bat_conf.topping_voltage + 0.1f;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
ZTEST(dcdc, test_no_buck_start_if_bat_chg_not_allowed)
{
init_structs_buck();
lv_terminal.bus->sink_current_margin = 0;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
ZTEST(dcdc, test_no_buck_start_if_solar_voltage_high)
{
init_structs_buck();
hv_terminal.bus->voltage = dcdc.hs_voltage_max + 1;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
ZTEST(dcdc, test_no_buck_start_if_solar_voltage_low)
{
init_structs_buck();
hv_terminal.bus->voltage = 17;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
// boost
@@ -180,28 +180,28 @@ ZTEST(dcdc, test_no_boost_start_if_bat_voltage_high)
{
init_structs_boost();
hv_terminal.bus->voltage = bat_conf.topping_voltage + 0.1f;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
ZTEST(dcdc, test_no_boost_start_if_bat_chg_not_allowed)
{
init_structs_boost();
hv_terminal.bus->sink_current_margin = 0;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
ZTEST(dcdc, test_no_boost_start_if_solar_voltage_high)
{
init_structs_boost();
lv_terminal.bus->voltage = dcdc.ls_voltage_max + 1;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
ZTEST(dcdc, test_no_boost_start_if_solar_voltage_low)
{
init_structs_boost();
lv_terminal.bus->voltage = 17;
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
zassert_equal(dcdc_check_start_conditions(&dcdc), DCDC_MODE_OFF);
}
// buck operation
@@ -210,7 +210,7 @@ ZTEST(dcdc, test_buck_increasing_power)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after > pwm_before);
}
@@ -220,7 +220,7 @@ ZTEST(dcdc, test_buck_derating_output_voltage_too_high)
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
lv_terminal.bus->voltage = lv_terminal.bus->sink_voltage_intercept + 0.1f;
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before); // less duty cycle = higher voltage
zassert_equal(dcdc.state, DCDC_CONTROL_CV_LS);
@@ -232,7 +232,7 @@ ZTEST(dcdc, test_buck_derating_output_current_too_high)
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
lv_terminal.current = lv_terminal.pos_current_limit + 0.1f;
power_port_update_bus_current_margins(&lv_terminal);
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CC_LS);
@@ -243,7 +243,7 @@ ZTEST(dcdc, test_buck_derating_inductor_current_too_high)
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.inductor_current = dcdc.inductor_current_max + 0.1f;
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CC_LS);
@@ -255,7 +255,7 @@ ZTEST(dcdc, test_buck_derating_input_voltage_too_low)
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
hv_terminal.bus->voltage = hv_terminal.bus->src_voltage_intercept - 0.1f;
dcdc.power = 1.2f;
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CV_HS);
@@ -267,7 +267,7 @@ ZTEST(dcdc, test_buck_derating_input_current_too_high)
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
hv_terminal.current = hv_terminal.neg_current_limit - 0.1f;
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CC_HS);
@@ -278,7 +278,7 @@ ZTEST(dcdc, test_buck_derating_temperature_limits_exceeded)
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.temp_mosfets = 81;
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
}
@@ -287,7 +287,7 @@ ZTEST(dcdc, test_buck_stop_input_power_too_low)
{
start_buck();
dcdc.power_good_timestamp = time(NULL) - 11;
dcdc.control();
dcdc_control(&dcdc);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
@@ -295,7 +295,7 @@ ZTEST(dcdc, test_buck_stop_high_voltage_emergency)
{
start_buck();
lv_terminal.bus->voltage = dcdc.ls_voltage_max + 0.1f;
dcdc.control();
dcdc_control(&dcdc);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
@@ -304,15 +304,15 @@ ZTEST(dcdc, test_buck_correct_mppt_operation)
start_buck();
dcdc.power = 5;
dcdc.control();
dcdc_control(&dcdc);
float pwm1 = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.power = 7;
dcdc.control();
dcdc_control(&dcdc);
float pwm2 = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm2 > pwm1);
dcdc.power = 6; // decrease power to make the direction turn around
dcdc.control();
dcdc_control(&dcdc);
float pwm3 = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm3 < pwm2);
}
@@ -324,7 +324,7 @@ ZTEST(dcdc, test_boost_increasing_power)
// make sure that without changing anything, dcdc_control goes in direction of increasing power
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after < pwm_before);
}
@@ -334,7 +334,7 @@ ZTEST(dcdc, test_boost_derating_output_voltage_too_high)
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
hv_terminal.bus->voltage = hv_terminal.bus->sink_voltage_intercept + 0.5f;
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after > pwm_before); // higher duty cycle = less power
}
@@ -345,7 +345,7 @@ ZTEST(dcdc, test_boost_derating_output_current_too_high)
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
hv_terminal.current = hv_terminal.pos_current_limit + 0.1f;
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after > pwm_before);
}
@@ -357,7 +357,7 @@ ZTEST(dcdc, test_boost_derating_input_voltage_too_low)
lv_terminal.bus->voltage = lv_terminal.bus->src_voltage_intercept - 0.1f;
dcdc.power = -1.2f;
power_port_update_bus_current_margins(&hv_terminal);
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_equal(dcdc.state, DCDC_CONTROL_CV_LS);
zassert_true(pwm_after > pwm_before);
@@ -368,7 +368,7 @@ ZTEST(dcdc, test_boost_derating_input_current_too_high)
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.inductor_current = lv_terminal.neg_current_limit - 0.1f;
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after > pwm_before);
}
@@ -378,7 +378,7 @@ ZTEST(dcdc, test_boost_derating_temperature_limits_exceeded)
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.temp_mosfets = 81;
dcdc.control();
dcdc_control(&dcdc);
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm_after > pwm_before);
}
@@ -387,7 +387,7 @@ ZTEST(dcdc, test_boost_stop_input_power_too_low)
{
start_boost();
dcdc.power_good_timestamp = time(NULL) - 11;
dcdc.control();
dcdc_control(&dcdc);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
@@ -395,7 +395,7 @@ ZTEST(dcdc, test_boost_stop_high_voltage_emergency)
{
start_boost();
hv_terminal.bus->voltage = dcdc.hs_voltage_max + 0.1f;
dcdc.control();
dcdc_control(&dcdc);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
@@ -404,15 +404,15 @@ ZTEST(dcdc, test_boost_correct_mppt_operation)
start_boost();
dcdc.power = -5;
dcdc.control();
dcdc_control(&dcdc);
float pwm1 = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.power = -7;
dcdc.control();
dcdc_control(&dcdc);
float pwm2 = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm2 < pwm1);
dcdc.power = -6; // decrease power to make the direction turn around
dcdc.control();
dcdc_control(&dcdc);
float pwm3 = half_bridge_get_duty_cycle(dcdc.half_bridge);
zassert_true(pwm3 > pwm2);
}
@@ -424,26 +424,26 @@ ZTEST(dcdc, test_fuse_destruction_forces_low_side_on)
// fuse destruction is delayed by 21 calls (seconds) to be able to send out data
for (int i = 0; i < 21; i++) {
dcdc.fuse_destruction();
dcdc_fuse_destruction(&dcdc);
}
zassert_not_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
dcdc.fuse_destruction();
dcdc_fuse_destruction(&dcdc);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge));
zassert_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
// control loop must not override the forced low side
dcdc.control();
dcdc_control(&dcdc);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge));
zassert_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
// forced state is restored after the half bridge was stopped
dcdc.stop();
dcdc.fuse_destruction();
dcdc_stop(&dcdc);
dcdc_fuse_destruction(&dcdc);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge));
zassert_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
dcdc.stop();
dcdc_stop(&dcdc);
device_status_clear_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
}