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
-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