You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/06/26 16:10:10 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #6525: drivers: add support of pwm capture driver

xiaoxiang781216 commented on code in PR #6525:
URL: https://github.com/apache/incubator-nuttx/pull/6525#discussion_r906840369


##########
drivers/timers/capture.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * drivers/timers/capture.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/arch.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/timers/capture.h>
+
+#include <arch/irq.h>
+
+#ifdef CONFIG_CAPTURE
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Debug ********************************************************************/
+
+/****************************************************************************
+ * Private Type Definitions
+ ****************************************************************************/
+
+/* This structure describes the state of the upper half driver */
+
+struct cap_upperhalf_s
+{
+  uint8_t                    crefs;    /* The number of times the device has been opened */
+  sem_t                      exclsem;  /* Supports mutual exclusion */
+  FAR struct cap_lowerhalf_s *lower;   /* lower-half state */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int     cap_open(FAR struct file *filep);
+static int     cap_close(FAR struct file *filep);
+static ssize_t cap_read(FAR struct file *filep, FAR char *buffer,
+                       size_t buflen);
+static ssize_t cap_write(FAR struct file *filep, FAR const char *buffer,
+                        size_t buflen);
+static int cap_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations g_capops =
+{
+  cap_open,  /* open */
+  cap_close, /* close */
+  cap_read,  /* read */
+  cap_write, /* write */
+  NULL,      /* seek */
+  cap_ioctl, /* ioctl */
+  NULL,      /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  NULL       /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: cap_open
+ *
+ * Description:
+ *   This function is called whenever the PWM Capture device is opened.
+ *
+ ****************************************************************************/
+
+static int cap_open(FAR struct file *filep)
+{
+  FAR struct inode           *inode = filep->f_inode;
+  FAR struct cap_upperhalf_s *upper = inode->i_private;
+  uint8_t                     tmp;
+  int                         ret;
+
+  /* Get exclusive access to the device structures */
+
+  ret = nxsem_wait(&upper->exclsem);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  /* Increment the count of references to the device.  If this is the first
+   * time that the driver has been opened for this device, then initialize
+   * the device.
+   */
+
+  tmp = upper->crefs + 1;
+  if (tmp == 0)
+    {
+      /* More than 255 opens; uint8_t overflows to zero */
+
+      ret = -EMFILE;
+      goto errout_with_sem;
+    }
+
+  /* Check if this is the first time that the driver has been opened. */
+
+  if (tmp == 1)
+    {
+      FAR struct cap_lowerhalf_s *lower = upper->lower;
+
+      /* Yes.. perform one time hardware initialization. */
+
+      DEBUGASSERT(lower->ops->start != NULL);
+
+      ret = lower->ops->start(lower);

Review Comment:
   should we let user control start/stop the capture through ioctl? so the user don't need open/close the same device multiple times.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org