app: use power switch driver for load and USB outputs

Replace the function pointers for the load and USB switches by power
switch devices. The register-level code for the short-circuit comparator
of the PWM 2420 LUS moved to the STM32 LPTIM power switch driver, which
reports short circuits via the fault callback. load_driver.c is removed.

The load and USB switches are now standalone devicetree nodes with the
labels load_switch and usb_switch instead of children of the outputs
node. The separate charge_pump node is replaced by the charge-pump-pwms
property of the load switch.

The comparator interrupt now only clears its own EXTI pending flag
instead of all pending flags.

Note for out-of-tree boards and overlays: The load and usb-pwr children
of the outputs node must be converted to nodes with the compatible
libresolar,gpio-power-switch and the labels load_switch and usb_switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-11-01 13:43:57 +01:00
co-authored by Claude Opus 5.5
parent 01b48b26e1
commit dc0356b41a
16 changed files with 157 additions and 386 deletions
+8 -1
View File
@@ -294,7 +294,7 @@ endmenu # Battery default settings
menu "Load output settings"
depends on $(dt_node_bool_prop,/outputs/load,kconfig-flag)
depends on $(dt_nodelabel_enabled,load_switch)
config LOAD_OC_RECOVERY_DELAY
int "Overcurrent recovery delay (s)"
@@ -387,6 +387,13 @@ config BAT_LOG_LEVEL
help
Log level for the battery module. 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
config LOAD_LOG_LEVEL
int "Log level for the load output module (if included)"
default 2
depends on LOG
help
Log level for the load output module (if included). 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
config PWM_LOG_LEVEL
int "Log level for the PWM module (if included)"
default 2
-1
View File
@@ -10,7 +10,6 @@ target_sources(app PRIVATE
hardware.cpp
leds.cpp
load.cpp
load_driver.c
main.cpp
power_port.cpp
pwm_switch_driver.c
+1 -1
View File
@@ -24,7 +24,7 @@ extern LoadOutput load;
// DISCHARGE_CURRENT_MAX used to estimate current-compensation of load disconnect voltage
#if BOARD_HAS_LOAD_OUTPUT
#define DISCHARGE_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max)
#define DISCHARGE_CURRENT_MAX DT_PROP(LOAD_SWITCH_NODE, current_max)
#else
#define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(pcb), dcdc_current_max)
#endif
+4 -2
View File
@@ -16,10 +16,12 @@
#include <zephyr/kernel.h>
#define HALF_BRIDGE_NODE DT_NODELABEL(halfbridge)
#define LOAD_SWITCH_NODE DT_NODELABEL(load_switch)
#define USB_SWITCH_NODE DT_NODELABEL(usb_switch)
#define BOARD_HAS_DCDC DT_NODE_HAS_STATUS_OKAY(HALF_BRIDGE_NODE)
#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))
#define BOARD_HAS_LOAD_OUTPUT DT_NODE_HAS_STATUS_OKAY(LOAD_SWITCH_NODE)
#define BOARD_HAS_USB_OUTPUT DT_NODE_HAS_STATUS_OKAY(USB_SWITCH_NODE)
#define BOARD_HAS_TEMP_FETS DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_fets))
#define BOARD_HAS_TEMP_BAT DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_bat))
+35 -11
View File
@@ -15,7 +15,13 @@
#include "helper.h"
#include "leds.h"
#define LOAD_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max)
#include <libresolar/drivers/power_switch.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(load, CONFIG_LOAD_LOG_LEVEL);
#define LOAD_CURRENT_MAX DT_PROP(LOAD_SWITCH_NODE, current_max)
#define PCB_LS_VOLTAGE_MAX DT_PROP(DT_PATH(pcb), ls_voltage_max)
#define PCB_MOSFETS_TJ_MAX DT_PROP(DT_PATH(pcb), mosfets_tj_max)
@@ -24,14 +30,18 @@
extern DeviceStatus dev_stat;
LoadOutput::LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)(),
bool (*pgood_fn)())
: PowerPort(dc_bus), switch_set(switch_fn), pgood_check(pgood_fn)
LoadOutput::LoadOutput(DcBus *dc_bus, const struct device *sw, int led_pos)
: PowerPort(dc_bus), power_switch(sw), led(led_pos)
{
state = LOAD_STATE_OFF;
// call driver initialization function
init_fn();
if (!device_is_ready(power_switch)) {
LOG_ERR("Power switch %s not ready", power_switch->name);
}
else {
// not supported by all switches, so the return value is ignored
power_switch_set_fault_callback(power_switch, short_circuit_callback, this);
}
switch_set(false);
junction_temperature = 25; // starting point: 25°C
@@ -61,7 +71,7 @@ void LoadOutput::control()
oc_timestamp = uptime();
}
if (pgood_check != NULL && !pgood_check()) {
if (power_switch_power_good(power_switch) == 0) {
flags_set(&error_flags, ERR_LOAD_OVERCURRENT);
oc_timestamp = uptime();
}
@@ -164,11 +174,25 @@ void LoadOutput::set_voltage_limits(float lvd, float lvr, float ov)
overvoltage = ov;
}
extern LoadOutput load;
void load_short_circuit_stop()
void LoadOutput::switch_set(bool on)
{
load.stop(ERR_LOAD_SHORT_CIRCUIT);
if (led >= 0) {
leds_set(led, on, LED_TIMEOUT_INFINITE);
}
if (device_is_ready(power_switch)) {
if (on) {
power_switch_on(power_switch);
}
else {
power_switch_off(power_switch);
}
}
}
void LoadOutput::short_circuit_callback(const struct device *dev, void *user_data)
{
static_cast<LoadOutput *>(user_data)->stop(ERR_LOAD_SHORT_CIRCUIT);
}
#endif /* BOARD_HAS_LOAD_OUTPUT */
+21 -29
View File
@@ -21,6 +21,8 @@
#include "power_port.h"
#include <zephyr/device.h>
/**
* Load/USB output states
*/
@@ -65,7 +67,7 @@ enum LoadErrorFlag
/**
* Short circuit detected at load port
*
* Set by LoadOutput::control() after overcurrent comparator triggered, cleared only if
* Set after the power switch reported a short circuit detected in hardware, cleared only if
* load output is manually disabled and enabled again.
*/
ERR_LOAD_SHORT_CIRCUIT = 1U << 3,
@@ -94,15 +96,13 @@ class LoadOutput : public PowerPort
{
public:
/**
* Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if
* existing)
* Initialize LoadOutput struct and switch off the output
*
* @param dc_bus DC bus the load is connected to
* @param switch_fn Pointer to function for enabling/disabling load switch
* @param init_fn Pointer to function for load driver initialization
* @param pgood_fn Pointer to pgood check
* @param power_switch Power switch device of the output
* @param led LED indicating the switch state or -1 if not existing
*/
LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)(), bool (*pgood_fn)());
LoadOutput(DcBus *dc_bus, const struct device *power_switch, int led = -1);
/** Main load control function, should be called by control timer
*
@@ -157,14 +157,24 @@ public:
private:
/**
* Pointer to the load switch function
* Switch the output and the indicator LED (if existing)
*/
void (*switch_set)(bool);
void switch_set(bool on);
/**
* Pointer to the function to check the power output status
* Callback for short circuits detected by the power switch hardware
*/
bool (*pgood_check)(void);
static void short_circuit_callback(const struct device *dev, void *user_data);
/**
* Power switch device
*/
const struct device *power_switch;
/**
* LED indicating the switch state or -1 if not existing
*/
int led;
/**
* Used to prevent switching of because of very short voltage dip
@@ -178,22 +188,4 @@ private:
};
#endif
#ifdef __cplusplus
extern "C" {
#endif
void load_out_init();
void usb_out_init();
void load_out_set(bool);
void usb_out_set(bool);
void load_short_circuit_stop();
bool pgood_check();
#ifdef __cplusplus
}
#endif
#endif /* LOAD_H */
-253
View File
@@ -1,253 +0,0 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "load.h"
#ifdef CONFIG_SOC_FAMILY_STM32
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/kernel.h>
#include "board.h"
#include "hardware.h"
#include "leds.h"
#include "mcu.h"
#include <stm32_ll_bus.h>
#include <stm32_ll_system.h>
#include <stdio.h>
#if BOARD_HAS_LOAD_OUTPUT
#define LOAD_NODE DT_CHILD(DT_PATH(outputs), load)
static const struct gpio_dt_spec load_switch = GPIO_DT_SPEC_GET(LOAD_NODE, gpios);
#endif
#if BOARD_HAS_USB_OUTPUT
#define USB_NODE DT_CHILD(DT_PATH(outputs), usb_pwr)
static const struct gpio_dt_spec usb_switch = GPIO_DT_SPEC_GET(USB_NODE, gpios);
#endif
// short circuit detection comparator only present in PWM 2420 LUS board so far
#ifdef CONFIG_BOARD_PWM_2420_LUS
static void lptim_init()
{
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPTIM1);
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
// Select alternate function mode on PB2 (first bit _1 = 1, second bit _0 = 0)
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE2)) | GPIO_MODER_MODE2_1;
// Select AF2 (LPTIM_OUT) on PB2
GPIOB->AFR[0] |= 0x2U << GPIO_AFRL_AFSEL2_Pos;
// Set prescaler to 32 (resulting in 1 MHz timer frequency)
LPTIM1->CFGR |= 0x5U << LPTIM_CFGR_PRESC_Pos;
// Enable trigger (rising edge)
LPTIM1->CFGR |= LPTIM_CFGR_TRIGEN_0;
// Select trigger 7 (COMP2_OUT)
LPTIM1->CFGR |= 0x7U << LPTIM_CFGR_TRIGSEL_Pos;
// LPTIM1->CFGR |= LPTIM_CFGR_WAVPOL;
LPTIM1->CFGR |= LPTIM_CFGR_PRELOAD;
// Glitch filter of 8 cycles
LPTIM1->CFGR |= LPTIM_CFGR_TRGFLT_0 | LPTIM_CFGR_TRGFLT_1;
// Enable set-once mode
LPTIM1->CFGR |= LPTIM_CFGR_WAVE;
// Enable timer (must be done *before* changing ARR or CMP, but *after*
// changing CFGR)
LPTIM1->CR |= LPTIM_CR_ENABLE;
// Auto Reload Register
LPTIM1->ARR = 1000;
// Set load switch-off delay in microseconds
// (actually takes approx. 4 us longer than this setting)
LPTIM1->CMP = 10;
// Continuous mode
// LPTIM1->CR |= LPTIM_CR_CNTSTRT;
// LPTIM1->CR |= LPTIM_CR_SNGSTRT;
}
void ADC1_COMP_IRQHandler(void *args)
{
// interrupt called because of COMP2?
if (COMP2->CSR & COMP_CSR_COMP2VALUE) {
// Load should be switched off by LPTIM trigger already. This interrupt
// is mainly used to indicate the failure.
load_short_circuit_stop();
}
// clear interrupt flag
EXTI->PR |= EXTI_PR_PIF22;
}
void short_circuit_comp_init()
{
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
// set GPIO pin to analog
GPIOB->MODER &= ~(GPIO_MODER_MODE4);
// enable VREFINT buffer
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP;
// select PB4 as positive input
COMP2->CSR |= COMP_INPUT_PLUS_IO2;
// select VREFINT divider as negative input
COMP2->CSR |= COMP_INPUT_MINUS_1_4VREFINT;
// propagate comparator value to LPTIM input
COMP2->CSR |= COMP_CSR_COMP2LPTIM1IN1;
// normal polarity
// COMP2->CSR |= COMP_CSR_COMP2POLARITY;
// set high-speed mode (1.2us instead of 2.5us propagation delay, but 3.5uA instead of 0.5uA
// current consumption)
// COMP2->CSR |= COMP_CSR_COMP2SPEED;
// enable COMP2
COMP2->CSR |= COMP_CSR_COMP2EN;
// enable EXTI software interrupt / event
EXTI->IMR |= EXTI_IMR_IM22;
EXTI->EMR |= EXTI_EMR_EM22;
EXTI->RTSR |= EXTI_RTSR_RT22;
EXTI->FTSR |= EXTI_FTSR_FT22;
EXTI->SWIER |= EXTI_SWIER_SWI22;
// 1 = second-highest priority of STM32L0/F0
IRQ_CONNECT(ADC1_COMP_IRQn, 1, ADC1_COMP_IRQHandler, 0, 0);
irq_enable(ADC1_COMP_IRQn);
}
#endif
void load_out_set(bool status)
{
#if LED_EXISTS(load)
leds_set(LED_POS(load), status, LED_TIMEOUT_INFINITE);
#endif
#if BOARD_HAS_LOAD_OUTPUT
if (!device_is_ready(load_switch.port)) {
printf("Load switch GPIO not ready\n");
return;
}
gpio_pin_configure_dt(&load_switch, GPIO_OUTPUT_INACTIVE);
if (status == true) {
#ifdef CONFIG_BOARD_PWM_2420_LUS
lptim_init();
#else
gpio_pin_set_dt(&load_switch, 1);
#endif
}
else {
gpio_pin_set_dt(&load_switch, 0);
}
#endif
}
void usb_out_set(bool status)
{
#if BOARD_HAS_USB_OUTPUT
if (!device_is_ready(usb_switch.port)) {
printf("USB switch GPIO not ready\n");
return;
}
gpio_pin_configure_dt(&usb_switch, GPIO_OUTPUT_INACTIVE);
if (status == true) {
gpio_pin_set_dt(&usb_switch, 1);
#if DT_PROP(DT_CHILD(DT_PATH(outputs), usb_pwr), latching_pgood)
k_sleep(K_MSEC(50));
gpio_pin_configure_dt(&usb_switch, GPIO_INPUT);
#endif
}
else {
gpio_pin_set_dt(&usb_switch, 0);
}
#endif
}
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), charge_pump))
#define CP_PWMS DT_CHILD(DT_PATH(outputs), charge_pump)
#define CP_PWM_CONTROLLER DT_PWMS_CTLR(CP_PWMS)
#define CP_PWM_PERIOD DT_PWMS_PERIOD(CP_PWMS)
#define CP_PWM_CHANNEL DT_PWMS_CHANNEL(CP_PWMS)
void load_cp_enable()
{
const struct device *pwm_dev = DEVICE_DT_GET(CP_PWM_CONTROLLER);
if (device_is_ready(pwm_dev)) {
// set to 50% duty cycle
pwm_set(pwm_dev, CP_PWM_CHANNEL, CP_PWM_PERIOD, CP_PWM_PERIOD / 2, 0);
}
}
#endif
void load_out_init()
{
// analog comparator to detect short circuits and trigger immediate load switch-off
#ifdef CONFIG_BOARD_PWM_2420_LUS
short_circuit_comp_init();
#endif
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), charge_pump))
// enable charge pump for high-side switches (if existing)
load_cp_enable();
#endif
}
void usb_out_init()
{
// nothing to do
}
bool pgood_check()
{
#if BOARD_HAS_USB_OUTPUT
return gpio_pin_get_dt(&usb_switch);
#else
return false;
#endif
}
#else // CONFIG_SOC_FAMILY_STM32
void load_out_init()
{}
void usb_out_init()
{}
void load_out_set(bool value)
{}
void usb_out_set(bool value)
{}
bool pgood_check()
{
return false;
}
#endif
+6 -2
View File
@@ -44,11 +44,15 @@ PwmSwitch pwm_switch(&lv_bus);
#endif
#if BOARD_HAS_LOAD_OUTPUT
LoadOutput load(&lv_bus, &load_out_set, &load_out_init, NULL);
#if LED_EXISTS(load)
LoadOutput load(&lv_bus, DEVICE_DT_GET(LOAD_SWITCH_NODE), LED_POS(load));
#else
LoadOutput load(&lv_bus, DEVICE_DT_GET(LOAD_SWITCH_NODE));
#endif
#endif
#if BOARD_HAS_USB_OUTPUT
LoadOutput usb_pwr(&lv_bus, &usb_out_set, &usb_out_init, &pgood_check);
LoadOutput usb_pwr(&lv_bus, DEVICE_DT_GET(USB_SWITCH_NODE));
#endif
#if CONFIG_HV_TERMINAL_SOLAR
@@ -58,19 +58,20 @@
};
};
load_switch: load-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
current-max = <10>;
};
usb_switch: usb-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>;
};
outputs {
compatible = "outputs";
load {
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
current-max = <10>;
kconfig-flag; /* make node visible in Kconfig */
};
usb-pwr {
gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>;
};
boot0 {
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
};
@@ -42,14 +42,6 @@
outputs {
compatible = "outputs";
load {
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
};
usb-pwr {
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
};
/delete-node/ v-solar;
/delete-node/ boot0;
};
@@ -74,3 +66,11 @@
/delete-node/ temp-bat;
};
};
&load_switch {
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
};
&usb_switch {
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
};
@@ -49,19 +49,17 @@
};
};
load_switch: load-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
/* 200 us period = 5 kHz */
charge-pump-pwms = <&pwm8 2 200000 PWM_POLARITY_NORMAL>;
current-max = <20>;
};
outputs {
compatible = "outputs";
load {
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
current-max = <20>;
kconfig-flag; /* make node visible in Kconfig */
};
charge_pump {
pwms = <&pwm8 2 200000 PWM_POLARITY_NORMAL>; /* 200 us period = 5 kHz */
};
can_en {
gpios = <&gpiob 11 GPIO_ACTIVE_LOW>;
};
@@ -42,6 +42,14 @@
};
};
load_switch: load-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpiob 15 GPIO_ACTIVE_LOW>;
/* 200 us period = 5 kHz */
charge-pump-pwms = <&pwm8 1 200000 PWM_POLARITY_NORMAL>;
current-max = <20>;
};
outputs {
compatible = "outputs";
@@ -51,16 +59,6 @@
kconfig-flag; /* make node visible in Kconfig */
};
load {
gpios = <&gpiob 15 GPIO_ACTIVE_LOW>;
current-max = <20>;
kconfig-flag; /* make node visible in Kconfig */
};
charge_pump {
pwms = <&pwm8 1 200000 PWM_POLARITY_NORMAL>; /* 200 us period = 5 kHz */
};
hv-out {
gpios = <&gpiob 12 GPIO_ACTIVE_LOW>;
};
@@ -48,15 +48,15 @@
};
};
load_switch: load-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
current-max = <20>;
};
outputs {
compatible = "outputs";
load {
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
current-max = <20>;
kconfig-flag; /* make node visible in Kconfig */
};
uext_en {
gpios = <&gpioc 14 GPIO_ACTIVE_LOW>;
};
+14 -10
View File
@@ -61,6 +61,20 @@
};
};
/* load switch with short-circuit comparator at i-load measurement (PB4) */
load_switch: load-switch {
compatible = "libresolar,stm32-lptim-power-switch";
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
pinctrl-0 = <&lptim1_out_pb2 &comp2_inp_pb4>;
pinctrl-names = "default";
current-max = <15>;
};
usb_switch: usb-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpiob 5 GPIO_ACTIVE_LOW>;
};
outputs {
compatible = "outputs";
@@ -72,16 +86,6 @@
kconfig-flag; /* make node visible in Kconfig */
};
load {
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
current-max = <15>;
kconfig-flag; /* make node visible in Kconfig */
};
usb-pwr {
gpios = <&gpiob 5 GPIO_ACTIVE_LOW>;
};
boot0 {
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
};
+6 -6
View File
@@ -43,6 +43,12 @@
};
};
load_switch: load-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
current-max = <20>;
};
outputs {
compatible = "outputs";
@@ -52,12 +58,6 @@
kconfig-flag; /* make node visible in Kconfig */
};
load {
gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
current-max = <20>;
kconfig-flag; /* make node visible in Kconfig */
};
can_en {
gpios = <&gpio0 11 GPIO_ACTIVE_LOW>;
};
+21 -26
View File
@@ -14,17 +14,9 @@
#include "setup.h"
static bool output_on;
#include <libresolar/drivers/power_switch.h>
static void load_drv_set(bool status)
{
output_on = status;
}
static void load_drv_init()
{
output_on = false;
}
static const struct device *const load_switch = DEVICE_DT_GET(LOAD_SWITCH_NODE);
static void load_init(LoadOutput *l, bool on = false, int num_batteries = 1)
{
@@ -42,7 +34,7 @@ static void load_init(LoadOutput *l, bool on = false, int num_batteries = 1)
l->enable = true;
if (on) {
load_drv_set(true);
power_switch_on(load_switch);
l->state = LOAD_STATE_ON;
// check if it stays on
@@ -54,19 +46,21 @@ static void load_init(LoadOutput *l, bool on = false, int num_batteries = 1)
ZTEST(load, test_control_off_to_on_if_everything_fine)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
zassert_false(power_switch_is_on(load_switch));
load_out.enable = true;
load_out.control();
zassert_equal(load_out.error_flags, 0);
zassert_equal(load_out.state, LOAD_STATE_ON);
zassert_true(power_switch_is_on(load_switch));
}
ZTEST(load, test_control_off_to_on_if_everything_fine_dual_battery)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, false, 2);
load_out.enable = true;
@@ -78,19 +72,20 @@ ZTEST(load, test_control_off_to_on_if_everything_fine_dual_battery)
ZTEST(load, test_control_on_to_off_shedding)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
bus.voltage = load_out.disconnect_voltage - 0.1f;
load_out.control();
zassert_equal(load_out.error_flags, ERR_LOAD_SHEDDING);
zassert_equal(load_out.state, LOAD_STATE_OFF);
zassert_false(power_switch_is_on(load_switch));
}
ZTEST(load, test_control_on_to_off_overvoltage)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
bus.voltage = bus.sink_voltage_intercept + 0.6f;
@@ -111,7 +106,7 @@ ZTEST(load, test_control_on_to_off_overvoltage)
ZTEST(load, test_control_on_to_off_overvoltage_dual_battery)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true, 2);
bus.voltage = (bus.sink_voltage_intercept + 0.6f) * bus.series_multiplier;
@@ -132,11 +127,11 @@ ZTEST(load, test_control_on_to_off_overvoltage_dual_battery)
ZTEST(load, test_control_on_to_off_overcurrent)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
// current slightly below factor 2 so that it is not switched off immediately
load_out.current = DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max) * 1.9f;
load_out.current = DT_PROP(LOAD_SWITCH_NODE, current_max) * 1.9f;
load_out.control();
zassert_equal(load_out.state, LOAD_STATE_ON);
@@ -153,7 +148,7 @@ ZTEST(load, test_control_on_to_off_overcurrent)
ZTEST(load, test_control_on_to_off_voltage_dip)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
load_out.stop(ERR_LOAD_VOLTAGE_DIP);
@@ -165,7 +160,7 @@ ZTEST(load, test_control_on_to_off_voltage_dip)
ZTEST(load, test_control_on_to_off_bus_limit)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
load_out.bus->src_current_margin = 0;
@@ -177,7 +172,7 @@ ZTEST(load, test_control_on_to_off_bus_limit)
ZTEST(load, test_control_on_to_off_if_enable_false)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, true);
load_out.enable = false;
@@ -189,7 +184,7 @@ ZTEST(load, test_control_on_to_off_if_enable_false)
ZTEST(load, test_control_off_shedding_to_on_after_delay)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
load_out.error_flags = ERR_LOAD_SHEDDING;
@@ -207,7 +202,7 @@ ZTEST(load, test_control_off_shedding_to_on_after_delay)
ZTEST(load, test_control_off_overcurrent_to_on_after_delay)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
load_out.error_flags = ERR_LOAD_OVERCURRENT;
@@ -225,7 +220,7 @@ ZTEST(load, test_control_off_overcurrent_to_on_after_delay)
ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
bus.voltage = load_out.overvoltage + 0.1f;
load_out.error_flags = ERR_LOAD_OVERVOLTAGE;
@@ -248,7 +243,7 @@ ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage)
ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage_dual_battery)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out, false, 2);
bus.voltage = (load_out.overvoltage + 0.1f) * bus.series_multiplier;
load_out.error_flags = ERR_LOAD_OVERVOLTAGE;
@@ -271,7 +266,7 @@ ZTEST(load, test_control_off_overvoltage_to_on_at_lower_voltage_dual_battery)
ZTEST(load, test_control_off_short_circuit_flag_reset)
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
LoadOutput load_out(&bus, load_switch);
load_init(&load_out);
load_out.error_flags = ERR_LOAD_SHORT_CIRCUIT;