drivers: power_switch: add power switch driver

Add a driver class for power switches, e.g. used for load and USB
outputs. Besides switching on and off, drivers may provide a power-good
feedback and report faults that were already handled in hardware.

Two backends are available:

- libresolar,gpio-power-switch: Switch driven by a GPIO with optional
  latching power-good feedback and charge pump PWM for high-side gate
  drivers. The latch time is handled by delayable work instead of
  blocking the caller.
- libresolar,stm32-lptim-power-switch: Switch driven by LPTIM1 of
  STM32L0, which is triggered by COMP2 to switch off within a few
  microseconds in case of a short circuit (PWM 2420 LUS).

The GPIO backend is tested on native_sim using the GPIO emulator and the
fake PWM driver.

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 a7838b022c
commit 01b48b26e1
18 changed files with 951 additions and 0 deletions
+1
View File
@@ -47,5 +47,6 @@ The full license text is available at `<https://creativecommons.org/licenses/by-
src/api/half_bridge
src/api/load
src/api/power_port
src/api/power_switch
src/api/pwm_switch
src/api/misc
+5
View File
@@ -0,0 +1,5 @@
Power Switch Driver
===================
.. doxygenfile:: power_switch.h
:project: app
+1
View File
@@ -3,3 +3,4 @@
add_subdirectory_ifdef(CONFIG_DAQ daq)
add_subdirectory_ifdef(CONFIG_HALF_BRIDGE half_bridge)
add_subdirectory_ifdef(CONFIG_POWER_SWITCH power_switch)
+1
View File
@@ -5,5 +5,6 @@ menu "Libre Solar drivers"
rsource "daq/Kconfig"
rsource "half_bridge/Kconfig"
rsource "power_switch/Kconfig"
endmenu
+7
View File
@@ -0,0 +1,7 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
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)
+54
View File
@@ -0,0 +1,54 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
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
help
Enable drivers for power switches, e.g. used for load or USB outputs.
if POWER_SWITCH
config POWER_SWITCH_INIT_PRIORITY
int "Power switch init priority"
default 70
help
Power switch driver device initialization priority. Must be lower than the priority
of the GPIO and PWM drivers.
module = POWER_SWITCH
module-str = power_switch
source "subsys/logging/Kconfig.template.log_config"
config POWER_SWITCH_GPIO
bool "GPIO power switch driver"
default y
depends on DT_HAS_LIBRESOLAR_GPIO_POWER_SWITCH_ENABLED
select GPIO
help
Enable driver for power switches driven by a GPIO.
DT_COMPAT_LIBRESOLAR_GPIO_POWER_SWITCH := libresolar,gpio-power-switch
config POWER_SWITCH_GPIO_CHARGE_PUMP
bool
default y
depends on POWER_SWITCH_GPIO
depends on $(dt_compat_any_has_prop,$(DT_COMPAT_LIBRESOLAR_GPIO_POWER_SWITCH),charge-pump-pwms)
select PWM
help
Support for charge pump PWM signals of GPIO power switches.
config POWER_SWITCH_STM32_LPTIM
bool "STM32 LPTIM power switch driver with short-circuit protection"
default y
depends on DT_HAS_LIBRESOLAR_STM32_LPTIM_POWER_SWITCH_ENABLED
depends on SOC_SERIES_STM32L0X
select GPIO
select PINCTRL
help
Enable power switch driver using LPTIM1 and COMP2 of STM32L0 for hardware short-circuit
protection.
endif # POWER_SWITCH
+196
View File
@@ -0,0 +1,196 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Power switch driven by a GPIO
*
* Optionally, the GPIO is used as a power-good feedback of a latching switch: The pin is driven
* active for a short time to turn the switch on and afterwards configured as an input. The switch
* latches its state and pulls the pin low if it turned off because of a fault.
*
* An optional PWM signal drives the charge pump of a high-side gate driver.
*/
#define DT_DRV_COMPAT libresolar_gpio_power_switch
#include <libresolar/drivers/power_switch.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/spinlock.h>
LOG_MODULE_REGISTER(power_switch_gpio, CONFIG_POWER_SWITCH_LOG_LEVEL);
/** Time the GPIO is driven active before switching to power-good input */
#define LATCH_TIME_MS 50
struct power_switch_gpio_config
{
struct gpio_dt_spec gpio;
#ifdef CONFIG_POWER_SWITCH_GPIO_CHARGE_PUMP
struct pwm_dt_spec charge_pump;
bool has_charge_pump;
#endif
bool latching_pgood;
};
struct power_switch_gpio_data
{
const struct device *dev;
struct k_work_delayable latch_work;
struct k_spinlock lock;
bool on;
};
static void latch_work_handler(struct k_work *work)
{
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
struct power_switch_gpio_data *data =
CONTAINER_OF(dwork, struct power_switch_gpio_data, latch_work);
const struct power_switch_gpio_config *config = data->dev->config;
K_SPINLOCK(&data->lock)
{
/* the switch might have been turned off in the meantime */
if (data->on) {
gpio_pin_configure_dt(&config->gpio, GPIO_INPUT);
}
}
}
static int power_switch_gpio_set(const struct device *dev, bool on)
{
const struct power_switch_gpio_config *config = dev->config;
struct power_switch_gpio_data *data = dev->data;
int err;
if (!on && config->latching_pgood) {
/* returns immediately, so it can be called from an ISR */
k_work_cancel_delayable(&data->latch_work);
}
K_SPINLOCK(&data->lock)
{
/* configure instead of set, as the pin might be an input in case of latching pgood */
err = gpio_pin_configure_dt(&config->gpio, on ? GPIO_OUTPUT_ACTIVE : GPIO_OUTPUT_INACTIVE);
data->on = on && err == 0;
}
if (err != 0) {
LOG_ERR("Failed to switch %s: %d", on ? "on" : "off", err);
return err;
}
if (on && config->latching_pgood) {
k_work_reschedule(&data->latch_work, K_MSEC(LATCH_TIME_MS));
}
return 0;
}
static bool power_switch_gpio_is_on(const struct device *dev)
{
struct power_switch_gpio_data *data = dev->data;
return data->on;
}
static int power_switch_gpio_power_good(const struct device *dev)
{
const struct power_switch_gpio_config *config = dev->config;
struct power_switch_gpio_data *data = dev->data;
if (!config->latching_pgood) {
return -ENOSYS;
}
if (!data->on || k_work_delayable_is_pending(&data->latch_work)) {
/* switch is off or still actively driven on */
return 1;
}
int ret = gpio_pin_get_dt(&config->gpio);
return ret < 0 ? ret : (ret > 0);
}
static DEVICE_API(power_switch, power_switch_gpio_api) = {
.set = power_switch_gpio_set,
.is_on = power_switch_gpio_is_on,
.power_good = power_switch_gpio_power_good,
};
static int power_switch_gpio_init(const struct device *dev)
{
const struct power_switch_gpio_config *config = dev->config;
struct power_switch_gpio_data *data = dev->data;
int err;
data->dev = dev;
k_work_init_delayable(&data->latch_work, latch_work_handler);
if (!gpio_is_ready_dt(&config->gpio)) {
LOG_ERR("GPIO not ready");
return -ENODEV;
}
err = gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_INACTIVE);
if (err != 0) {
return err;
}
#ifdef CONFIG_POWER_SWITCH_GPIO_CHARGE_PUMP
if (config->has_charge_pump) {
if (!pwm_is_ready_dt(&config->charge_pump)) {
LOG_ERR("Charge pump PWM not ready");
return -ENODEV;
}
/* runs continuously with 50% duty cycle */
err = pwm_set_pulse_dt(&config->charge_pump, config->charge_pump.period / 2);
if (err != 0) {
LOG_ERR("Failed to start charge pump: %d", err);
return err;
}
}
#endif
return 0;
}
#define CHARGE_PUMP_PWM_SPEC(n) \
{ \
.dev = DEVICE_DT_GET(DT_INST_PHANDLE(n, charge_pump_pwms)), \
.channel = DT_INST_PHA(n, charge_pump_pwms, channel), \
.period = DT_INST_PHA(n, charge_pump_pwms, period), \
.flags = DT_INST_PHA_BY_IDX_OR(n, charge_pump_pwms, 0, flags, 0), \
}
#ifdef CONFIG_POWER_SWITCH_GPIO_CHARGE_PUMP
#define CHARGE_PUMP_CONFIG(n) \
IF_ENABLED(DT_INST_NODE_HAS_PROP(n, charge_pump_pwms), \
(.charge_pump = CHARGE_PUMP_PWM_SPEC(n), .has_charge_pump = true, ))
#else
#define CHARGE_PUMP_CONFIG(n)
#endif
#define POWER_SWITCH_GPIO_INIT(n) \
static const struct power_switch_gpio_config power_switch_gpio_config_##n = { \
.gpio = GPIO_DT_SPEC_INST_GET(n, gpios), \
CHARGE_PUMP_CONFIG(n).latching_pgood = DT_INST_PROP(n, latching_pgood), \
}; \
\
static struct power_switch_gpio_data power_switch_gpio_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, power_switch_gpio_init, NULL, &power_switch_gpio_data_##n, \
&power_switch_gpio_config_##n, POST_KERNEL, \
CONFIG_POWER_SWITCH_INIT_PRIORITY, &power_switch_gpio_api);
DT_INST_FOREACH_STATUS_OKAY(POWER_SWITCH_GPIO_INIT)
@@ -0,0 +1,227 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Power switch with hardware short-circuit protection for STM32L0
*
* The switch is driven by the LPTIM1 output while it is on. The comparator COMP2 compares the
* current measurement with a fraction of VREFINT and triggers LPTIM1 in set-once mode, which
* switches off the output a few microseconds after a short circuit without any software
* interaction. The comparator interrupt is used afterwards to report the fault.
*
* While the switch is off, the pin is configured as a GPIO output driven inactive.
*
* Only a single instance is supported, as LPTIM1 and COMP2 are hard-wired.
*/
#define DT_DRV_COMPAT libresolar_stm32_lptim_power_switch
#include <libresolar/drivers/power_switch.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/irq.h>
#include <zephyr/logging/log.h>
#include <soc.h>
#include <stm32_ll_bus.h>
LOG_MODULE_REGISTER(power_switch_stm32_lptim, CONFIG_POWER_SWITCH_LOG_LEVEL);
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1,
"Only a single STM32 LPTIM power switch is supported");
/* 1 = second-highest priority of STM32L0/F0 */
#define COMP_IRQ_PRIORITY 1
struct power_switch_stm32_lptim_config
{
struct gpio_dt_spec gpio;
const struct pinctrl_dev_config *pcfg;
};
struct power_switch_stm32_lptim_data
{
power_switch_fault_callback_t callback;
void *user_data;
bool on;
};
static void lptim_init(void)
{
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPTIM1);
/* 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_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;
/* switch-off delay in microseconds (actually takes approx. 4 us longer than this setting) */
LPTIM1->CMP = 10;
}
static void comp_init(void)
{
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
/* 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;
/* 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;
}
static int power_switch_stm32_lptim_set(const struct device *dev, bool on)
{
const struct power_switch_stm32_lptim_config *config = dev->config;
struct power_switch_stm32_lptim_data *data = dev->data;
int err;
/* back to GPIO mode, which also switches off the output if it was driven by the LPTIM */
err = gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_INACTIVE);
if (err != 0) {
return err;
}
if (on) {
lptim_init();
/* hand over the pin to the LPTIM output */
err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
if (err != 0) {
return err;
}
}
data->on = on;
return 0;
}
static bool power_switch_stm32_lptim_is_on(const struct device *dev)
{
struct power_switch_stm32_lptim_data *data = dev->data;
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)
{
struct power_switch_stm32_lptim_data *data = dev->data;
unsigned int key = irq_lock();
data->callback = callback;
data->user_data = user_data;
irq_unlock(key);
return 0;
}
static void power_switch_stm32_lptim_isr(const struct device *dev)
{
struct power_switch_stm32_lptim_data *data = dev->data;
if (COMP2->CSR & COMP_CSR_COMP2VALUE) {
/*
* The output was already switched off by the LPTIM trigger. Keep it off permanently
* by configuring the pin as GPIO again.
*/
power_switch_stm32_lptim_set(dev, false);
if (data->callback != NULL) {
data->callback(dev, data->user_data);
}
}
/* clear interrupt flag */
EXTI->PR = EXTI_PR_PIF22;
}
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,
};
static int power_switch_stm32_lptim_init(const struct device *dev)
{
const struct power_switch_stm32_lptim_config *config = dev->config;
int err;
if (!gpio_is_ready_dt(&config->gpio)) {
LOG_ERR("GPIO not ready");
return -ENODEV;
}
err = gpio_pin_configure_dt(&config->gpio, GPIO_OUTPUT_INACTIVE);
if (err != 0) {
return err;
}
comp_init();
IRQ_CONNECT(ADC1_COMP_IRQn, COMP_IRQ_PRIORITY, power_switch_stm32_lptim_isr,
DEVICE_DT_INST_GET(0), 0);
irq_enable(ADC1_COMP_IRQn);
return 0;
}
#define POWER_SWITCH_STM32_LPTIM_INIT(n) \
PINCTRL_DT_INST_DEFINE(n); \
\
static const struct power_switch_stm32_lptim_config power_switch_stm32_lptim_config_##n = { \
.gpio = GPIO_DT_SPEC_INST_GET(n, gpios), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
}; \
\
static struct power_switch_stm32_lptim_data power_switch_stm32_lptim_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, power_switch_stm32_lptim_init, NULL, \
&power_switch_stm32_lptim_data_##n, \
&power_switch_stm32_lptim_config_##n, POST_KERNEL, \
CONFIG_POWER_SWITCH_INIT_PRIORITY, &power_switch_stm32_lptim_api);
DT_INST_FOREACH_STATUS_OKAY(POWER_SWITCH_STM32_LPTIM_INIT)
@@ -0,0 +1,41 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
description: |
Power switch driven by a GPIO
Example of a high-side load switch with charge pump:
load_switch: load-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
charge-pump-pwms = <&pwm8 2 PWM_USEC(200) PWM_POLARITY_NORMAL>;
current-max = <20>;
};
compatible: "libresolar,gpio-power-switch"
include: [base.yaml, power-switch-common.yaml]
properties:
gpios:
type: phandle-array
required: true
description: GPIO to switch the output on and off
latching-pgood:
type: boolean
description: |
The switch latches its on state and the GPIO is used as power-good feedback
The GPIO is driven active for a short time to switch on and configured as an input
afterwards. The switch pulls the pin to inactive level if it turned off because of a
fault (e.g. overcurrent).
charge-pump-pwms:
type: phandle-array
specifier-space: pwm
description: |
PWM signal for the charge pump of a high-side gate driver
The PWM runs continuously with 50% duty cycle after initialization.
@@ -0,0 +1,38 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
description: |
Power switch with hardware short-circuit protection for STM32L0
While switched on, the output is driven by LPTIM1, which is triggered by COMP2 and switches
off the output a few microseconds after a short circuit was detected. The positive comparator
input is the current measurement, the negative input is 1/4 VREFINT.
The pinctrl default state must contain the LPTIM1 output on the pin specified in gpios and the
COMP2 positive input.
Example:
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>;
};
compatible: "libresolar,stm32-lptim-power-switch"
include: [base.yaml, pinctrl-device.yaml, power-switch-common.yaml]
properties:
gpios:
type: phandle-array
required: true
description: Output pin used as GPIO while the switch is off
pinctrl-0:
required: true
pinctrl-names:
required: true
@@ -0,0 +1,9 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
description: Common properties for power switches
properties:
current-max:
type: int
description: Maximum continuous current of the switch in amps
+148
View File
@@ -0,0 +1,148 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef LIBRESOLAR_DRIVERS_POWER_SWITCH_H_
#define LIBRESOLAR_DRIVERS_POWER_SWITCH_H_
/**
* @file
* @brief Power switch driver API
*
* A power switch connects or disconnects a power path, e.g. a load or USB output. Besides the
* 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.
*/
#include <zephyr/device.h>
#include <errno.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup power_switch_interface Power switch
* @{
*/
/**
* Callback for faults detected and handled by the hardware
*
* The switch is already off when the callback is called. It may be called from an ISR.
*
* @param dev Power switch device
* @param user_data User data provided with power_switch_set_fault_callback()
*/
typedef void (*power_switch_fault_callback_t)(const struct device *dev, void *user_data);
/** @cond INTERNAL_HIDDEN */
__subsystem struct power_switch_driver_api
{
int (*set)(const struct device *dev, bool on);
bool (*is_on)(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);
};
/** @endcond */
/**
* Switch the output on
*
* @param dev Power switch device
*
* @retval 0 on success
* @retval -errno otherwise
*/
static inline int power_switch_on(const struct device *dev)
{
return DEVICE_API_GET(power_switch, dev)->set(dev, true);
}
/**
* Switch the output off
*
* Can be called from an ISR.
*
* @param dev Power switch device
*
* @retval 0 on success
* @retval -errno otherwise
*/
static inline int power_switch_off(const struct device *dev)
{
return DEVICE_API_GET(power_switch, dev)->set(dev, false);
}
/**
* Get the commanded state of the switch
*
* The state changes to off if the hardware switched off the output because of a fault.
*
* @param dev Power switch device
*
* @returns true if the switch is on
*/
static inline bool power_switch_is_on(const struct device *dev)
{
return DEVICE_API_GET(power_switch, dev)->is_on(dev);
}
/**
* Check the power-good feedback of the switch
*
* @param dev Power switch device
*
* @retval 1 if the power is good or the switch is off
* @retval 0 if the switch is on, but the power-good signal indicates a failure
* @retval -ENOSYS if the switch does not provide a power-good feedback
*/
static inline int power_switch_power_good(const struct device *dev)
{
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
if (api->power_good == NULL) {
return -ENOSYS;
}
return api->power_good(dev);
}
/**
* Set callback for faults detected and handled by the hardware (e.g. short circuit)
*
* @param dev Power switch device
* @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
*/
static inline int power_switch_set_fault_callback(const struct device *dev,
power_switch_fault_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) {
return -ENOSYS;
}
return api->set_fault_callback(dev, callback, user_data);
}
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* LIBRESOLAR_DRIVERS_POWER_SWITCH_H_ */
@@ -0,0 +1,8 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
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)
@@ -0,0 +1,30 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/ {
fake_pwm: fake-pwm {
compatible = "zephyr,fake-pwm";
#pwm-cells = <2>;
frequency = <1000000>;
};
basic_switch: basic-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
};
latching_switch: latching-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>;
latching-pgood;
};
charge_pump_switch: charge-pump-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
charge-pump-pwms = <&fake_pwm 0 200000>;
};
};
@@ -0,0 +1,30 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/ {
fake_pwm: fake-pwm {
compatible = "zephyr,fake-pwm";
#pwm-cells = <2>;
frequency = <1000000>;
};
basic_switch: basic-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
};
latching_switch: latching-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>;
latching-pgood;
};
charge_pump_switch: charge-pump-switch {
compatible = "libresolar,gpio-power-switch";
gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
charge-pump-pwms = <&fake_pwm 0 200000>;
};
};
+2
View File
@@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_GPIO=y
+143
View File
@@ -0,0 +1,143 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <libresolar/drivers/power_switch.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/drivers/pwm/pwm_fake.h>
#include <zephyr/fff.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
DEFINE_FFF_GLOBALS;
#define BASIC_NODE DT_NODELABEL(basic_switch)
#define LATCHING_NODE DT_NODELABEL(latching_switch)
#define CHARGE_PUMP_NODE DT_NODELABEL(charge_pump_switch)
/* must be larger than the latch time of the driver */
#define LATCH_WAIT_MS 100
static const struct device *const basic = DEVICE_DT_GET(BASIC_NODE);
static const struct device *const latching = DEVICE_DT_GET(LATCHING_NODE);
static const struct gpio_dt_spec basic_gpio = GPIO_DT_SPEC_GET(BASIC_NODE, gpios);
static const struct gpio_dt_spec latching_gpio = GPIO_DT_SPEC_GET(LATCHING_NODE, gpios);
/* charge pump calls during driver initialization (fake is reset before each test) */
static unsigned int charge_pump_call_count;
static uint32_t charge_pump_period;
static uint32_t charge_pump_pulse;
static void *power_switch_setup(void)
{
charge_pump_call_count = fake_pwm_set_cycles_fake.call_count;
charge_pump_period = fake_pwm_set_cycles_fake.arg2_val;
charge_pump_pulse = fake_pwm_set_cycles_fake.arg3_val;
zassert_true(device_is_ready(basic));
zassert_true(device_is_ready(latching));
return NULL;
}
static void power_switch_before(void *fixture)
{
ARG_UNUSED(fixture);
zassert_ok(power_switch_off(basic));
zassert_ok(power_switch_off(latching));
}
static bool is_input(const struct gpio_dt_spec *spec)
{
gpio_flags_t flags;
zassert_ok(gpio_emul_flags_get(spec->port, spec->pin, &flags));
return (flags & GPIO_INPUT) != 0 && (flags & GPIO_OUTPUT) == 0;
}
ZTEST(power_switch, test_off_after_init)
{
/* active low, so the physical level must be high */
zassert_equal(gpio_emul_output_get(basic_gpio.port, basic_gpio.pin), 1);
zassert_false(power_switch_is_on(basic));
}
ZTEST(power_switch, test_on_off)
{
zassert_ok(power_switch_on(basic));
zassert_true(power_switch_is_on(basic));
zassert_equal(gpio_emul_output_get(basic_gpio.port, basic_gpio.pin), 0);
zassert_ok(power_switch_off(basic));
zassert_false(power_switch_is_on(basic));
zassert_equal(gpio_emul_output_get(basic_gpio.port, basic_gpio.pin), 1);
}
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);
}
ZTEST(power_switch, test_latching_releases_pin)
{
zassert_ok(power_switch_on(latching));
zassert_false(is_input(&latching_gpio));
zassert_equal(gpio_emul_output_get(latching_gpio.port, latching_gpio.pin), 1);
zassert_equal(power_switch_power_good(latching), 1);
k_msleep(LATCH_WAIT_MS);
zassert_true(is_input(&latching_gpio));
zassert_true(power_switch_is_on(latching));
}
ZTEST(power_switch, test_latching_power_good)
{
zassert_ok(power_switch_on(latching));
k_msleep(LATCH_WAIT_MS);
zassert_ok(gpio_emul_input_set(latching_gpio.port, latching_gpio.pin, 1));
zassert_equal(power_switch_power_good(latching), 1);
/* switch latched off because of a fault */
zassert_ok(gpio_emul_input_set(latching_gpio.port, latching_gpio.pin, 0));
zassert_equal(power_switch_power_good(latching), 0);
/* power good is reported for a switch commanded off */
zassert_ok(power_switch_off(latching));
zassert_equal(power_switch_power_good(latching), 1);
zassert_false(is_input(&latching_gpio));
zassert_equal(gpio_emul_output_get(latching_gpio.port, latching_gpio.pin), 0);
}
ZTEST(power_switch, test_latching_off_before_release)
{
zassert_ok(power_switch_on(latching));
zassert_ok(power_switch_off(latching));
k_msleep(LATCH_WAIT_MS);
/* pin must stay an output driving the switch off */
zassert_false(is_input(&latching_gpio));
zassert_equal(gpio_emul_output_get(latching_gpio.port, latching_gpio.pin), 0);
}
ZTEST(power_switch, test_charge_pump_started_at_init)
{
zassert_true(device_is_ready(DEVICE_DT_GET(CHARGE_PUMP_NODE)));
zassert_equal(charge_pump_call_count, 1);
zassert_true(charge_pump_period > 0);
zassert_equal(charge_pump_pulse, charge_pump_period / 2);
}
ZTEST_SUITE(power_switch, NULL, power_switch_setup, power_switch_before, NULL, NULL);
+10
View File
@@ -0,0 +1,10 @@
tests:
drivers.power_switch.gpio:
tags:
- drivers
- power_switch
platform_allow:
- native_sim
- native_sim/native/64
integration_platforms:
- native_sim