2017-04-30 11:11:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/analog/opamp.c
|
|
|
|
*
|
2020-10-10 14:28:05 +02:00
|
|
|
* 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
|
2017-04-30 11:11:17 +02:00
|
|
|
*
|
2020-10-10 14:28:05 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-04-30 11:11:17 +02:00
|
|
|
*
|
2020-10-10 14:28:05 +02:00
|
|
|
* 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.
|
2017-04-30 11:11:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/analog/opamp.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-26 21:51:22 +01:00
|
|
|
static int opamp_open(FAR struct file *filep);
|
|
|
|
static int opamp_close(FAR struct file *filep);
|
|
|
|
static int opamp_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
2017-04-30 11:11:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations opamp_fops =
|
|
|
|
{
|
|
|
|
opamp_open, /* open */
|
|
|
|
opamp_close, /* close */
|
|
|
|
NULL, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
opamp_ioctl, /* ioctl */
|
|
|
|
NULL /* poll */
|
2017-04-30 11:11:17 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2019-12-05 22:13:55 +01:00
|
|
|
|
2017-04-30 11:11:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: opamp_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the OPAMP device is opened.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int opamp_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct opamp_dev_s *dev = inode->i_private;
|
|
|
|
uint8_t tmp;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2017-04-30 11:11:17 +02:00
|
|
|
|
2020-06-12 16:33:38 +02:00
|
|
|
/* If the port is the middle of closing, wait until the close is
|
|
|
|
* finished.
|
|
|
|
*/
|
2017-04-30 11:11:17 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->ad_closesem);
|
|
|
|
if (ret >= 0)
|
2017-04-30 11:11:17 +02:00
|
|
|
{
|
2020-06-12 16:33:38 +02:00
|
|
|
/* Increment the count of references to the device. If this is the
|
|
|
|
* first time that the driver has been opened for this device, then
|
2020-03-10 13:25:22 +01:00
|
|
|
* initialize the device.
|
2017-04-30 11:11:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = dev->ad_ocount + 1;
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t overflows to zero */
|
|
|
|
|
|
|
|
ret = -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-12 16:33:38 +02:00
|
|
|
/* Check if this is the first time that the driver has been
|
|
|
|
* opened.
|
|
|
|
*/
|
2017-04-30 11:11:17 +02:00
|
|
|
|
|
|
|
if (tmp == 1)
|
|
|
|
{
|
|
|
|
/* Yes.. perform one time hardware initialization. */
|
|
|
|
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
ret = dev->ad_ops->ao_setup(dev);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
dev->ad_ocount = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->ad_closesem);
|
2017-04-30 11:11:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: opamp_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the OPAMP device is closed.
|
|
|
|
* It waits for the last remaining data to be sent.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int opamp_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct opamp_dev_s *dev = inode->i_private;
|
|
|
|
irqstate_t flags;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2017-04-30 11:11:17 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->ad_closesem);
|
|
|
|
if (ret >= 0)
|
2017-04-30 11:11:17 +02:00
|
|
|
{
|
|
|
|
/* Decrement the references to the driver. If the reference count will
|
|
|
|
* decrement to 0, then uninitialize the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->ad_ocount > 1)
|
|
|
|
{
|
|
|
|
dev->ad_ocount--;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->ad_closesem);
|
2017-04-30 11:11:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There are no more references to the port */
|
|
|
|
|
|
|
|
dev->ad_ocount = 0;
|
|
|
|
|
|
|
|
/* Free the IRQ and disable the OPAMP device */
|
|
|
|
|
|
|
|
flags = enter_critical_section(); /* Disable interrupts */
|
|
|
|
dev->ad_ops->ao_shutdown(dev); /* Disable the OPAMP */
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->ad_closesem);
|
2017-04-30 11:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: opamp_ioctl
|
2019-12-05 22:13:55 +01:00
|
|
|
****************************************************************************/
|
2017-04-30 11:11:17 +02:00
|
|
|
|
|
|
|
static int opamp_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct opamp_dev_s *dev = inode->i_private;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: opamp_register
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int opamp_register(FAR const char *path, FAR struct opamp_dev_s *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the OPAMP device structure */
|
|
|
|
|
|
|
|
dev->ad_ocount = 0;
|
|
|
|
|
|
|
|
/* Initialize semaphores */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&dev->ad_closesem, 0, 1);
|
2017-04-30 11:11:17 +02:00
|
|
|
|
|
|
|
/* Register the OPAMP character driver */
|
|
|
|
|
2022-01-26 13:52:09 +01:00
|
|
|
ret = register_driver(path, &opamp_fops, 0444, dev);
|
2017-04-30 11:11:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&dev->ad_closesem);
|
2017-04-30 11:11:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|