zouboan commented on code in PR #6525: URL: https://github.com/apache/incubator-nuttx/pull/6525#discussion_r906900591
########## 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: Thanks for your agile eyes! Maybe we can start capture through open,because the capture driver is different from pwm driver, the pwm driver should be started through ioctl when system is ready, It's dangerous if pwm output at device open stage. There's no such things for capture driver, if no pulse at the beginning, the capture will not be triggered, and if there's pulse at the beginning,the results will be overwritten. In fact, I implemented capture driver by reference to ./drivers/sensors/hall3ph.c -- 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-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org