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
+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);