From 25d496949ff80c0c758f1d071b1d6126161eb317 Mon Sep 17 00:00:00 2001 From: Sean Nyekjaer Date: Fri, 23 Jun 2023 08:24:07 +0200 Subject: [PATCH] drivers: pwm: pwm_stm32: reset timer using RCC before initialization If a timer is left running on an stm32mp1, (most likely) on the next run the timer is stuck. A simple timer reset before initialization fixes the issue. Signed-off-by: Sean Nyekjaer --- drivers/pwm/Kconfig.stm32 | 1 + drivers/pwm/pwm_stm32.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/Kconfig.stm32 b/drivers/pwm/Kconfig.stm32 index 8fc52f6cf5a..7f914af4ff3 100644 --- a/drivers/pwm/Kconfig.stm32 +++ b/drivers/pwm/Kconfig.stm32 @@ -9,6 +9,7 @@ config PWM_STM32 depends on DT_HAS_ST_STM32_PWM_ENABLED select USE_STM32_LL_TIM select USE_STM32_LL_RCC if SOC_SERIES_STM32F4X || SOC_SERIES_STM32F7X || SOC_SERIES_STM32H7X + select RESET help This option enables the PWM driver for STM32 family of processors. Say y if you wish to use PWM port on STM32 diff --git a/drivers/pwm/pwm_stm32.c b/drivers/pwm/pwm_stm32.c index 3c5103c69c4..44f021a874d 100644 --- a/drivers/pwm/pwm_stm32.c +++ b/drivers/pwm/pwm_stm32.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,8 @@ struct pwm_stm32_capture_data { struct pwm_stm32_data { /** Timer clock (Hz). */ uint32_t tim_clk; + /* Reset controller device configuration */ + const struct reset_dt_spec reset; #ifdef CONFIG_PWM_CAPTURE struct pwm_stm32_capture_data capture; #endif /* CONFIG_PWM_CAPTURE */ @@ -686,6 +689,9 @@ static int pwm_stm32_init(const struct device *dev) return r; } + /* Reset timer to default state using RCC */ + (void)reset_line_toggle_dt(&data->reset); + /* configure pinmux */ r = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT); if (r < 0) { @@ -764,7 +770,10 @@ static void pwm_stm32_irq_config_func_##index(const struct device *dev) \ #define PWM_DEVICE_INIT(index) \ - static struct pwm_stm32_data pwm_stm32_data_##index; \ + static struct pwm_stm32_data pwm_stm32_data_##index = { \ + .reset = RESET_DT_SPEC_GET(PWM(index)), \ + }; \ + \ IRQ_CONFIG_FUNC(index) \ \ PINCTRL_DT_INST_DEFINE(index); \