2011-08-25 16:11:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/analog/dac.c
|
|
|
|
*
|
2022-02-21 13:21:09 +01: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
|
2014-04-13 22:32:20 +02:00
|
|
|
*
|
2022-02-21 13:21:09 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-08-25 16:11:39 +02:00
|
|
|
*
|
2022-02-21 13:21:09 +01: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.
|
2011-08-25 16:11:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-08-25 16:11:39 +02:00
|
|
|
#include <nuttx/analog/dac.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define HALF_SECOND_MSEC 500
|
|
|
|
#define HALF_SECOND_USEC 500000L
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int dac_open(FAR struct file *filep);
|
|
|
|
static int dac_close(FAR struct file *filep);
|
2015-10-10 18:41:00 +02:00
|
|
|
static ssize_t dac_write(FAR struct file *filep, FAR const char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen);
|
2015-10-10 18:41:00 +02:00
|
|
|
static int dac_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations dac_fops =
|
|
|
|
{
|
2022-01-10 15:51:17 +01:00
|
|
|
dac_open, /* open */
|
|
|
|
dac_close, /* close */
|
2022-01-26 13:52:09 +01:00
|
|
|
NULL, /* read */
|
2022-01-10 15:51:17 +01:00
|
|
|
dac_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
dac_ioctl, /* ioctl */
|
|
|
|
NULL /* poll */
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
2011-08-25 16:11:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2019-12-05 22:13:55 +01:00
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
/****************************************************************************
|
2011-08-25 16:11:39 +02:00
|
|
|
* Name: dac_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the DAC device is opened.
|
|
|
|
*
|
2020-03-10 13:25:22 +01:00
|
|
|
****************************************************************************/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
static int dac_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct dac_dev_s *dev = inode->i_private;
|
|
|
|
uint8_t tmp;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2020-06-12 16:33:38 +02:00
|
|
|
/* If the port is in the middle of closing, wait until the close is
|
|
|
|
* finished.
|
|
|
|
*/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->ad_closelock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret >= 0)
|
2011-08-25 16:11:39 +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.
|
2011-08-25 16:11:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = dev->ad_ocount + 1;
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t overflows to zero */
|
|
|
|
|
|
|
|
ret = -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-17 16:47:40 +02:00
|
|
|
/* Check if this is the first time that the driver has been
|
|
|
|
* opened.
|
|
|
|
*/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
if (tmp == 1)
|
|
|
|
{
|
|
|
|
/* Yes.. perform one time hardware initialization. */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2011-08-25 16:11:39 +02:00
|
|
|
ret = dev->ad_ops->ao_setup(dev);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Mark the FIFOs empty */
|
|
|
|
|
|
|
|
dev->ad_xmit.af_head = 0;
|
|
|
|
dev->ad_xmit.af_tail = 0;
|
|
|
|
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
dev->ad_ocount = tmp;
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->ad_closelock);
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2011-08-25 16:11:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
/****************************************************************************
|
2011-08-25 16:11:39 +02:00
|
|
|
* Name: dac_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the DAC device is closed.
|
|
|
|
* It waits for the last remaining data to be sent.
|
|
|
|
*
|
2020-03-10 13:25:22 +01:00
|
|
|
****************************************************************************/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
static int dac_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct dac_dev_s *dev = inode->i_private;
|
|
|
|
irqstate_t flags;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->ad_closelock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret >= 0)
|
2011-08-25 16:11:39 +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--;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->ad_closelock);
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There are no more references to the port */
|
|
|
|
|
|
|
|
dev->ad_ocount = 0;
|
|
|
|
|
|
|
|
/* Now we wait for the transmit FIFO to clear */
|
|
|
|
|
|
|
|
while (dev->ad_xmit.af_head != dev->ad_xmit.af_tail)
|
|
|
|
{
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(HALF_SECOND_USEC);
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the IRQ and disable the DAC device */
|
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
flags = enter_critical_section(); /* Disable interrupts */
|
2011-08-25 16:11:39 +02:00
|
|
|
dev->ad_ops->ao_shutdown(dev); /* Disable the DAC */
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->ad_closelock);
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2011-08-25 16:11:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
/****************************************************************************
|
2011-08-25 16:11:39 +02:00
|
|
|
* Name: dac_xmit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the message at the head of the ad_xmit FIFO
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with interrupts disabled
|
|
|
|
*
|
2020-03-10 13:25:22 +01:00
|
|
|
****************************************************************************/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
static int dac_xmit(FAR struct dac_dev_s *dev)
|
|
|
|
{
|
|
|
|
bool enable = false;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Check if the xmit FIFO is empty */
|
|
|
|
|
|
|
|
if (dev->ad_xmit.af_head != dev->ad_xmit.af_tail)
|
|
|
|
{
|
|
|
|
/* Send the next message at the head of the FIFO */
|
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
ret = dev->ad_ops->ao_send(dev,
|
|
|
|
&dev->ad_xmit.af_buffer[dev->ad_xmit.af_head]);
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
/* Make sure the TX done interrupts are enabled */
|
|
|
|
|
|
|
|
enable = (ret == OK ? true : false);
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2011-08-25 16:11:39 +02:00
|
|
|
dev->ad_ops->ao_txint(dev, enable);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
/****************************************************************************
|
2011-08-25 16:11:39 +02:00
|
|
|
* Name: dac_write
|
2020-03-10 13:25:22 +01:00
|
|
|
****************************************************************************/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2019-12-05 22:13:55 +01:00
|
|
|
static ssize_t dac_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
2011-08-25 16:11:39 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct dac_dev_s *dev = inode->i_private;
|
|
|
|
FAR struct dac_fifo_s *fifo = &dev->ad_xmit;
|
|
|
|
FAR struct dac_msg_s *msg;
|
2014-02-11 01:08:49 +01:00
|
|
|
bool empty;
|
2011-08-25 16:11:39 +02:00
|
|
|
ssize_t nsent = 0;
|
|
|
|
irqstate_t flags;
|
|
|
|
int nexttail;
|
|
|
|
int msglen;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-12-21 16:46:33 +01:00
|
|
|
/* Interrupts must be disabled throughout the following */
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
/* Check if the TX FIFO was empty when we started. That is a clue that we
|
|
|
|
* have to kick off a new TX sequence.
|
2011-08-25 16:11:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
empty = (fifo->af_head == fifo->af_tail);
|
|
|
|
|
|
|
|
/* Add the messages to the FIFO. Ignore any trailing messages that are
|
|
|
|
* shorter than the minimum.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
if (buflen % 5 == 0)
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
|
|
|
msglen = 5;
|
|
|
|
}
|
|
|
|
else if (buflen % 4 == 0)
|
|
|
|
{
|
|
|
|
msglen = 4;
|
|
|
|
}
|
|
|
|
else if (buflen % 3 == 0)
|
|
|
|
{
|
|
|
|
msglen = 3;
|
|
|
|
}
|
|
|
|
else if (buflen % 2 == 0)
|
|
|
|
{
|
|
|
|
msglen = 2;
|
|
|
|
}
|
2011-08-25 16:11:39 +02:00
|
|
|
else if (buflen == 1)
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
|
|
|
msglen = 1;
|
|
|
|
}
|
2011-08-25 16:11:39 +02:00
|
|
|
else
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
|
|
|
msglen = 5;
|
|
|
|
}
|
2011-08-25 16:11:39 +02:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
while ((buflen - nsent) >= msglen)
|
2011-08-25 16:11:39 +02:00
|
|
|
{
|
2019-12-05 22:13:55 +01:00
|
|
|
/* Check if adding this new message would over-run the drivers ability
|
|
|
|
* to enqueue xmit data.
|
2011-08-25 16:11:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
nexttail = fifo->af_tail + 1;
|
|
|
|
if (nexttail >= CONFIG_DAC_FIFOSIZE)
|
|
|
|
{
|
|
|
|
nexttail = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-12 16:33:38 +02:00
|
|
|
/* If the XMIT FIFO becomes full, then wait for space to become
|
2020-05-17 16:47:40 +02:00
|
|
|
* available.
|
|
|
|
*/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
while (nexttail == fifo->af_head)
|
|
|
|
{
|
2020-06-12 16:33:38 +02:00
|
|
|
/* The transmit FIFO is full -- was non-blocking mode selected? */
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
if (nsent == 0)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nsent;
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2011-08-25 16:11:39 +02:00
|
|
|
goto return_with_irqdisabled;
|
|
|
|
}
|
|
|
|
|
2020-06-12 16:33:38 +02:00
|
|
|
/* If the FIFO was empty when we started, then we will have to
|
2011-08-25 16:11:39 +02:00
|
|
|
* start the XMIT sequence to clear the FIFO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (empty)
|
|
|
|
{
|
|
|
|
dac_xmit(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for a message to be sent */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
ret = nxsem_wait_uninterruptible(&fifo->af_sem);
|
|
|
|
if (ret < 0)
|
2011-08-25 16:11:39 +02:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
goto return_with_irqdisabled;
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-check the FIFO state */
|
|
|
|
|
|
|
|
empty = (fifo->af_head == fifo->af_tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We get here if there is space at the end of the FIFO. Add the new
|
2018-12-21 16:46:33 +01:00
|
|
|
* DAC message at the tail of the FIFO.
|
2011-08-25 16:11:39 +02:00
|
|
|
*/
|
2011-12-15 17:39:06 +01:00
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
if (msglen == 5)
|
|
|
|
{
|
|
|
|
msg = (FAR struct dac_msg_s *)&buffer[nsent];
|
|
|
|
memcpy(&fifo->af_buffer[fifo->af_tail], msg, msglen);
|
|
|
|
}
|
|
|
|
else if (msglen == 4)
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
fifo->af_buffer[fifo->af_tail].am_channel = buffer[nsent];
|
2019-12-05 22:13:55 +01:00
|
|
|
fifo->af_buffer[fifo->af_tail].am_data =
|
|
|
|
*(FAR uint32_t *)&buffer[nsent];
|
|
|
|
fifo->af_buffer[fifo->af_tail].am_data &= 0xffffff00;
|
2014-02-11 01:08:49 +01:00
|
|
|
}
|
2014-04-12 20:53:19 +02:00
|
|
|
else if (msglen == 3)
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
fifo->af_buffer[fifo->af_tail].am_channel = buffer[nsent];
|
2019-12-05 22:13:55 +01:00
|
|
|
fifo->af_buffer[fifo->af_tail].am_data =
|
|
|
|
(*(FAR uint16_t *)&buffer[nsent + 1]);
|
|
|
|
fifo->af_buffer[fifo->af_tail].am_data <<= 16;
|
2014-02-11 01:08:49 +01:00
|
|
|
}
|
2014-04-12 20:53:19 +02:00
|
|
|
else if (msglen == 2)
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
fifo->af_buffer[fifo->af_tail].am_channel = 0;
|
2019-12-05 22:13:55 +01:00
|
|
|
fifo->af_buffer[fifo->af_tail].am_data =
|
|
|
|
(*(FAR uint16_t *)&buffer[nsent]);
|
|
|
|
fifo->af_buffer[fifo->af_tail].am_data <<= 16;
|
2014-02-11 01:08:49 +01:00
|
|
|
}
|
2014-04-12 20:53:19 +02:00
|
|
|
else if (msglen == 1)
|
2019-12-05 22:13:55 +01:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
fifo->af_buffer[fifo->af_tail].am_channel = 0;
|
|
|
|
fifo->af_buffer[fifo->af_tail].am_data = buffer[nsent];
|
|
|
|
fifo->af_buffer[fifo->af_tail].am_data <<= 24;
|
2019-12-05 22:13:55 +01:00
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2011-08-25 16:11:39 +02:00
|
|
|
/* Increment the tail of the circular buffer */
|
|
|
|
|
|
|
|
fifo->af_tail = nexttail;
|
|
|
|
|
|
|
|
/* Increment the number of bytes that were sent */
|
|
|
|
|
|
|
|
nsent += msglen;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* We get here after all messages have been added to the FIFO. Check if
|
|
|
|
* we need to kick of the XMIT sequence.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (empty)
|
|
|
|
{
|
|
|
|
dac_xmit(dev);
|
|
|
|
}
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
/* Return the number of bytes that were sent */
|
|
|
|
|
|
|
|
ret = nsent;
|
|
|
|
|
|
|
|
return_with_irqdisabled:
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-08-25 16:11:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
/****************************************************************************
|
2011-08-25 16:11:39 +02:00
|
|
|
* Name: dac_ioctl
|
2020-03-10 13:25:22 +01:00
|
|
|
****************************************************************************/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
static int dac_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2014-02-11 01:08:49 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct dac_dev_s *dev = inode->i_private;
|
|
|
|
int ret;
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-10 13:25:22 +01:00
|
|
|
/****************************************************************************
|
2011-08-25 16:11:39 +02:00
|
|
|
* Name: dac_txdone
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-10 13:25:22 +01:00
|
|
|
* Called from the DAC interrupt handler at the completion of a send
|
|
|
|
* operation.
|
2011-08-25 16:11:39 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-08-25 16:11:39 +02:00
|
|
|
* OK on success; a negated errno on failure.
|
|
|
|
*
|
2020-03-10 13:25:22 +01:00
|
|
|
****************************************************************************/
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
int dac_txdone(FAR struct dac_dev_s *dev)
|
|
|
|
{
|
|
|
|
int ret = -ENOENT;
|
2016-08-15 16:00:36 +02:00
|
|
|
int sval;
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
/* Verify that the xmit FIFO is not empty */
|
|
|
|
|
|
|
|
if (dev->ad_xmit.af_head != dev->ad_xmit.af_tail)
|
|
|
|
{
|
|
|
|
/* Remove the message at the head of the xmit FIFO */
|
|
|
|
|
|
|
|
if (++dev->ad_xmit.af_head >= CONFIG_DAC_FIFOSIZE)
|
|
|
|
{
|
|
|
|
dev->ad_xmit.af_head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the next message in the FIFO */
|
|
|
|
|
|
|
|
ret = dac_xmit(dev);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Inform any waiting threads that new xmit space is available */
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
ret = nxsem_get_value(&dev->ad_xmit.af_sem, &sval);
|
2016-08-15 16:00:36 +02:00
|
|
|
if (ret == OK && sval <= 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
ret = nxsem_post(&dev->ad_xmit.af_sem);
|
2016-08-15 16:00:36 +02:00
|
|
|
}
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2011-08-25 16:11:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-06-12 16:33:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dac_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a dac driver.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* path - The full path to the DAC device to be registered. This could
|
|
|
|
* be, as an example, "/dev/dac0"
|
|
|
|
* dev - An instance of the device-specific DAC interface
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success; A negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-08-25 16:11:39 +02:00
|
|
|
int dac_register(FAR const char *path, FAR struct dac_dev_s *dev)
|
|
|
|
{
|
|
|
|
/* Initialize the DAC device structure */
|
|
|
|
|
|
|
|
dev->ad_ocount = 0;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize semaphores & mutex */
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&dev->ad_xmit.af_sem, 0, 0);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->ad_closelock);
|
2011-08-25 16:11:39 +02:00
|
|
|
|
|
|
|
dev->ad_ops->ao_reset(dev);
|
|
|
|
|
2020-06-12 16:41:38 +02:00
|
|
|
return register_driver(path, &dac_fops, 0222, dev);
|
2011-08-25 16:11:39 +02:00
|
|
|
}
|