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:
co-authored by
Claude Opus 5.5
parent
01b48b26e1
commit
dc0356b41a
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user