From 5d9cbbc658276b64771572710e02e754b5c2a0cc Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Thu, 17 Mar 2022 13:48:38 +0100 Subject: [PATCH] drivers: pwm capture with stm32 can have no callback When configuring the pwm capture, the callback function might be reset, this i not an error. However the isr should not call it and enabling the capture should always provide a callback function. Signed-off-by: Francois Ramu --- drivers/pwm/pwm_stm32.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/pwm/pwm_stm32.c b/drivers/pwm/pwm_stm32.c index a7613ad2370..85e7911dff7 100644 --- a/drivers/pwm/pwm_stm32.c +++ b/drivers/pwm/pwm_stm32.c @@ -371,11 +371,6 @@ static int pwm_stm32_pin_configure_capture(const struct device *dev, return -EBUSY; } - if (cb == NULL) { - LOG_ERR("No PWM capture callback specified"); - return -EINVAL; - } - if (!(flags & PWM_CAPTURE_TYPE_MASK)) { LOG_ERR("No PWM capture type specified"); return -EINVAL; @@ -386,7 +381,7 @@ static int pwm_stm32_pin_configure_capture(const struct device *dev, return -ENOTSUP; } - cpt->callback = cb; + cpt->callback = cb; /* even if the cb is reset, this is not an error */ cpt->user_data = user_data; cpt->capture_period = (flags & PWM_CAPTURE_TYPE_PERIOD) ? true : false; cpt->capture_pulse = (flags & PWM_CAPTURE_TYPE_PULSE) ? true : false; @@ -439,6 +434,11 @@ static int pwm_stm32_pin_enable_capture(const struct device *dev, uint32_t pwm) return -EBUSY; } + if (!data->capture.callback) { + LOG_ERR("PWM capture not configured"); + return -EINVAL; + } + data->capture.skip_irq = SKIPPED_PWM_CAPTURES; data->capture.overflows = 0u; LL_TIM_ClearFlag_CC1(cfg->timer); @@ -527,10 +527,12 @@ static void pwm_stm32_isr(const struct device *dev) cpt->overflows = 0u; } - cpt->callback(dev, in_ch, + if (cpt->callback != NULL) { + cpt->callback(dev, in_ch, cpt->capture_period ? cpt->period : 0u, cpt->capture_pulse ? cpt->pulse : 0u, status, cpt->user_data); + } } } else { if (LL_TIM_IsActiveFlag_UPDATE(cfg->timer)) {