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>
132 lines
3.2 KiB
C
132 lines
3.2 KiB
C
/*
|
|
* 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)
|