drivers: half_bridge: drive outputs low when stopped

The advtim and gptim drivers left the outputs floating while the half
bridge was stopped, so the state of the MOSFETs only depended on the
pull-down resistors at the gate driver inputs.

- advtim: Enable the off-state selection for idle mode (OSSI), so that
  both outputs are driven to their idle level (low) while the main
  output is disabled.
- gptim: Keep the output channels enabled and switch between PWM mode
  and forced output levels to start and stop. The forced low-side
  state uses forced output levels as well.

The HRTIM driver already drives the idle level of disabled outputs.

The requirement is added to the API documentation. External pull-down
resistors are still required for the time during reset and before the
driver is initialized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-10-22 00:04:23 +02:00
co-authored by Claude Opus 5.5
parent 7a1a1b02b5
commit 4debb311b2
3 changed files with 51 additions and 18 deletions
@@ -10,6 +10,9 @@
*
* The high-side switch is driven by output CHx and the low-side switch by the complementary
* output CHxN. The timer runs in edge-aligned mode and the dead time is generated in hardware.
*
* Starting and stopping is done with the main output enable (MOE). While stopped, both outputs
* are actively driven low.
*/
#define DT_DRV_COMPAT libresolar_stm32_advtim_half_bridge
@@ -230,6 +233,12 @@ static int half_bridge_stm32_advtim_init(const struct device *dev)
LL_TIM_SetCounterMode(tim, LL_TIM_COUNTERMODE_UP);
LL_TIM_SetAutoReload(tim, tim_clk / config->frequency);
/*
* Drive both outputs to their idle level (low, as OISx and OISxN are 0 after reset) while the
* main output is disabled, instead of leaving them floating.
*/
LL_TIM_SetOffStates(tim, LL_TIM_OSSI_ENABLE, LL_TIM_OSSR_DISABLE);
/* PWM mode 1 with preload and active high polarity for both outputs */
LL_TIM_OC_SetMode(tim, ll_channel, LL_TIM_OCMODE_PWM1);
LL_TIM_OC_EnablePreload(tim, ll_channel);
+39 -18
View File
@@ -12,6 +12,10 @@
* 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.
*
* The output channels are always enabled after initialization. Starting and stopping is done by
* switching between PWM mode and forced output levels, so that both outputs are actively driven
* to their off state while stopped.
*/
#define DT_DRV_COMPAT libresolar_stm32_gptim_half_bridge
@@ -42,6 +46,7 @@ struct half_bridge_stm32_gptim_data
uint32_t deadtime_clocks;
uint32_t compare_min;
uint32_t compare_max;
bool enabled;
bool low_side_forced;
};
@@ -51,6 +56,25 @@ static inline uint32_t ll_channels(const struct half_bridge_stm32_gptim_config *
| half_bridge_stm32_tim_ll_channel(config->low_side_channel);
}
/*
* Set the output compare modes of both channels
*
* Note that the low-side output has inverted polarity, so OCxREF must be high to switch it off.
*/
static void set_output_modes(const struct half_bridge_stm32_gptim_config *config, uint32_t hs_mode,
uint32_t ls_mode)
{
TIM_TypeDef *tim = config->tim.timer;
LL_TIM_OC_SetMode(tim, half_bridge_stm32_tim_ll_channel(config->high_side_channel), hs_mode);
LL_TIM_OC_SetMode(tim, half_bridge_stm32_tim_ll_channel(config->low_side_channel), ls_mode);
}
static inline void set_outputs_off(const struct half_bridge_stm32_gptim_config *config)
{
set_output_modes(config, LL_TIM_OCMODE_FORCED_INACTIVE, LL_TIM_OCMODE_FORCED_ACTIVE);
}
static int half_bridge_stm32_gptim_start(const struct device *dev)
{
const struct half_bridge_stm32_gptim_config *config = dev->config;
@@ -65,7 +89,8 @@ static int half_bridge_stm32_gptim_start(const struct device *dev)
return -EINVAL;
}
LL_TIM_CC_EnableChannel(tim, ll_channels(config));
set_output_modes(config, LL_TIM_OCMODE_PWM1, LL_TIM_OCMODE_PWM1);
data->enabled = true;
return 0;
}
@@ -75,7 +100,8 @@ 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));
set_outputs_off(config);
data->enabled = false;
data->low_side_forced = false;
return 0;
@@ -83,10 +109,9 @@ static int half_bridge_stm32_gptim_stop(const struct device *dev)
static bool half_bridge_stm32_gptim_is_enabled(const struct device *dev)
{
const struct half_bridge_stm32_gptim_config *config = dev->config;
struct half_bridge_stm32_gptim_data *data = dev->data;
return LL_TIM_CC_IsEnabledChannel(config->tim.timer,
half_bridge_stm32_tim_ll_channel(config->high_side_channel));
return data->enabled;
}
static int half_bridge_stm32_gptim_set_compare(const struct device *dev, uint32_t compare)
@@ -146,21 +171,15 @@ 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;
data->enabled = 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.
* OCxREF forced low for both channels: The high-side output (active high) is off and the
* low-side output (active low) is 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));
set_output_modes(config, LL_TIM_OCMODE_FORCED_INACTIVE, LL_TIM_OCMODE_FORCED_INACTIVE);
return 0;
}
@@ -211,15 +230,17 @@ static int half_bridge_stm32_gptim_init(const struct device *dev)
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);
/* compare preload, high-side active high, low-side active low */
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);
/* PWM mode is only selected when starting, outputs are actively driven off until then */
set_outputs_off(config);
LL_TIM_CC_EnableChannel(tim, ll_channels(config));
/*
* (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
+3
View File
@@ -71,6 +71,9 @@ 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
*
* Drivers must actively drive both outputs to their off state instead of leaving them floating.
* The same applies after initialization, before the PWM generation is started.
*
* This also ends the state entered with half_bridge_force_low_side().
*
* @param dev Half-bridge device