drivers: half_bridge: add STM32 timer driver

Add a half-bridge driver for STM32 timers (TIM), replacing the
register-level code for TIM1 and TIM3 in the application.

The timer and channels are configured via devicetree instead of being
derived from the timer base address. Two operating modes are supported:

- Complementary outputs CHx/CHxN of advanced-control timers (e.g. TIM1)
  with edge-aligned PWM and hardware dead time generation.
- Two independent channels of general-purpose timers (e.g. TIM3) with
  center-aligned PWM and dead time generated by shifting the compare
  value of the low-side channel.

Compared to the previous implementation, the timer clock is obtained
from the clock control driver instead of assuming SystemCoreClock, so
that APB prescalers are taken into account. The dead time register is
now encoded properly using __LL_TIM_CALC_DEADTIME instead of masking the
lowest 7 bits, which was wrong for more than 127 timer clocks.

The ADC trigger via OC6/TRGO2 (previously enabled for all STM32G4 MCUs)
must now be enabled explicitly with the adc-trigger property.

The period test of the driver test suite is skipped for non-emulated
devices so that the suite can also be built for STM32 boards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-10-20 20:49:27 +02:00
co-authored by Claude Opus 5.5
parent 1cdd5d90c1
commit 8570d9d274
5 changed files with 529 additions and 0 deletions
+1
View File
@@ -3,4 +3,5 @@
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32 half_bridge_stm32.c)
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_EMUL half_bridge_emul.c)
+11
View File
@@ -3,6 +3,7 @@
menuconfig HALF_BRIDGE
bool "Half-bridge drivers"
default y if DT_HAS_LIBRESOLAR_STM32_HALF_BRIDGE_ENABLED
default y if DT_HAS_LIBRESOLAR_HALF_BRIDGE_EMUL_ENABLED
help
Enable drivers for half bridges consisting of two complementary PWM outputs with dead time,
@@ -20,6 +21,16 @@ module = HALF_BRIDGE
module-str = half_bridge
source "subsys/logging/Kconfig.template.log_config"
config HALF_BRIDGE_STM32
bool "STM32 timer half-bridge driver"
default y
depends on DT_HAS_LIBRESOLAR_STM32_HALF_BRIDGE_ENABLED
select USE_STM32_LL_TIM
select PINCTRL
select RESET
help
Enable half-bridge driver for STM32 timers (TIM).
config HALF_BRIDGE_EMUL
bool "Emulated half-bridge driver"
default y
+449
View File
@@ -0,0 +1,449 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Half-bridge driver for STM32 timers (TIM)
*
* Two operating modes are supported:
*
* - Complementary outputs: High-side switch driven by CHx and low-side switch by CHxN of an
* advanced-control timer (e.g. TIM1). Edge-aligned mode with dead time generated in hardware.
*
* - Two independent channels: High-side and low-side switch driven by two channels of a
* general-purpose timer (e.g. TIM3). Center-aligned mode with dead time generated by shifting
* the compare value of the low-side channel (with inverted polarity).
*/
#define DT_DRV_COMPAT libresolar_stm32_half_bridge
#include <libresolar/drivers/half_bridge.h>
#include <zephyr/device.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/reset.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#include <soc.h>
#include <stm32_ll_tim.h>
LOG_MODULE_REGISTER(half_bridge_stm32, CONFIG_HALF_BRIDGE_LOG_LEVEL);
struct half_bridge_stm32_config
{
TIM_TypeDef *timer;
const struct stm32_pclken *pclken;
size_t pclk_len;
struct reset_dt_spec reset;
const struct pinctrl_dev_config *pcfg;
uint32_t frequency;
uint32_t deadtime_ns;
/** Timer channel (1-4) for the high-side switch */
uint8_t channel;
/** Timer channel (1-4) for the low-side switch or 0 if complementary output is used */
uint8_t low_side_channel;
bool adc_trigger;
};
struct half_bridge_stm32_data
{
/** Dead time in timer clocks (only used for mode with two independent channels) */
uint32_t deadtime_clocks;
uint32_t compare_min;
uint32_t compare_max;
};
static const uint32_t ch2ll[] = {
LL_TIM_CHANNEL_CH1,
LL_TIM_CHANNEL_CH2,
LL_TIM_CHANNEL_CH3,
LL_TIM_CHANNEL_CH4,
};
#ifdef LL_TIM_CHANNEL_CH1N
static const uint32_t ch2ll_n[] = {
LL_TIM_CHANNEL_CH1N,
LL_TIM_CHANNEL_CH2N,
LL_TIM_CHANNEL_CH3N,
#ifdef LL_TIM_CHANNEL_CH4N
LL_TIM_CHANNEL_CH4N,
#endif
};
#endif
static inline bool is_complementary_mode(const struct half_bridge_stm32_config *config)
{
return config->low_side_channel == 0;
}
static void tim_set_compare(TIM_TypeDef *tim, uint8_t channel, uint32_t value)
{
switch (channel) {
case 1:
LL_TIM_OC_SetCompareCH1(tim, value);
break;
case 2:
LL_TIM_OC_SetCompareCH2(tim, value);
break;
case 3:
LL_TIM_OC_SetCompareCH3(tim, value);
break;
case 4:
LL_TIM_OC_SetCompareCH4(tim, value);
break;
}
}
static uint32_t tim_get_compare(TIM_TypeDef *tim, uint8_t channel)
{
switch (channel) {
case 1:
return LL_TIM_OC_GetCompareCH1(tim);
case 2:
return LL_TIM_OC_GetCompareCH2(tim);
case 3:
return LL_TIM_OC_GetCompareCH3(tim);
case 4:
return LL_TIM_OC_GetCompareCH4(tim);
default:
return 0;
}
}
static int half_bridge_stm32_start(const struct device *dev)
{
const struct half_bridge_stm32_config *config = dev->config;
TIM_TypeDef *tim = config->timer;
if (tim_get_compare(tim, config->channel) == 0U) {
return -EINVAL;
}
if (is_complementary_mode(config)) {
#ifdef LL_TIM_CHANNEL_CH1N
/* main output enable */
LL_TIM_EnableAllOutputs(tim);
#endif
}
else {
LL_TIM_CC_EnableChannel(tim,
ch2ll[config->channel - 1] | ch2ll[config->low_side_channel - 1]);
}
return 0;
}
static int half_bridge_stm32_stop(const struct device *dev)
{
const struct half_bridge_stm32_config *config = dev->config;
TIM_TypeDef *tim = config->timer;
if (is_complementary_mode(config)) {
#ifdef LL_TIM_CHANNEL_CH1N
LL_TIM_DisableAllOutputs(tim);
#endif
}
else {
LL_TIM_CC_DisableChannel(tim,
ch2ll[config->channel - 1] | ch2ll[config->low_side_channel - 1]);
}
return 0;
}
static bool half_bridge_stm32_is_enabled(const struct device *dev)
{
const struct half_bridge_stm32_config *config = dev->config;
TIM_TypeDef *tim = config->timer;
if (is_complementary_mode(config)) {
#ifdef LL_TIM_CHANNEL_CH1N
return LL_TIM_IsEnabledAllOutputs(tim);
#else
return false;
#endif
}
else {
return LL_TIM_CC_IsEnabledChannel(tim, ch2ll[config->channel - 1]);
}
}
static int half_bridge_stm32_set_compare(const struct device *dev, uint32_t compare)
{
const struct half_bridge_stm32_config *config = dev->config;
struct half_bridge_stm32_data *data = dev->data;
TIM_TypeDef *tim = config->timer;
/* protection against wrong settings which could destroy the hardware */
compare = CLAMP(compare, data->compare_min, data->compare_max);
tim_set_compare(tim, config->channel, compare);
if (is_complementary_mode(config)) {
#ifdef LL_TIM_CHANNEL_CH6
if (config->adc_trigger) {
/* trigger ADC for current measurement in the middle of the on-time */
LL_TIM_OC_SetCompareCH6(tim, compare / 2);
}
#endif
}
else {
tim_set_compare(tim, config->low_side_channel, compare + data->deadtime_clocks);
}
return 0;
}
static uint32_t half_bridge_stm32_get_compare(const struct device *dev)
{
const struct half_bridge_stm32_config *config = dev->config;
return tim_get_compare(config->timer, config->channel);
}
static uint32_t half_bridge_stm32_get_period(const struct device *dev)
{
const struct half_bridge_stm32_config *config = dev->config;
return LL_TIM_GetAutoReload(config->timer);
}
static int half_bridge_stm32_set_compare_limits(const struct device *dev, uint32_t min,
uint32_t max)
{
struct half_bridge_stm32_data *data = dev->data;
if (min > max || max > half_bridge_stm32_get_period(dev)) {
return -EINVAL;
}
data->compare_min = min;
data->compare_max = max;
return half_bridge_stm32_set_compare(dev, half_bridge_stm32_get_compare(dev));
}
static DEVICE_API(half_bridge, half_bridge_stm32_api) = {
.start = half_bridge_stm32_start,
.stop = half_bridge_stm32_stop,
.is_enabled = half_bridge_stm32_is_enabled,
.set_compare = half_bridge_stm32_set_compare,
.get_compare = half_bridge_stm32_get_compare,
.get_period = half_bridge_stm32_get_period,
.set_compare_limits = half_bridge_stm32_set_compare_limits,
};
static int init_complementary_mode(const struct device *dev, uint32_t tim_clk)
{
#ifdef LL_TIM_CHANNEL_CH1N
const struct half_bridge_stm32_config *config = dev->config;
TIM_TypeDef *tim = config->timer;
if (!IS_TIM_BREAK_INSTANCE(tim) || config->channel > ARRAY_SIZE(ch2ll_n)) {
LOG_ERR("Complementary output not supported for channel %u", config->channel);
return -ENOTSUP;
}
uint32_t ll_channel = ch2ll[config->channel - 1];
/* edge-aligned mode to increase resolution (no division by 2 necessary) */
LL_TIM_SetCounterMode(tim, LL_TIM_COUNTERMODE_UP);
LL_TIM_SetAutoReload(tim, tim_clk / config->frequency);
/* 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);
LL_TIM_CC_EnableChannel(tim, ll_channel | ch2ll_n[config->channel - 1]);
if (config->adc_trigger) {
#ifdef LL_TIM_CHANNEL_CH6
if (!IS_TIM_TRGO2_INSTANCE(tim)) {
LOG_ERR("ADC trigger not supported by this timer");
return -ENOTSUP;
}
/* use OC6 for ADC triggering, but don't configure any pin for PWM mode */
LL_TIM_OC_SetMode(tim, LL_TIM_CHANNEL_CH6, LL_TIM_OCMODE_PWM1);
LL_TIM_OC_EnablePreload(tim, LL_TIM_CHANNEL_CH6);
LL_TIM_CC_EnableChannel(tim, LL_TIM_CHANNEL_CH6);
/* trigger ADC via TRGO2 on OC6REF falling edge (OC4REF is not used) */
LL_TIM_SetTriggerOutput2(tim, LL_TIM_TRGO2_OC4_RISING_OC6_FALLING);
#else
LOG_ERR("ADC trigger not supported by this timer");
return -ENOTSUP;
#endif
}
LL_TIM_OC_SetDeadTime(
tim, __LL_TIM_CALC_DEADTIME(tim_clk, LL_TIM_CLOCKDIVISION_DIV1, config->deadtime_ns));
/* lock break and dead-time register to prevent accidental changes */
LL_TIM_CC_SetLockLevel(tim, LL_TIM_LOCKLEVEL_3);
return 0;
#else
LOG_ERR("Complementary outputs not supported by this MCU");
return -ENOTSUP;
#endif
}
static int init_two_channel_mode(const struct device *dev, uint32_t tim_clk)
{
const struct half_bridge_stm32_config *config = dev->config;
struct half_bridge_stm32_data *data = dev->data;
TIM_TypeDef *tim = config->timer;
uint32_t ll_channel_hs = ch2ll[config->channel - 1];
uint32_t ll_channel_ls = ch2ll[config->low_side_channel - 1];
if (config->adc_trigger) {
LOG_ERR("ADC trigger only supported with complementary outputs");
return -ENOTSUP;
}
if (!IS_TIM_COUNTER_MODE_SELECT_INSTANCE(tim)) {
LOG_ERR("Center-aligned mode not supported by this timer");
return -ENOTSUP;
}
/*
* 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;
#ifdef LL_TIM_CHANNEL_CH1N
if (IS_TIM_BREAK_INSTANCE(tim)) {
/* outputs of advanced-control timers are only enabled with main output enable */
LL_TIM_EnableAllOutputs(tim);
}
#endif
return 0;
}
static int half_bridge_stm32_init(const struct device *dev)
{
const struct half_bridge_stm32_config *config = dev->config;
struct half_bridge_stm32_data *data = dev->data;
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
TIM_TypeDef *tim = config->timer;
uint32_t tim_clk;
int err;
if (!device_is_ready(clk)) {
LOG_ERR("Clock control device not ready");
return -ENODEV;
}
err = clock_control_on(clk, (clock_control_subsys_t)&config->pclken[0]);
if (err < 0) {
LOG_ERR("Could not initialize clock (%d)", err);
return err;
}
if (config->pclk_len < 2) {
LOG_ERR("Timer clock source is not specified");
return -EINVAL;
}
err = clock_control_configure(clk, (clock_control_subsys_t)&config->pclken[1], NULL);
if (err < 0) {
LOG_ERR("Could not configure clock (%d)", err);
return err;
}
err = clock_control_get_rate(clk, (clock_control_subsys_t)&config->pclken[1], &tim_clk);
if (err < 0) {
LOG_ERR("Timer clock rate get error (%d)", err);
return err;
}
/* reset timer to default state using RCC */
(void)reset_line_toggle_dt(&config->reset);
err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
if (err < 0) {
LOG_ERR("Pinctrl setup failed (%d)", err);
return err;
}
/* no prescaler --> counter frequency == timer clock */
LL_TIM_SetPrescaler(tim, 0);
if (is_complementary_mode(config)) {
err = init_complementary_mode(dev, tim_clk);
}
else {
err = init_two_channel_mode(dev, tim_clk);
}
if (err < 0) {
return err;
}
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 TIMER(n) DT_INST_PARENT(n)
#define HALF_BRIDGE_STM32_INIT(n) \
BUILD_ASSERT(DT_NODE_HAS_COMPAT(TIMER(n), st_stm32_timers), \
"Half bridge must be a child of an st,stm32-timers node"); \
BUILD_ASSERT(DT_INST_PROP_OR(n, low_side_channel, 0) != DT_INST_PROP(n, channel), \
"High-side and low-side channel must be different"); \
\
PINCTRL_DT_INST_DEFINE(n); \
\
static const struct stm32_pclken half_bridge_stm32_pclken_##n[] = STM32_DT_CLOCKS(TIMER(n)); \
\
static const struct half_bridge_stm32_config half_bridge_stm32_config_##n = { \
.timer = (TIM_TypeDef *)DT_REG_ADDR(TIMER(n)), \
.pclken = half_bridge_stm32_pclken_##n, \
.pclk_len = DT_NUM_CLOCKS(TIMER(n)), \
.reset = RESET_DT_SPEC_GET(TIMER(n)), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
.frequency = DT_INST_PROP(n, frequency), \
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
.channel = DT_INST_PROP(n, channel), \
.low_side_channel = DT_INST_PROP_OR(n, low_side_channel, 0), \
.adc_trigger = DT_INST_PROP(n, adc_trigger), \
}; \
\
static struct half_bridge_stm32_data half_bridge_stm32_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, half_bridge_stm32_init, NULL, &half_bridge_stm32_data_##n, \
&half_bridge_stm32_config_##n, POST_KERNEL, \
CONFIG_HALF_BRIDGE_INIT_PRIORITY, &half_bridge_stm32_api);
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_STM32_INIT)
@@ -0,0 +1,64 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
description: |
Half bridge using an STM32 timer (TIM)
The node must be a child of an st,stm32-timers node. Two operating modes are supported:
- Complementary outputs (default): The high-side switch is driven by output CHx and the
low-side switch by the complementary output CHxN of an advanced-control timer like TIM1.
The timer runs in edge-aligned mode and the dead time is generated in hardware.
- Two independent channels (if low-side-channel is specified): The high-side and low-side
switches are driven by two channels of a general-purpose timer like TIM3. 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.
Example for complementary outputs:
&timers1 {
status = "okay";
halfbridge: half-bridge {
compatible = "libresolar,stm32-half-bridge";
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pc13>;
pinctrl-names = "default";
channel = <1>;
frequency = <70000>;
deadtime-ns = <300>;
};
};
compatible: "libresolar,stm32-half-bridge"
include: [base.yaml, pinctrl-device.yaml, half-bridge-common.yaml]
properties:
pinctrl-0:
required: true
pinctrl-names:
required: true
channel:
type: int
required: true
enum: [1, 2, 3, 4]
description: Timer channel driving the high-side switch
low-side-channel:
type: int
enum: [1, 2, 3, 4]
description: |
Timer channel driving the low-side switch
If not specified, the complementary output of the high-side channel is used.
adc-trigger:
type: boolean
description: |
Trigger ADC conversions in the middle of the on-time using OC6 via TRGO2
Only supported with complementary outputs and timers providing channel 6 and TRGO2
(e.g. TIM1 of STM32G4).
+4
View File
@@ -38,10 +38,14 @@ static void half_bridge_before(void *fixture)
ZTEST(half_bridge, test_period_from_devicetree)
{
#if DT_NODE_HAS_COMPAT(HALF_BRIDGE_NODE, libresolar_half_bridge_emul)
uint32_t expected =
DT_PROP(HALF_BRIDGE_NODE, clock_frequency) / DT_PROP(HALF_BRIDGE_NODE, frequency);
zassert_equal(half_bridge_get_period(dev), expected);
#else
ztest_test_skip();
#endif
}
ZTEST(half_bridge, test_set_duty_cycle)