Files
Martin JägerandClaude Opus 5.5 0adc9edb7a 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>
2026-11-01 13:48:56 +01:00

109 lines
2.7 KiB
C

/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Common functions for drivers using STM32 timers (TIM)
*/
#ifndef LIBRESOLAR_STM32_TIM_H_
#define LIBRESOLAR_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 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 STM32_TIM_CONFIG for instance n
*/
#define STM32_TIM_DEFINE(n) \
BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_INST_PARENT(n), st_stm32_timers), \
"Device must be a child of an st,stm32-timers node"); \
\
PINCTRL_DT_INST_DEFINE(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.
*/
/* clang-format off */
/**
* Initializer for struct stm32_tim_config of instance n
*/
#define STM32_TIM_CONFIG(n) \
{ \
.timer = (TIM_TypeDef *)DT_REG_ADDR(DT_INST_PARENT(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), \
}
/* clang-format on */
/** LL channel definitions for channels 1 to 4 */
extern const uint32_t stm32_tim_ll_channels[4];
/**
* Get LL channel definition
*
* @param channel Timer channel (1-4)
*/
static inline uint32_t stm32_tim_ll_channel(uint8_t channel)
{
return 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 stm32_tim_init(const struct 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 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 stm32_tim_get_compare(TIM_TypeDef *tim, uint8_t channel);
#endif /* LIBRESOLAR_STM32_TIM_H_ */