drivers: half_bridge: add emulated driver and tests
Add an emulated half-bridge driver for tests on native_sim. It behaves
like an edge-aligned STM32 timer, similar to the dummy registers used
in the unit tests so far.
The driver API is tested in a new ztest suite, which can be run with
twister:
west twister -T tests/drivers -p native_sim
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5e0c6dc73f
commit
749c84eee1
@@ -2,3 +2,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_EMUL half_bridge_emul.c)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
menuconfig HALF_BRIDGE
|
||||
bool "Half-bridge drivers"
|
||||
default y if DT_HAS_LIBRESOLAR_HALF_BRIDGE_EMUL_ENABLED
|
||||
help
|
||||
Enable drivers for half bridges consisting of two complementary PWM outputs with dead time,
|
||||
e.g. used in DC/DC converters.
|
||||
@@ -19,4 +20,11 @@ module = HALF_BRIDGE
|
||||
module-str = half_bridge
|
||||
source "subsys/logging/Kconfig.template.log_config"
|
||||
|
||||
config HALF_BRIDGE_EMUL
|
||||
bool "Emulated half-bridge driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_HALF_BRIDGE_EMUL_ENABLED
|
||||
help
|
||||
Enable emulated half-bridge driver for tests on native_sim.
|
||||
|
||||
endif # HALF_BRIDGE
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Emulated half-bridge driver for tests on native_sim
|
||||
*
|
||||
* Behaves like an edge-aligned STM32 timer, i.e. the period is clock-frequency / frequency.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_half_bridge_emul
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
struct half_bridge_emul_config
|
||||
{
|
||||
uint32_t clock_frequency;
|
||||
uint32_t frequency;
|
||||
};
|
||||
|
||||
struct half_bridge_emul_data
|
||||
{
|
||||
uint32_t period;
|
||||
uint32_t compare;
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
static int half_bridge_emul_start(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->enabled = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int half_bridge_emul_stop(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->enabled = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool half_bridge_emul_is_enabled(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
return data->enabled;
|
||||
}
|
||||
|
||||
static int half_bridge_emul_set_compare(const struct device *dev, uint32_t compare)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_emul_get_compare(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
return data->compare;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_emul_get_period(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
return data->period;
|
||||
}
|
||||
|
||||
static int half_bridge_emul_set_compare_limits(const struct device *dev, uint32_t min, uint32_t max)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
if (min > max || max > data->period) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->compare_min = min;
|
||||
data->compare_max = max;
|
||||
|
||||
return half_bridge_emul_set_compare(dev, data->compare);
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_emul_api) = {
|
||||
.start = half_bridge_emul_start,
|
||||
.stop = half_bridge_emul_stop,
|
||||
.is_enabled = half_bridge_emul_is_enabled,
|
||||
.set_compare = half_bridge_emul_set_compare,
|
||||
.get_compare = half_bridge_emul_get_compare,
|
||||
.get_period = half_bridge_emul_get_period,
|
||||
.set_compare_limits = half_bridge_emul_set_compare_limits,
|
||||
};
|
||||
|
||||
static int half_bridge_emul_init(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_emul_config *config = dev->config;
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->period = config->clock_frequency / config->frequency;
|
||||
data->compare_max = data->period;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define HALF_BRIDGE_EMUL_INIT(n) \
|
||||
static const struct half_bridge_emul_config half_bridge_emul_config_##n = { \
|
||||
.clock_frequency = DT_INST_PROP(n, clock_frequency), \
|
||||
.frequency = DT_INST_PROP(n, frequency), \
|
||||
}; \
|
||||
\
|
||||
static struct half_bridge_emul_data half_bridge_emul_data_##n; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(n, half_bridge_emul_init, NULL, &half_bridge_emul_data_##n, \
|
||||
&half_bridge_emul_config_##n, POST_KERNEL, \
|
||||
CONFIG_HALF_BRIDGE_INIT_PRIORITY, &half_bridge_emul_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_EMUL_INIT)
|
||||
@@ -0,0 +1,17 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
description: |
|
||||
Emulated half bridge for tests on native_sim
|
||||
|
||||
Behaves like an edge-aligned timer, i.e. the period is clock-frequency / frequency.
|
||||
|
||||
compatible: "libresolar,half-bridge-emul"
|
||||
|
||||
include: [base.yaml, half-bridge-common.yaml]
|
||||
|
||||
properties:
|
||||
clock-frequency:
|
||||
type: int
|
||||
default: 24000000
|
||||
description: Emulated timer clock frequency in Hz
|
||||
@@ -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(half_bridge)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
halfbridge: half-bridge {
|
||||
compatible = "libresolar,half-bridge-emul";
|
||||
clock-frequency = <24000000>;
|
||||
frequency = <70000>;
|
||||
deadtime-ns = <300>;
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
CONFIG_ZTEST=y
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
#define HALF_BRIDGE_NODE DT_NODELABEL(halfbridge)
|
||||
|
||||
#define MAX_PWM_DUTY 0.97F
|
||||
#define MIN_PWM_DUTY 0.1F
|
||||
#define MID_PWM_DUTY ((MIN_PWM_DUTY + MAX_PWM_DUTY) / 2)
|
||||
|
||||
/* calculated duty cycle may deviate +/- DUTY_EPSILON from test target value */
|
||||
#define DUTY_EPSILON 0.006F
|
||||
|
||||
static const struct device *const dev = DEVICE_DT_GET(HALF_BRIDGE_NODE);
|
||||
|
||||
static void *half_bridge_setup(void)
|
||||
{
|
||||
zassert_true(device_is_ready(dev), "half-bridge device not ready");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void half_bridge_before(void *fixture)
|
||||
{
|
||||
ARG_UNUSED(fixture);
|
||||
|
||||
zassert_ok(half_bridge_set_duty_limits(dev, MIN_PWM_DUTY, MAX_PWM_DUTY));
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MAX_PWM_DUTY));
|
||||
zassert_ok(half_bridge_stop(dev));
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_period_from_devicetree)
|
||||
{
|
||||
uint32_t expected =
|
||||
DT_PROP(HALF_BRIDGE_NODE, clock_frequency) / DT_PROP(HALF_BRIDGE_NODE, frequency);
|
||||
|
||||
zassert_equal(half_bridge_get_period(dev), expected);
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_set_duty_cycle)
|
||||
{
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MID_PWM_DUTY));
|
||||
zassert_within(half_bridge_get_duty_cycle(dev), MID_PWM_DUTY, DUTY_EPSILON);
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_invalid_duty_cycle_ignored)
|
||||
{
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MID_PWM_DUTY));
|
||||
zassert_equal(half_bridge_set_duty_cycle(dev, 1.5F), -EINVAL);
|
||||
zassert_equal(half_bridge_set_duty_cycle(dev, -0.1F), -EINVAL);
|
||||
zassert_within(half_bridge_get_duty_cycle(dev), MID_PWM_DUTY, DUTY_EPSILON);
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_start)
|
||||
{
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MID_PWM_DUTY));
|
||||
zassert_ok(half_bridge_start(dev));
|
||||
zassert_true(half_bridge_is_enabled(dev));
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_stop)
|
||||
{
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MID_PWM_DUTY));
|
||||
zassert_ok(half_bridge_start(dev));
|
||||
zassert_ok(half_bridge_stop(dev));
|
||||
zassert_false(half_bridge_is_enabled(dev));
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_duty_limits_not_violated)
|
||||
{
|
||||
/* maximum limit */
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, 1.0F));
|
||||
zassert_within(half_bridge_get_duty_cycle(dev), MAX_PWM_DUTY, DUTY_EPSILON);
|
||||
|
||||
/* minimum limit */
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, 0.0F));
|
||||
zassert_within(half_bridge_get_duty_cycle(dev), MIN_PWM_DUTY, DUTY_EPSILON);
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_compare_limits_not_violated)
|
||||
{
|
||||
/* maximum limit */
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MAX_PWM_DUTY));
|
||||
zassert_ok(half_bridge_set_compare(dev, half_bridge_get_compare(dev) + 1));
|
||||
zassert_within(half_bridge_get_duty_cycle(dev), MAX_PWM_DUTY, DUTY_EPSILON);
|
||||
|
||||
/* minimum limit */
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MIN_PWM_DUTY));
|
||||
zassert_ok(half_bridge_set_compare(dev, half_bridge_get_compare(dev) - 1));
|
||||
zassert_within(half_bridge_get_duty_cycle(dev), MIN_PWM_DUTY, DUTY_EPSILON);
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_new_limits_clamp_compare)
|
||||
{
|
||||
zassert_ok(half_bridge_set_duty_cycle(dev, MAX_PWM_DUTY));
|
||||
zassert_ok(half_bridge_set_duty_limits(dev, MIN_PWM_DUTY, MID_PWM_DUTY));
|
||||
zassert_within(half_bridge_get_duty_cycle(dev), MID_PWM_DUTY, DUTY_EPSILON);
|
||||
}
|
||||
|
||||
ZTEST(half_bridge, test_invalid_limits_rejected)
|
||||
{
|
||||
uint32_t period = half_bridge_get_period(dev);
|
||||
|
||||
zassert_equal(half_bridge_set_duty_limits(dev, MAX_PWM_DUTY, MIN_PWM_DUTY), -EINVAL);
|
||||
zassert_equal(half_bridge_set_duty_limits(dev, MIN_PWM_DUTY, 1.1F), -EINVAL);
|
||||
zassert_equal(half_bridge_set_compare_limits(dev, 0, period + 1), -EINVAL);
|
||||
}
|
||||
|
||||
ZTEST_SUITE(half_bridge, NULL, half_bridge_setup, half_bridge_before, NULL, NULL);
|
||||
@@ -0,0 +1,10 @@
|
||||
tests:
|
||||
drivers.half_bridge.emul:
|
||||
tags:
|
||||
- drivers
|
||||
- half_bridge
|
||||
platform_allow:
|
||||
- native_sim
|
||||
- native_sim/native/64
|
||||
integration_platforms:
|
||||
- native_sim
|
||||
Reference in New Issue
Block a user