You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/11/19 01:27:20 UTC

[incubator-nuttx] 02/02: driver/motor: Remove the unnecessary critical section operation

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit a29ee19af4bb8b8e6b2f1111712512fd399c22c0
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Nov 15 22:32:55 2021 +0800

    driver/motor: Remove the unnecessary critical section operation
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 arch/arm/src/stm32/stm32_foc.c | 11 --------
 arch/sim/src/sim/up_foc.c      | 43 +++++++++++--------------------
 drivers/motor/foc/foc_dev.c    | 57 +++---------------------------------------
 drivers/motor/motor.c          |  7 ------
 include/nuttx/motor/foc/foc.h  |  1 -
 include/nuttx/motor/motor.h    |  4 +--
 6 files changed, 20 insertions(+), 103 deletions(-)

diff --git a/arch/arm/src/stm32/stm32_foc.c b/arch/arm/src/stm32/stm32_foc.c
index bcdda9f..e1d8c5d 100644
--- a/arch/arm/src/stm32/stm32_foc.c
+++ b/arch/arm/src/stm32/stm32_foc.c
@@ -2037,15 +2037,6 @@ static int stm32_foc_bind(FAR struct foc_dev_s *dev,
   DEBUGASSERT(cb);
   DEBUGASSERT(priv);
 
-  /* Do we support given FOC instance? */
-
-  if (dev->devno > CONFIG_MOTOR_FOC_INST)
-    {
-      mtrerr("Unsupported STM32 FOC instance %d\n", dev->devno);
-      ret = -EINVAL;
-      goto errout;
-    }
-
   /* Validate callbacks */
 
   DEBUGASSERT(cb->notifier);
@@ -2053,8 +2044,6 @@ static int stm32_foc_bind(FAR struct foc_dev_s *dev,
   /* Bind upper-half FOC device callbacks */
 
   priv->cb = cb;
-
-errout:
   return ret;
 }
 
diff --git a/arch/sim/src/sim/up_foc.c b/arch/sim/src/sim/up_foc.c
index 3042737..656688a 100644
--- a/arch/sim/src/sim/up_foc.c
+++ b/arch/sim/src/sim/up_foc.c
@@ -175,7 +175,7 @@ static int sim_foc_pwm_setup(FAR struct foc_dev_s *dev, uint32_t freq)
   DEBUGASSERT(dev);
   DEBUGASSERT(sim);
 
-  mtrinfo("[PWM_SETUP] devno=%d freq=%d\n", dev->devno, freq);
+  mtrinfo("[PWM_SETUP] freq=%d\n", freq);
 
   DEBUGASSERT(freq > 0);
 
@@ -200,7 +200,7 @@ static int sim_foc_start(FAR struct foc_dev_s *dev, bool state)
   irqstate_t                 flags;
   int                        ret = OK;
 
-  mtrinfo("[FOC_START] devno=%d state=%d\n", dev->devno, state);
+  mtrinfo("[FOC_START] state=%d\n", state);
 
   /* Start PWM */
 
@@ -242,7 +242,7 @@ static int sim_foc_pwm_start(FAR struct foc_dev_s *dev, bool state)
 {
   DEBUGASSERT(dev);
 
-  mtrinfo("[PWM_START] devno=%d state=%d\n", dev->devno, state);
+  mtrinfo("[PWM_START] state=%d\n", state);
 
   return OK;
 }
@@ -259,7 +259,7 @@ static int sim_foc_adc_setup(FAR struct foc_dev_s *dev)
 {
   DEBUGASSERT(dev);
 
-  mtrinfo("[ADC_SETUP] devno=%d\n", dev->devno);
+  mtrinfo("[ADC_SETUP]\n");
 
   return OK;
 }
@@ -276,7 +276,7 @@ static int sim_foc_adc_start(FAR struct foc_dev_s *dev, bool state)
 {
   DEBUGASSERT(dev);
 
-  mtrinfo("[ADC_START] devno=%d state=%d\n", dev->devno, state);
+  mtrinfo("[ADC_START] state=%d\n", state);
 
   return OK;
 }
@@ -297,7 +297,7 @@ static int sim_foc_notifier_cfg(FAR struct foc_dev_s *dev, uint32_t freq)
   DEBUGASSERT(dev);
   DEBUGASSERT(sim);
 
-  mtrinfo("[NOTIFIER_CFG] devno=%d freq=%d\n", dev->devno, freq);
+  mtrinfo("[NOTIFIER_CFG] freq=%d\n", freq);
 
   DEBUGASSERT(freq > 0);
 
@@ -336,7 +336,7 @@ static int sim_foc_configure(FAR struct foc_dev_s *dev,
   DEBUGASSERT(cfg->pwm_freq > 0);
   DEBUGASSERT(cfg->notifier_freq > 0);
 
-  mtrinfo("[FOC_SETUP] devno=%d\n", dev->devno);
+  mtrinfo("[FOC_SETUP]\n");
 
   /* Configure ADC */
 
@@ -385,7 +385,7 @@ static int sim_foc_setup(FAR struct foc_dev_s *dev)
 {
   DEBUGASSERT(dev);
 
-  mtrinfo("[FOC_SETUP] devno=%d\n", dev->devno);
+  mtrinfo("[FOC_SETUP]\n");
 
   /* Get HW configuration */
 
@@ -406,7 +406,7 @@ static int sim_foc_shutdown(FAR struct foc_dev_s *dev)
 {
   DEBUGASSERT(dev);
 
-  mtrinfo("[FOC_SHUTDOWN] devno=%d\n", dev->devno);
+  mtrinfo("[FOC_SHUTDOWN]\n");
 
   return OK;
 }
@@ -426,7 +426,7 @@ static int sim_foc_ioctl(FAR struct foc_dev_s *dev, int cmd,
 
   DEBUGASSERT(dev);
 
-  mtrinfo("[FOC_IOCTL] devno=%d cmd=%d\n", dev->devno, cmd);
+  mtrinfo("[FOC_IOCTL]cmd=%d\n", cmd);
 
   switch (cmd)
     {
@@ -456,8 +456,7 @@ static int sim_foc_notifier_handler(FAR struct foc_dev_s *dev)
   DEBUGASSERT(dev);
   DEBUGASSERT(sim);
 
-  mtrinfo("[FOC_NOTIFIER_HANDLER] devno=%d cntr=%d\n",
-          dev->devno, sim->notifier_cntr);
+  mtrinfo("[FOC_NOTIFIER_HANDLER] cntr=%d\n", sim->notifier_cntr);
 
   flags = enter_critical_section();
 
@@ -500,7 +499,7 @@ static int sim_foc_pwm_duty_set(FAR struct foc_dev_s *dev,
       DEBUGASSERT(duty[i] >= 0);
     }
 
-  mtrinfo("[PWM_DUTY_SET] devno=%d duty= ", dev->devno);
+  mtrinfo("[PWM_DUTY_SET]\n");
 
 #if CONFIG_MOTOR_FOC_PHASES == 2
   mtrinfo("[%d %d]\n", duty[0], duty[1]);
@@ -551,22 +550,11 @@ static int sim_foc_bind(FAR struct foc_dev_s *dev,
   DEBUGASSERT(cb);
   DEBUGASSERT(sim);
 
-  mtrinfo("[FOC_BIND] devno=%d\n", dev->devno);
-
-  /* Do we support given FOC instance? */
-
-  if (dev->devno > CONFIG_MOTOR_FOC_INST)
-    {
-      mtrerr("unsupported SIM FOC instance %d\n", dev->devno);
-      ret = -EINVAL;
-      goto errout;
-    }
+  mtrinfo("[FOC_BIND]\n");
 
   /* Bind upper-half FOC controller callbacks */
 
   sim->cb = cb;
-
-errout:
   return ret;
 }
 
@@ -582,7 +570,7 @@ static int sim_foc_fault_clear(FAR struct foc_dev_s *dev)
 {
   DEBUGASSERT(dev);
 
-  mtrinfo("[FAULT_CLEAR] devno=%d\n", dev->devno);
+  mtrinfo("[FAULT_CLEAR]\n");
 
   return OK;
 }
@@ -600,8 +588,7 @@ static void sim_foc_trace(FAR struct foc_dev_s *dev, int type, bool state)
 {
   DEBUGASSERT(dev);
 
-  mtrinfo("[FOC_TRACE] devno=%d type=%d state=%d\n",
-          dev->devno, type, state);
+  mtrinfo("[FOC_TRACE] type=%d state=%d\n", type, state);
 }
 #endif  /* CONFIG_MOTOR_FOC_TRACE */
 
diff --git a/drivers/motor/foc/foc_dev.c b/drivers/motor/foc/foc_dev.c
index 23ebe3f..8484f86 100644
--- a/drivers/motor/foc/foc_dev.c
+++ b/drivers/motor/foc/foc_dev.c
@@ -68,10 +68,6 @@ static int foc_notifier(FAR struct foc_dev_s *dev,
  * Private Data
  ****************************************************************************/
 
-/* Device counter */
-
-static uint8_t g_devno_cntr = 0;
-
 /* File operations */
 
 static const struct file_operations g_foc_fops =
@@ -113,7 +109,6 @@ static int foc_open(FAR struct file *filep)
   FAR struct foc_dev_s *dev   = inode->i_private;
   uint8_t               tmp   = 0;
   int                   ret   = OK;
-  irqstate_t            flags;
 
   /* Non-blocking operations not supported */
 
@@ -147,10 +142,6 @@ static int foc_open(FAR struct file *filep)
 
           if (tmp == 1)
             {
-              /* Yes.. perform one time driver setup */
-
-              flags = enter_critical_section();
-
               ret = foc_setup(dev);
               if (ret == OK)
                 {
@@ -158,8 +149,6 @@ static int foc_open(FAR struct file *filep)
 
                   dev->ocount = tmp;
                 }
-
-              leave_critical_section(flags);
             }
           else
             {
@@ -189,7 +178,6 @@ static int foc_close(FAR struct file *filep)
   FAR struct inode     *inode = filep->f_inode;
   FAR struct foc_dev_s *dev   = inode->i_private;
   int                   ret   = 0;
-  irqstate_t            flags;
 
   ret = nxsem_wait(&dev->closesem);
   if (ret >= 0)
@@ -211,10 +199,7 @@ static int foc_close(FAR struct file *filep)
 
           /* Shutdown the device */
 
-          flags = enter_critical_section();
           ret = foc_shutdown(dev);
-          leave_critical_section(flags);
-
           nxsem_post(&dev->closesem);
         }
     }
@@ -259,9 +244,6 @@ static int foc_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
   FAR struct inode     *inode = filep->f_inode;
   FAR struct foc_dev_s *dev   = inode->i_private;
   int                   ret   = 0;
-  irqstate_t            flags;
-
-  flags = enter_critical_section();
 
   switch (cmd)
     {
@@ -388,8 +370,6 @@ static int foc_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
         }
     }
 
-  leave_critical_section(flags);
-
   return ret;
 }
 
@@ -462,7 +442,6 @@ static int foc_setup(FAR struct foc_dev_s *dev)
   if (ret < 0)
     {
       mtrerr("foc_lower_bind failed %d\n", ret);
-      set_errno(EINVAL);
       goto errout;
     }
 
@@ -606,8 +585,8 @@ static int foc_cfg_set(FAR struct foc_dev_s *dev, FAR struct foc_cfg_s *cfg)
 
   memcpy(&dev->cfg, cfg, sizeof(struct foc_cfg_s));
 
-  mtrinfo("FOC %" PRIu8 " PWM=%" PRIu32 " notifier=%" PRIu32 "\n",
-          dev->devno, dev->cfg.pwm_freq, dev->cfg.notifier_freq);
+  mtrinfo("FOC PWM=%" PRIu32 " notifier=%" PRIu32 "\n",
+          dev->cfg.pwm_freq, dev->cfg.notifier_freq);
 
   /* Call arch configuration */
 
@@ -762,13 +741,6 @@ static int foc_notifier(FAR struct foc_dev_s *dev,
   FOC_OPS_TRACE(dev, FOC_TRACE_NOTIFIER, true);
 #endif
 
-  /* Disable pre-emption until all of the waiting threads have been
-   * restarted. This is necessary to assure that the sval behaves as
-   * expected in the following while loop
-   */
-
-  sched_lock();
-
   /* Copy currents */
 
   memcpy(&dev->state.curr,
@@ -817,10 +789,6 @@ static int foc_notifier(FAR struct foc_dev_s *dev,
         }
     }
 
-  /* Now we can let the restarted threads run */
-
-  sched_unlock();
-
 #ifdef CONFIG_MOTOR_FOC_TRACE
   FOC_OPS_TRACE(dev, FOC_TRACE_NOTIFIER, false);
 #endif
@@ -857,24 +825,10 @@ int foc_register(FAR const char *path, FAR struct foc_dev_s *dev)
   DEBUGASSERT(dev->lower->ops);
   DEBUGASSERT(dev->lower->data);
 
-  /* Check if the device instance is supported by the driver */
-
-  if (dev->devno > CONFIG_MOTOR_FOC_INST)
-    {
-      mtrerr("Unsupported foc devno %d\n\n", dev->devno);
-      set_errno(EINVAL);
-      ret = ERROR;
-      goto errout;
-    }
-
   /* Reset counter */
 
   dev->ocount = 0;
 
-  /* Store device number */
-
-  dev->devno = g_devno_cntr;
-
   /* Assert the lower-half interface */
 
   ret = foc_lower_ops_assert(dev->lower->ops);
@@ -895,15 +849,10 @@ int foc_register(FAR const char *path, FAR struct foc_dev_s *dev)
   if (ret < 0)
     {
       nxsem_destroy(&dev->closesem);
-      set_errno(ret);
-      ret = ERROR;
+      nxsem_destroy(&dev->statesem);
       goto errout;
     }
 
-  /* Increase device counter */
-
-  g_devno_cntr += 1;
-
 errout:
   return ret;
 }
diff --git a/drivers/motor/motor.c b/drivers/motor/motor.c
index 9a7b889..241701f 100644
--- a/drivers/motor/motor.c
+++ b/drivers/motor/motor.c
@@ -133,7 +133,6 @@ static int motor_open(FAR struct file *filep)
             {
               /* Yes.. perform one time hardware initialization. */
 
-              irqstate_t flags = enter_critical_section();
               ret = lower->ops->setup(lower);
               if (ret == OK)
                 {
@@ -141,8 +140,6 @@ static int motor_open(FAR struct file *filep)
 
                   upper->ocount = tmp;
                 }
-
-              leave_critical_section(flags);
             }
         }
 
@@ -165,7 +162,6 @@ static int motor_close(FAR struct file *filep)
   FAR struct inode *inode = filep->f_inode;
   FAR struct motor_upperhalf_s *upper = inode->i_private;
   FAR struct motor_lowerhalf_s *lower = upper->lower;
-  irqstate_t flags;
   int ret;
 
   ret = nxsem_wait(&upper->closesem);
@@ -188,10 +184,7 @@ static int motor_close(FAR struct file *filep)
 
           /* Free the IRQ and disable the motor device */
 
-          flags = enter_critical_section();      /* Disable interrupts */
           lower->ops->shutdown(lower);           /* Disable the motor */
-          leave_critical_section(flags);
-
           nxsem_post(&upper->closesem);
         }
     }
diff --git a/include/nuttx/motor/foc/foc.h b/include/nuttx/motor/foc/foc.h
index 7468976..17d995f 100644
--- a/include/nuttx/motor/foc/foc.h
+++ b/include/nuttx/motor/foc/foc.h
@@ -112,7 +112,6 @@ struct foc_dev_s
 {
   /* Fields managed by common upper-half FOC logic **************************/
 
-  uint8_t                    devno;      /* FOC device instance number */
   uint8_t                    ocount;     /* The number of times the device
                                           * has been opened
                                           */
diff --git a/include/nuttx/motor/motor.h b/include/nuttx/motor/motor.h
index 867c572..8ec4e59 100644
--- a/include/nuttx/motor/motor.h
+++ b/include/nuttx/motor/motor.h
@@ -18,8 +18,8 @@
  *
  ****************************************************************************/
 
-#ifndef __INCLUDE_NUTTX_DRIVERS_MOTOR_MOTOR_H
-#define __INCLUDE_NUTTX_DRIVERS_MOTOR_MOTOR_H
+#ifndef __INCLUDE_NUTTX_MOTOR_MOTOR_H
+#define __INCLUDE_NUTTX_MOTOR_MOTOR_H
 
 /* The motor driver is split into two parts:
  *