drivers: half_bridge: add function to force low-side switch on

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>
This commit is contained in:
Martin Jäger
2026-10-21 08:43:53 +02:00
co-authored by Claude Opus 5.5
parent f8226e0687
commit 427943994c
6 changed files with 208 additions and 1 deletions
+26
View File
@@ -31,12 +31,17 @@ struct half_bridge_emul_data
uint32_t compare_min;
uint32_t compare_max;
bool enabled;
bool low_side_forced;
};
static int half_bridge_emul_start(const struct device *dev)
{
struct half_bridge_emul_data *data = dev->data;
if (data->low_side_forced) {
return -EBUSY;
}
data->enabled = true;
return 0;
@@ -47,6 +52,7 @@ static int half_bridge_emul_stop(const struct device *dev)
struct half_bridge_emul_data *data = dev->data;
data->enabled = false;
data->low_side_forced = false;
return 0;
}
@@ -62,6 +68,10 @@ static int half_bridge_emul_set_compare(const struct device *dev, uint32_t compa
{
struct half_bridge_emul_data *data = dev->data;
if (data->low_side_forced) {
return -EBUSY;
}
data->compare = CLAMP(compare, data->compare_min, data->compare_max);
return 0;
@@ -85,6 +95,10 @@ static int half_bridge_emul_set_compare_limits(const struct device *dev, uint32_
{
struct half_bridge_emul_data *data = dev->data;
if (data->low_side_forced) {
return -EBUSY;
}
if (min > max || max > data->period) {
return -EINVAL;
}
@@ -95,6 +109,17 @@ static int half_bridge_emul_set_compare_limits(const struct device *dev, uint32_
return half_bridge_emul_set_compare(dev, data->compare);
}
static int half_bridge_emul_force_low_side(const struct device *dev)
{
struct half_bridge_emul_data *data = dev->data;
data->compare = 0;
data->enabled = true;
data->low_side_forced = true;
return 0;
}
static DEVICE_API(half_bridge, half_bridge_emul_api) = {
.start = half_bridge_emul_start,
.stop = half_bridge_emul_stop,
@@ -103,6 +128,7 @@ static DEVICE_API(half_bridge, half_bridge_emul_api) = {
.get_compare = half_bridge_emul_get_compare,
.get_period = half_bridge_emul_get_period,
.set_compare_limits = half_bridge_emul_set_compare_limits,
.force_low_side = half_bridge_emul_force_low_side,
};
static int half_bridge_emul_init(const struct device *dev)
@@ -42,6 +42,7 @@ struct half_bridge_stm32_advtim_data
{
uint32_t compare_min;
uint32_t compare_max;
bool low_side_forced;
};
static const uint32_t ch2ll_n[] = {
@@ -56,8 +57,13 @@ static const uint32_t ch2ll_n[] = {
static int half_bridge_stm32_advtim_start(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
struct half_bridge_stm32_advtim_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->channel) == 0U) {
return -EINVAL;
}
@@ -71,8 +77,10 @@ static int half_bridge_stm32_advtim_start(const struct device *dev)
static int half_bridge_stm32_advtim_stop(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
struct half_bridge_stm32_advtim_data *data = dev->data;
LL_TIM_DisableAllOutputs(config->tim.timer);
data->low_side_forced = false;
return 0;
}
@@ -90,6 +98,10 @@ static int half_bridge_stm32_advtim_set_compare(const struct device *dev, uint32
struct half_bridge_stm32_advtim_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);
@@ -124,6 +136,10 @@ static int half_bridge_stm32_advtim_set_compare_limits(const struct device *dev,
{
struct half_bridge_stm32_advtim_data *data = dev->data;
if (data->low_side_forced) {
return -EBUSY;
}
if (min > max || max > half_bridge_stm32_advtim_get_period(dev)) {
return -EINVAL;
}
@@ -134,6 +150,29 @@ static int half_bridge_stm32_advtim_set_compare_limits(const struct device *dev,
return half_bridge_stm32_advtim_set_compare(dev, half_bridge_stm32_advtim_get_compare(dev));
}
static int half_bridge_stm32_advtim_force_low_side(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
struct half_bridge_stm32_advtim_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, so CHx (high side) stays off and the
* complementary output CHxN (low side) stays on. The output mode itself can't be changed, as
* it is locked after initialization.
*/
half_bridge_stm32_tim_set_compare(tim, config->channel, 0);
/* load preload registers immediately */
LL_TIM_GenerateEvent_UPDATE(tim);
LL_TIM_EnableAllOutputs(tim);
return 0;
}
static DEVICE_API(half_bridge, half_bridge_stm32_advtim_api) = {
.start = half_bridge_stm32_advtim_start,
.stop = half_bridge_stm32_advtim_stop,
@@ -142,6 +181,7 @@ static DEVICE_API(half_bridge, half_bridge_stm32_advtim_api) = {
.get_compare = half_bridge_stm32_advtim_get_compare,
.get_period = half_bridge_stm32_advtim_get_period,
.set_compare_limits = half_bridge_stm32_advtim_set_compare_limits,
.force_low_side = half_bridge_stm32_advtim_force_low_side,
};
static int half_bridge_stm32_advtim_init_adc_trigger(TIM_TypeDef *tim)
@@ -42,6 +42,7 @@ 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)
@@ -53,8 +54,13 @@ static inline uint32_t ll_channels(const struct half_bridge_stm32_gptim_config *
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;
}
@@ -67,8 +73,10 @@ static int half_bridge_stm32_gptim_start(const struct device *dev)
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;
}
@@ -87,6 +95,10 @@ static int half_bridge_stm32_gptim_set_compare(const struct device *dev, uint32_
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);
@@ -116,6 +128,10 @@ static int half_bridge_stm32_gptim_set_compare_limits(const struct device *dev,
{
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;
}
@@ -126,6 +142,29 @@ static int half_bridge_stm32_gptim_set_compare_limits(const struct device *dev,
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,
@@ -134,6 +173,7 @@ static DEVICE_API(half_bridge, half_bridge_stm32_gptim_api) = {
.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)
@@ -55,6 +55,7 @@ struct half_bridge_stm32_hrtim_data
{
uint32_t compare_min;
uint32_t compare_max;
bool low_side_forced;
};
static inline HRTIM_Timerx_TypeDef *timer_a(const struct device *dev)
@@ -67,6 +68,11 @@ static inline HRTIM_Timerx_TypeDef *timer_a(const struct device *dev)
static int half_bridge_stm32_hrtim_start(const struct device *dev)
{
const struct half_bridge_stm32_hrtim_config *config = dev->config;
struct half_bridge_stm32_hrtim_data *data = dev->data;
if (data->low_side_forced) {
return -EBUSY;
}
config->hrtim->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
@@ -76,9 +82,18 @@ static int half_bridge_stm32_hrtim_start(const struct device *dev)
static int half_bridge_stm32_hrtim_stop(const struct device *dev)
{
const struct half_bridge_stm32_hrtim_config *config = dev->config;
struct half_bridge_stm32_hrtim_data *data = dev->data;
config->hrtim->sCommonRegs.ODISR = HRTIM_ODISR_TA1ODIS | HRTIM_ODISR_TA2ODIS;
if (data->low_side_forced) {
/* restore PWM generation for output 1 */
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
tim->SETx1R = HRTIM_SET1R_PER;
tim->RSTx1R = HRTIM_RST1R_CMP1;
data->low_side_forced = false;
}
return 0;
}
@@ -95,6 +110,10 @@ static int half_bridge_stm32_hrtim_set_compare(const struct device *dev, uint32_
struct half_bridge_stm32_hrtim_data *data = dev->data;
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
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);
@@ -123,6 +142,10 @@ static int half_bridge_stm32_hrtim_set_compare_limits(const struct device *dev,
{
struct half_bridge_stm32_hrtim_data *data = dev->data;
if (data->low_side_forced) {
return -EBUSY;
}
if (min > max || max > half_bridge_stm32_hrtim_get_period(dev)) {
return -EINVAL;
}
@@ -133,6 +156,26 @@ static int half_bridge_stm32_hrtim_set_compare_limits(const struct device *dev,
return half_bridge_stm32_hrtim_set_compare(dev, half_bridge_stm32_hrtim_get_compare(dev));
}
static int half_bridge_stm32_hrtim_force_low_side(const struct device *dev)
{
const struct half_bridge_stm32_hrtim_config *config = dev->config;
struct half_bridge_stm32_hrtim_data *data = dev->data;
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
data->low_side_forced = true;
/*
* Remove all set events and reset output 1 (high side) by software, so that it stays
* inactive. Output 2 (low side) is generated as the complement with dead time and stays on.
*/
tim->SETx1R = 0;
tim->RSTx1R = HRTIM_RST1R_SRT;
config->hrtim->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
return 0;
}
static DEVICE_API(half_bridge, half_bridge_stm32_hrtim_api) = {
.start = half_bridge_stm32_hrtim_start,
.stop = half_bridge_stm32_hrtim_stop,
@@ -141,6 +184,7 @@ static DEVICE_API(half_bridge, half_bridge_stm32_hrtim_api) = {
.get_compare = half_bridge_stm32_hrtim_get_compare,
.get_period = half_bridge_stm32_hrtim_get_period,
.set_compare_limits = half_bridge_stm32_hrtim_set_compare_limits,
.force_low_side = half_bridge_stm32_hrtim_force_low_side,
};
static int half_bridge_stm32_hrtim_init(const struct device *dev)
+23
View File
@@ -46,6 +46,7 @@ __subsystem struct half_bridge_driver_api
uint32_t (*get_compare)(const struct device *dev);
uint32_t (*get_period)(const struct device *dev);
int (*set_compare_limits)(const struct device *dev, uint32_t min, uint32_t max);
int (*force_low_side)(const struct device *dev);
};
/** @endcond */
@@ -60,6 +61,7 @@ __subsystem struct half_bridge_driver_api
*
* @retval 0 on success
* @retval -EINVAL if the output could not be started with the current compare value
* @retval -EBUSY if the low-side switch is forced on
*/
static inline int half_bridge_start(const struct device *dev)
{
@@ -69,6 +71,8 @@ static inline int half_bridge_start(const struct device *dev)
/**
* Stop the PWM generation, i.e. switch off both high-side and low-side switches
*
* This also ends the state entered with half_bridge_force_low_side().
*
* @param dev Half-bridge device
*
* @retval 0 on success
@@ -100,6 +104,7 @@ static inline bool half_bridge_is_enabled(const struct device *dev)
* @param compare Timer compare value (between 0 and period)
*
* @retval 0 on success
* @retval -EBUSY if the low-side switch is forced on (setting is ignored)
*/
static inline int half_bridge_set_compare(const struct device *dev, uint32_t compare)
{
@@ -143,6 +148,7 @@ static inline uint32_t half_bridge_get_period(const struct device *dev)
*
* @retval 0 on success
* @retval -EINVAL if min > max or max > period
* @retval -EBUSY if the low-side switch is forced on (setting is ignored)
*/
static inline int half_bridge_set_compare_limits(const struct device *dev, uint32_t min,
uint32_t max)
@@ -150,6 +156,23 @@ static inline int half_bridge_set_compare_limits(const struct device *dev, uint3
return DEVICE_API_GET(half_bridge, dev)->set_compare_limits(dev, min, max);
}
/**
* Switch the low-side switch continuously on and the high-side switch off
*
* This bypasses the compare value limits and is intended for safety measures, e.g. to blow a
* fuse at the low-side terminal by shorting it through the inductor. The state is kept until
* half_bridge_stop() is called. Changes of the compare value or its limits are rejected in the
* meantime.
*
* @param dev Half-bridge device
*
* @retval 0 on success
*/
static inline int half_bridge_force_low_side(const struct device *dev)
{
return DEVICE_API_GET(half_bridge, dev)->force_low_side(dev);
}
/**
* Set the duty cycle of the PWM signal
*
+35 -1
View File
@@ -31,9 +31,10 @@ static void half_bridge_before(void *fixture)
{
ARG_UNUSED(fixture);
/* stop first, as it also ends a forced low-side state from previous tests */
zassert_ok(half_bridge_stop(dev));
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)
@@ -117,4 +118,37 @@ ZTEST(half_bridge, test_invalid_limits_rejected)
zassert_equal(half_bridge_set_compare_limits(dev, 0, period + 1), -EINVAL);
}
ZTEST(half_bridge, test_force_low_side)
{
zassert_ok(half_bridge_set_duty_cycle(dev, MID_PWM_DUTY));
zassert_ok(half_bridge_force_low_side(dev));
/* duty cycle limits are bypassed */
zassert_true(half_bridge_is_enabled(dev));
zassert_equal(half_bridge_get_compare(dev), 0);
}
ZTEST(half_bridge, test_force_low_side_rejects_changes)
{
zassert_ok(half_bridge_force_low_side(dev));
zassert_equal(half_bridge_set_duty_cycle(dev, MID_PWM_DUTY), -EBUSY);
zassert_equal(half_bridge_start(dev), -EBUSY);
zassert_equal(half_bridge_set_duty_limits(dev, MIN_PWM_DUTY, MID_PWM_DUTY), -EBUSY);
zassert_equal(half_bridge_get_compare(dev), 0);
zassert_true(half_bridge_is_enabled(dev));
}
ZTEST(half_bridge, test_stop_ends_forced_low_side)
{
zassert_ok(half_bridge_force_low_side(dev));
zassert_ok(half_bridge_stop(dev));
zassert_false(half_bridge_is_enabled(dev));
zassert_ok(half_bridge_set_duty_cycle(dev, MID_PWM_DUTY));
zassert_within(half_bridge_get_duty_cycle(dev), MID_PWM_DUTY, DUTY_EPSILON);
zassert_ok(half_bridge_start(dev));
zassert_true(half_bridge_is_enabled(dev));
}
ZTEST_SUITE(half_bridge, NULL, half_bridge_setup, half_bridge_before, NULL, NULL);