From bf27e4d75d4af9d16101454b0636ffbf4218dacd Mon Sep 17 00:00:00 2001 From: chao an Date: Wed, 11 Dec 2024 09:46:08 +0800 Subject: [PATCH] esp/mcpwm: fix unpaired spin lock N/A Signed-off-by: chao an --- arch/risc-v/src/common/espressif/esp_mcpwm.c | 8 ++++++++ arch/xtensa/src/common/espressif/esp_mcpwm.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/arch/risc-v/src/common/espressif/esp_mcpwm.c b/arch/risc-v/src/common/espressif/esp_mcpwm.c index 56b38ef04a..994a90f4ad 100644 --- a/arch/risc-v/src/common/espressif/esp_mcpwm.c +++ b/arch/risc-v/src/common/espressif/esp_mcpwm.c @@ -427,6 +427,7 @@ static int esp_motor_setup(struct motor_lowerhalf_s *dev) if ((priv->state.state == MOTOR_STATE_FAULT) || (priv->state.state == MOTOR_STATE_CRITICAL)) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor is in fault state. Clear faults first\n"); return ERROR; } @@ -553,6 +554,7 @@ static int esp_motor_stop(struct motor_lowerhalf_s *dev) if (priv->state.state == MOTOR_STATE_IDLE) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor already stopped\n"); return -EPERM; } @@ -569,6 +571,7 @@ static int esp_motor_stop(struct motor_lowerhalf_s *dev) ret = esp_motor_set_duty_cycle(priv, 0.0); if (ret < 0) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Failed setting duty cycle to 0 on stop: %d\n", ret); return ret; } @@ -622,6 +625,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) flags = spin_lock_irqsave(&g_mcpwm_common.mcpwm_spinlock); if (priv->state.state == MOTOR_STATE_RUN) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor already running\n"); return -EINVAL; } @@ -629,6 +633,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) if ((priv->state.state == MOTOR_STATE_CRITICAL) || (priv->state.state == MOTOR_STATE_FAULT)) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor is in fault state\n"); return -EINVAL; } @@ -636,6 +641,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) ret = esp_motor_pwm_config(priv); if (ret < 0) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Failed setting PWM configuration\n"); return ret; } @@ -655,6 +661,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) ret = esp_motor_set_duty_cycle(priv, duty); if (ret < 0) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Failed starting motor\n"); return ret; } @@ -1022,6 +1029,7 @@ static int esp_motor_fault_configure(struct mcpwm_motor_lowerhalf_s *lower, flags = spin_lock_irqsave(&g_mcpwm_common.mcpwm_spinlock); if (!enable) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mcpwm_ll_fault_enable_detection(hal->dev, lower->fault_id, false); mcpwm_ll_intr_enable(hal->dev, MCPWM_LL_EVENT_FAULT_ENTER(lower->fault_id), diff --git a/arch/xtensa/src/common/espressif/esp_mcpwm.c b/arch/xtensa/src/common/espressif/esp_mcpwm.c index 4a19c54087..3e67036fe4 100644 --- a/arch/xtensa/src/common/espressif/esp_mcpwm.c +++ b/arch/xtensa/src/common/espressif/esp_mcpwm.c @@ -454,6 +454,7 @@ static int esp_motor_setup(struct motor_lowerhalf_s *dev) if ((priv->state.state == MOTOR_STATE_FAULT) || (priv->state.state == MOTOR_STATE_CRITICAL)) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor is in fault state. Clear faults first\n"); return ERROR; } @@ -579,6 +580,7 @@ static int esp_motor_stop(struct motor_lowerhalf_s *dev) flags = spin_lock_irqsave(&g_mcpwm_common.mcpwm_spinlock); if (priv->state.state == MOTOR_STATE_IDLE) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor already stopped\n"); return -EPERM; } @@ -595,6 +597,7 @@ static int esp_motor_stop(struct motor_lowerhalf_s *dev) ret = esp_motor_set_duty_cycle(priv, 0.0); if (ret < 0) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Failed setting duty cycle to 0 on stop: %d\n", ret); return ret; } @@ -649,6 +652,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) flags = spin_lock_irqsave(&g_mcpwm_common.mcpwm_spinlock); if (priv->state.state == MOTOR_STATE_RUN) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor already running\n"); return -EINVAL; } @@ -656,6 +660,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) if ((priv->state.state == MOTOR_STATE_CRITICAL) || (priv->state.state == MOTOR_STATE_FAULT)) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Motor is in fault state\n"); return -EINVAL; } @@ -663,6 +668,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) ret = esp_motor_pwm_config(priv); if (ret < 0) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Failed setting PWM configuration\n"); return ret; } @@ -682,6 +688,7 @@ static int esp_motor_start(struct motor_lowerhalf_s *dev) ret = esp_motor_set_duty_cycle(priv, duty); if (ret < 0) { + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); mtrerr("Failed starting motor\n"); return ret; } @@ -1056,6 +1063,7 @@ static int esp_motor_fault_configure(struct mcpwm_motor_lowerhalf_s *lower, mcpwm_ll_intr_enable(hal->dev, MCPWM_LL_EVENT_FAULT_EXIT(lower->fault_id), false); + spin_unlock_irqrestore(&g_mcpwm_common.mcpwm_spinlock, flags); return OK; }