* drivers/analog/dac.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 <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/signal.h>
#include <nuttx/fs/fs.h>
#include <nuttx/analog/dac.h>
#include <nuttx/irq.h>
* 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);
static ssize_t dac_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int dac_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
* Private Data
****************************************************************************/
static const struct file_operations g_dac_fops =
{
dac_open,
dac_close,
NULL,
dac_write,
NULL,
dac_ioctl,
};
* Private Functions
****************************************************************************/
* Name: dac_open
*
* Description:
* This function is called whenever the DAC device is opened.
*
****************************************************************************/
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;
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_xmit.af_head = 0;
dev->ad_xmit.af_tail = 0;
dev->ad_ocount = tmp;
}
}
}
nxmutex_unlock(&dev->ad_lock);
}
return ret;
}
* Name: dac_close
*
* Description:
* This routine is called when the DAC device is closed.
* It waits for the last remaining data to be sent.
*
****************************************************************************/
static int dac_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct dac_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;
while (dev->ad_xmit.af_head != dev->ad_xmit.af_tail)
{
nxsig_usleep(HALF_SECOND_USEC);
}
dev->ad_ops->ao_shutdown(dev);
nxmutex_unlock(&dev->ad_lock);
}
}
return ret;
}
* Name: dac_xmit
*
* Description:
* Send the message at the head of the ad_xmit FIFO
*
* Assumptions:
* Called with interrupts disabled
*
****************************************************************************/
static int dac_xmit(FAR struct dac_dev_s *dev)
{
bool enable = false;
int ret = OK;
if (dev->ad_xmit.af_head != dev->ad_xmit.af_tail)
{
ret = dev->ad_ops->ao_send(dev,
&dev->ad_xmit.af_buffer[dev->ad_xmit.af_head]);
enable = (ret == OK ? true : false);
}
dev->ad_ops->ao_txint(dev, enable);
return ret;
}
* Name: dac_write
****************************************************************************/
static ssize_t dac_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
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;
bool empty;
ssize_t nsent = 0;
irqstate_t flags;
int nexttail;
int msglen;
int ret = 0;
flags = spin_lock_irqsave(&dev->ad_spinlock);
* have to kick off a new TX sequence.
*/
empty = (fifo->af_head == fifo->af_tail);
* shorter than the minimum.
*/
if (buflen % 5 == 0)
{
msglen = 5;
}
else if (buflen % 4 == 0)
{
msglen = 4;
}
else if (buflen % 3 == 0)
{
msglen = 3;
}
else if (buflen % 2 == 0)
{
msglen = 2;
}
else if (buflen == 1)
{
msglen = 1;
}
else
{
msglen = 5;
}
while ((buflen - nsent) >= msglen)
{
* to enqueue xmit data.
*/
nexttail = fifo->af_tail + 1;
if (nexttail >= CONFIG_DAC_FIFOSIZE)
{
nexttail = 0;
}
* available.
*/
while (nexttail == fifo->af_head)
{
if (filep->f_oflags & O_NONBLOCK)
{
if (nsent == 0)
{
ret = -EAGAIN;
}
else
{
ret = nsent;
}
spin_unlock_irqrestore(&dev->ad_spinlock, flags);
return ret;
}
* start the XMIT sequence to clear the FIFO.
*/
if (empty)
{
dac_xmit(dev);
}
spin_unlock_irqrestore(&dev->ad_spinlock, flags);
ret = nxsem_wait_uninterruptible(&fifo->af_sem);
if (ret < 0)
{
return ret;
}
flags = spin_lock_irqsave(&dev->ad_spinlock);
empty = (fifo->af_head == fifo->af_tail);
}
* DAC message at the tail of the FIFO.
*/
if (msglen == 5)
{
msg = (FAR struct dac_msg_s *)&buffer[nsent];
memcpy(&fifo->af_buffer[fifo->af_tail], msg, msglen);
}
else if (msglen == 4)
{
fifo->af_buffer[fifo->af_tail].am_channel = buffer[nsent];
fifo->af_buffer[fifo->af_tail].am_data =
*(FAR uint32_t *)&buffer[nsent];
fifo->af_buffer[fifo->af_tail].am_data &= 0xffffff00;
}
else if (msglen == 3)
{
fifo->af_buffer[fifo->af_tail].am_channel = buffer[nsent];
fifo->af_buffer[fifo->af_tail].am_data =
(*(FAR uint16_t *)&buffer[nsent + 1]);
fifo->af_buffer[fifo->af_tail].am_data <<= 16;
}
else if (msglen == 2)
{
fifo->af_buffer[fifo->af_tail].am_channel = 0;
fifo->af_buffer[fifo->af_tail].am_data =
(*(FAR uint16_t *)&buffer[nsent]);
fifo->af_buffer[fifo->af_tail].am_data <<= 16;
}
else if (msglen == 1)
{
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;
}
fifo->af_tail = nexttail;
nsent += msglen;
}
* we need to kick of the XMIT sequence.
*/
if (empty)
{
dac_xmit(dev);
}
ret = nsent;
spin_unlock_irqrestore(&dev->ad_spinlock, flags);
return ret;
}
* Name: dac_ioctl
****************************************************************************/
static int dac_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct dac_dev_s *dev = inode->i_private;
int ret;
ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
return ret;
}
* Public Functions
****************************************************************************/
* Name: dac_txdone
*
* Description:
* Called from the DAC interrupt handler at the completion of a send
* operation.
*
* Returned Value:
* OK on success; a negated errno on failure.
*
****************************************************************************/
int dac_txdone(FAR struct dac_dev_s *dev)
{
int ret = -ENOENT;
int sval;
if (dev->ad_xmit.af_head != dev->ad_xmit.af_tail)
{
if (++dev->ad_xmit.af_head >= CONFIG_DAC_FIFOSIZE)
{
dev->ad_xmit.af_head = 0;
}
ret = dac_xmit(dev);
if (ret == OK)
{
ret = nxsem_get_value(&dev->ad_xmit.af_sem, &sval);
if (ret == OK && sval <= 0)
{
ret = nxsem_post(&dev->ad_xmit.af_sem);
}
}
}
return ret;
}
* 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.
*
****************************************************************************/
int dac_register(FAR const char *path, FAR struct dac_dev_s *dev)
{
dev->ad_ocount = 0;
nxsem_init(&dev->ad_xmit.af_sem, 0, 0);
nxmutex_init(&dev->ad_lock);
spin_lock_init(&dev->ad_spinlock);
dev->ad_ops->ao_reset(dev);
return register_driver(path, &g_dac_fops, 0222, dev);
}