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
+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
*