* drivers/analog/comp.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <poll.h>
#include <nuttx/arch.h>
#include <nuttx/fs/fs.h>
#include <nuttx/analog/comp.h>
#include <nuttx/irq.h>
* Private Function Prototypes
****************************************************************************/
static int comp_open(FAR struct file *filep);
static int comp_close(FAR struct file *filep);
static ssize_t comp_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static int comp_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
static int comp_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup);
static int comp_notify(FAR struct comp_dev_s *dev, uint8_t val);
* Private Data
****************************************************************************/
static const struct file_operations g_comp_fops =
{
comp_open,
comp_close,
comp_read,
NULL,
NULL,
comp_ioctl,
NULL,
NULL,
comp_poll
};
static const struct comp_callback_s g_comp_callback =
{
comp_notify
};
* Private Functions
****************************************************************************/
* Name: comp_poll
****************************************************************************/
static int comp_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
int ret = OK;
int i;
DEBUGASSERT(dev && fds);
ret = nxmutex_lock(&dev->ad_lock);
if (ret < 0)
{
return ret;
}
if (setup)
{
* slot for the poll structure reference
*/
for (i = 0; i < CONFIG_DEV_COMP_NPOLLWAITERS; i++)
{
if (!dev->d_fds[i])
{
dev->d_fds[i] = fds;
fds->priv = &dev->d_fds[i];
break;
}
}
if (i >= CONFIG_DEV_COMP_NPOLLWAITERS)
{
fds->priv = NULL;
ret = -EBUSY;
goto errout;
}
}
else
{
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
#ifdef CONFIG_DEBUG_FEATURES
if (!slot)
{
ret = -EIO;
goto errout;
}
#endif
*slot = NULL;
fds->priv = NULL;
}
errout:
nxmutex_unlock(&dev->ad_lock);
return ret;
}
* Name: comp_notify
*
* Description:
* This function is called from the lower half driver to notify the change
* of the comparator output.
*
****************************************************************************/
static int comp_notify(FAR struct comp_dev_s *dev, uint8_t val)
{
dev->val = val;
poll_notify(dev->d_fds, CONFIG_DEV_COMP_NPOLLWAITERS, POLLIN);
nxsem_post(&dev->ad_readsem);
return 0;
}
* Name: comp_open
*
* Description:
* This function is called whenever the COMP device is opened.
*
****************************************************************************/
static int comp_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
uint8_t tmp;
int ret;
* finished.
*/
ret = nxmutex_lock(&dev->ad_lock);
if (ret >= 0)
{
* first time that the driver has been opened for this device, then
* initialize the device.
*/
tmp = dev->ad_ocount + 1;
if (tmp == 0)
{
ret = -EMFILE;
}
else
{
* opened.
*/
if (tmp == 1)
{
ret = dev->ad_ops->ao_setup(dev);
if (ret == OK)
{
dev->ad_ocount = tmp;
}
}
}
nxmutex_unlock(&dev->ad_lock);
}
return ret;
}
* Name: comp_close
*
* Description:
* This routine is called when the COMP device is closed.
* It waits for the last remaining data to be sent.
*
****************************************************************************/
static int comp_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
int ret;
ret = nxmutex_lock(&dev->ad_lock);
if (ret >= 0)
{
* decrement to 0, then uninitialize the driver.
*/
if (dev->ad_ocount > 1)
{
dev->ad_ocount--;
nxmutex_unlock(&dev->ad_lock);
}
else
{
dev->ad_ocount = 0;
dev->ad_ops->ao_shutdown(dev);
nxmutex_unlock(&dev->ad_lock);
}
}
return ret;
}
* Name: comp_read
****************************************************************************/
static ssize_t comp_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
int ret;
if (filep->f_oflags & O_NONBLOCK)
{
ret = dev->ad_ops->ao_read(dev);
buffer[0] = (uint8_t)ret;
return 1;
}
ret = nxsem_wait(&dev->ad_readsem);
if (ret < 0)
{
aerr("nxsem_wait() failed: %d\n", ret);
return ret;
}
buffer[0] = dev->val;
return 1;
}
* Name: comp_ioctl
****************************************************************************/
static int comp_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
int ret;
ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
return ret;
}
* Public Functions
****************************************************************************/
* Name: comp_register
****************************************************************************/
int comp_register(FAR const char *path, FAR struct comp_dev_s *dev)
{
int ret;
dev->ad_ocount = 0;
nxmutex_init(&dev->ad_lock);
nxsem_init(&dev->ad_readsem, 0, 0);
DEBUGASSERT(dev->ad_ops != NULL);
if (dev->ad_ops->ao_bind != NULL)
{
ret = dev->ad_ops->ao_bind(dev, &g_comp_callback);
if (ret < 0)
{
aerr("ERROR: Failed to bind callbacks: %d\n", ret);
nxmutex_destroy(&dev->ad_lock);
nxsem_destroy(&dev->ad_readsem);
return ret;
}
}
ret = register_driver(path, &g_comp_fops, 0444, dev);
if (ret < 0)
{
nxmutex_destroy(&dev->ad_lock);
nxsem_destroy(&dev->ad_readsem);
}
return ret;
}