drivers: move STM32 timer helpers to common library

The functions for timer clock, reset and pinctrl setup are not specific
to half bridges and will also be used by the PWM power switch driver.
They are moved to a separate library enabled by the hidden Kconfig
symbol LIBRESOLAR_STM32_TIM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-11-01 13:48:56 +01:00
co-authored by Claude Opus 5.5
parent 88336bee67
commit 0adc9edb7a
10 changed files with 71 additions and 63 deletions
+1
View File
@@ -4,3 +4,4 @@
add_subdirectory_ifdef(CONFIG_DAQ daq)
add_subdirectory_ifdef(CONFIG_HALF_BRIDGE half_bridge)
add_subdirectory_ifdef(CONFIG_POWER_SWITCH power_switch)
add_subdirectory_ifdef(CONFIG_LIBRESOLAR_STM32_TIM stm32_tim)
+1
View File
@@ -6,5 +6,6 @@ menu "Libre Solar drivers"
rsource "daq/Kconfig"
rsource "half_bridge/Kconfig"
rsource "power_switch/Kconfig"
rsource "stm32_tim/Kconfig"
endmenu
+2 -1
View File
@@ -3,7 +3,8 @@
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_TIM half_bridge_stm32_tim.c)
zephyr_library_include_directories(../stm32_tim)
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)
+2 -10
View File
@@ -23,19 +23,11 @@ module = HALF_BRIDGE
module-str = half_bridge
source "subsys/logging/Kconfig.template.log_config"
config HALF_BRIDGE_STM32_TIM
bool
select USE_STM32_LL_TIM
select PINCTRL
select RESET
help
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
select LIBRESOLAR_STM32_TIM
help
Enable half-bridge driver for STM32 advanced-control timers (e.g. TIM1) using
complementary outputs with hardware dead time generation.
@@ -44,7 +36,7 @@ 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
select LIBRESOLAR_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.
+10 -10
View File
@@ -17,7 +17,7 @@
#define DT_DRV_COMPAT libresolar_stm32_advtim_half_bridge
#include "half_bridge_stm32_tim.h"
#include "stm32_tim.h"
#include <libresolar/drivers/half_bridge.h>
@@ -33,7 +33,7 @@ LOG_MODULE_REGISTER(half_bridge_stm32_advtim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
struct half_bridge_stm32_advtim_config
{
struct half_bridge_stm32_tim_config tim;
struct 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 */
@@ -67,7 +67,7 @@ static int half_bridge_stm32_advtim_start(const struct device *dev)
return -EBUSY;
}
if (half_bridge_stm32_tim_get_compare(tim, config->channel) == 0U) {
if (stm32_tim_get_compare(tim, config->channel) == 0U) {
return -EINVAL;
}
@@ -108,7 +108,7 @@ static int half_bridge_stm32_advtim_set_compare(const struct device *dev, uint32
/* 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);
stm32_tim_set_compare(tim, config->channel, compare);
#ifdef LL_TIM_CHANNEL_CH6
if (config->adc_trigger) {
@@ -124,7 +124,7 @@ 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);
return stm32_tim_get_compare(config->tim.timer, config->channel);
}
static uint32_t half_bridge_stm32_advtim_get_period(const struct device *dev)
@@ -166,7 +166,7 @@ static int half_bridge_stm32_advtim_force_low_side(const struct device *dev)
* complementary output CHxN (low side) stays on. The output mode itself can't be changed, as
* it is locked after initialization.
*/
half_bridge_stm32_tim_set_compare(tim, config->channel, 0);
stm32_tim_set_compare(tim, config->channel, 0);
/* load preload registers immediately */
LL_TIM_GenerateEvent_UPDATE(tim);
@@ -215,7 +215,7 @@ 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 ll_channel = stm32_tim_ll_channel(config->channel);
uint32_t tim_clk;
int err;
@@ -224,7 +224,7 @@ static int half_bridge_stm32_advtim_init(const struct device *dev)
return -ENOTSUP;
}
err = half_bridge_stm32_tim_init(&config->tim, &tim_clk);
err = stm32_tim_init(&config->tim, &tim_clk);
if (err < 0) {
return err;
}
@@ -268,10 +268,10 @@ static int half_bridge_stm32_advtim_init(const struct device *dev)
}
#define HALF_BRIDGE_STM32_ADVTIM_INIT(n) \
HALF_BRIDGE_STM32_TIM_DEFINE(n) \
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), \
.tim = STM32_TIM_CONFIG(n), \
.frequency = DT_INST_PROP(n, frequency), \
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
.channel = DT_INST_PROP(n, channel), \
+15 -16
View File
@@ -20,7 +20,7 @@
#define DT_DRV_COMPAT libresolar_stm32_gptim_half_bridge
#include "half_bridge_stm32_tim.h"
#include "stm32_tim.h"
#include <libresolar/drivers/half_bridge.h>
@@ -32,7 +32,7 @@ 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;
struct stm32_tim_config tim;
uint32_t frequency;
uint32_t deadtime_ns;
/** Timer channel (1-4) for the high-side switch */
@@ -52,8 +52,8 @@ struct half_bridge_stm32_gptim_data
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);
return stm32_tim_ll_channel(config->high_side_channel)
| stm32_tim_ll_channel(config->low_side_channel);
}
/*
@@ -66,8 +66,8 @@ static void set_output_modes(const struct half_bridge_stm32_gptim_config *config
{
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);
LL_TIM_OC_SetMode(tim, stm32_tim_ll_channel(config->high_side_channel), hs_mode);
LL_TIM_OC_SetMode(tim, stm32_tim_ll_channel(config->low_side_channel), ls_mode);
}
static inline void set_outputs_off(const struct half_bridge_stm32_gptim_config *config)
@@ -85,7 +85,7 @@ static int half_bridge_stm32_gptim_start(const struct device *dev)
return -EBUSY;
}
if (half_bridge_stm32_tim_get_compare(tim, config->high_side_channel) == 0U) {
if (stm32_tim_get_compare(tim, config->high_side_channel) == 0U) {
return -EINVAL;
}
@@ -127,9 +127,8 @@ static int half_bridge_stm32_gptim_set_compare(const struct device *dev, uint32_
/* 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);
stm32_tim_set_compare(tim, config->high_side_channel, compare);
stm32_tim_set_compare(tim, config->low_side_channel, compare + data->deadtime_clocks);
return 0;
}
@@ -138,7 +137,7 @@ 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);
return stm32_tim_get_compare(config->tim.timer, config->high_side_channel);
}
static uint32_t half_bridge_stm32_gptim_get_period(const struct device *dev)
@@ -200,8 +199,8 @@ 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 ll_channel_hs = stm32_tim_ll_channel(config->high_side_channel);
uint32_t ll_channel_ls = stm32_tim_ll_channel(config->low_side_channel);
uint32_t tim_clk;
int err;
@@ -218,7 +217,7 @@ static int half_bridge_stm32_gptim_init(const struct device *dev)
}
#endif
err = half_bridge_stm32_tim_init(&config->tim, &tim_clk);
err = stm32_tim_init(&config->tim, &tim_clk);
if (err < 0) {
return err;
}
@@ -262,10 +261,10 @@ static int half_bridge_stm32_gptim_init(const struct device *dev)
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) \
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), \
.tim = 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), \
+6
View File
@@ -0,0 +1,6 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources(stm32_tim.c)
+10
View File
@@ -0,0 +1,10 @@
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
config LIBRESOLAR_STM32_TIM
bool
select USE_STM32_LL_TIM
select PINCTRL
select RESET
help
Common functions for drivers using STM32 timers (TIM).
@@ -4,21 +4,21 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "half_bridge_stm32_tim.h"
#include "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);
LOG_MODULE_REGISTER(stm32_tim, LOG_LEVEL_ERR);
const uint32_t half_bridge_stm32_tim_ll_channels[4] = {
const uint32_t 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)
int stm32_tim_init(const struct stm32_tim_config *config, uint32_t *tim_clk)
{
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
int err;
@@ -66,7 +66,7 @@ int half_bridge_stm32_tim_init(const struct half_bridge_stm32_tim_config *config
return 0;
}
void half_bridge_stm32_tim_set_compare(TIM_TypeDef *tim, uint8_t channel, uint32_t value)
void stm32_tim_set_compare(TIM_TypeDef *tim, uint8_t channel, uint32_t value)
{
switch (channel) {
case 1:
@@ -84,7 +84,7 @@ void half_bridge_stm32_tim_set_compare(TIM_TypeDef *tim, uint8_t channel, uint32
}
}
uint32_t half_bridge_stm32_tim_get_compare(TIM_TypeDef *tim, uint8_t channel)
uint32_t stm32_tim_get_compare(TIM_TypeDef *tim, uint8_t channel)
{
switch (channel) {
case 1:
@@ -6,11 +6,11 @@
/**
* @file
* @brief Common functions for half-bridge drivers using STM32 timers (TIM)
* @brief Common functions for drivers using STM32 timers (TIM)
*/
#ifndef HALF_BRIDGE_STM32_TIM_H_
#define HALF_BRIDGE_STM32_TIM_H_
#ifndef LIBRESOLAR_STM32_TIM_H_
#define LIBRESOLAR_STM32_TIM_H_
#include <zephyr/device.h>
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
@@ -23,7 +23,7 @@
/**
* Timer configuration obtained from the parent st,stm32-timers node
*/
struct half_bridge_stm32_tim_config
struct stm32_tim_config
{
TIM_TypeDef *timer;
const struct stm32_pclken *pclken;
@@ -33,16 +33,15 @@ struct half_bridge_stm32_tim_config
};
/**
* Define variables required by HALF_BRIDGE_STM32_TIM_CONFIG for instance n
* Define variables required by STM32_TIM_CONFIG for instance n
*/
#define HALF_BRIDGE_STM32_TIM_DEFINE(n) \
#define 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"); \
"Device 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));
static const struct stm32_pclken stm32_tim_pclken_##n[] = STM32_DT_CLOCKS(DT_INST_PARENT(n));
/*
* Formatting disabled, as different clang-format versions disagree on braced lists in macros.
@@ -50,12 +49,12 @@ struct half_bridge_stm32_tim_config
/* clang-format off */
/**
* Initializer for struct half_bridge_stm32_tim_config of instance n
* Initializer for struct stm32_tim_config of instance n
*/
#define HALF_BRIDGE_STM32_TIM_CONFIG(n) \
#define STM32_TIM_CONFIG(n) \
{ \
.timer = (TIM_TypeDef *)DT_REG_ADDR(DT_INST_PARENT(n)), \
.pclken = half_bridge_stm32_tim_pclken_##n, \
.pclken = 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), \
@@ -64,16 +63,16 @@ struct half_bridge_stm32_tim_config
/* clang-format on */
/** LL channel definitions for channels 1 to 4 */
extern const uint32_t half_bridge_stm32_tim_ll_channels[4];
extern const uint32_t 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)
static inline uint32_t stm32_tim_ll_channel(uint8_t channel)
{
return half_bridge_stm32_tim_ll_channels[channel - 1];
return stm32_tim_ll_channels[channel - 1];
}
/**
@@ -85,8 +84,7 @@ static inline uint32_t half_bridge_stm32_tim_ll_channel(uint8_t channel)
* @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);
int stm32_tim_init(const struct stm32_tim_config *config, uint32_t *tim_clk);
/**
* Set compare value of a timer channel
@@ -95,7 +93,7 @@ int half_bridge_stm32_tim_init(const struct half_bridge_stm32_tim_config *config
* @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);
void stm32_tim_set_compare(TIM_TypeDef *tim, uint8_t channel, uint32_t value);
/**
* Get compare value of a timer channel
@@ -105,6 +103,6 @@ void half_bridge_stm32_tim_set_compare(TIM_TypeDef *tim, uint8_t channel, uint32
*
* @returns Compare value
*/
uint32_t half_bridge_stm32_tim_get_compare(TIM_TypeDef *tim, uint8_t channel);
uint32_t stm32_tim_get_compare(TIM_TypeDef *tim, uint8_t channel);
#endif /* HALF_BRIDGE_STM32_TIM_H_ */
#endif /* LIBRESOLAR_STM32_TIM_H_ */