drivers: half_bridge: split STM32 timer driver into advtim and gptim

The STM32 timer half-bridge driver supported two operating modes, which
only shared the clock, reset and pinctrl setup, but branched in every
API function. Split it into two drivers with separate compatibles:

- libresolar,stm32-advtim-half-bridge: Advanced-control timers (e.g.
  TIM1) with complementary outputs and hardware dead time generation.
- libresolar,stm32-gptim-half-bridge: General-purpose timers (e.g. TIM3)
  without dead time generator, using two independent channels with dead
  time generated in software.

This allows the devicetree bindings to only contain the properties
valid for the respective timer type (e.g. adc-trigger only for advtim,
high-side-channel and low-side-channel required for gptim) and removes
most of the preprocessor conditionals for MCUs without advanced-control
timers. The common timer setup is moved to half_bridge_stm32_tim.c.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-10-20 21:43:58 +02:00
co-authored by Claude Opus 5.5
parent b271dc0aba
commit 0164841139
16 changed files with 796 additions and 527 deletions
+1 -1
View File
@@ -137,7 +137,7 @@
status = "okay";
halfbridge: halfbridge {
compatible = "libresolar,stm32-half-bridge";
compatible = "libresolar,stm32-advtim-half-bridge";
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
pinctrl-names = "default";
channel = <1>;
@@ -195,10 +195,10 @@
status = "okay";
halfbridge: halfbridge {
compatible = "libresolar,stm32-half-bridge";
compatible = "libresolar,stm32-gptim-half-bridge";
pinctrl-0 = <&tim3_ch3_pb0 &tim3_ch4_pb1>;
pinctrl-names = "default";
channel = <3>;
high-side-channel = <3>;
low-side-channel = <4>;
frequency = <50000>;
deadtime-ns = <230>;
@@ -206,7 +206,7 @@
status = "okay";
halfbridge: halfbridge {
compatible = "libresolar,stm32-half-bridge";
compatible = "libresolar,stm32-advtim-half-bridge";
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pc13>;
pinctrl-names = "default";
channel = <1>;
@@ -226,7 +226,7 @@
status = "okay";
halfbridge: halfbridge {
compatible = "libresolar,stm32-half-bridge";
compatible = "libresolar,stm32-advtim-half-bridge";
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
pinctrl-names = "default";
channel = <1>;
@@ -225,7 +225,7 @@
status = "okay";
halfbridge: halfbridge {
compatible = "libresolar,stm32-half-bridge";
compatible = "libresolar,stm32-advtim-half-bridge";
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
pinctrl-names = "default";
channel = <1>;
@@ -181,7 +181,7 @@
status = "okay";
halfbridge: halfbridge {
compatible = "libresolar,stm32-half-bridge";
compatible = "libresolar,stm32-advtim-half-bridge";
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
pinctrl-names = "default";
channel = <1>;
+3 -1
View File
@@ -3,6 +3,8 @@
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32 half_bridge_stm32.c)
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_TIM half_bridge_stm32_tim.c)
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_ADVTIM half_bridge_stm32_advtim.c)
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_GPTIM half_bridge_stm32_gptim.c)
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_HRTIM half_bridge_stm32_hrtim.c)
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_EMUL half_bridge_emul.c)
+23 -6
View File
@@ -3,7 +3,8 @@
menuconfig HALF_BRIDGE
bool "Half-bridge drivers"
default y if DT_HAS_LIBRESOLAR_STM32_HALF_BRIDGE_ENABLED
default y if DT_HAS_LIBRESOLAR_STM32_ADVTIM_HALF_BRIDGE_ENABLED
default y if DT_HAS_LIBRESOLAR_STM32_GPTIM_HALF_BRIDGE_ENABLED
default y if DT_HAS_LIBRESOLAR_STM32_HRTIM_HALF_BRIDGE_ENABLED
default y if DT_HAS_LIBRESOLAR_HALF_BRIDGE_EMUL_ENABLED
help
@@ -22,15 +23,31 @@ 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
config HALF_BRIDGE_STM32_TIM
bool
select USE_STM32_LL_TIM
select PINCTRL
select RESET
help
Enable half-bridge driver for STM32 timers (TIM).
Common functions for half-bridge drivers using STM32 timers (TIM).
config HALF_BRIDGE_STM32_ADVTIM
bool "STM32 advanced-control timer half-bridge driver"
default y
depends on DT_HAS_LIBRESOLAR_STM32_ADVTIM_HALF_BRIDGE_ENABLED
select HALF_BRIDGE_STM32_TIM
help
Enable half-bridge driver for STM32 advanced-control timers (e.g. TIM1) using
complementary outputs with hardware dead time generation.
config HALF_BRIDGE_STM32_GPTIM
bool "STM32 general-purpose timer half-bridge driver"
default y
depends on DT_HAS_LIBRESOLAR_STM32_GPTIM_HALF_BRIDGE_ENABLED
select HALF_BRIDGE_STM32_TIM
help
Enable half-bridge driver for STM32 general-purpose timers (e.g. TIM3) using two
independent channels with dead time generated by software.
config HALF_BRIDGE_STM32_HRTIM
bool "STM32 HRTIM half-bridge driver"
-449
View File
@@ -1,449 +0,0 @@
/*
* 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,239 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Half-bridge driver for STM32 advanced-control timers (e.g. TIM1)
*
* 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.
*/
#define DT_DRV_COMPAT libresolar_stm32_advtim_half_bridge
#include "half_bridge_stm32_tim.h"
#include <libresolar/drivers/half_bridge.h>
#include <zephyr/device.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
LOG_MODULE_REGISTER(half_bridge_stm32_advtim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
#ifndef LL_TIM_CHANNEL_CH1N
#error "Advanced-control timers with complementary outputs not supported by this MCU"
#endif
struct half_bridge_stm32_advtim_config
{
struct half_bridge_stm32_tim_config tim;
uint32_t frequency;
uint32_t deadtime_ns;
/** Timer channel (1-4) for the high-side switch, CHxN is used for the low-side switch */
uint8_t channel;
bool adc_trigger;
};
struct half_bridge_stm32_advtim_data
{
uint32_t compare_min;
uint32_t compare_max;
};
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
};
static int half_bridge_stm32_advtim_start(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
TIM_TypeDef *tim = config->tim.timer;
if (half_bridge_stm32_tim_get_compare(tim, config->channel) == 0U) {
return -EINVAL;
}
/* main output enable */
LL_TIM_EnableAllOutputs(tim);
return 0;
}
static int half_bridge_stm32_advtim_stop(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
LL_TIM_DisableAllOutputs(config->tim.timer);
return 0;
}
static bool half_bridge_stm32_advtim_is_enabled(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
return LL_TIM_IsEnabledAllOutputs(config->tim.timer);
}
static int half_bridge_stm32_advtim_set_compare(const struct device *dev, uint32_t compare)
{
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;
/* protection against wrong settings which could destroy the hardware */
compare = CLAMP(compare, data->compare_min, data->compare_max);
half_bridge_stm32_tim_set_compare(tim, config->channel, compare);
#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
return 0;
}
static uint32_t half_bridge_stm32_advtim_get_compare(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
return half_bridge_stm32_tim_get_compare(config->tim.timer, config->channel);
}
static uint32_t half_bridge_stm32_advtim_get_period(const struct device *dev)
{
const struct half_bridge_stm32_advtim_config *config = dev->config;
return LL_TIM_GetAutoReload(config->tim.timer);
}
static int half_bridge_stm32_advtim_set_compare_limits(const struct device *dev, uint32_t min,
uint32_t max)
{
struct half_bridge_stm32_advtim_data *data = dev->data;
if (min > max || max > half_bridge_stm32_advtim_get_period(dev)) {
return -EINVAL;
}
data->compare_min = min;
data->compare_max = max;
return half_bridge_stm32_advtim_set_compare(dev, half_bridge_stm32_advtim_get_compare(dev));
}
static DEVICE_API(half_bridge, half_bridge_stm32_advtim_api) = {
.start = half_bridge_stm32_advtim_start,
.stop = half_bridge_stm32_advtim_stop,
.is_enabled = half_bridge_stm32_advtim_is_enabled,
.set_compare = half_bridge_stm32_advtim_set_compare,
.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,
};
static int half_bridge_stm32_advtim_init_adc_trigger(TIM_TypeDef *tim)
{
#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);
return 0;
#else
LOG_ERR("ADC trigger not supported by this timer");
return -ENOTSUP;
#endif
}
static int half_bridge_stm32_advtim_init(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;
uint32_t ll_channel = half_bridge_stm32_tim_ll_channel(config->channel);
uint32_t tim_clk;
int err;
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;
}
err = half_bridge_stm32_tim_init(&config->tim, &tim_clk);
if (err < 0) {
return err;
}
/* 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) {
err = half_bridge_stm32_advtim_init_adc_trigger(tim);
if (err < 0) {
return err;
}
}
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);
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 HALF_BRIDGE_STM32_ADVTIM_INIT(n) \
HALF_BRIDGE_STM32_TIM_DEFINE(n) \
\
static const struct half_bridge_stm32_advtim_config half_bridge_stm32_advtim_config_##n = { \
.tim = HALF_BRIDGE_STM32_TIM_CONFIG(n), \
.frequency = DT_INST_PROP(n, frequency), \
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
.channel = DT_INST_PROP(n, channel), \
.adc_trigger = DT_INST_PROP(n, adc_trigger), \
}; \
\
static struct half_bridge_stm32_advtim_data half_bridge_stm32_advtim_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, half_bridge_stm32_advtim_init, NULL, \
&half_bridge_stm32_advtim_data_##n, \
&half_bridge_stm32_advtim_config_##n, POST_KERNEL, \
CONFIG_HALF_BRIDGE_INIT_PRIORITY, &half_bridge_stm32_advtim_api);
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_STM32_ADVTIM_INIT)
@@ -0,0 +1,221 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Half-bridge driver for STM32 general-purpose timers (e.g. TIM3)
*
* The high-side and low-side switches are driven by two independent channels, as general-purpose
* 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.
*/
#define DT_DRV_COMPAT libresolar_stm32_gptim_half_bridge
#include "half_bridge_stm32_tim.h"
#include <libresolar/drivers/half_bridge.h>
#include <zephyr/device.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
LOG_MODULE_REGISTER(half_bridge_stm32_gptim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
struct half_bridge_stm32_gptim_config
{
struct half_bridge_stm32_tim_config tim;
uint32_t frequency;
uint32_t deadtime_ns;
/** Timer channel (1-4) for the high-side switch */
uint8_t high_side_channel;
/** Timer channel (1-4) for the low-side switch */
uint8_t low_side_channel;
};
struct half_bridge_stm32_gptim_data
{
uint32_t deadtime_clocks;
uint32_t compare_min;
uint32_t compare_max;
};
static inline uint32_t ll_channels(const struct half_bridge_stm32_gptim_config *config)
{
return half_bridge_stm32_tim_ll_channel(config->high_side_channel)
| half_bridge_stm32_tim_ll_channel(config->low_side_channel);
}
static int half_bridge_stm32_gptim_start(const struct device *dev)
{
const struct half_bridge_stm32_gptim_config *config = dev->config;
TIM_TypeDef *tim = config->tim.timer;
if (half_bridge_stm32_tim_get_compare(tim, config->high_side_channel) == 0U) {
return -EINVAL;
}
LL_TIM_CC_EnableChannel(tim, ll_channels(config));
return 0;
}
static int half_bridge_stm32_gptim_stop(const struct device *dev)
{
const struct half_bridge_stm32_gptim_config *config = dev->config;
LL_TIM_CC_DisableChannel(config->tim.timer, ll_channels(config));
return 0;
}
static bool half_bridge_stm32_gptim_is_enabled(const struct device *dev)
{
const struct half_bridge_stm32_gptim_config *config = dev->config;
return LL_TIM_CC_IsEnabledChannel(config->tim.timer,
half_bridge_stm32_tim_ll_channel(config->high_side_channel));
}
static int half_bridge_stm32_gptim_set_compare(const struct device *dev, uint32_t compare)
{
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;
/* protection against wrong settings which could destroy the hardware */
compare = CLAMP(compare, data->compare_min, data->compare_max);
half_bridge_stm32_tim_set_compare(tim, config->high_side_channel, compare);
half_bridge_stm32_tim_set_compare(tim, config->low_side_channel,
compare + data->deadtime_clocks);
return 0;
}
static uint32_t half_bridge_stm32_gptim_get_compare(const struct device *dev)
{
const struct half_bridge_stm32_gptim_config *config = dev->config;
return half_bridge_stm32_tim_get_compare(config->tim.timer, config->high_side_channel);
}
static uint32_t half_bridge_stm32_gptim_get_period(const struct device *dev)
{
const struct half_bridge_stm32_gptim_config *config = dev->config;
return LL_TIM_GetAutoReload(config->tim.timer);
}
static int half_bridge_stm32_gptim_set_compare_limits(const struct device *dev, uint32_t min,
uint32_t max)
{
struct half_bridge_stm32_gptim_data *data = dev->data;
if (min > max || max > half_bridge_stm32_gptim_get_period(dev)) {
return -EINVAL;
}
data->compare_min = min;
data->compare_max = max;
return half_bridge_stm32_gptim_set_compare(dev, half_bridge_stm32_gptim_get_compare(dev));
}
static DEVICE_API(half_bridge, half_bridge_stm32_gptim_api) = {
.start = half_bridge_stm32_gptim_start,
.stop = half_bridge_stm32_gptim_stop,
.is_enabled = half_bridge_stm32_gptim_is_enabled,
.set_compare = half_bridge_stm32_gptim_set_compare,
.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,
};
static int half_bridge_stm32_gptim_init(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;
uint32_t ll_channel_hs = half_bridge_stm32_tim_ll_channel(config->high_side_channel);
uint32_t ll_channel_ls = half_bridge_stm32_tim_ll_channel(config->low_side_channel);
uint32_t tim_clk;
int err;
if (!IS_TIM_COUNTER_MODE_SELECT_INSTANCE(tim)) {
LOG_ERR("Center-aligned mode not supported by this timer");
return -ENOTSUP;
}
#ifdef IS_TIM_BREAK_INSTANCE
if (IS_TIM_BREAK_INSTANCE(tim)) {
/* outputs would additionally require main output enable (MOE) */
LOG_ERR("Timer has dead time generator, use advtim half-bridge instead");
return -ENOTSUP;
}
#endif
err = half_bridge_stm32_tim_init(&config->tim, &tim_clk);
if (err < 0) {
return err;
}
/*
* 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;
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 HALF_BRIDGE_STM32_GPTIM_INIT(n) \
BUILD_ASSERT(DT_INST_PROP(n, high_side_channel) != DT_INST_PROP(n, low_side_channel), \
"High-side and low-side channel must be different"); \
\
HALF_BRIDGE_STM32_TIM_DEFINE(n) \
\
static const struct half_bridge_stm32_gptim_config half_bridge_stm32_gptim_config_##n = { \
.tim = HALF_BRIDGE_STM32_TIM_CONFIG(n), \
.frequency = DT_INST_PROP(n, frequency), \
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
.high_side_channel = DT_INST_PROP(n, high_side_channel), \
.low_side_channel = DT_INST_PROP(n, low_side_channel), \
}; \
\
static struct half_bridge_stm32_gptim_data half_bridge_stm32_gptim_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, half_bridge_stm32_gptim_init, NULL, \
&half_bridge_stm32_gptim_data_##n, &half_bridge_stm32_gptim_config_##n, \
POST_KERNEL, CONFIG_HALF_BRIDGE_INIT_PRIORITY, \
&half_bridge_stm32_gptim_api);
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_STM32_GPTIM_INIT)
+101
View File
@@ -0,0 +1,101 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "half_bridge_stm32_tim.h"
#include <zephyr/drivers/clock_control.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(half_bridge_stm32_tim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
const uint32_t half_bridge_stm32_tim_ll_channels[4] = {
LL_TIM_CHANNEL_CH1,
LL_TIM_CHANNEL_CH2,
LL_TIM_CHANNEL_CH3,
LL_TIM_CHANNEL_CH4,
};
int half_bridge_stm32_tim_init(const struct half_bridge_stm32_tim_config *config, uint32_t *tim_clk)
{
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
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(config->timer, 0);
return 0;
}
void half_bridge_stm32_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;
}
}
uint32_t half_bridge_stm32_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;
}
}
+103
View File
@@ -0,0 +1,103 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Common functions for half-bridge drivers using STM32 timers (TIM)
*/
#ifndef HALF_BRIDGE_STM32_TIM_H_
#define HALF_BRIDGE_STM32_TIM_H_
#include <zephyr/device.h>
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/reset.h>
#include <soc.h>
#include <stm32_ll_tim.h>
/**
* Timer configuration obtained from the parent st,stm32-timers node
*/
struct half_bridge_stm32_tim_config
{
TIM_TypeDef *timer;
const struct stm32_pclken *pclken;
size_t pclk_len;
struct reset_dt_spec reset;
const struct pinctrl_dev_config *pcfg;
};
/**
* Define variables required by HALF_BRIDGE_STM32_TIM_CONFIG for instance n
*/
#define HALF_BRIDGE_STM32_TIM_DEFINE(n) \
BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_INST_PARENT(n), st_stm32_timers), \
"Half bridge must be a child of an st,stm32-timers node"); \
\
PINCTRL_DT_INST_DEFINE(n); \
\
static const struct stm32_pclken half_bridge_stm32_tim_pclken_##n[] = \
STM32_DT_CLOCKS(DT_INST_PARENT(n));
/**
* Initializer for struct half_bridge_stm32_tim_config of instance n
*/
#define HALF_BRIDGE_STM32_TIM_CONFIG(n) \
{ \
.timer = (TIM_TypeDef *)DT_REG_ADDR(DT_INST_PARENT(n)), \
.pclken = half_bridge_stm32_tim_pclken_##n, \
.pclk_len = DT_NUM_CLOCKS(DT_INST_PARENT(n)), \
.reset = RESET_DT_SPEC_GET(DT_INST_PARENT(n)), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
}
/** LL channel definitions for channels 1 to 4 */
extern const uint32_t half_bridge_stm32_tim_ll_channels[4];
/**
* Get LL channel definition
*
* @param channel Timer channel (1-4)
*/
static inline uint32_t half_bridge_stm32_tim_ll_channel(uint8_t channel)
{
return half_bridge_stm32_tim_ll_channels[channel - 1];
}
/**
* Enable clock, reset the timer and apply pin configuration
*
* @param config Timer configuration
* @param tim_clk Pointer to store timer clock frequency in Hz
*
* @retval 0 on success
* @retval -errno otherwise
*/
int half_bridge_stm32_tim_init(const struct half_bridge_stm32_tim_config *config,
uint32_t *tim_clk);
/**
* Set compare value of a timer channel
*
* @param tim Timer instance
* @param channel Timer channel (1-4)
* @param value Compare value
*/
void half_bridge_stm32_tim_set_compare(TIM_TypeDef *tim, uint8_t channel, uint32_t value);
/**
* Get compare value of a timer channel
*
* @param tim Timer instance
* @param channel Timer channel (1-4)
*
* @returns Compare value
*/
uint32_t half_bridge_stm32_tim_get_compare(TIM_TypeDef *tim, uint8_t channel);
#endif /* HALF_BRIDGE_STM32_TIM_H_ */
@@ -0,0 +1,50 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
description: |
Half bridge using an STM32 advanced-control timer (e.g. TIM1)
The node must be a child of an st,stm32-timers node. 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.
Example:
&timers1 {
status = "okay";
halfbridge: half-bridge {
compatible = "libresolar,stm32-advtim-half-bridge";
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pc13>;
pinctrl-names = "default";
channel = <1>;
frequency = <70000>;
deadtime-ns = <300>;
};
};
compatible: "libresolar,stm32-advtim-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 with output CHx and the low-side switch with
complementary output CHxN
adc-trigger:
type: boolean
description: |
Trigger ADC conversions in the middle of the on-time using OC6 via TRGO2
Only supported by timers providing channel 6 and TRGO2 (e.g. TIM1 of STM32G4).
@@ -0,0 +1,49 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
description: |
Half bridge using an STM32 general-purpose timer (e.g. TIM3)
The node must be a child of an st,stm32-timers node. As general-purpose timers have no
hardware dead time generation, the high-side and low-side switches are driven by two
independent channels. 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:
&timers3 {
status = "okay";
halfbridge: half-bridge {
compatible = "libresolar,stm32-gptim-half-bridge";
pinctrl-0 = <&tim3_ch3_pb0 &tim3_ch4_pb1>;
pinctrl-names = "default";
high-side-channel = <3>;
low-side-channel = <4>;
frequency = <50000>;
deadtime-ns = <230>;
};
};
compatible: "libresolar,stm32-gptim-half-bridge"
include: [base.yaml, pinctrl-device.yaml, half-bridge-common.yaml]
properties:
pinctrl-0:
required: true
pinctrl-names:
required: true
high-side-channel:
type: int
required: true
enum: [1, 2, 3, 4]
description: Timer channel driving the high-side switch
low-side-channel:
type: int
required: true
enum: [1, 2, 3, 4]
description: Timer channel driving the low-side switch
@@ -1,64 +0,0 @@
# 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).