drivers: power_switch: add PWM support
Extend the power switch API for switches driven by a PWM signal, as used in PWM solar charge controllers: - Set and get the duty cycle - Check if the switch is currently conducting (momentary PWM state) - Generic event callback replacing the fault callback, which also reports the rising edges of the PWM signal New backends: - libresolar,stm32-pwm-power-switch: PWM signal generated by a channel of an STM32 timer. The output is driven inactive while switched off instead of disabling the channel, which left the pin floating. - libresolar,power-switch-emul: Emulated switch for tests on native_sim supporting all features of the API. Power-good failures and events can be injected by the test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0adc9edb7a
commit
da59ab8276
+5
-3
@@ -40,7 +40,8 @@ LoadOutput::LoadOutput(DcBus *dc_bus, const struct device *sw, int led_pos)
|
||||
}
|
||||
else {
|
||||
// not supported by all switches, so the return value is ignored
|
||||
power_switch_set_fault_callback(power_switch, short_circuit_callback, this);
|
||||
power_switch_set_callback(power_switch, POWER_SWITCH_EVENT_FAULT, short_circuit_callback,
|
||||
this);
|
||||
}
|
||||
|
||||
switch_set(false);
|
||||
@@ -158,7 +159,8 @@ void LoadOutput::stop(uint32_t flag)
|
||||
|
||||
// flicker the load LED if failure was most probably caused by the user
|
||||
if (flags_check(&error_flags,
|
||||
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) {
|
||||
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT))
|
||||
{
|
||||
#if LED_EXISTS(load)
|
||||
leds_flicker(LED_POS(load), LED_TIMEOUT_INFINITE);
|
||||
#endif
|
||||
@@ -190,7 +192,7 @@ void LoadOutput::switch_set(bool on)
|
||||
}
|
||||
}
|
||||
|
||||
void LoadOutput::short_circuit_callback(const struct device *dev, void *user_data)
|
||||
void LoadOutput::short_circuit_callback(const struct device *dev, uint32_t events, void *user_data)
|
||||
{
|
||||
static_cast<LoadOutput *>(user_data)->stop(ERR_LOAD_SHORT_CIRCUIT);
|
||||
}
|
||||
|
||||
+1
-1
@@ -164,7 +164,7 @@ private:
|
||||
/**
|
||||
* Callback for short circuits detected by the power switch hardware
|
||||
*/
|
||||
static void short_circuit_callback(const struct device *dev, void *user_data);
|
||||
static void short_circuit_callback(const struct device *dev, uint32_t events, void *user_data);
|
||||
|
||||
/**
|
||||
* Power switch device
|
||||
|
||||
@@ -3,5 +3,9 @@
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_include_directories(../stm32_tim)
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_GPIO power_switch_gpio.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_STM32_LPTIM power_switch_stm32_lptim.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_STM32_PWM power_switch_stm32_pwm.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_EMUL power_switch_emul.c)
|
||||
|
||||
@@ -5,6 +5,8 @@ menuconfig POWER_SWITCH
|
||||
bool "Power switch drivers"
|
||||
default y if DT_HAS_LIBRESOLAR_GPIO_POWER_SWITCH_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_LPTIM_POWER_SWITCH_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_PWM_POWER_SWITCH_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_POWER_SWITCH_EMUL_ENABLED
|
||||
help
|
||||
Enable drivers for power switches, e.g. used for load or USB outputs.
|
||||
|
||||
@@ -51,4 +53,20 @@ config POWER_SWITCH_STM32_LPTIM
|
||||
Enable power switch driver using LPTIM1 and COMP2 of STM32L0 for hardware short-circuit
|
||||
protection.
|
||||
|
||||
config POWER_SWITCH_STM32_PWM
|
||||
bool "STM32 timer PWM power switch driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_PWM_POWER_SWITCH_ENABLED
|
||||
select LIBRESOLAR_STM32_TIM
|
||||
help
|
||||
Enable power switch driver using a PWM signal of an STM32 timer, e.g. for PWM solar
|
||||
charge controllers.
|
||||
|
||||
config POWER_SWITCH_EMUL
|
||||
bool "Emulated power switch driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_POWER_SWITCH_EMUL_ENABLED
|
||||
help
|
||||
Enable emulated power switch driver for tests on native_sim.
|
||||
|
||||
endif # POWER_SWITCH
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Emulated power switch for tests on native_sim
|
||||
*
|
||||
* Supports all features of the power switch API. Power-good failures and events can be injected
|
||||
* via the functions in power_switch_emul.h.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_power_switch_emul
|
||||
|
||||
#include <libresolar/drivers/power_switch.h>
|
||||
#include <libresolar/drivers/power_switch_emul.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/irq.h>
|
||||
|
||||
struct power_switch_emul_data
|
||||
{
|
||||
power_switch_callback_t callback;
|
||||
void *user_data;
|
||||
uint32_t callback_events;
|
||||
float duty;
|
||||
bool on;
|
||||
bool power_good;
|
||||
};
|
||||
|
||||
static int power_switch_emul_set(const struct device *dev, bool on)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
data->on = on;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool power_switch_emul_is_on(const struct device *dev)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
return data->on;
|
||||
}
|
||||
|
||||
static bool power_switch_emul_is_conducting(const struct device *dev)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
/* no actual PWM signal emulated, so the switch is considered conducting if duty > 0 */
|
||||
return data->on && data->duty > 0.0F;
|
||||
}
|
||||
|
||||
static int power_switch_emul_power_good(const struct device *dev)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
return !data->on || data->power_good;
|
||||
}
|
||||
|
||||
static int power_switch_emul_set_duty_cycle(const struct device *dev, float duty)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
data->duty = duty;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static float power_switch_emul_get_duty_cycle(const struct device *dev)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
return data->duty;
|
||||
}
|
||||
|
||||
static int power_switch_emul_set_callback(const struct device *dev, uint32_t events,
|
||||
power_switch_callback_t callback, void *user_data)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
unsigned int key = irq_lock();
|
||||
|
||||
data->callback = callback;
|
||||
data->user_data = user_data;
|
||||
data->callback_events = events;
|
||||
|
||||
irq_unlock(key);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void power_switch_emul_set_power_good(const struct device *dev, bool power_good)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
data->power_good = power_good;
|
||||
}
|
||||
|
||||
void power_switch_emul_trigger_events(const struct device *dev, uint32_t events)
|
||||
{
|
||||
struct power_switch_emul_data *data = dev->data;
|
||||
|
||||
if (events & POWER_SWITCH_EVENT_FAULT) {
|
||||
data->on = false;
|
||||
}
|
||||
|
||||
events &= data->callback_events;
|
||||
|
||||
if (events != 0 && data->callback != NULL) {
|
||||
data->callback(dev, events, data->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
static DEVICE_API(power_switch, power_switch_emul_api) = {
|
||||
.set = power_switch_emul_set,
|
||||
.is_on = power_switch_emul_is_on,
|
||||
.is_conducting = power_switch_emul_is_conducting,
|
||||
.power_good = power_switch_emul_power_good,
|
||||
.set_duty_cycle = power_switch_emul_set_duty_cycle,
|
||||
.get_duty_cycle = power_switch_emul_get_duty_cycle,
|
||||
.set_callback = power_switch_emul_set_callback,
|
||||
};
|
||||
|
||||
#define POWER_SWITCH_EMUL_INIT(n) \
|
||||
static struct power_switch_emul_data power_switch_emul_data_##n = { \
|
||||
.duty = 1.0F, \
|
||||
.power_good = true, \
|
||||
}; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(n, NULL, NULL, &power_switch_emul_data_##n, NULL, POST_KERNEL, \
|
||||
CONFIG_POWER_SWITCH_INIT_PRIORITY, &power_switch_emul_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(POWER_SWITCH_EMUL_INIT)
|
||||
@@ -47,7 +47,7 @@ struct power_switch_stm32_lptim_config
|
||||
|
||||
struct power_switch_stm32_lptim_data
|
||||
{
|
||||
power_switch_fault_callback_t callback;
|
||||
power_switch_callback_t callback;
|
||||
void *user_data;
|
||||
bool on;
|
||||
};
|
||||
@@ -144,11 +144,15 @@ static bool power_switch_stm32_lptim_is_on(const struct device *dev)
|
||||
return data->on;
|
||||
}
|
||||
|
||||
static int power_switch_stm32_lptim_set_fault_callback(const struct device *dev,
|
||||
power_switch_fault_callback_t callback,
|
||||
void *user_data)
|
||||
static int power_switch_stm32_lptim_set_callback(const struct device *dev, uint32_t events,
|
||||
power_switch_callback_t callback, void *user_data)
|
||||
{
|
||||
struct power_switch_stm32_lptim_data *data = dev->data;
|
||||
|
||||
if (events & ~POWER_SWITCH_EVENT_FAULT) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
unsigned int key = irq_lock();
|
||||
|
||||
data->callback = callback;
|
||||
@@ -171,7 +175,7 @@ static void power_switch_stm32_lptim_isr(const struct device *dev)
|
||||
power_switch_stm32_lptim_set(dev, false);
|
||||
|
||||
if (data->callback != NULL) {
|
||||
data->callback(dev, data->user_data);
|
||||
data->callback(dev, POWER_SWITCH_EVENT_FAULT, data->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +186,7 @@ static void power_switch_stm32_lptim_isr(const struct device *dev)
|
||||
static DEVICE_API(power_switch, power_switch_stm32_lptim_api) = {
|
||||
.set = power_switch_stm32_lptim_set,
|
||||
.is_on = power_switch_stm32_lptim_is_on,
|
||||
.set_fault_callback = power_switch_stm32_lptim_set_fault_callback,
|
||||
.set_callback = power_switch_stm32_lptim_set_callback,
|
||||
};
|
||||
|
||||
static int power_switch_stm32_lptim_init(const struct device *dev)
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Power switch driven by a PWM signal of an STM32 timer
|
||||
*
|
||||
* Used for PWM solar charge controllers, where the solar panel is connected to the battery via a
|
||||
* switch with low frequency PWM.
|
||||
*
|
||||
* The output channel is always enabled after initialization. Switching on and off is done by
|
||||
* changing between PWM mode and forced inactive level, so that the output is actively driven
|
||||
* while the switch is off.
|
||||
*
|
||||
* The timer update interrupt (start of each PWM period) is used to report the rising edges of the
|
||||
* PWM signal.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_stm32_pwm_power_switch
|
||||
|
||||
#include "stm32_tim.h"
|
||||
|
||||
#include <libresolar/drivers/power_switch.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/irq.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(power_switch_stm32_pwm, CONFIG_POWER_SWITCH_LOG_LEVEL);
|
||||
|
||||
/* 1 = second-highest priority of STM32L0/F0 */
|
||||
#define TIM_IRQ_PRIORITY 1
|
||||
|
||||
struct power_switch_stm32_pwm_config
|
||||
{
|
||||
struct stm32_tim_config tim;
|
||||
void (*irq_config_func)(void);
|
||||
uint32_t frequency;
|
||||
/** Timer channel (1-4) */
|
||||
uint8_t channel;
|
||||
};
|
||||
|
||||
struct power_switch_stm32_pwm_data
|
||||
{
|
||||
power_switch_callback_t callback;
|
||||
void *user_data;
|
||||
uint32_t callback_events;
|
||||
/** Number of timer counts per PWM period */
|
||||
uint32_t period;
|
||||
volatile bool on;
|
||||
};
|
||||
|
||||
static int power_switch_stm32_pwm_set(const struct device *dev, bool on)
|
||||
{
|
||||
const struct power_switch_stm32_pwm_config *config = dev->config;
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
|
||||
LL_TIM_OC_SetMode(config->tim.timer, stm32_tim_ll_channel(config->channel),
|
||||
on ? LL_TIM_OCMODE_PWM1 : LL_TIM_OCMODE_FORCED_INACTIVE);
|
||||
|
||||
data->on = on;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool power_switch_stm32_pwm_is_on(const struct device *dev)
|
||||
{
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
|
||||
return data->on;
|
||||
}
|
||||
|
||||
static bool power_switch_stm32_pwm_is_conducting(const struct device *dev)
|
||||
{
|
||||
const struct power_switch_stm32_pwm_config *config = dev->config;
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (!data->on) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* PWM mode 1 in up-counting mode: output active as long as counter < compare */
|
||||
return LL_TIM_GetCounter(tim) < stm32_tim_get_compare(tim, config->channel);
|
||||
}
|
||||
|
||||
static int power_switch_stm32_pwm_set_duty_cycle(const struct device *dev, float duty)
|
||||
{
|
||||
const struct power_switch_stm32_pwm_config *config = dev->config;
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
|
||||
/* round to nearest value, so that small duty cycle steps are not lost */
|
||||
stm32_tim_set_compare(config->tim.timer, config->channel,
|
||||
(uint32_t)(duty * data->period + 0.5F));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static float power_switch_stm32_pwm_get_duty_cycle(const struct device *dev)
|
||||
{
|
||||
const struct power_switch_stm32_pwm_config *config = dev->config;
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
|
||||
return (float)stm32_tim_get_compare(config->tim.timer, config->channel) / data->period;
|
||||
}
|
||||
|
||||
static int power_switch_stm32_pwm_set_callback(const struct device *dev, uint32_t events,
|
||||
power_switch_callback_t callback, void *user_data)
|
||||
{
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
|
||||
if (events & ~POWER_SWITCH_EVENT_PWM_EDGE) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
unsigned int key = irq_lock();
|
||||
|
||||
data->callback = callback;
|
||||
data->user_data = user_data;
|
||||
data->callback_events = events;
|
||||
|
||||
irq_unlock(key);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void power_switch_stm32_pwm_isr(const struct device *dev)
|
||||
{
|
||||
const struct power_switch_stm32_pwm_config *config = dev->config;
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (!LL_TIM_IsActiveFlag_UPDATE(tim)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LL_TIM_ClearFlag_UPDATE(tim);
|
||||
|
||||
if (data->on && data->callback != NULL && (data->callback_events & POWER_SWITCH_EVENT_PWM_EDGE))
|
||||
{
|
||||
uint32_t compare = stm32_tim_get_compare(tim, config->channel);
|
||||
|
||||
/* no rising edge at 0% or 100% duty cycle */
|
||||
if (compare > 0 && compare < data->period) {
|
||||
data->callback(dev, POWER_SWITCH_EVENT_PWM_EDGE, data->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static DEVICE_API(power_switch, power_switch_stm32_pwm_api) = {
|
||||
.set = power_switch_stm32_pwm_set,
|
||||
.is_on = power_switch_stm32_pwm_is_on,
|
||||
.is_conducting = power_switch_stm32_pwm_is_conducting,
|
||||
.set_duty_cycle = power_switch_stm32_pwm_set_duty_cycle,
|
||||
.get_duty_cycle = power_switch_stm32_pwm_get_duty_cycle,
|
||||
.set_callback = power_switch_stm32_pwm_set_callback,
|
||||
};
|
||||
|
||||
static int power_switch_stm32_pwm_init(const struct device *dev)
|
||||
{
|
||||
const struct power_switch_stm32_pwm_config *config = dev->config;
|
||||
struct power_switch_stm32_pwm_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
uint32_t ll_channel = stm32_tim_ll_channel(config->channel);
|
||||
uint32_t tim_clk;
|
||||
int err;
|
||||
|
||||
err = stm32_tim_init(&config->tim, &tim_clk);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* smallest prescaler which allows the period to fit into a 16-bit counter */
|
||||
uint32_t clocks_per_period = tim_clk / config->frequency;
|
||||
uint32_t prescaler = (clocks_per_period - 1) / 0x10000;
|
||||
|
||||
data->period = clocks_per_period / (prescaler + 1);
|
||||
if (data->period < 2) {
|
||||
LOG_ERR("Frequency %u Hz too high", config->frequency);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
LL_TIM_SetPrescaler(tim, prescaler);
|
||||
LL_TIM_SetCounterMode(tim, LL_TIM_COUNTERMODE_UP);
|
||||
LL_TIM_SetAutoReload(tim, data->period - 1);
|
||||
LL_TIM_EnableARRPreload(tim);
|
||||
|
||||
LL_TIM_OC_SetMode(tim, ll_channel, LL_TIM_OCMODE_FORCED_INACTIVE);
|
||||
LL_TIM_OC_SetPolarity(tim, ll_channel, LL_TIM_OCPOLARITY_HIGH);
|
||||
LL_TIM_OC_EnablePreload(tim, ll_channel);
|
||||
stm32_tim_set_compare(tim, config->channel, 0);
|
||||
LL_TIM_CC_EnableChannel(tim, ll_channel);
|
||||
|
||||
#ifdef IS_TIM_BREAK_INSTANCE
|
||||
if (IS_TIM_BREAK_INSTANCE(tim)) {
|
||||
/* main output enable required for advanced-control timers */
|
||||
LL_TIM_EnableAllOutputs(tim);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* load prescaler and preload registers */
|
||||
LL_TIM_GenerateEvent_UPDATE(tim);
|
||||
LL_TIM_ClearFlag_UPDATE(tim);
|
||||
|
||||
config->irq_config_func();
|
||||
LL_TIM_EnableIT_UPDATE(tim);
|
||||
|
||||
LL_TIM_EnableCounter(tim);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* advanced-control timers have a separate update interrupt */
|
||||
#define TIM_IRQ_BY_IDX(n, cell) \
|
||||
COND_CODE_1(DT_IRQ_HAS_NAME(DT_INST_PARENT(n), up), \
|
||||
(DT_IRQ_BY_NAME(DT_INST_PARENT(n), up, cell)), \
|
||||
(DT_IRQ_BY_NAME(DT_INST_PARENT(n), global, cell)))
|
||||
|
||||
#define POWER_SWITCH_STM32_PWM_INIT(n) \
|
||||
STM32_TIM_DEFINE(n); \
|
||||
\
|
||||
static void power_switch_stm32_pwm_irq_config_##n(void) \
|
||||
{ \
|
||||
IRQ_CONNECT(TIM_IRQ_BY_IDX(n, irq), TIM_IRQ_PRIORITY, power_switch_stm32_pwm_isr, \
|
||||
DEVICE_DT_INST_GET(n), 0); \
|
||||
irq_enable(TIM_IRQ_BY_IDX(n, irq)); \
|
||||
} \
|
||||
\
|
||||
static const struct power_switch_stm32_pwm_config power_switch_stm32_pwm_config_##n = { \
|
||||
.tim = STM32_TIM_CONFIG(n), \
|
||||
.irq_config_func = power_switch_stm32_pwm_irq_config_##n, \
|
||||
.frequency = DT_INST_PROP(n, frequency), \
|
||||
.channel = DT_INST_PROP(n, channel), \
|
||||
}; \
|
||||
\
|
||||
static struct power_switch_stm32_pwm_data power_switch_stm32_pwm_data_##n; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(n, power_switch_stm32_pwm_init, NULL, &power_switch_stm32_pwm_data_##n, \
|
||||
&power_switch_stm32_pwm_config_##n, POST_KERNEL, \
|
||||
CONFIG_POWER_SWITCH_INIT_PRIORITY, &power_switch_stm32_pwm_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(POWER_SWITCH_STM32_PWM_INIT)
|
||||
@@ -0,0 +1,18 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
description: |
|
||||
Emulated power switch for tests on native_sim
|
||||
|
||||
Supports PWM, power-good feedback and events, which can be injected by the test via
|
||||
power_switch_emul.h.
|
||||
|
||||
compatible: "libresolar,power-switch-emul"
|
||||
|
||||
include: [base.yaml, power-switch-common.yaml]
|
||||
|
||||
properties:
|
||||
frequency:
|
||||
type: int
|
||||
description: |
|
||||
Emulated PWM frequency in Hz (only informative, no PWM signal is generated)
|
||||
@@ -0,0 +1,46 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
description: |
|
||||
Power switch driven by a PWM signal of an STM32 timer
|
||||
|
||||
The node must be a child of an st,stm32-timers node. The timer is used exclusively by this
|
||||
device, as the prescaler and period are determined by the switch frequency. The PWM output is
|
||||
active high.
|
||||
|
||||
Example:
|
||||
|
||||
&timers3 {
|
||||
status = "okay";
|
||||
|
||||
pwm_switch: pwm-switch {
|
||||
compatible = "libresolar,stm32-pwm-power-switch";
|
||||
pinctrl-0 = <&tim3_ch4_pb1>;
|
||||
pinctrl-names = "default";
|
||||
channel = <4>;
|
||||
frequency = <50>;
|
||||
current-max = <15>;
|
||||
};
|
||||
};
|
||||
|
||||
compatible: "libresolar,stm32-pwm-power-switch"
|
||||
|
||||
include: [base.yaml, pinctrl-device.yaml, power-switch-common.yaml]
|
||||
|
||||
properties:
|
||||
pinctrl-0:
|
||||
required: true
|
||||
|
||||
pinctrl-names:
|
||||
required: true
|
||||
|
||||
channel:
|
||||
type: int
|
||||
required: true
|
||||
enum: [1, 2, 3, 4]
|
||||
description: Timer channel driving the switch
|
||||
|
||||
frequency:
|
||||
type: int
|
||||
required: true
|
||||
description: PWM frequency in Hz
|
||||
@@ -15,12 +15,17 @@
|
||||
* switching itself, drivers may provide a power-good feedback from the switch or its gate driver
|
||||
* and report faults detected in hardware, e.g. a short circuit that already switched off the
|
||||
* output without any software interaction.
|
||||
*
|
||||
* Switches supporting PWM (e.g. used in PWM solar charge controllers) are switched on and off
|
||||
* periodically with the configured duty cycle while they are on.
|
||||
*/
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -32,14 +37,35 @@ extern "C" {
|
||||
*/
|
||||
|
||||
/**
|
||||
* Callback for faults detected and handled by the hardware
|
||||
* Power switch events
|
||||
*/
|
||||
enum power_switch_event
|
||||
{
|
||||
/**
|
||||
* Fault detected and handled by the hardware (e.g. short circuit)
|
||||
*
|
||||
* The switch is already off when the callback is called.
|
||||
*/
|
||||
POWER_SWITCH_EVENT_FAULT = BIT(0),
|
||||
|
||||
/**
|
||||
* Rising edge of the PWM signal, i.e. the switch starts conducting
|
||||
*
|
||||
* Not generated if the duty cycle is 0% or 100%.
|
||||
*/
|
||||
POWER_SWITCH_EVENT_PWM_EDGE = BIT(1),
|
||||
};
|
||||
|
||||
/**
|
||||
* Callback for power switch events
|
||||
*
|
||||
* The switch is already off when the callback is called. It may be called from an ISR.
|
||||
* The callback may be called from an ISR.
|
||||
*
|
||||
* @param dev Power switch device
|
||||
* @param user_data User data provided with power_switch_set_fault_callback()
|
||||
* @param events Bitmask of events according to enum power_switch_event
|
||||
* @param user_data User data provided with power_switch_set_callback()
|
||||
*/
|
||||
typedef void (*power_switch_fault_callback_t)(const struct device *dev, void *user_data);
|
||||
typedef void (*power_switch_callback_t)(const struct device *dev, uint32_t events, void *user_data);
|
||||
|
||||
/** @cond INTERNAL_HIDDEN */
|
||||
|
||||
@@ -47,9 +73,12 @@ __subsystem struct power_switch_driver_api
|
||||
{
|
||||
int (*set)(const struct device *dev, bool on);
|
||||
bool (*is_on)(const struct device *dev);
|
||||
bool (*is_conducting)(const struct device *dev);
|
||||
int (*power_good)(const struct device *dev);
|
||||
int (*set_fault_callback)(const struct device *dev, power_switch_fault_callback_t callback,
|
||||
void *user_data);
|
||||
int (*set_duty_cycle)(const struct device *dev, float duty);
|
||||
float (*get_duty_cycle)(const struct device *dev);
|
||||
int (*set_callback)(const struct device *dev, uint32_t events, power_switch_callback_t callback,
|
||||
void *user_data);
|
||||
};
|
||||
|
||||
/** @endcond */
|
||||
@@ -96,6 +125,29 @@ static inline bool power_switch_is_on(const struct device *dev)
|
||||
return DEVICE_API_GET(power_switch, dev)->is_on(dev);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the switch is currently conducting
|
||||
*
|
||||
* For switches with PWM, this is the momentary state of the PWM signal. For other switches, it is
|
||||
* the same as power_switch_is_on().
|
||||
*
|
||||
* Can be called from an ISR.
|
||||
*
|
||||
* @param dev Power switch device
|
||||
*
|
||||
* @returns true if the switch is conducting
|
||||
*/
|
||||
static inline bool power_switch_is_conducting(const struct device *dev)
|
||||
{
|
||||
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
|
||||
|
||||
if (api->is_conducting == NULL) {
|
||||
return api->is_on(dev);
|
||||
}
|
||||
|
||||
return api->is_conducting(dev);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the power-good feedback of the switch
|
||||
*
|
||||
@@ -117,26 +169,76 @@ static inline int power_switch_power_good(const struct device *dev)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set callback for faults detected and handled by the hardware (e.g. short circuit)
|
||||
* Set the PWM duty cycle
|
||||
*
|
||||
* The duty cycle can be set before switching on and is kept while the switch is off.
|
||||
*
|
||||
* @param dev Power switch device
|
||||
* @param duty Duty cycle between 0.0 and 1.0
|
||||
*
|
||||
* @retval 0 on success
|
||||
* @retval -EINVAL if duty cycle is out of range (setting is ignored)
|
||||
* @retval -ENOSYS if the switch does not support PWM
|
||||
*/
|
||||
static inline int power_switch_set_duty_cycle(const struct device *dev, float duty)
|
||||
{
|
||||
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
|
||||
|
||||
if (api->set_duty_cycle == NULL) {
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
if (duty < 0.0F || duty > 1.0F) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return api->set_duty_cycle(dev, duty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PWM duty cycle
|
||||
*
|
||||
* For switches without PWM support, the duty cycle is 1.0 while the switch is on.
|
||||
*
|
||||
* @param dev Power switch device
|
||||
*
|
||||
* @returns Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
static inline float power_switch_get_duty_cycle(const struct device *dev)
|
||||
{
|
||||
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
|
||||
|
||||
if (api->get_duty_cycle == NULL) {
|
||||
return api->is_on(dev) ? 1.0F : 0.0F;
|
||||
}
|
||||
|
||||
return api->get_duty_cycle(dev);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set callback for power switch events
|
||||
*
|
||||
* Only a single callback is supported. A new callback replaces the previous one.
|
||||
*
|
||||
* @param dev Power switch device
|
||||
* @param events Bitmask of events (enum power_switch_event) the callback should be called for
|
||||
* @param callback Callback function or NULL to remove the callback
|
||||
* @param user_data User data passed to the callback
|
||||
*
|
||||
* @retval 0 on success
|
||||
* @retval -ENOSYS if the switch does not detect faults in hardware
|
||||
* @retval -ENOTSUP if one of the requested events is not supported by the switch
|
||||
* @retval -ENOSYS if the switch does not support any events
|
||||
*/
|
||||
static inline int power_switch_set_fault_callback(const struct device *dev,
|
||||
power_switch_fault_callback_t callback,
|
||||
void *user_data)
|
||||
static inline int power_switch_set_callback(const struct device *dev, uint32_t events,
|
||||
power_switch_callback_t callback, void *user_data)
|
||||
{
|
||||
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
|
||||
|
||||
if (api->set_fault_callback == NULL) {
|
||||
if (api->set_callback == NULL) {
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
return api->set_fault_callback(dev, callback, user_data);
|
||||
return api->set_callback(dev, events, callback, user_data);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef LIBRESOLAR_DRIVERS_POWER_SWITCH_EMUL_H_
|
||||
#define LIBRESOLAR_DRIVERS_POWER_SWITCH_EMUL_H_
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Backend API of the emulated power switch for tests
|
||||
*/
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Set the power-good feedback reported while the switch is on
|
||||
*
|
||||
* @param dev Emulated power switch device
|
||||
* @param power_good Power-good state
|
||||
*/
|
||||
void power_switch_emul_set_power_good(const struct device *dev, bool power_good);
|
||||
|
||||
/**
|
||||
* Trigger events as if they were detected by the hardware
|
||||
*
|
||||
* A fault event switches off the output before the callback is called.
|
||||
*
|
||||
* @param dev Emulated power switch device
|
||||
* @param events Bitmask of events according to enum power_switch_event
|
||||
*/
|
||||
void power_switch_emul_trigger_events(const struct device *dev, uint32_t events);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LIBRESOLAR_DRIVERS_POWER_SWITCH_EMUL_H_ */
|
||||
@@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.20.0)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(power_switch)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
target_sources(app PRIVATE src/main.c src/emul.c)
|
||||
|
||||
@@ -27,4 +27,8 @@
|
||||
gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
|
||||
charge-pump-pwms = <&fake_pwm 0 200000>;
|
||||
};
|
||||
|
||||
emul_switch: emul-switch {
|
||||
compatible = "libresolar,power-switch-emul";
|
||||
};
|
||||
};
|
||||
|
||||
@@ -27,4 +27,8 @@
|
||||
gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
|
||||
charge-pump-pwms = <&fake_pwm 0 200000>;
|
||||
};
|
||||
|
||||
emul_switch: emul-switch {
|
||||
compatible = "libresolar,power-switch-emul";
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <libresolar/drivers/power_switch.h>
|
||||
#include <libresolar/drivers/power_switch_emul.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
static const struct device *const emul = DEVICE_DT_GET(DT_NODELABEL(emul_switch));
|
||||
|
||||
static uint32_t callback_events;
|
||||
static int callback_count;
|
||||
|
||||
static void callback(const struct device *dev, uint32_t events, void *user_data)
|
||||
{
|
||||
zassert_equal(dev, emul);
|
||||
zassert_equal(user_data, &callback_count);
|
||||
|
||||
callback_events = events;
|
||||
callback_count++;
|
||||
}
|
||||
|
||||
static void *power_switch_emul_setup(void)
|
||||
{
|
||||
zassert_true(device_is_ready(emul));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void power_switch_emul_before(void *fixture)
|
||||
{
|
||||
ARG_UNUSED(fixture);
|
||||
|
||||
zassert_ok(power_switch_off(emul));
|
||||
zassert_ok(power_switch_set_duty_cycle(emul, 1.0F));
|
||||
zassert_ok(power_switch_set_callback(emul, 0, NULL, NULL));
|
||||
power_switch_emul_set_power_good(emul, true);
|
||||
|
||||
callback_events = 0;
|
||||
callback_count = 0;
|
||||
}
|
||||
|
||||
ZTEST(power_switch_emul, test_duty_cycle)
|
||||
{
|
||||
zassert_ok(power_switch_set_duty_cycle(emul, 0.25F));
|
||||
zassert_equal(power_switch_get_duty_cycle(emul), 0.25F);
|
||||
|
||||
zassert_equal(power_switch_set_duty_cycle(emul, 1.1F), -EINVAL);
|
||||
zassert_equal(power_switch_set_duty_cycle(emul, -0.1F), -EINVAL);
|
||||
zassert_equal(power_switch_get_duty_cycle(emul), 0.25F);
|
||||
}
|
||||
|
||||
ZTEST(power_switch_emul, test_conducting)
|
||||
{
|
||||
zassert_false(power_switch_is_conducting(emul));
|
||||
|
||||
zassert_ok(power_switch_on(emul));
|
||||
zassert_true(power_switch_is_conducting(emul));
|
||||
|
||||
zassert_ok(power_switch_set_duty_cycle(emul, 0.0F));
|
||||
zassert_false(power_switch_is_conducting(emul));
|
||||
}
|
||||
|
||||
ZTEST(power_switch_emul, test_power_good)
|
||||
{
|
||||
power_switch_emul_set_power_good(emul, false);
|
||||
zassert_equal(power_switch_power_good(emul), 1, "off switch must report power good");
|
||||
|
||||
zassert_ok(power_switch_on(emul));
|
||||
zassert_equal(power_switch_power_good(emul), 0);
|
||||
}
|
||||
|
||||
ZTEST(power_switch_emul, test_fault_switches_off)
|
||||
{
|
||||
zassert_ok(
|
||||
power_switch_set_callback(emul, POWER_SWITCH_EVENT_FAULT, callback, &callback_count));
|
||||
zassert_ok(power_switch_on(emul));
|
||||
|
||||
power_switch_emul_trigger_events(emul, POWER_SWITCH_EVENT_FAULT);
|
||||
|
||||
zassert_false(power_switch_is_on(emul));
|
||||
zassert_equal(callback_count, 1);
|
||||
zassert_equal(callback_events, POWER_SWITCH_EVENT_FAULT);
|
||||
}
|
||||
|
||||
ZTEST(power_switch_emul, test_callback_only_for_requested_events)
|
||||
{
|
||||
zassert_ok(
|
||||
power_switch_set_callback(emul, POWER_SWITCH_EVENT_PWM_EDGE, callback, &callback_count));
|
||||
|
||||
power_switch_emul_trigger_events(emul, POWER_SWITCH_EVENT_FAULT);
|
||||
zassert_equal(callback_count, 0);
|
||||
|
||||
power_switch_emul_trigger_events(emul, POWER_SWITCH_EVENT_PWM_EDGE);
|
||||
zassert_equal(callback_count, 1);
|
||||
zassert_equal(callback_events, POWER_SWITCH_EVENT_PWM_EDGE);
|
||||
}
|
||||
|
||||
ZTEST_SUITE(power_switch_emul, NULL, power_switch_emul_setup, power_switch_emul_before, NULL, NULL);
|
||||
@@ -84,7 +84,18 @@ ZTEST(power_switch, test_on_off)
|
||||
ZTEST(power_switch, test_optional_functions_not_supported)
|
||||
{
|
||||
zassert_equal(power_switch_power_good(basic), -ENOSYS);
|
||||
zassert_equal(power_switch_set_fault_callback(basic, NULL, NULL), -ENOSYS);
|
||||
zassert_equal(power_switch_set_duty_cycle(basic, 0.5F), -ENOSYS);
|
||||
zassert_equal(power_switch_set_callback(basic, POWER_SWITCH_EVENT_FAULT, NULL, NULL), -ENOSYS);
|
||||
}
|
||||
|
||||
ZTEST(power_switch, test_default_duty_cycle_and_conducting)
|
||||
{
|
||||
zassert_equal(power_switch_get_duty_cycle(basic), 0.0F);
|
||||
zassert_false(power_switch_is_conducting(basic));
|
||||
|
||||
zassert_ok(power_switch_on(basic));
|
||||
zassert_equal(power_switch_get_duty_cycle(basic), 1.0F);
|
||||
zassert_true(power_switch_is_conducting(basic));
|
||||
}
|
||||
|
||||
ZTEST(power_switch, test_latching_releases_pin)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
tests:
|
||||
drivers.power_switch.gpio:
|
||||
drivers.power_switch.native_sim:
|
||||
tags:
|
||||
- drivers
|
||||
- power_switch
|
||||
|
||||
Reference in New Issue
Block a user