2007-03-16 23:07:14 +01:00
|
|
|
/************************************************************************************
|
2009-10-18 15:52:21 +02:00
|
|
|
* drivers/serial/serial.c
|
2007-03-16 23:07:14 +01:00
|
|
|
*
|
2016-02-14 14:32:58 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011-2013, 2016 Gregory Nutt. All rights reserved.
|
2012-02-11 04:50:52 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-03-16 23:07:14 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-01 00:52:53 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-16 23:07:14 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Included Files
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2008-10-09 21:20:02 +02:00
|
|
|
#include <fcntl.h>
|
2008-11-18 00:22:27 +01:00
|
|
|
#include <poll.h>
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/arch.h>
|
2016-11-03 15:16:59 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2012-03-21 20:47:23 +01:00
|
|
|
#include <nuttx/serial/serial.h>
|
2013-08-11 03:14:05 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2007-03-16 23:07:14 +01:00
|
|
|
|
|
|
|
/************************************************************************************
|
2015-04-08 15:15:32 +02:00
|
|
|
* Pre-processor Definitions
|
2007-03-16 23:07:14 +01:00
|
|
|
************************************************************************************/
|
|
|
|
|
2008-09-18 16:48:26 +02:00
|
|
|
/* The architecture must provide up_putc for this driver */
|
|
|
|
|
|
|
|
#ifndef CONFIG_ARCH_LOWPUTC
|
|
|
|
# error "Architecture must provide up_putc() for this driver"
|
2007-03-16 23:07:14 +01:00
|
|
|
#endif
|
|
|
|
|
2008-09-18 16:48:26 +02:00
|
|
|
#define uart_putc(ch) up_putc(ch)
|
|
|
|
|
2009-12-13 16:13:37 +01:00
|
|
|
#define HALF_SECOND_MSEC 500
|
|
|
|
#define HALF_SECOND_USEC 500000L
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
/************************************************************************************
|
|
|
|
* Private Types
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
************************************************************************************/
|
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
static int uart_open(FAR struct file *filep);
|
|
|
|
static int uart_close(FAR struct file *filep);
|
|
|
|
static ssize_t uart_read(FAR struct file *filep, FAR char *buffer, size_t buflen);
|
|
|
|
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen);
|
|
|
|
static int uart_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
2008-11-18 00:22:27 +01:00
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
2009-12-15 15:25:14 +01:00
|
|
|
static int uart_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup);
|
2008-11-18 00:22:27 +01:00
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
|
|
|
|
/************************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2007-03-16 23:07:14 +01:00
|
|
|
************************************************************************************/
|
|
|
|
|
2009-06-10 17:22:49 +02:00
|
|
|
static const struct file_operations g_serialops =
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
|
|
|
uart_open, /* open */
|
|
|
|
uart_close, /* close */
|
|
|
|
uart_read, /* read */
|
|
|
|
uart_write, /* write */
|
2007-05-09 02:08:54 +02:00
|
|
|
0, /* seek */
|
2008-11-18 00:22:27 +01:00
|
|
|
uart_ioctl /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, uart_poll /* poll */
|
|
|
|
#endif
|
2016-05-26 18:01:15 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_takesem
|
|
|
|
************************************************************************************/
|
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
static int uart_takesem(FAR sem_t *sem, bool errout)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2012-08-12 19:37:04 +02:00
|
|
|
/* Loop, ignoring interrupts, until we have successfully acquired the semaphore */
|
|
|
|
|
|
|
|
while (sem_wait(sem) != OK)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2012-08-12 19:37:04 +02:00
|
|
|
/* The only case that an error should occur here is if the wait was awakened
|
|
|
|
* by a signal.
|
2007-03-16 23:07:14 +01:00
|
|
|
*/
|
|
|
|
|
2012-07-15 16:56:25 +02:00
|
|
|
ASSERT(get_errno() == EINTR);
|
2012-08-12 19:37:04 +02:00
|
|
|
|
|
|
|
/* When the signal is received, should we errout? Or should we just continue
|
|
|
|
* waiting until we have the semaphore?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (errout)
|
|
|
|
{
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
|
|
|
return OK;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_givesem
|
|
|
|
************************************************************************************/
|
|
|
|
|
2008-09-24 23:05:53 +02:00
|
|
|
#define uart_givesem(sem) (void)sem_post(sem)
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
static void uart_pollnotify(FAR uart_dev_t *dev, pollevent_t eventset)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-02-11 04:50:52 +01:00
|
|
|
for (i = 0; i < CONFIG_SERIAL_NPOLLWAITERS; i++)
|
2008-11-18 00:22:27 +01:00
|
|
|
{
|
|
|
|
struct pollfd *fds = dev->fds[i];
|
|
|
|
if (fds)
|
|
|
|
{
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
2015-10-10 18:41:00 +02:00
|
|
|
fds->revents |= ((fds->events | (POLLERR | POLLHUP)) & eventset);
|
2013-01-31 17:52:20 +01:00
|
|
|
#else
|
2008-11-18 00:22:27 +01:00
|
|
|
fds->revents |= (fds->events & eventset);
|
2013-01-31 17:52:20 +01:00
|
|
|
#endif
|
2008-11-18 00:22:27 +01:00
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Report events: %02x\n", fds->revents);
|
2008-11-18 00:22:27 +01:00
|
|
|
sem_post(fds->sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define uart_pollnotify(dev,event)
|
|
|
|
#endif
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_putxmitchar
|
|
|
|
************************************************************************************/
|
|
|
|
|
2013-03-18 17:04:29 +01:00
|
|
|
static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2011-07-13 15:30:38 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
int nexthead;
|
2012-08-12 19:37:04 +02:00
|
|
|
int ret;
|
2011-07-13 15:30:38 +02:00
|
|
|
|
|
|
|
/* Increment to see what the next head pointer will be. We need to use the "next"
|
|
|
|
* head pointer to determine when the circular buffer would overrun
|
|
|
|
*/
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2011-07-13 15:30:38 +02:00
|
|
|
nexthead = dev->xmit.head + 1;
|
2007-03-16 23:07:14 +01:00
|
|
|
if (nexthead >= dev->xmit.size)
|
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
|
|
|
|
2016-07-22 15:47:59 +02:00
|
|
|
/* Loop until we are able to add the character to the TX buffer. */
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
for (; ; )
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2016-07-22 15:47:59 +02:00
|
|
|
/* Check if the TX buffer is full */
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
if (nexthead != dev->xmit.tail)
|
|
|
|
{
|
2016-07-22 15:47:59 +02:00
|
|
|
/* No.. not full. Add the character to the TX buffer and return. */
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
dev->xmit.buffer[dev->xmit.head] = ch;
|
|
|
|
dev->xmit.head = nexthead;
|
2012-08-12 19:37:04 +02:00
|
|
|
return OK;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2013-03-18 17:04:29 +01:00
|
|
|
|
2016-07-22 15:47:59 +02:00
|
|
|
/* The TX buffer is full. Should be block, waiting for the hardware
|
|
|
|
* to remove some data from the TX buffer?
|
2013-03-18 17:04:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
else if (oktoblock)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2016-07-22 15:47:59 +02:00
|
|
|
/* The following steps must be atomic with respect to serial
|
|
|
|
* interrupt handling.
|
2011-07-13 15:30:38 +02:00
|
|
|
*/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2016-07-22 15:47:59 +02:00
|
|
|
/* Check again... In certain race conditions an interrupt may
|
|
|
|
* have occurred between the test at the top of the loop and
|
|
|
|
* entering the critical section and the TX buffer may no longer
|
|
|
|
* be full.
|
|
|
|
*
|
|
|
|
* NOTE: On certain devices, such as USB CDC/ACM, the entire TX
|
|
|
|
* buffer may have been emptied in this race condition. In that
|
|
|
|
* case, the logic would hang below waiting for space in the TX
|
|
|
|
* buffer without this test.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nexthead != dev->xmit.tail)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2013-02-02 01:29:55 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* Check if the removable device is no longer connected while we
|
|
|
|
* have interrupts off. We do not want the transition to occur
|
|
|
|
* as a race condition before we begin the wait.
|
|
|
|
*/
|
|
|
|
|
2016-07-22 15:47:59 +02:00
|
|
|
else if (dev->disconnected)
|
2013-02-02 01:29:55 +01:00
|
|
|
{
|
2013-02-02 02:50:34 +01:00
|
|
|
ret = -ENOTCONN;
|
2013-02-02 01:29:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
2016-07-22 15:47:59 +02:00
|
|
|
else
|
2013-02-02 02:50:34 +01:00
|
|
|
{
|
2016-07-22 15:47:59 +02:00
|
|
|
/* Inform the interrupt level logic that we are waiting. */
|
|
|
|
|
|
|
|
dev->xmitwaiting = true;
|
|
|
|
|
2013-02-02 02:50:34 +01:00
|
|
|
/* Wait for some characters to be sent from the buffer with
|
2016-07-22 15:47:59 +02:00
|
|
|
* the TX interrupt enabled. When the TX interrupt is enabled,
|
|
|
|
* uart_xmitchars() should execute and remove some of the data
|
|
|
|
* from the TX buffer.
|
|
|
|
*
|
|
|
|
* NOTE that interrupts will be re-enabled while we wait for
|
|
|
|
* the semaphore.
|
2013-02-02 02:50:34 +01:00
|
|
|
*/
|
|
|
|
|
2015-11-12 21:16:19 +01:00
|
|
|
#ifdef CONFIG_SERIAL_DMA
|
|
|
|
uart_dmatxavail(dev);
|
|
|
|
#endif
|
2013-02-02 02:50:34 +01:00
|
|
|
uart_enabletxint(dev);
|
|
|
|
ret = uart_takesem(&dev->xmitsem, true);
|
|
|
|
uart_disabletxint(dev);
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
2013-02-02 01:29:55 +01:00
|
|
|
/* Check if the removable device was disconnected while we were
|
|
|
|
* waiting.
|
|
|
|
*/
|
2013-01-31 17:52:20 +01:00
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-12 19:37:04 +02:00
|
|
|
/* Check if we were awakened by signal. */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
/* A signal received while waiting for the xmit buffer to become
|
|
|
|
* non-full will abort the transfer.
|
|
|
|
*/
|
2012-08-12 19:37:04 +02:00
|
|
|
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2013-03-18 17:04:29 +01:00
|
|
|
|
|
|
|
/* The caller has request that we not block for data. So return the
|
|
|
|
* EAGAIN error to signal this situation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2012-12-09 18:34:53 +01:00
|
|
|
/* We won't get here. Some compilers may complain that this code is
|
|
|
|
* unreachable.
|
|
|
|
*/
|
2012-08-12 19:37:04 +02:00
|
|
|
|
|
|
|
return OK;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_irqwrite
|
|
|
|
************************************************************************************/
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
|
|
|
ssize_t ret = buflen;
|
|
|
|
|
|
|
|
/* Force each character through the low level interface */
|
|
|
|
|
|
|
|
for (; buflen; buflen--)
|
|
|
|
{
|
|
|
|
int ch = *buffer++;
|
|
|
|
|
2012-08-14 00:27:06 +02:00
|
|
|
/* If this is the console, then we should replace LF with CR-LF */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
uart_putc('\r');
|
|
|
|
}
|
2012-08-14 00:27:06 +02:00
|
|
|
|
|
|
|
/* Output the character, using the low-level direct UART interfaces */
|
|
|
|
|
|
|
|
uart_putc(ch);
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_write
|
|
|
|
************************************************************************************/
|
|
|
|
|
2013-08-11 03:14:05 +02:00
|
|
|
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
ssize_t nwritten = buflen;
|
2013-03-18 17:04:29 +01:00
|
|
|
bool oktoblock;
|
2012-08-12 19:37:04 +02:00
|
|
|
int ret;
|
2013-08-11 03:14:05 +02:00
|
|
|
char ch;
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2012-08-14 00:27:06 +02:00
|
|
|
/* We may receive console writes through this path from interrupt handlers and
|
|
|
|
* from debug output in the IDLE task! In these cases, we will need to do things
|
|
|
|
* a little differently.
|
2007-03-16 23:07:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (up_interrupt_context() || getpid() == 0)
|
|
|
|
{
|
2013-02-01 23:51:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* If the removable device is no longer connected, refuse to write to
|
|
|
|
* the device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-14 00:27:06 +02:00
|
|
|
/* up_putc() will be used to generate the output in a busy-wait loop.
|
|
|
|
* up_putc() is only available for the console device.
|
|
|
|
*/
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
if (dev->isconsole)
|
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2007-03-16 23:07:14 +01:00
|
|
|
ret = uart_irqwrite(dev, buffer, buflen);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2007-03-16 23:07:14 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-14 00:27:06 +02:00
|
|
|
return -EPERM;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
/* Only one user can access dev->xmit.head at a time */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
ret = (ssize_t)uart_takesem(&dev->xmit.sem, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* A signal received while waiting for access to the xmit.head will
|
|
|
|
* abort the transfer. After the transfer has started, we are committed
|
|
|
|
* and signals will be ignored.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2013-02-01 23:51:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* If the removable device is no longer connected, refuse to write to the
|
2013-02-02 01:29:55 +01:00
|
|
|
* device. This check occurs after taking the xmit.sem because the
|
|
|
|
* disconnection event might have occurred while we were waiting for
|
|
|
|
* access to the transmit buffers.
|
2013-02-01 23:51:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
|
|
|
uart_givesem(&dev->xmit.sem);
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-18 17:04:29 +01:00
|
|
|
/* Can the following loop block, waiting for space in the TX
|
|
|
|
* buffer?
|
|
|
|
*/
|
|
|
|
|
|
|
|
oktoblock = ((filep->f_oflags & O_NONBLOCK) == 0);
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
/* Loop while we still have data to copy to the transmit buffer.
|
|
|
|
* we add data to the head of the buffer; uart_xmitchars takes the
|
|
|
|
* data from the end of the buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uart_disabletxint(dev);
|
|
|
|
for (; buflen; buflen--)
|
|
|
|
{
|
2013-08-11 03:14:05 +02:00
|
|
|
ch = *buffer++;
|
|
|
|
ret = OK;
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2013-08-11 03:14:05 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
/* Do output post-processing */
|
|
|
|
|
2015-01-02 20:59:47 +01:00
|
|
|
if ((dev->tc_oflag & OPOST) != 0)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
|
|
|
/* Mapping CR to NL? */
|
|
|
|
|
2015-01-02 20:59:47 +01:00
|
|
|
if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
|
|
|
ch = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we interested in newline processing? */
|
|
|
|
|
2015-01-02 20:59:47 +01:00
|
|
|
if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
|
|
|
ret = uart_putxmitchar(dev, '\r', oktoblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-09-04 18:28:38 +02:00
|
|
|
nwritten = ret;
|
2013-08-11 03:14:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Specifically not handled:
|
|
|
|
*
|
|
|
|
* OXTABS - primarily a full-screen terminal optimisation
|
|
|
|
* ONOEOT - Unix interoperability hack
|
2014-08-10 18:47:38 +02:00
|
|
|
* OLCUC - Not specified by POSIX
|
|
|
|
* ONOCR - low-speed interactive optimisation
|
2013-08-11 03:14:05 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !CONFIG_SERIAL_TERMIOS */
|
|
|
|
/* If this is the console, convert \n -> \r\n */
|
2012-08-12 19:37:04 +02:00
|
|
|
|
|
|
|
if (dev->isconsole && ch == '\n')
|
|
|
|
{
|
2013-03-18 17:04:29 +01:00
|
|
|
ret = uart_putxmitchar(dev, '\r', oktoblock);
|
2012-08-12 19:37:04 +02:00
|
|
|
}
|
2013-08-11 03:14:05 +02:00
|
|
|
#endif
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
/* Put the character into the transmit buffer */
|
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2013-03-18 17:04:29 +01:00
|
|
|
ret = uart_putxmitchar(dev, ch, oktoblock);
|
2012-08-12 19:37:04 +02:00
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2013-02-02 01:29:55 +01:00
|
|
|
/* uart_putxmitchar() might return an error under one of two
|
|
|
|
* conditions: (1) The wait for buffer space might have been
|
2013-03-18 17:04:29 +01:00
|
|
|
* interrupted by a signal (ret should be -EINTR), (2) if
|
2013-02-02 01:29:55 +01:00
|
|
|
* CONFIG_SERIAL_REMOVABLE is defined, then uart_putxmitchar()
|
|
|
|
* might also return if the serial device was disconnected
|
2013-03-18 17:04:29 +01:00
|
|
|
* (with -ENOTCONN), or (3) if O_NONBLOCK is specified, then
|
|
|
|
* then uart_putxmitchar() might return -EAGAIN if the output
|
|
|
|
* TX buffer is full.
|
2012-08-12 19:37:04 +02:00
|
|
|
*/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
if (ret < 0)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2012-08-12 19:37:04 +02:00
|
|
|
/* POSIX requires that we return -1 and errno set if no data was
|
|
|
|
* transferred. Otherwise, we return the number of bytes in the
|
|
|
|
* interrupted transfer.
|
|
|
|
*/
|
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
if (buflen < (size_t)nwritten)
|
2012-08-12 19:37:04 +02:00
|
|
|
{
|
2013-02-02 02:50:34 +01:00
|
|
|
/* Some data was transferred. Return the number of bytes that
|
|
|
|
* were successfully transferred.
|
2012-08-12 19:37:04 +02:00
|
|
|
*/
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
nwritten -= buflen;
|
2012-08-12 19:37:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-02 02:50:34 +01:00
|
|
|
/* No data was transferred. Return the negated errno value.
|
|
|
|
* The VFS layer will set the errno value appropriately).
|
2012-08-12 19:37:04 +02:00
|
|
|
*/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
nwritten = ret;
|
2012-08-12 19:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->xmit.head != dev->xmit.tail)
|
|
|
|
{
|
2015-11-12 21:16:19 +01:00
|
|
|
#ifdef CONFIG_SERIAL_DMA
|
|
|
|
uart_dmatxavail(dev);
|
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
uart_enabletxint(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
uart_givesem(&dev->xmit.sem);
|
2013-08-11 19:13:11 +02:00
|
|
|
return nwritten;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_read
|
|
|
|
************************************************************************************/
|
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
static ssize_t uart_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2008-02-08 18:25:29 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2014-12-27 15:15:41 +01:00
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
FAR struct uart_buffer_s *rxbuf = &dev->recv;
|
2014-12-27 14:43:06 +01:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
2014-12-27 15:15:41 +01:00
|
|
|
unsigned int nbuffered;
|
|
|
|
unsigned int watermark;
|
2014-12-27 14:43:06 +01:00
|
|
|
#endif
|
2014-12-27 15:15:41 +01:00
|
|
|
irqstate_t flags;
|
|
|
|
ssize_t recvd = 0;
|
|
|
|
int16_t tail;
|
|
|
|
char ch;
|
|
|
|
int ret;
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
/* Only one user can access rxbuf->tail at a time */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
ret = uart_takesem(&rxbuf->sem, true);
|
2012-08-12 19:37:04 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* A signal received while waiting for access to the recv.tail will avort
|
|
|
|
* the transfer. After the transfer has started, we are committed and
|
|
|
|
* signals will be ignored.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
|
|
|
/* Loop while we still have data to copy to the receive buffer.
|
|
|
|
* we add data to the head of the buffer; uart_xmitchars takes the
|
|
|
|
* data from the end of the buffer.
|
|
|
|
*/
|
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
while ((size_t)recvd < buflen)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2013-02-02 01:29:55 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* If the removable device is no longer connected, refuse to read any
|
|
|
|
* further from the device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
|
|
|
if (recvd == 0)
|
|
|
|
{
|
|
|
|
recvd = -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-18 19:08:27 +02:00
|
|
|
/* Check if there is more data to return in the circular buffer.
|
2014-12-27 14:43:06 +01:00
|
|
|
* NOTE: Rx interrupt handling logic may asynchronously increment
|
2012-04-18 19:08:27 +02:00
|
|
|
* the head index but must not modify the tail index. The tail
|
|
|
|
* index is only modified in this function. Therefore, no
|
|
|
|
* special handshaking is required here.
|
2012-04-18 21:17:30 +02:00
|
|
|
*
|
|
|
|
* The head and tail pointers are 16-bit values. The only time that
|
|
|
|
* the following could be unsafe is if the CPU made two non-atomic
|
|
|
|
* 8-bit accesses to obtain the 16-bit head index.
|
2012-04-18 19:08:27 +02:00
|
|
|
*/
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
tail = rxbuf->tail;
|
|
|
|
if (rxbuf->head != tail)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2012-04-18 19:08:27 +02:00
|
|
|
/* Take the next character from the tail of the buffer */
|
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
ch = rxbuf->buffer[tail];
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2012-04-18 19:08:27 +02:00
|
|
|
/* Increment the tail index. Most operations are done using the
|
2014-12-27 15:15:41 +01:00
|
|
|
* local variable 'tail' so that the final rxbuf->tail update
|
2012-04-18 19:08:27 +02:00
|
|
|
* is atomic.
|
|
|
|
*/
|
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
if (++tail >= rxbuf->size)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2012-04-18 19:08:27 +02:00
|
|
|
tail = 0;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
rxbuf->tail = tail;
|
2013-08-11 03:14:05 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
/* Do input processing if any is enabled */
|
|
|
|
|
|
|
|
if (dev->tc_iflag & (INLCR | IGNCR | ICRNL))
|
|
|
|
{
|
|
|
|
/* \n -> \r or \r -> \n translation? */
|
|
|
|
|
|
|
|
if ((ch == '\n') && (dev->tc_iflag & INLCR))
|
|
|
|
{
|
|
|
|
ch = '\r';
|
|
|
|
}
|
|
|
|
else if ((ch == '\r') && (dev->tc_iflag & ICRNL))
|
|
|
|
{
|
|
|
|
ch = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Discarding \r ? */
|
|
|
|
|
|
|
|
if ((ch == '\r') & (dev->tc_iflag & IGNCR))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Specifically not handled:
|
|
|
|
*
|
|
|
|
* All of the local modes; echo, line editing, etc.
|
|
|
|
* Anything to do with break or parity errors.
|
|
|
|
* ISTRIP - we should be 8-bit clean.
|
|
|
|
* IUCLC - Not Posix
|
|
|
|
* IXON/OXOFF - no xon/xoff flow control.
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Store the received character */
|
|
|
|
|
|
|
|
*buffer++ = ch;
|
|
|
|
recvd++;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2008-11-18 15:54:43 +01:00
|
|
|
#ifdef CONFIG_DEV_SERIAL_FULLBLOCKS
|
2008-10-09 21:20:02 +02:00
|
|
|
/* No... then we would have to wait to get receive more data.
|
2008-11-18 15:54:43 +01:00
|
|
|
* If the user has specified the O_NONBLOCK option, then just
|
2008-10-09 21:20:02 +02:00
|
|
|
* return what we have.
|
|
|
|
*/
|
|
|
|
|
2013-02-10 01:46:27 +01:00
|
|
|
else if ((filep->f_oflags & O_NONBLOCK) != 0)
|
2008-10-09 21:20:02 +02:00
|
|
|
{
|
|
|
|
/* If nothing was transferred, then return the -EAGAIN
|
|
|
|
* error (not zero which means end of file).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (recvd < 1)
|
|
|
|
{
|
|
|
|
recvd = -EAGAIN;
|
|
|
|
}
|
2013-02-02 01:29:55 +01:00
|
|
|
|
2008-11-18 15:54:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* No... the circular buffer is empty. Have we returned anything
|
|
|
|
* to the caller?
|
|
|
|
*/
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2008-11-18 15:54:43 +01:00
|
|
|
else if (recvd > 0)
|
|
|
|
{
|
|
|
|
/* Yes.. break out of the loop and return the number of bytes
|
2008-10-09 21:20:02 +02:00
|
|
|
* received up to the wait condition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
2008-11-18 15:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No... then we would have to wait to get receive some data.
|
|
|
|
* If the user has specified the O_NONBLOCK option, then do not
|
|
|
|
* wait.
|
|
|
|
*/
|
|
|
|
|
2013-02-10 01:46:27 +01:00
|
|
|
else if ((filep->f_oflags & O_NONBLOCK) != 0)
|
2008-11-18 15:54:43 +01:00
|
|
|
{
|
|
|
|
/* Break out of the loop returning -EAGAIN */
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2008-11-18 15:54:43 +01:00
|
|
|
recvd = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Otherwise we are going to have to wait for data to arrive */
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
else
|
|
|
|
{
|
2012-04-18 19:08:27 +02:00
|
|
|
/* Disable Rx interrupts and test again... */
|
|
|
|
|
|
|
|
uart_disablerxint(dev);
|
|
|
|
|
|
|
|
/* If the Rx ring buffer still empty? Bytes may have been addded
|
|
|
|
* between the last time that we checked and when we disabled Rx
|
|
|
|
* interrupts.
|
2007-03-16 23:07:14 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
if (rxbuf->head == rxbuf->tail)
|
2012-04-18 19:08:27 +02:00
|
|
|
{
|
|
|
|
/* Yes.. the buffer is still empty. Wait for some characters
|
|
|
|
* to be received into the buffer with the RX interrupt re-
|
|
|
|
* enabled. All interrupts are disabled briefly to assure
|
|
|
|
* that the following operations are atomic.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-11-12 21:16:19 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_DMA
|
|
|
|
/* If RX buffer is empty move tail and head to zero position */
|
|
|
|
|
|
|
|
if (rxbuf->head == rxbuf->tail)
|
|
|
|
{
|
|
|
|
rxbuf->head = rxbuf->tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify DMA that there is free space in the RX buffer */
|
|
|
|
|
|
|
|
uart_dmarxfree(dev);
|
|
|
|
#endif
|
|
|
|
|
2013-02-02 02:50:34 +01:00
|
|
|
uart_enablerxint(dev);
|
2012-04-18 19:08:27 +02:00
|
|
|
|
2013-02-02 01:29:55 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
2013-02-02 02:50:34 +01:00
|
|
|
/* Check again if the removable device is still connected
|
|
|
|
* while we have interrupts off. We do not want the transition
|
|
|
|
* to occur as a race condition before we begin the wait.
|
2013-02-02 01:29:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
2013-02-02 02:50:34 +01:00
|
|
|
{
|
2013-02-02 01:29:55 +01:00
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
2013-02-02 02:50:34 +01:00
|
|
|
else
|
2013-02-02 01:29:55 +01:00
|
|
|
#endif
|
2013-02-02 02:50:34 +01:00
|
|
|
{
|
|
|
|
/* Now wait with the Rx interrupt re-enabled. NuttX will
|
|
|
|
* automatically re-enable global interrupts when this
|
|
|
|
* thread goes to sleep.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev->recvwaiting = true;
|
|
|
|
ret = uart_takesem(&dev->recvsem, true);
|
|
|
|
}
|
2012-04-18 19:08:27 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Was a signal received while waiting for data to be
|
2013-02-02 01:29:55 +01:00
|
|
|
* received? Was a removable device disconnected while
|
|
|
|
* we were waiting?
|
2013-01-31 17:52:20 +01:00
|
|
|
*/
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
if (ret < 0 || dev->disconnected)
|
|
|
|
#else
|
2012-08-12 19:37:04 +02:00
|
|
|
if (ret < 0)
|
2013-01-31 17:52:20 +01:00
|
|
|
#endif
|
2012-08-12 19:37:04 +02:00
|
|
|
{
|
|
|
|
/* POSIX requires that we return after a signal is received.
|
|
|
|
* If some bytes were read, we need to return the number of bytes
|
|
|
|
* read; if no bytes were read, we need to return -1 with the
|
|
|
|
* errno set correctly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (recvd == 0)
|
|
|
|
{
|
|
|
|
/* No bytes were read, return -EINTR (the VFS layer will
|
|
|
|
* set the errno value appropriately.
|
|
|
|
*/
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
recvd = dev->disconnected ? -ENOTCONN : -EINTR;
|
|
|
|
#else
|
2012-08-12 19:37:04 +02:00
|
|
|
recvd = -EINTR;
|
2013-01-31 17:52:20 +01:00
|
|
|
#endif
|
2012-08-12 19:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-04-18 19:08:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No... the ring buffer is no longer empty. Just re-enable Rx
|
|
|
|
* interrupts and accept the new data on the next time through
|
|
|
|
* the loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uart_enablerxint(dev);
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 21:16:19 +01:00
|
|
|
#ifdef CONFIG_SERIAL_DMA
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-11-12 21:16:19 +01:00
|
|
|
|
|
|
|
/* If RX buffer is empty move tail and head to zero position */
|
|
|
|
|
|
|
|
if (rxbuf->head == rxbuf->tail)
|
|
|
|
{
|
|
|
|
rxbuf->head = rxbuf->tail = 0;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-11-12 21:16:19 +01:00
|
|
|
|
|
|
|
/* Notify DMA that there is free space in the RX buffer */
|
|
|
|
|
|
|
|
uart_dmarxfree(dev);
|
|
|
|
|
|
|
|
/* RX interrupt could be disabled by RX buffer overflow. Enable it now. */
|
|
|
|
|
|
|
|
uart_enablerxint(dev);
|
|
|
|
#endif
|
|
|
|
|
2014-05-08 17:01:14 +02:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
2014-12-27 14:43:06 +01:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
|
|
|
/* How many bytes are now buffered */
|
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
rxbuf = &dev->recv;
|
|
|
|
if (rxbuf->head >= rxbuf->tail)
|
2014-12-27 14:43:06 +01:00
|
|
|
{
|
2014-12-27 15:15:41 +01:00
|
|
|
nbuffered = rxbuf->head - rxbuf->tail;
|
2014-12-27 14:43:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-27 15:15:41 +01:00
|
|
|
nbuffered = rxbuf->size - rxbuf->tail + rxbuf->head;
|
2014-12-27 14:43:06 +01:00
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Is the level now below the watermark level that we need to report? */
|
2014-12-27 14:43:06 +01:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
watermark = (CONFIG_SERIAL_IFLOWCONTROL_LOWER_WATERMARK * rxbuf->size) / 100;
|
2014-12-27 14:43:06 +01:00
|
|
|
if (nbuffered <= watermark)
|
|
|
|
{
|
|
|
|
/* Let the lower level driver know that the watermark level has been
|
2014-12-27 16:45:45 +01:00
|
|
|
* crossed. It will probably deactivate RX flow control.
|
2014-12-27 14:43:06 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
(void)uart_rxflowcontrol(dev, nbuffered, false);
|
2014-12-27 14:43:06 +01:00
|
|
|
}
|
|
|
|
#else
|
2014-12-27 16:45:45 +01:00
|
|
|
/* If the RX buffer empty */
|
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
if (rxbuf->head == rxbuf->tail)
|
2014-05-08 17:01:14 +02:00
|
|
|
{
|
2014-12-27 16:45:45 +01:00
|
|
|
/* Deactivate RX flow control. */
|
2014-05-08 17:01:14 +02:00
|
|
|
|
2014-12-27 16:45:45 +01:00
|
|
|
(void)uart_rxflowcontrol(dev, 0, false);
|
2014-05-08 17:01:14 +02:00
|
|
|
}
|
2014-12-27 14:43:06 +01:00
|
|
|
#endif
|
2014-05-08 17:01:14 +02:00
|
|
|
#endif
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
uart_givesem(&dev->recv.sem);
|
2008-10-09 21:20:02 +02:00
|
|
|
return recvd;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_ioctl
|
|
|
|
************************************************************************************/
|
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
static int uart_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2008-02-08 18:25:29 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2013-08-11 03:14:05 +02:00
|
|
|
/* Handle TTY-level IOCTLs here */
|
|
|
|
/* Let low-level driver handle the call first */
|
|
|
|
|
|
|
|
int ret = dev->ops->ioctl(filep, cmd, arg);
|
|
|
|
|
|
|
|
/* The device ioctl() handler returns -ENOTTY when it doesn't know
|
|
|
|
* how to handle the command. Check if we can handle it here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == -ENOTTY)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2016-07-25 22:06:32 +02:00
|
|
|
/* Get the number of bytes that may be read from the RX buffer
|
|
|
|
* (without waiting)
|
|
|
|
*/
|
|
|
|
|
2013-08-11 03:14:05 +02:00
|
|
|
case FIONREAD:
|
|
|
|
{
|
|
|
|
int count;
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2016-07-25 22:06:32 +02:00
|
|
|
/* Determine the number of bytes available in the RX buffer */
|
2013-08-11 03:14:05 +02:00
|
|
|
|
|
|
|
if (dev->recv.tail <= dev->recv.head)
|
|
|
|
{
|
|
|
|
count = dev->recv.head - dev->recv.tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = dev->recv.size - (dev->recv.tail - dev->recv.head);
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2016-01-06 17:05:39 +01:00
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
2013-08-11 03:14:05 +02:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-07-25 22:06:32 +02:00
|
|
|
/* Get the number of bytes that have been written to the TX buffer. */
|
|
|
|
|
2013-08-11 03:14:05 +02:00
|
|
|
case FIONWRITE:
|
|
|
|
{
|
|
|
|
int count;
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2016-07-25 22:06:32 +02:00
|
|
|
/* Determine the number of bytes waiting in the TX buffer */
|
|
|
|
|
2016-07-26 14:47:53 +02:00
|
|
|
if (dev->xmit.tail <= dev->xmit.head)
|
2016-07-25 22:06:32 +02:00
|
|
|
{
|
|
|
|
count = dev->xmit.head - dev->xmit.tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = dev->xmit.size - (dev->xmit.tail - dev->xmit.head);
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Get the number of free bytes in the TX buffer */
|
|
|
|
|
|
|
|
case FIONSPACE:
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* Determine the number of bytes free in the TX buffer */
|
2013-08-11 03:14:05 +02:00
|
|
|
|
|
|
|
if (dev->xmit.head < dev->xmit.tail)
|
|
|
|
{
|
|
|
|
count = dev->xmit.tail - dev->xmit.head - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-13 17:40:09 +01:00
|
|
|
count = dev->xmit.size - (dev->xmit.head - dev->xmit.tail) - 1;
|
2013-08-11 03:14:05 +02:00
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2016-01-06 17:05:39 +01:00
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
2013-08-11 03:14:05 +02:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
/* Append any higher level TTY flags */
|
|
|
|
|
|
|
|
else if (ret == OK)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case TCGETS:
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)arg;
|
2013-08-11 03:14:05 +02:00
|
|
|
|
|
|
|
if (!termiosp)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And update with flags from this layer */
|
|
|
|
|
|
|
|
termiosp->c_iflag = dev->tc_iflag;
|
|
|
|
termiosp->c_oflag = dev->tc_oflag;
|
|
|
|
termiosp->c_lflag = dev->tc_lflag;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCSETS:
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)arg;
|
2013-08-11 03:14:05 +02:00
|
|
|
|
|
|
|
if (!termiosp)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the flags we keep at this layer */
|
|
|
|
|
|
|
|
dev->tc_iflag = termiosp->c_iflag;
|
|
|
|
dev->tc_oflag = termiosp->c_oflag;
|
|
|
|
dev->tc_lflag = termiosp->c_lflag;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
2009-12-15 15:25:14 +01:00
|
|
|
int uart_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
|
2008-11-18 00:22:27 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
pollevent_t eventset;
|
|
|
|
int ndx;
|
2012-08-12 19:37:04 +02:00
|
|
|
int ret;
|
2008-11-18 00:22:27 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
2008-11-19 19:43:50 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-11-19 19:43:50 +01:00
|
|
|
if (!dev || !fds)
|
2008-11-18 00:22:27 +01:00
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
ret = uart_takesem(&dev->pollsem, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* A signal received while waiting for access to the poll data
|
|
|
|
* will abort the operation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (setup)
|
2008-11-18 00:22:27 +01:00
|
|
|
{
|
2008-11-19 19:43:50 +01:00
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
2008-11-18 00:22:27 +01:00
|
|
|
*/
|
|
|
|
|
2012-02-11 04:50:52 +01:00
|
|
|
for (i = 0; i < CONFIG_SERIAL_NPOLLWAITERS; i++)
|
2008-11-18 00:22:27 +01:00
|
|
|
{
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Find an available slot */
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (!dev->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
dev->fds[i] = fds;
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = &dev->fds[i];
|
2008-11-19 19:43:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 03:39:21 +01:00
|
|
|
|
2012-02-11 04:50:52 +01:00
|
|
|
if (i >= CONFIG_SERIAL_NPOLLWAITERS)
|
2008-11-19 19:43:50 +01:00
|
|
|
{
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = NULL;
|
2008-11-19 19:43:50 +01:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
2008-11-19 03:39:21 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
/* Should we immediately notify on any of the requested events?
|
2008-11-19 19:43:50 +01:00
|
|
|
* First, check if the xmit buffer is full.
|
2012-08-12 19:37:04 +02:00
|
|
|
*
|
|
|
|
* Get exclusive access to the xmit buffer indices. NOTE: that we do not
|
|
|
|
* let this wait be interrupted by a signal (we probably should, but that
|
|
|
|
* would be a little awkward).
|
2008-11-19 19:43:50 +01:00
|
|
|
*/
|
2008-11-18 00:22:27 +01:00
|
|
|
|
|
|
|
eventset = 0;
|
2012-08-12 19:37:04 +02:00
|
|
|
(void)uart_takesem(&dev->xmit.sem, false);
|
2008-11-18 00:22:27 +01:00
|
|
|
|
|
|
|
ndx = dev->xmit.head + 1;
|
|
|
|
if (ndx >= dev->xmit.size)
|
|
|
|
{
|
|
|
|
ndx = 0;
|
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
if (ndx != dev->xmit.tail)
|
|
|
|
{
|
2013-01-31 17:52:20 +01:00
|
|
|
eventset |= (fds->events & POLLOUT);
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
uart_givesem(&dev->xmit.sem);
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Check if the receive buffer is empty.
|
2012-08-12 19:37:04 +02:00
|
|
|
*
|
|
|
|
* Get exclusive access to the recv buffer indices. NOTE: that we do not
|
|
|
|
* let this wait be interrupted by a signal (we probably should, but that
|
|
|
|
* would be a little awkward).
|
|
|
|
*/
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
(void)uart_takesem(&dev->recv.sem, false);
|
2008-11-18 00:22:27 +01:00
|
|
|
if (dev->recv.head != dev->recv.tail)
|
|
|
|
{
|
2013-01-31 17:52:20 +01:00
|
|
|
eventset |= (fds->events & POLLIN);
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
uart_givesem(&dev->recv.sem);
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* Check if a removable device has been disconnected. */
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
eventset |= (POLLERR | POLLHUP);
|
2013-01-31 17:52:20 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
uart_pollnotify(dev, eventset);
|
|
|
|
}
|
2008-11-19 19:43:50 +01:00
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
2009-06-17 01:23:31 +02:00
|
|
|
else if (fds->priv)
|
2008-11-19 19:43:50 +01:00
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
struct pollfd **slot = (struct pollfd **)fds->priv;
|
2008-11-19 19:43:50 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-11-19 19:43:50 +01:00
|
|
|
if (!slot)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = NULL;
|
2008-11-19 19:43:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2008-11-18 00:22:27 +01:00
|
|
|
uart_givesem(&dev->pollsem);
|
2008-11-19 19:43:50 +01:00
|
|
|
return ret;
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the serial port gets closed.
|
|
|
|
* It waits for the last remaining data to be sent.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
static int uart_close(FAR struct file *filep)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2008-02-08 18:25:29 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
irqstate_t flags;
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
/* Get exclusive access to the close semaphore (to synchronize open/close operations.
|
|
|
|
* NOTE: that we do not let this wait be interrupted by a signal. Technically, we
|
|
|
|
* should, but almost no one every checks the return value from close() so we avoid
|
|
|
|
* a potential memory leak by ignoring signals in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)uart_takesem(&dev->closesem, false);
|
2007-03-16 23:07:14 +01:00
|
|
|
if (dev->open_count > 1)
|
|
|
|
{
|
|
|
|
dev->open_count--;
|
|
|
|
uart_givesem(&dev->closesem);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There are no more references to the port */
|
|
|
|
|
|
|
|
dev->open_count = 0;
|
|
|
|
|
|
|
|
/* Stop accepting input */
|
|
|
|
|
|
|
|
uart_disablerxint(dev);
|
|
|
|
|
|
|
|
/* Now we wait for the transmit buffer to clear */
|
|
|
|
|
|
|
|
while (dev->xmit.head != dev->xmit.tail)
|
|
|
|
{
|
2008-01-30 01:59:17 +01:00
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
2009-12-13 16:13:37 +01:00
|
|
|
usleep(HALF_SECOND_USEC);
|
2008-01-30 01:59:17 +01:00
|
|
|
#else
|
2009-12-13 16:13:37 +01:00
|
|
|
up_mdelay(HALF_SECOND_MSEC);
|
2008-01-30 01:59:17 +01:00
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And wait for the TX fifo to drain */
|
|
|
|
|
2008-01-27 00:46:09 +01:00
|
|
|
while (!uart_txempty(dev))
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2008-01-30 01:59:17 +01:00
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
2009-12-13 16:13:37 +01:00
|
|
|
usleep(HALF_SECOND_USEC);
|
2008-01-30 01:59:17 +01:00
|
|
|
#else
|
2009-12-13 16:13:37 +01:00
|
|
|
up_mdelay(HALF_SECOND_MSEC);
|
2008-01-30 01:59:17 +01:00
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the IRQ and disable the UART */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section(); /* Disable interrupts */
|
2008-01-27 00:46:09 +01:00
|
|
|
uart_detach(dev); /* Detach interrupts */
|
|
|
|
if (!dev->isconsole) /* Check for the serial console UART */
|
|
|
|
{
|
|
|
|
uart_shutdown(dev); /* Disable the UART */
|
|
|
|
}
|
2013-02-02 01:29:55 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2014-12-13 15:07:21 +01:00
|
|
|
/* We need to re-initialize the semaphores if this is the last close
|
|
|
|
* of the device, as the close might be caused by pthread_cancel() of
|
2014-12-13 15:44:28 +01:00
|
|
|
* a thread currently blocking on any of them.
|
2014-12-13 15:07:21 +01:00
|
|
|
*/
|
|
|
|
|
2016-11-03 15:16:59 +01:00
|
|
|
sem_reset(&dev->xmitsem, 0);
|
|
|
|
sem_reset(&dev->recvsem, 0);
|
|
|
|
sem_reset(&dev->xmit.sem, 1);
|
|
|
|
sem_reset(&dev->recv.sem, 1);
|
2014-12-13 15:07:21 +01:00
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
2016-11-03 15:16:59 +01:00
|
|
|
sem_reset(&dev->pollsem, 1);
|
2014-12-13 15:07:21 +01:00
|
|
|
#endif
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
uart_givesem(&dev->closesem);
|
|
|
|
return OK;
|
2015-10-04 23:04:00 +02:00
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called whenever a serial port is opened.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
static int uart_open(FAR struct file *filep)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2014-11-28 00:42:16 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
uint8_t tmp;
|
|
|
|
int ret;
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
/* If the port is the middle of closing, wait until the close is finished.
|
|
|
|
* If a signal is received while we are waiting, then return EINTR.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = uart_takesem(&dev->closesem, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* A signal received while waiting for the last close operation. */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2013-02-02 01:29:55 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* If the removable device is no longer connected, refuse to open the
|
|
|
|
* device. We check this after obtaining the close semaphore because
|
|
|
|
* we might have been waiting when the device was disconnected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
/* Start up serial port */
|
2008-02-08 18:25:29 +01:00
|
|
|
/* Increment the count of references to the device. */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
tmp = dev->open_count + 1;
|
|
|
|
if (tmp == 0)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
/* More than 255 opens; uint8_t overflows to zero */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
ret = -EMFILE;
|
2008-10-07 22:44:00 +02:00
|
|
|
goto errout_with_sem;
|
2008-02-08 18:25:29 +01:00
|
|
|
}
|
2008-10-07 22:44:00 +02:00
|
|
|
|
|
|
|
/* Check if this is the first time that the driver has been opened. */
|
|
|
|
|
|
|
|
if (tmp == 1)
|
2008-02-08 18:25:29 +01:00
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
/* If this is the console, then the UART has already been initialized. */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
if (!dev->isconsole)
|
|
|
|
{
|
|
|
|
/* Perform one time hardware initialization */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
ret = uart_setup(dev);
|
|
|
|
if (ret < 0)
|
2008-02-08 18:25:29 +01:00
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-09 21:20:02 +02:00
|
|
|
goto errout_with_sem;
|
2008-02-08 18:25:29 +01:00
|
|
|
}
|
2008-10-07 22:44:00 +02:00
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
/* In any event, we do have to configure for interrupt driven mode of
|
|
|
|
* operation. Attach the hardware IRQ(s). Hmm.. should shutdown() the
|
|
|
|
* the device in the rare case that uart_attach() fails, tmp==1, and
|
|
|
|
* this is not the console.
|
|
|
|
*/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
ret = uart_attach(dev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
uart_shutdown(dev);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-09 21:20:02 +02:00
|
|
|
goto errout_with_sem;
|
2008-10-07 22:44:00 +02:00
|
|
|
}
|
2008-02-08 18:25:29 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
/* Mark the io buffers empty */
|
2008-02-08 18:25:29 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
dev->xmit.head = 0;
|
|
|
|
dev->xmit.tail = 0;
|
|
|
|
dev->recv.head = 0;
|
|
|
|
dev->recv.tail = 0;
|
2008-02-08 18:25:29 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
/* Initialize termios state */
|
2013-08-11 03:14:05 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
dev->tc_iflag = 0;
|
2015-01-02 20:59:47 +01:00
|
|
|
if (dev->isconsole)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
|
|
|
/* Enable \n -> \r\n translation for the console */
|
|
|
|
|
|
|
|
dev->tc_oflag = OPOST | ONLCR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->tc_oflag = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-12 21:16:19 +01:00
|
|
|
#ifdef CONFIG_SERIAL_DMA
|
|
|
|
/* Notify DMA that there is free space in the RX buffer */
|
|
|
|
|
|
|
|
uart_dmarxfree(dev);
|
|
|
|
#endif
|
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
/* Enable the RX interrupt */
|
2008-02-08 18:25:29 +01:00
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
uart_enablerxint(dev);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2008-10-07 22:44:00 +02:00
|
|
|
|
2008-10-09 21:20:02 +02:00
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
dev->open_count = tmp;
|
|
|
|
|
2008-10-07 22:44:00 +02:00
|
|
|
errout_with_sem:
|
2007-03-16 23:07:14 +01:00
|
|
|
uart_givesem(&dev->closesem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register serial console and serial ports.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
int uart_register(FAR const char *path, FAR uart_dev_t *dev)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2016-11-03 15:16:59 +01:00
|
|
|
/* Initialize semaphores */
|
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
sem_init(&dev->xmit.sem, 0, 1);
|
|
|
|
sem_init(&dev->recv.sem, 0, 1);
|
|
|
|
sem_init(&dev->closesem, 0, 1);
|
|
|
|
sem_init(&dev->xmitsem, 0, 0);
|
|
|
|
sem_init(&dev->recvsem, 0, 0);
|
2008-11-18 00:22:27 +01:00
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
sem_init(&dev->pollsem, 0, 1);
|
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2016-11-03 15:16:59 +01:00
|
|
|
/* The recvsem and xmitsem are used for signaling and, hence, should not have
|
2016-11-03 18:00:47 +01:00
|
|
|
* priority inheritance enabled.
|
2016-11-03 15:16:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
sem_setprotocol(&dev->xmitsem, SEM_PRIO_NONE);
|
|
|
|
sem_setprotocol(&dev->recvsem, SEM_PRIO_NONE);
|
|
|
|
|
|
|
|
/* Register the serial driver */
|
|
|
|
|
2017-01-15 20:00:50 +01:00
|
|
|
sinfo("Registering %s\n", path);
|
2007-05-09 02:08:54 +02:00
|
|
|
return register_driver(path, &g_serialops, 0666, dev);
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2008-11-18 00:22:27 +01:00
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_datareceived
|
|
|
|
*
|
|
|
|
* Description:
|
2013-08-11 03:14:05 +02:00
|
|
|
* This function is called from uart_recvchars when new serial data is place in
|
2008-11-18 00:22:27 +01:00
|
|
|
* the driver's circular buffer. This function will wake-up any stalled read()
|
|
|
|
* operations that are waiting for incoming data.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
void uart_datareceived(FAR uart_dev_t *dev)
|
|
|
|
{
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Is there a thread waiting for read data? */
|
2008-11-18 00:22:27 +01:00
|
|
|
|
|
|
|
if (dev->recvwaiting)
|
|
|
|
{
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Yes... wake it up */
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
dev->recvwaiting = false;
|
2008-11-18 00:22:27 +01:00
|
|
|
(void)sem_post(&dev->recvsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify all poll/select waiters that they can read from the recv buffer */
|
|
|
|
|
|
|
|
uart_pollnotify(dev, POLLIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_datasent
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called from uart_xmitchars after serial data has been sent,
|
|
|
|
* freeing up some space in the driver's circular buffer. This function will
|
|
|
|
* wake-up any stalled write() operations that was waiting for space to buffer
|
|
|
|
* outgoing data.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
void uart_datasent(FAR uart_dev_t *dev)
|
|
|
|
{
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Is there a thread waiting for space in xmit.buffer? */
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
if (dev->xmitwaiting)
|
|
|
|
{
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Yes... wake it up */
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
dev->xmitwaiting = false;
|
2008-11-18 00:22:27 +01:00
|
|
|
(void)sem_post(&dev->xmitsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify all poll/select waiters that they can write to xmit buffer */
|
|
|
|
|
|
|
|
uart_pollnotify(dev, POLLOUT);
|
|
|
|
}
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/************************************************************************************
|
|
|
|
* Name: uart_connected
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Serial devices (like USB serial) can be removed. In that case, the "upper
|
|
|
|
* half" serial driver must be informed that there is no longer a valid serial
|
|
|
|
* channel associated with the driver.
|
|
|
|
*
|
|
|
|
* In this case, the driver will terminate all pending transfers wint ENOTCONN and
|
|
|
|
* will refuse all further transactions while the "lower half" is disconnected.
|
|
|
|
* The driver will continue to be registered, but will be in an unusable state.
|
|
|
|
*
|
|
|
|
* Conversely, the "upper half" serial driver needs to know when the serial
|
|
|
|
* device is reconnected so that it can resume normal operations.
|
|
|
|
*
|
|
|
|
* Assumptions/Limitations:
|
|
|
|
* This function may be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
void uart_connected(FAR uart_dev_t *dev, bool connected)
|
|
|
|
{
|
2013-02-01 00:29:34 +01:00
|
|
|
irqstate_t flags;
|
|
|
|
|
2013-02-10 01:46:27 +01:00
|
|
|
/* Is the device disconnected? Interrupts are disabled because this
|
|
|
|
* function may be called from interrupt handling logic.
|
|
|
|
*/
|
2013-01-31 17:52:20 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2013-01-31 17:52:20 +01:00
|
|
|
dev->disconnected = !connected;
|
|
|
|
if (!connected)
|
|
|
|
{
|
|
|
|
/* Yes.. wake up all waiting threads. Each thread should detect the
|
|
|
|
* disconnection and return the ENOTCONN error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Is there a thread waiting for space in xmit.buffer? */
|
|
|
|
|
|
|
|
if (dev->xmitwaiting)
|
|
|
|
{
|
|
|
|
/* Yes... wake it up */
|
|
|
|
|
|
|
|
dev->xmitwaiting = false;
|
|
|
|
(void)sem_post(&dev->xmitsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is there a thread waiting for read data? */
|
|
|
|
|
|
|
|
if (dev->recvwaiting)
|
|
|
|
{
|
|
|
|
/* Yes... wake it up */
|
|
|
|
|
|
|
|
dev->recvwaiting = false;
|
|
|
|
(void)sem_post(&dev->recvsem);
|
|
|
|
}
|
|
|
|
|
2014-10-29 17:48:50 +01:00
|
|
|
/* Notify all poll/select waiters that a hangup occurred */
|
2013-01-31 17:52:20 +01:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
uart_pollnotify(dev, (POLLERR | POLLHUP));
|
2013-01-31 17:52:20 +01:00
|
|
|
}
|
2013-02-01 00:29:34 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2013-01-31 17:52:20 +01:00
|
|
|
}
|
|
|
|
#endif
|