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-apps.git

commit 33b34f88522547d1ab5d41805b25b167348ba90a
Author: raiden00pl <raide...@railab.me>
AuthorDate: Sun Oct 31 10:35:43 2021 +0100

    examples/foc: open FOC device in FOC threads
---
 examples/foc/foc_fixed16_thr.c | 52 ++++++++++++++++++++++++++++--------------
 examples/foc/foc_float_thr.c   | 50 +++++++++++++++++++++++++++-------------
 examples/foc/foc_main.c        | 17 --------------
 examples/foc/foc_thr.h         |  1 -
 4 files changed, 69 insertions(+), 51 deletions(-)

diff --git a/examples/foc/foc_fixed16_thr.c b/examples/foc/foc_fixed16_thr.c
index b41f313..44fb1e6 100644
--- a/examples/foc/foc_fixed16_thr.c
+++ b/examples/foc/foc_fixed16_thr.c
@@ -64,6 +64,7 @@
 struct foc_motor_b16_s
 {
   FAR struct foc_ctrl_env_s    *envp;         /* Thread env */
+  struct foc_device_s           dev;          /* FOC device */
   bool                          fault;        /* Fault flag */
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
   bool                          openloop_now; /* Open-loop now */
@@ -121,15 +122,6 @@ static int foc_motor_init(FAR struct foc_motor_b16_s 
*motor,
 
   motor->envp = envp;
 
-  /* Get device info */
-
-  ret = foc_dev_getinfo(envp->dev.fd, &motor->info);
-  if (ret < 0)
-    {
-      PRINTF("ERROR: foc_dev_getinfo failed %d!\n", ret);
-      goto errout;
-    }
-
   /* Initialize motor data */
 
   motor->per        = b16divi(b16ONE, CONFIG_EXAMPLES_FOC_NOTIFIER_FREQ);
@@ -297,7 +289,7 @@ static int foc_motor_configure(FAR struct foc_motor_b16_s 
*motor)
 
   /* Configure FOC device */
 
-  ret = foc_dev_setcfg(motor->envp->dev.fd, &cfg);
+  ret = foc_dev_setcfg(motor->dev.fd, &cfg);
   if (ret < 0)
     {
       PRINTF("ERROR: foc_dev_setcfg %d!\n", ret);
@@ -339,7 +331,7 @@ static int foc_motor_start(FAR struct foc_motor_b16_s 
*motor, bool start)
 
           PRINTF("Start FOC device %d!\n", motor->envp->id);
 
-          ret = foc_dev_start(motor->envp->dev.fd);
+          ret = foc_dev_start(motor->dev.fd);
           if (ret < 0)
             {
               PRINTF("ERROR: foc_dev_start failed %d!\n", ret);
@@ -353,7 +345,7 @@ static int foc_motor_start(FAR struct foc_motor_b16_s 
*motor, bool start)
 
       PRINTF("Stop FOC device %d!\n", motor->envp->id);
 
-      ret = foc_dev_stop(motor->envp->dev.fd);
+      ret = foc_dev_stop(motor->dev.fd);
       if (ret < 0)
         {
           PRINTF("ERROR: foc_dev_stop failed %d!\n", ret);
@@ -663,7 +655,7 @@ static int foc_dev_state_get(FAR struct foc_motor_b16_s 
*motor)
 
   /* Get FOC state - blocking */
 
-  ret = foc_dev_getstate(motor->envp->dev.fd, &motor->dev_state);
+  ret = foc_dev_getstate(motor->dev.fd, &motor->dev_state);
   if (ret < 0)
     {
       PRINTF("ERROR: foc_dev_getstate failed %d!\n", ret);
@@ -699,7 +691,7 @@ static int foc_dev_params_set(FAR struct foc_motor_b16_s 
*motor)
 
   /* Write FOC parameters */
 
-  ret = foc_dev_setparams(motor->envp->dev.fd, &motor->dev_params);
+  ret = foc_dev_setparams(motor->dev.fd, &motor->dev_params);
   if (ret < 0)
     {
       PRINTF("ERROR: foc_dev_setparams failed %d!\n", ret);
@@ -871,6 +863,24 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
       goto errout;
     }
 
+  /* Open FOC device as blocking */
+
+  ret = foc_device_open(&motor.dev, envp->id);
+  if (ret < 0)
+    {
+      PRINTF("ERROR: foc_device_open failed %d!\n", ret);
+      goto errout;
+    }
+
+  /* Get device info */
+
+  ret = foc_dev_getinfo(motor.dev.fd, &motor.info);
+  if (ret < 0)
+    {
+      PRINTF("ERROR: foc_dev_getinfo failed %d!\n", ret);
+      goto errout;
+    }
+
   /* Initialize controller mode */
 
   ret = foc_mode_init(&motor);
@@ -930,7 +940,7 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
           ret = foc_state_handle(&motor);
           if (ret < 0)
             {
-              PRINTF("ERROR: foc_state_handle failed %d!\n", ret);
+              PRINTF("ERROR: foc_dev_state_handle failed %d!\n", ret);
               goto errout;
             }
 
@@ -938,7 +948,7 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
             {
               /* Clear fault state */
 
-              ret = foc_dev_clearfault(envp->dev.fd);
+              ret = foc_dev_clearfault(motor.dev.fd);
               if (ret != OK)
                 {
                   PRINTF("ERROR: foc_dev_clearfault failed %d!\n", ret);
@@ -1023,12 +1033,20 @@ errout:
 
   /* Stop FOC device */
 
-  ret = foc_dev_stop(envp->dev.fd);
+  ret = foc_dev_stop(motor.dev.fd);
   if (ret < 0)
     {
       PRINTF("ERROR: foc_dev_stop %d!\n", ret);
     }
 
+  /* Close FOC control device */
+
+  ret = foc_device_close(&motor.dev);
+  if (ret < 0)
+    {
+      PRINTF("ERROR: foc_device_close %d failed %d\n", envp->id, ret);
+    }
+
   PRINTF("foc_fixed16_thr %d exit\n", envp->id);
 
   return ret;
diff --git a/examples/foc/foc_float_thr.c b/examples/foc/foc_float_thr.c
index 3a5ef25..1386ae8 100644
--- a/examples/foc/foc_float_thr.c
+++ b/examples/foc/foc_float_thr.c
@@ -64,6 +64,7 @@
 struct foc_motor_f32_s
 {
   FAR struct foc_ctrl_env_s    *envp;         /* Thread env */
+  struct foc_device_s           dev;          /* FOC device */
   bool                          fault;        /* Fault flag */
 #ifdef CONFIG_EXAMPLES_FOC_HAVE_OPENLOOP
   bool                          openloop_now; /* Open-loop now */
@@ -121,15 +122,6 @@ static int foc_motor_init(FAR struct foc_motor_f32_s 
*motor,
 
   motor->envp = envp;
 
-  /* Get device info */
-
-  ret = foc_dev_getinfo(envp->dev.fd, &motor->info);
-  if (ret < 0)
-    {
-      PRINTFV("ERROR: foc_dev_getinfo failed %d!\n", ret);
-      goto errout;
-    }
-
   /* Initialize motor data */
 
   motor->per        = (float)(1.0f / CONFIG_EXAMPLES_FOC_NOTIFIER_FREQ);
@@ -297,7 +289,7 @@ static int foc_motor_configure(FAR struct foc_motor_f32_s 
*motor)
 
   /* Configure FOC device */
 
-  ret = foc_dev_setcfg(motor->envp->dev.fd, &cfg);
+  ret = foc_dev_setcfg(motor->dev.fd, &cfg);
   if (ret < 0)
     {
       PRINTFV("ERROR: foc_dev_setcfg %d!\n", ret);
@@ -339,7 +331,7 @@ static int foc_motor_start(FAR struct foc_motor_f32_s 
*motor, bool start)
 
           PRINTF("Start FOC device %d!\n", motor->envp->id);
 
-          ret = foc_dev_start(motor->envp->dev.fd);
+          ret = foc_dev_start(motor->dev.fd);
           if (ret < 0)
             {
               PRINTFV("ERROR: foc_dev_start failed %d!\n", ret);
@@ -360,7 +352,7 @@ static int foc_motor_start(FAR struct foc_motor_f32_s 
*motor, bool start)
 
       PRINTF("Stop FOC device %d!\n", motor->envp->id);
 
-      ret = foc_dev_stop(motor->envp->dev.fd);
+      ret = foc_dev_stop(motor->dev.fd);
       if (ret < 0)
         {
           PRINTFV("ERROR: foc_dev_stop failed %d!\n", ret);
@@ -663,7 +655,7 @@ static int foc_dev_state_get(FAR struct foc_motor_f32_s 
*motor)
 
   /* Get FOC state - blocking */
 
-  ret = foc_dev_getstate(motor->envp->dev.fd, &motor->dev_state);
+  ret = foc_dev_getstate(motor->dev.fd, &motor->dev_state);
   if (ret < 0)
     {
       PRINTFV("ERROR: foc_dev_getstate failed %d!\n", ret);
@@ -699,7 +691,7 @@ static int foc_dev_params_set(FAR struct foc_motor_f32_s 
*motor)
 
   /* Write FOC parameters */
 
-  ret = foc_dev_setparams(motor->envp->dev.fd, &motor->dev_params);
+  ret = foc_dev_setparams(motor->dev.fd, &motor->dev_params);
   if (ret < 0)
     {
       PRINTFV("ERROR: foc_dev_setparams failed %d!\n", ret);
@@ -874,6 +866,24 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
       goto errout;
     }
 
+  /* Open FOC device as blocking */
+
+  ret = foc_device_open(&motor.dev, envp->id);
+  if (ret < 0)
+    {
+      PRINTF("ERROR: foc_device_open failed %d!\n", ret);
+      goto errout;
+    }
+
+  /* Get device info */
+
+  ret = foc_dev_getinfo(motor.dev.fd, &motor.info);
+  if (ret < 0)
+    {
+      PRINTF("ERROR: foc_dev_getinfo failed %d!\n", ret);
+      goto errout;
+    }
+
   /* Initialize controller mode */
 
   ret = foc_mode_init(&motor);
@@ -941,7 +951,7 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
             {
               /* Clear fault state */
 
-              ret = foc_dev_clearfault(envp->dev.fd);
+              ret = foc_dev_clearfault(motor.dev.fd);
               if (ret != OK)
                 {
                   goto errout;
@@ -1025,12 +1035,20 @@ errout:
 
   /* Stop FOC device */
 
-  ret = foc_dev_stop(envp->dev.fd);
+  ret = foc_dev_stop(motor.dev.fd);
   if (ret < 0)
     {
       PRINTFV("ERROR: foc_dev_stop failed %d!\n", ret);
     }
 
+  /* Close FOC control device */
+
+  ret = foc_device_close(&motor.dev);
+  if (ret < 0)
+    {
+      PRINTF("ERROR: foc_device_close %d failed %d\n", envp->id, ret);
+    }
+
   PRINTF("foc_float_thr %d exit\n", envp->id);
 
   return ret;
diff --git a/examples/foc/foc_main.c b/examples/foc/foc_main.c
index cfe72e1..61f7062 100644
--- a/examples/foc/foc_main.c
+++ b/examples/foc/foc_main.c
@@ -303,15 +303,6 @@ FAR void *foc_control_thr(FAR void *arg)
 
   DEBUGASSERT(envp);
 
-  /* Open FOC device as blocking */
-
-  ret = foc_device_open(&envp->dev, envp->id);
-  if (ret < 0)
-    {
-      PRINTF("ERROR: foc_device_open failed %d!\n", ret);
-      goto errout;
-    }
-
   /* Get controller type */
 
   pthread_mutex_lock(&g_cntr_lock);
@@ -411,14 +402,6 @@ FAR void *foc_control_thr(FAR void *arg)
 
 errout:
 
-  /* Close FOC control device */
-
-  ret = foc_device_close(&envp->dev);
-  if (ret < 0)
-    {
-      PRINTF("ERROR: foc_device_close %d failed %d\n", envp->id, ret);
-    }
-
   /* Close queue */
 
   if (envp->mqd == (mqd_t)-1)
diff --git a/examples/foc/foc_thr.h b/examples/foc/foc_thr.h
index 0797fbb..a714a3d 100644
--- a/examples/foc/foc_thr.h
+++ b/examples/foc/foc_thr.h
@@ -70,7 +70,6 @@ enum foc_operation_mode_e
 
 struct foc_ctrl_env_s
 {
-  struct foc_device_s dev;      /* FOC device */
   mqd_t               mqd;      /* Control msg queue */
   int                 id;       /* FOC device id */
   int                 inst;     /* Type specific instance counter */

Reply via email to