Add half_bridge_force_low_side() to switch the low-side switch continuously on, bypassing the compare value limits. This is required for safety measures like blowing the battery fuse in case of a shorted high-side MOSFET. Previously, this was attempted by setting a duty cycle of 0 and starting the PWM. However, the STM32 timer drivers refuse to start with a compare value of 0, so the low-side switch was never turned on. The forced state is kept until half_bridge_stop() is called. Changes of the compare value or its limits and start requests are rejected with -EBUSY meanwhile, so that a concurrently running control loop can't override it. - advtim: Compare value 0 keeps OCxREF low, so that CHxN stays on. The output mode can't be changed, as it is locked during initialization. - gptim: Compare value 0 for both channels, so that the low-side channel with inverted polarity stays on. - HRTIM: Output 1 is reset by software and set events are removed, so that the complementary output 2 stays on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
262 lines
8.8 KiB
C
262 lines
8.8 KiB
C
/*
|
|
* Copyright (c) The Libre Solar Project Contributors
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @file
|
|
* @brief Half-bridge driver for STM32 general-purpose timers (e.g. TIM3)
|
|
*
|
|
* The high-side and low-side switches are driven by two independent channels, as general-purpose
|
|
* timers have no hardware dead time generation. The timer runs in center-aligned mode and the dead
|
|
* time is generated by shifting the compare value of the low-side channel, which has inverted
|
|
* polarity.
|
|
*/
|
|
|
|
#define DT_DRV_COMPAT libresolar_stm32_gptim_half_bridge
|
|
|
|
#include "half_bridge_stm32_tim.h"
|
|
|
|
#include <libresolar/drivers/half_bridge.h>
|
|
|
|
#include <zephyr/device.h>
|
|
#include <zephyr/logging/log.h>
|
|
#include <zephyr/sys/util.h>
|
|
|
|
LOG_MODULE_REGISTER(half_bridge_stm32_gptim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
|
|
|
|
struct half_bridge_stm32_gptim_config
|
|
{
|
|
struct half_bridge_stm32_tim_config tim;
|
|
uint32_t frequency;
|
|
uint32_t deadtime_ns;
|
|
/** Timer channel (1-4) for the high-side switch */
|
|
uint8_t high_side_channel;
|
|
/** Timer channel (1-4) for the low-side switch */
|
|
uint8_t low_side_channel;
|
|
};
|
|
|
|
struct half_bridge_stm32_gptim_data
|
|
{
|
|
uint32_t deadtime_clocks;
|
|
uint32_t compare_min;
|
|
uint32_t compare_max;
|
|
bool low_side_forced;
|
|
};
|
|
|
|
static inline uint32_t ll_channels(const struct half_bridge_stm32_gptim_config *config)
|
|
{
|
|
return half_bridge_stm32_tim_ll_channel(config->high_side_channel)
|
|
| half_bridge_stm32_tim_ll_channel(config->low_side_channel);
|
|
}
|
|
|
|
static int half_bridge_stm32_gptim_start(const struct device *dev)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
struct half_bridge_stm32_gptim_data *data = dev->data;
|
|
TIM_TypeDef *tim = config->tim.timer;
|
|
|
|
if (data->low_side_forced) {
|
|
return -EBUSY;
|
|
}
|
|
|
|
if (half_bridge_stm32_tim_get_compare(tim, config->high_side_channel) == 0U) {
|
|
return -EINVAL;
|
|
}
|
|
|
|
LL_TIM_CC_EnableChannel(tim, ll_channels(config));
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int half_bridge_stm32_gptim_stop(const struct device *dev)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
struct half_bridge_stm32_gptim_data *data = dev->data;
|
|
|
|
LL_TIM_CC_DisableChannel(config->tim.timer, ll_channels(config));
|
|
data->low_side_forced = false;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static bool half_bridge_stm32_gptim_is_enabled(const struct device *dev)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
|
|
return LL_TIM_CC_IsEnabledChannel(config->tim.timer,
|
|
half_bridge_stm32_tim_ll_channel(config->high_side_channel));
|
|
}
|
|
|
|
static int half_bridge_stm32_gptim_set_compare(const struct device *dev, uint32_t compare)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
struct half_bridge_stm32_gptim_data *data = dev->data;
|
|
TIM_TypeDef *tim = config->tim.timer;
|
|
|
|
if (data->low_side_forced) {
|
|
return -EBUSY;
|
|
}
|
|
|
|
/* protection against wrong settings which could destroy the hardware */
|
|
compare = CLAMP(compare, data->compare_min, data->compare_max);
|
|
|
|
half_bridge_stm32_tim_set_compare(tim, config->high_side_channel, compare);
|
|
half_bridge_stm32_tim_set_compare(tim, config->low_side_channel,
|
|
compare + data->deadtime_clocks);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static uint32_t half_bridge_stm32_gptim_get_compare(const struct device *dev)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
|
|
return half_bridge_stm32_tim_get_compare(config->tim.timer, config->high_side_channel);
|
|
}
|
|
|
|
static uint32_t half_bridge_stm32_gptim_get_period(const struct device *dev)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
|
|
return LL_TIM_GetAutoReload(config->tim.timer);
|
|
}
|
|
|
|
static int half_bridge_stm32_gptim_set_compare_limits(const struct device *dev, uint32_t min,
|
|
uint32_t max)
|
|
{
|
|
struct half_bridge_stm32_gptim_data *data = dev->data;
|
|
|
|
if (data->low_side_forced) {
|
|
return -EBUSY;
|
|
}
|
|
|
|
if (min > max || max > half_bridge_stm32_gptim_get_period(dev)) {
|
|
return -EINVAL;
|
|
}
|
|
|
|
data->compare_min = min;
|
|
data->compare_max = max;
|
|
|
|
return half_bridge_stm32_gptim_set_compare(dev, half_bridge_stm32_gptim_get_compare(dev));
|
|
}
|
|
|
|
static int half_bridge_stm32_gptim_force_low_side(const struct device *dev)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
struct half_bridge_stm32_gptim_data *data = dev->data;
|
|
TIM_TypeDef *tim = config->tim.timer;
|
|
|
|
data->low_side_forced = true;
|
|
|
|
/*
|
|
* A compare value of 0 keeps OCxREF low in PWM mode 1 for both channels. The high-side output
|
|
* (active high) stays off and the low-side output (active low) stays on.
|
|
*/
|
|
half_bridge_stm32_tim_set_compare(tim, config->high_side_channel, 0);
|
|
half_bridge_stm32_tim_set_compare(tim, config->low_side_channel, 0);
|
|
|
|
/* load preload registers immediately */
|
|
LL_TIM_GenerateEvent_UPDATE(tim);
|
|
|
|
LL_TIM_CC_EnableChannel(tim, ll_channels(config));
|
|
|
|
return 0;
|
|
}
|
|
|
|
static DEVICE_API(half_bridge, half_bridge_stm32_gptim_api) = {
|
|
.start = half_bridge_stm32_gptim_start,
|
|
.stop = half_bridge_stm32_gptim_stop,
|
|
.is_enabled = half_bridge_stm32_gptim_is_enabled,
|
|
.set_compare = half_bridge_stm32_gptim_set_compare,
|
|
.get_compare = half_bridge_stm32_gptim_get_compare,
|
|
.get_period = half_bridge_stm32_gptim_get_period,
|
|
.set_compare_limits = half_bridge_stm32_gptim_set_compare_limits,
|
|
.force_low_side = half_bridge_stm32_gptim_force_low_side,
|
|
};
|
|
|
|
static int half_bridge_stm32_gptim_init(const struct device *dev)
|
|
{
|
|
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
|
struct half_bridge_stm32_gptim_data *data = dev->data;
|
|
TIM_TypeDef *tim = config->tim.timer;
|
|
uint32_t ll_channel_hs = half_bridge_stm32_tim_ll_channel(config->high_side_channel);
|
|
uint32_t ll_channel_ls = half_bridge_stm32_tim_ll_channel(config->low_side_channel);
|
|
uint32_t tim_clk;
|
|
int err;
|
|
|
|
if (!IS_TIM_COUNTER_MODE_SELECT_INSTANCE(tim)) {
|
|
LOG_ERR("Center-aligned mode not supported by this timer");
|
|
return -ENOTSUP;
|
|
}
|
|
|
|
#ifdef IS_TIM_BREAK_INSTANCE
|
|
if (IS_TIM_BREAK_INSTANCE(tim)) {
|
|
/* outputs would additionally require main output enable (MOE) */
|
|
LOG_ERR("Timer has dead time generator, use advtim half-bridge instead");
|
|
return -ENOTSUP;
|
|
}
|
|
#endif
|
|
|
|
err = half_bridge_stm32_tim_init(&config->tim, &tim_clk);
|
|
if (err < 0) {
|
|
return err;
|
|
}
|
|
|
|
/*
|
|
* Center-aligned mode counts up and down in each period, so we need half the clocks as
|
|
* resolution for the given frequency.
|
|
*/
|
|
LL_TIM_SetCounterMode(tim, LL_TIM_COUNTERMODE_CENTER_DOWN);
|
|
LL_TIM_SetAutoReload(tim, tim_clk / config->frequency / 2);
|
|
|
|
/* PWM mode 1 with preload, high-side active high, low-side active low */
|
|
LL_TIM_OC_SetMode(tim, ll_channel_hs, LL_TIM_OCMODE_PWM1);
|
|
LL_TIM_OC_EnablePreload(tim, ll_channel_hs);
|
|
LL_TIM_OC_SetPolarity(tim, ll_channel_hs, LL_TIM_OCPOLARITY_HIGH);
|
|
|
|
LL_TIM_OC_SetMode(tim, ll_channel_ls, LL_TIM_OCMODE_PWM1);
|
|
LL_TIM_OC_EnablePreload(tim, ll_channel_ls);
|
|
LL_TIM_OC_SetPolarity(tim, ll_channel_ls, LL_TIM_OCPOLARITY_LOW);
|
|
|
|
/*
|
|
* (clocks per ms * deadtime in ns) / 1000 == (clocks per ms * deadtime in ms)
|
|
* although the C operator precedence does the "right thing" to allow deadtime_ns < 1000 to
|
|
* be handled nicely, parentheses make this more explicit
|
|
*/
|
|
data->deadtime_clocks = ((tim_clk / 1000000) * config->deadtime_ns) / 1000;
|
|
|
|
data->compare_min = 0;
|
|
data->compare_max = LL_TIM_GetAutoReload(tim);
|
|
|
|
/* force update generation to load the preload registers and start the counter */
|
|
LL_TIM_GenerateEvent_UPDATE(tim);
|
|
LL_TIM_EnableCounter(tim);
|
|
|
|
return 0;
|
|
}
|
|
|
|
#define HALF_BRIDGE_STM32_GPTIM_INIT(n) \
|
|
BUILD_ASSERT(DT_INST_PROP(n, high_side_channel) != DT_INST_PROP(n, low_side_channel), \
|
|
"High-side and low-side channel must be different"); \
|
|
\
|
|
HALF_BRIDGE_STM32_TIM_DEFINE(n) \
|
|
\
|
|
static const struct half_bridge_stm32_gptim_config half_bridge_stm32_gptim_config_##n = { \
|
|
.tim = HALF_BRIDGE_STM32_TIM_CONFIG(n), \
|
|
.frequency = DT_INST_PROP(n, frequency), \
|
|
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
|
|
.high_side_channel = DT_INST_PROP(n, high_side_channel), \
|
|
.low_side_channel = DT_INST_PROP(n, low_side_channel), \
|
|
}; \
|
|
\
|
|
static struct half_bridge_stm32_gptim_data half_bridge_stm32_gptim_data_##n; \
|
|
\
|
|
DEVICE_DT_INST_DEFINE(n, half_bridge_stm32_gptim_init, NULL, \
|
|
&half_bridge_stm32_gptim_data_##n, &half_bridge_stm32_gptim_config_##n, \
|
|
POST_KERNEL, CONFIG_HALF_BRIDGE_INIT_PRIORITY, \
|
|
&half_bridge_stm32_gptim_api);
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_STM32_GPTIM_INIT)
|