2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 15:52:21 +02:00
|
|
|
* drivers/serial/serial.c
|
2007-03-16 23:07:14 +01:00
|
|
|
*
|
2020-03-14 17:30:07 +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
|
2007-03-16 23:07:14 +01:00
|
|
|
*
|
2020-03-14 17:30:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-16 23:07:14 +01:00
|
|
|
*
|
2020-03-14 17:30:07 +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.
|
2007-03-16 23:07:14 +01:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Included Files
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2023-03-01 16:25:35 +01:00
|
|
|
#include <ctype.h>
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <sys/types.h>
|
2023-03-24 06:44:34 +01:00
|
|
|
#include <sys/param.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2023-03-24 06:44:34 +01:00
|
|
|
#include <time.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>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2023-03-02 02:51:21 +01:00
|
|
|
#include <spawn.h>
|
2008-10-09 21:20:02 +02:00
|
|
|
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <nuttx/irq.h>
|
2023-03-01 16:25:35 +01:00
|
|
|
#include <nuttx/ascii.h>
|
2007-03-16 23:07:14 +01:00
|
|
|
#include <nuttx/arch.h>
|
2018-05-27 19:39:34 +02:00
|
|
|
#include <nuttx/clock.h>
|
2017-05-11 01:36:08 +02:00
|
|
|
#include <nuttx/sched.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2021-12-17 15:02:43 +01:00
|
|
|
#include <nuttx/cancelpt.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>
|
2019-01-27 17:50:15 +01:00
|
|
|
#include <nuttx/power/pm.h>
|
2021-12-15 06:40:45 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
#include <nuttx/kthread.h>
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2015-04-08 15:15:32 +02:00
|
|
|
* Pre-processor Definitions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2017-10-18 14:49:11 +02:00
|
|
|
/* Check watermark levels */
|
|
|
|
|
|
|
|
#if defined(CONFIG_SERIAL_IFLOWCONTROL) && \
|
|
|
|
defined(CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS)
|
|
|
|
# if CONFIG_SERIAL_IFLOWCONTROL_LOWER_WATERMARK < 1
|
|
|
|
# warning CONFIG_SERIAL_IFLOWCONTROL_LOWER_WATERMARK too small
|
|
|
|
# endif
|
|
|
|
# if CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK > 99
|
|
|
|
# warning CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK too large
|
|
|
|
# endif
|
|
|
|
# if CONFIG_SERIAL_IFLOWCONTROL_LOWER_WATERMARK >= CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK
|
|
|
|
# warning CONFIG_SERIAL_IFLOWCONTROL_LOWER_WATERMARK too large
|
|
|
|
# warning Must be less than CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Timing */
|
|
|
|
|
2017-11-02 15:53:14 +01:00
|
|
|
#define POLL_DELAY_USEC 1000
|
2009-12-13 16:13:37 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Private Types
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Private Function Prototypes
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Write support */
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
static int uart_putxmitchar(FAR uart_dev_t *dev, int ch,
|
|
|
|
bool oktoblock);
|
|
|
|
static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev,
|
|
|
|
FAR const char *buffer,
|
2017-08-01 18:13:59 +02:00
|
|
|
size_t buflen);
|
2021-12-17 15:02:43 +01:00
|
|
|
static int uart_tcdrain(FAR uart_dev_t *dev,
|
|
|
|
bool cancelable, clock_t timeout);
|
2017-08-01 18:13:59 +02:00
|
|
|
|
2023-02-09 01:51:11 +01:00
|
|
|
static int uart_tcsendbreak(FAR uart_dev_t *dev,
|
|
|
|
FAR struct file *filep,
|
|
|
|
unsigned int ms);
|
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Character driver methods */
|
|
|
|
|
2008-02-08 18:25:29 +01:00
|
|
|
static int uart_open(FAR struct file *filep);
|
|
|
|
static int uart_close(FAR struct file *filep);
|
2021-03-04 08:02:21 +01:00
|
|
|
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,
|
2020-03-14 17:30:07 +01:00
|
|
|
size_t buflen);
|
2021-03-04 08:02:21 +01:00
|
|
|
static int uart_ioctl(FAR struct file *filep,
|
|
|
|
int cmd, unsigned long arg);
|
|
|
|
static int uart_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds, bool setup);
|
2023-12-11 14:11:41 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int uart_unlink(FAR struct inode *inode);
|
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-12-15 06:40:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_TTY_LAUNCH_ENTRY
|
|
|
|
/* Lanch program entry, this must be supplied by the application. */
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
int CONFIG_TTY_LAUNCH_ENTRYPOINT(int argc, FAR char *argv[]);
|
2021-12-15 06:40:45 +01:00
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
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
|
|
|
{
|
2023-12-11 14:11:41 +01:00
|
|
|
uart_open, /* open */
|
|
|
|
uart_close, /* close */
|
|
|
|
uart_read, /* read */
|
|
|
|
uart_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
uart_ioctl, /* ioctl */
|
|
|
|
NULL, /* mmap */
|
|
|
|
NULL, /* truncate */
|
|
|
|
uart_poll /* poll */
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, uart_unlink /* unlink */
|
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
};
|
|
|
|
|
2021-12-15 06:40:45 +01:00
|
|
|
#ifdef CONFIG_TTY_LAUNCH
|
|
|
|
static struct work_s g_serial_work;
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Private Functions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Name: uart_putxmitchar
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
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
|
|
|
|
2021-03-04 08:02:21 +01: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
|
2011-07-13 15:30:38 +02:00
|
|
|
*/
|
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;
|
2024-01-31 02:02:08 +01:00
|
|
|
break;
|
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
|
|
|
{
|
|
|
|
/* 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
|
|
|
*/
|
|
|
|
|
2019-04-24 20:11:40 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TXDMA
|
2015-11-12 21:16:19 +01:00
|
|
|
uart_dmatxavail(dev);
|
|
|
|
#endif
|
2013-02-02 02:50:34 +01:00
|
|
|
uart_enabletxint(dev);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxsem_wait(&dev->xmitsem);
|
2013-02-02 02:50:34 +01:00
|
|
|
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)
|
|
|
|
{
|
2021-06-06 13:08:26 +02:00
|
|
|
return -ENOTCONN;
|
2013-01-31 17:52:20 +01:00
|
|
|
}
|
|
|
|
#endif
|
2020-03-14 17:30:07 +01:00
|
|
|
|
2012-08-12 19:37:04 +02:00
|
|
|
/* Check if we were awakened by signal. */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* A signal received while waiting for the xmit buffer to
|
|
|
|
* become non-full will abort the transfer.
|
2015-10-10 18:41:00 +02:00
|
|
|
*/
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2021-06-06 13:08:26 +02:00
|
|
|
return -EINTR;
|
2012-08-12 19:37:04 +02:00
|
|
|
}
|
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
|
|
|
|
{
|
2021-06-06 13:08:26 +02:00
|
|
|
return -EAGAIN;
|
2013-03-18 17:04:29 +01:00
|
|
|
}
|
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
|
|
|
|
2021-06-06 13:08:26 +02:00
|
|
|
return OK;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2018-08-26 17:47:25 +02:00
|
|
|
* Name: uart_putc
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2018-08-26 17:47:25 +02:00
|
|
|
|
|
|
|
static inline void uart_putc(FAR uart_dev_t *dev, int ch)
|
|
|
|
{
|
|
|
|
while (!uart_txready(dev))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
uart_send(dev, ch);
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Name: uart_irqwrite
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev,
|
|
|
|
FAR const char *buffer,
|
2013-08-11 19:13:11 +02:00
|
|
|
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++;
|
|
|
|
|
2019-01-27 17:40:54 +01:00
|
|
|
/* Do output post-processing */
|
|
|
|
|
|
|
|
if ((dev->tc_oflag & OPOST) != 0)
|
|
|
|
{
|
|
|
|
/* Mapping CR to NL? */
|
|
|
|
|
|
|
|
if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
|
|
|
|
{
|
|
|
|
ch = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we interested in newline processing? */
|
|
|
|
|
|
|
|
if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
|
|
|
|
{
|
|
|
|
uart_putc(dev, '\r');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 00:27:06 +02:00
|
|
|
/* Output the character, using the low-level direct UART interfaces */
|
|
|
|
|
2018-08-26 17:47:25 +02:00
|
|
|
uart_putc(dev, ch);
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2017-08-01 18:13:59 +02:00
|
|
|
* Name: uart_tcdrain
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* Block further TX input.
|
|
|
|
* Wait until all data has been transferred from the TX buffer and
|
|
|
|
* until the hardware TX FIFOs are empty.
|
2017-08-01 18:13:59 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-12-17 15:02:43 +01:00
|
|
|
static int uart_tcdrain(FAR uart_dev_t *dev,
|
|
|
|
bool cancelable, clock_t timeout)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
int ret;
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-12-17 15:02:43 +01:00
|
|
|
/* tcdrain is a cancellation point */
|
|
|
|
|
|
|
|
if (cancelable && enter_cancellation_point())
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* If there is a pending cancellation, then do not perform
|
|
|
|
* the wait. Exit now with ECANCELED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
leave_cancellation_point();
|
|
|
|
return -ECANCELED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get exclusive access to the to dev->tmit. We cannot permit new data to
|
|
|
|
* be written while we are trying to flush the old data.
|
2017-08-01 18:13:59 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
* A signal received while waiting for access to the xmit.head will abort
|
|
|
|
* the operation with EINTR.
|
2007-03-16 23:07:14 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->xmit.lock);
|
2017-08-01 18:13:59 +02:00
|
|
|
if (ret >= 0)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
irqstate_t flags;
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t start;
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
/* Trigger emission to flush the contents of the tx buffer */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
2013-02-01 23:51:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
2017-08-01 18:13:59 +02:00
|
|
|
/* 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.
|
2013-02-01 23:51:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
2018-08-26 18:03:30 +02:00
|
|
|
dev->xmit.tail = dev->xmit.head; /* Drop the buffered TX data */
|
2017-08-01 18:13:59 +02:00
|
|
|
ret = -ENOTCONN;
|
2013-02-01 23:51:34 +01:00
|
|
|
}
|
2017-08-01 18:13:59 +02:00
|
|
|
else
|
2013-02-01 23:51:34 +01:00
|
|
|
#endif
|
2017-08-01 18:13:59 +02:00
|
|
|
{
|
2018-05-27 19:39:34 +02:00
|
|
|
/* Continue waiting while the TX buffer is not empty.
|
|
|
|
*
|
|
|
|
* NOTE: There is no timeout on the following loop. In
|
|
|
|
* situations were this loop could hang (with hardware flow
|
|
|
|
* control, as an example), the caller should call
|
|
|
|
* tcflush() first to discard this buffered Tx data.
|
|
|
|
*/
|
2013-02-01 23:51:34 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
ret = OK;
|
|
|
|
while (ret >= 0 && dev->xmit.head != dev->xmit.tail)
|
|
|
|
{
|
|
|
|
/* Wait for some characters to be sent from the buffer with
|
|
|
|
* the TX interrupt enabled. When the TX interrupt is
|
|
|
|
* enabled, uart_xmitchars() should execute and remove some
|
2018-05-27 19:39:34 +02:00
|
|
|
* of the data from the TX buffer. We may have to wait several
|
2017-08-01 18:13:59 +02:00
|
|
|
* times for the TX buffer to be entirely emptied.
|
|
|
|
*
|
|
|
|
* NOTE that interrupts will be re-enabled while we wait for
|
|
|
|
* the semaphore.
|
|
|
|
*/
|
|
|
|
|
2019-04-24 20:11:40 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TXDMA
|
2017-08-01 18:13:59 +02:00
|
|
|
uart_dmatxavail(dev);
|
|
|
|
#endif
|
|
|
|
uart_enabletxint(dev);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxsem_wait(&dev->xmitsem);
|
2017-08-01 18:13:59 +02:00
|
|
|
uart_disabletxint(dev);
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
/* The TX buffer is empty (or an error occurred). But there still may
|
|
|
|
* be data in the UART TX FIFO. We get no asynchronous indication of
|
|
|
|
* this event, so we have to do a busy wait poll.
|
|
|
|
*/
|
|
|
|
|
2018-05-27 23:13:54 +02:00
|
|
|
/* Set up for the timeout
|
|
|
|
*
|
|
|
|
* REVISIT: This is a kludge. The correct fix would be add an
|
|
|
|
* interface to the lower half driver so that the tcflush() operation
|
|
|
|
* all also cause the lower half driver to clear and reset the Tx FIFO.
|
|
|
|
*/
|
2018-05-27 19:39:34 +02:00
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
start = clock_systime_ticks();
|
2018-05-27 19:39:34 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if (ret >= 0)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
while (!uart_txempty(dev))
|
|
|
|
{
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t elapsed;
|
2018-05-27 19:39:34 +02:00
|
|
|
|
2017-11-02 15:53:14 +01:00
|
|
|
nxsig_usleep(POLL_DELAY_USEC);
|
2018-05-27 19:39:34 +02:00
|
|
|
|
|
|
|
/* Check for a timeout */
|
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
elapsed = clock_systime_ticks() - start;
|
2018-05-27 19:39:34 +02:00
|
|
|
if (elapsed >= timeout)
|
|
|
|
{
|
2022-11-03 10:54:58 +01:00
|
|
|
nxmutex_unlock(&dev->xmit.lock);
|
2018-05-27 19:39:34 +02:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
2017-08-01 18:13:59 +02:00
|
|
|
}
|
2020-03-14 17:30:07 +01:00
|
|
|
}
|
2017-08-01 18:13:59 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->xmit.lock);
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:02:43 +01:00
|
|
|
if (cancelable)
|
|
|
|
{
|
|
|
|
leave_cancellation_point();
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2023-02-09 01:51:11 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_tcsendbreak
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Request a serial line Break by calling the lower half driver's
|
|
|
|
* BSD-compatible Break IOCTLs TIOCSBRK and TIOCCBRK, with a sleep of the
|
|
|
|
* specified duration between them.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Serial device.
|
|
|
|
* filep - Required for issuing lower half driver IOCTL call.
|
|
|
|
* ms - If non-zero, duration of the Break in milliseconds; if
|
|
|
|
* zero, duration is 400 milliseconds.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success or a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uart_tcsendbreak(FAR uart_dev_t *dev, FAR struct file *filep,
|
|
|
|
unsigned int ms)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* REVISIT: Do we need to perform the equivalent of tcdrain() before
|
|
|
|
* beginning the Break to avoid corrupting the transmit data? If so, note
|
|
|
|
* that just calling uart_tcdrain() here would create a race condition,
|
|
|
|
* since new transmit data could be written after uart_tcdrain() returns
|
|
|
|
* but before we re-acquire the dev->xmit.lock here. Therefore, we would
|
|
|
|
* need to refactor the functional portion of uart_tcdrain() to a separate
|
|
|
|
* function and call it from both uart_tcdrain() and uart_tcsendbreak()
|
|
|
|
* in critical section and with xmit lock already held.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->ops->ioctl)
|
|
|
|
{
|
|
|
|
ret = nxmutex_lock(&dev->xmit.lock);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Request lower half driver to start the Break */
|
|
|
|
|
|
|
|
ret = dev->ops->ioctl(filep, TIOCSBRK, 0);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Wait 400 ms or the requested Break duration */
|
|
|
|
|
|
|
|
nxsig_usleep((ms == 0) ? 400000 : ms * 1000);
|
|
|
|
|
|
|
|
/* Request lower half driver to end the Break */
|
|
|
|
|
|
|
|
ret = dev->ops->ioctl(filep, TIOCCBRK, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nxmutex_unlock(&dev->xmit.lock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* With no lower half IOCTL, we cannot request Break at all. */
|
|
|
|
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2017-08-01 18:13:59 +02:00
|
|
|
* Name: uart_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called whenever a serial port is opened.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
static int uart_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
uint8_t tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->closelock);
|
2012-08-12 19:37:04 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
/* A signal received while waiting for the last close operation. */
|
2012-08-12 19:37:04 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2013-02-01 23:51:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
2017-08-01 18:13:59 +02:00
|
|
|
/* 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.
|
2013-02-01 23:51:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
ret = -ENOTCONN;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-02-01 23:51:34 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Start up serial port */
|
2020-03-14 17:30:07 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Increment the count of references to the device. */
|
2013-03-18 17:04:29 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
tmp = dev->open_count + 1;
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t overflows to zero */
|
2013-03-18 17:04:29 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
ret = -EMFILE;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-08-01 18:13:59 +02:00
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Check if this is the first time that the driver has been opened. */
|
|
|
|
|
|
|
|
if (tmp == 1)
|
2007-03-16 23:07:14 +01:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* If this is the console, then the UART has already been
|
|
|
|
* initialized.
|
|
|
|
*/
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if (!dev->isconsole)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Perform one time hardware initialization */
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
ret = uart_setup(dev);
|
|
|
|
if (ret < 0)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
leave_critical_section(flags);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2013-08-11 03:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:13:59 +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.
|
|
|
|
*/
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
ret = uart_attach(dev);
|
|
|
|
if (ret < 0)
|
2012-08-12 19:37:04 +02:00
|
|
|
{
|
2022-12-16 20:25:27 +01:00
|
|
|
if (!dev->isconsole)
|
|
|
|
{
|
|
|
|
uart_shutdown(dev);
|
|
|
|
}
|
|
|
|
|
2020-03-14 17:30:07 +01:00
|
|
|
leave_critical_section(flags);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2012-08-12 19:37:04 +02:00
|
|
|
}
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2019-04-24 20:11:40 +02:00
|
|
|
#ifdef CONFIG_SERIAL_RXDMA
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Notify DMA that there is free space in the RX buffer */
|
|
|
|
|
|
|
|
uart_dmarxfree(dev);
|
2015-11-12 21:16:19 +01:00
|
|
|
#endif
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Enable the RX interrupt */
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
uart_enablerxint(dev);
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
dev->open_count = tmp;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&dev->closelock);
|
2017-08-01 18:13:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2017-08-01 18:13:59 +02: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.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
static int uart_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2020-03-14 17:30:07 +01:00
|
|
|
/* Get exclusive access to the close semaphore (to synchronize open/close
|
2021-03-04 08:02:21 +01:00
|
|
|
* 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.
|
2017-08-01 18:13:59 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&dev->closelock);
|
2017-08-01 18:13:59 +02:00
|
|
|
if (dev->open_count > 1)
|
|
|
|
{
|
|
|
|
dev->open_count--;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->closelock);
|
2017-08-01 18:13:59 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There are no more references to the port */
|
|
|
|
|
|
|
|
dev->open_count = 0;
|
|
|
|
|
|
|
|
/* Stop accepting input */
|
|
|
|
|
|
|
|
uart_disablerxint(dev);
|
|
|
|
|
|
|
|
/* Prevent blocking if the device is opened with O_NONBLOCK */
|
|
|
|
|
|
|
|
if ((filep->f_oflags & O_NONBLOCK) == 0)
|
|
|
|
{
|
|
|
|
/* Now we wait for the transmit buffer(s) to clear */
|
|
|
|
|
2021-12-17 15:02:43 +01:00
|
|
|
uart_tcdrain(dev, false, 4 * TICK_PER_SEC);
|
2017-08-01 18:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the IRQ and disable the UART */
|
|
|
|
|
|
|
|
flags = enter_critical_section(); /* Disable interrupts */
|
|
|
|
uart_detach(dev); /* Detach interrupts */
|
2024-08-25 01:21:12 +02:00
|
|
|
|
|
|
|
/* Check for the serial console UART */
|
|
|
|
|
|
|
|
if (!dev->isconsole)
|
2017-08-01 18:13:59 +02:00
|
|
|
{
|
|
|
|
uart_shutdown(dev); /* Disable the UART */
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2020-10-19 11:24:39 +02:00
|
|
|
/* Wake up read and poll functions */
|
|
|
|
|
|
|
|
uart_datareceived(dev);
|
|
|
|
|
2017-08-01 18:13:59 +02: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
|
|
|
|
* a thread currently blocking on any of them.
|
|
|
|
*/
|
|
|
|
|
2018-08-26 16:49:08 +02:00
|
|
|
uart_reset_sem(dev);
|
2023-12-11 14:11:41 +01:00
|
|
|
|
|
|
|
if (dev->unlinked)
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&dev->closelock);
|
|
|
|
nxmutex_destroy(&dev->xmit.lock);
|
|
|
|
nxmutex_destroy(&dev->recv.lock);
|
|
|
|
nxmutex_destroy(&dev->closelock);
|
|
|
|
nxmutex_destroy(&dev->polllock);
|
|
|
|
nxsem_destroy(&dev->xmitsem);
|
|
|
|
nxsem_destroy(&dev->recvsem);
|
|
|
|
uart_release(dev);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->closelock);
|
2017-08-01 18:13:59 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2017-08-01 18:13:59 +02:00
|
|
|
* Name: uart_read
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +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;
|
2023-03-08 08:10:41 +01:00
|
|
|
bool echoed = false;
|
2014-12-27 15:15:41 +01:00
|
|
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->recv.lock);
|
2012-08-12 19:37:04 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* A signal received while waiting for access to the recv.tail will
|
|
|
|
* abort the transfer. After the transfer has started, we are
|
|
|
|
* committed and signals will be ignored.
|
2012-08-12 19:37:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/* 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 ? */
|
|
|
|
|
2023-07-12 12:17:32 +02:00
|
|
|
if ((ch == '\r') && (dev->tc_iflag & IGNCR))
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Store the received character */
|
|
|
|
|
|
|
|
*buffer++ = ch;
|
|
|
|
recvd++;
|
2023-03-01 16:25:35 +01:00
|
|
|
|
2023-03-19 17:09:45 +01:00
|
|
|
if (dev->tc_lflag & ECHO)
|
2023-03-01 16:25:35 +01:00
|
|
|
{
|
|
|
|
/* Check for the beginning of a VT100 escape sequence, 3 byte */
|
|
|
|
|
|
|
|
if (ch == ASCII_ESC)
|
|
|
|
{
|
|
|
|
/* Mark that we should skip 2 more bytes */
|
|
|
|
|
|
|
|
dev->escape = 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (dev->escape == 2 && ch != ASCII_LBRACKET)
|
|
|
|
{
|
|
|
|
/* It's not an <esc>[x 3 byte sequence, show it */
|
|
|
|
|
|
|
|
dev->escape = 0;
|
|
|
|
}
|
2023-08-29 11:17:07 +02:00
|
|
|
else if (dev->escape > 0)
|
2023-07-31 21:00:29 +02:00
|
|
|
{
|
|
|
|
/* Skipping character count down */
|
|
|
|
|
2024-01-31 02:54:35 +01:00
|
|
|
dev->escape--;
|
|
|
|
continue;
|
2023-07-31 21:00:29 +02:00
|
|
|
}
|
2023-03-01 16:25:35 +01:00
|
|
|
|
|
|
|
/* Echo if the character is not a control byte */
|
|
|
|
|
2023-07-31 21:00:29 +02:00
|
|
|
if (!iscntrl(ch & 0xff) || ch == '\n')
|
2023-03-01 16:25:35 +01:00
|
|
|
{
|
2023-03-04 20:10:17 +01:00
|
|
|
if (ch == '\n')
|
2023-03-01 16:25:35 +01:00
|
|
|
{
|
2023-03-04 20:10:17 +01:00
|
|
|
uart_putxmitchar(dev, '\r', true);
|
2023-03-01 16:25:35 +01:00
|
|
|
}
|
2023-03-04 20:10:17 +01:00
|
|
|
|
|
|
|
uart_putxmitchar(dev, ch, true);
|
2023-03-08 08:10:41 +01:00
|
|
|
|
|
|
|
/* Mark the tx buffer have echoed content here,
|
|
|
|
* to avoid the tx buffer is empty such as special escape
|
|
|
|
* sequence received, but enable the tx interrupt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
echoed = true;
|
2023-03-01 16:25:35 +01:00
|
|
|
}
|
|
|
|
}
|
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;
|
2020-03-14 17:30:07 +01:00
|
|
|
}
|
2008-11-18 15:54:43 +01:00
|
|
|
#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)
|
2020-03-14 17:30:07 +01:00
|
|
|
{
|
2008-11-18 15:54:43 +01:00
|
|
|
/* 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;
|
2020-03-14 17:30:07 +01:00
|
|
|
}
|
2008-11-18 15:54:43 +01:00
|
|
|
|
2020-10-19 11:24:39 +02:00
|
|
|
else if (filep->f_inode == 0)
|
|
|
|
{
|
|
|
|
/* File has been closed.
|
|
|
|
* Descriptor is not valid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
recvd = -EBADFD;
|
|
|
|
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
|
2020-03-14 17:30:07 +01:00
|
|
|
|
2008-11-18 15:54:43 +01:00
|
|
|
/* 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
|
|
|
|
{
|
2021-04-29 14:23:15 +02:00
|
|
|
/* Disable all interrupts and test again... */
|
2018-08-26 18:30:48 +02:00
|
|
|
|
|
|
|
flags = enter_critical_section();
|
2021-04-29 14:23:15 +02:00
|
|
|
|
2012-04-18 19:08:27 +02:00
|
|
|
/* Disable Rx interrupts and test again... */
|
|
|
|
|
|
|
|
uart_disablerxint(dev);
|
|
|
|
|
2018-08-26 18:30:48 +02:00
|
|
|
/* If the Rx ring buffer still empty? Bytes may have been added
|
|
|
|
* between the last time that we checked and when we disabled
|
2012-04-18 19:08:27 +02:00
|
|
|
* 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
|
|
|
{
|
2018-08-26 18:30:48 +02:00
|
|
|
/* Yes.. the buffer is still empty. We will need to wait for
|
|
|
|
* additional data to be received.
|
2012-04-18 19:08:27 +02:00
|
|
|
*/
|
|
|
|
|
2019-04-24 20:11:40 +02:00
|
|
|
#ifdef CONFIG_SERIAL_RXDMA
|
2015-11-12 21:16:19 +01:00
|
|
|
/* Notify DMA that there is free space in the RX buffer */
|
|
|
|
|
|
|
|
uart_dmarxfree(dev);
|
2021-04-29 14:23:15 +02:00
|
|
|
#endif
|
2018-08-26 18:30:48 +02:00
|
|
|
/* Wait with the RX interrupt re-enabled. All interrupts are
|
|
|
|
* disabled briefly to assure that the following operations
|
|
|
|
* are atomic.
|
|
|
|
*/
|
|
|
|
|
2018-08-26 18:03:30 +02:00
|
|
|
/* Re-enable UART Rx interrupts */
|
2015-11-12 21:16:19 +01:00
|
|
|
|
2013-02-02 02:50:34 +01:00
|
|
|
uart_enablerxint(dev);
|
2020-11-19 12:19:46 +01:00
|
|
|
|
|
|
|
/* Check again if the RX buffer is empty. The UART driver
|
|
|
|
* might have buffered data received between disabling the
|
|
|
|
* RX interrupt and entering the critical section. Some
|
|
|
|
* drivers (looking at you, cdcacm...) will push the buffer
|
|
|
|
* to the receive queue during uart_enablerxint().
|
|
|
|
* Just continue processing the RX queue if this happens.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rxbuf->head != rxbuf->tail)
|
|
|
|
{
|
|
|
|
leave_critical_section(flags);
|
|
|
|
continue;
|
|
|
|
}
|
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
|
2020-03-14 17:30:07 +01:00
|
|
|
{
|
2018-08-26 18:30:48 +02:00
|
|
|
/* Now wait with the Rx interrupt enabled. NuttX will
|
2013-02-02 02:50:34 +01:00
|
|
|
* automatically re-enable global interrupts when this
|
|
|
|
* thread goes to sleep.
|
|
|
|
*/
|
|
|
|
|
2023-03-24 06:44:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
dev->minrecv = MIN(buflen - recvd, dev->minread - recvd);
|
|
|
|
if (dev->timeout)
|
|
|
|
{
|
2024-05-27 04:37:54 +02:00
|
|
|
nxmutex_unlock(&dev->recv.lock);
|
2023-03-24 06:44:34 +01:00
|
|
|
ret = nxsem_tickwait(&dev->recvsem,
|
|
|
|
DSEC2TICK(dev->timeout));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2024-05-27 04:37:54 +02:00
|
|
|
nxmutex_unlock(&dev->recv.lock);
|
2023-03-24 06:44:34 +01:00
|
|
|
ret = nxsem_wait(&dev->recvsem);
|
|
|
|
}
|
2024-01-25 11:49:16 +01:00
|
|
|
|
2024-05-27 04:37:54 +02:00
|
|
|
nxmutex_lock(&dev->recv.lock);
|
|
|
|
|
2024-01-25 11:49:16 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
dev->minrecv = dev->minread;
|
|
|
|
#endif
|
2013-02-02 02:50:34 +01:00
|
|
|
}
|
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
|
|
|
{
|
2021-03-04 08:02:21 +01: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.
|
2012-08-12 19:37:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (recvd == 0)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* No bytes were read, return -EINTR
|
|
|
|
* (the VFS layer will set the errno value
|
|
|
|
* appropriately).
|
2012-08-12 19:37:04 +02:00
|
|
|
*/
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
2023-03-24 06:44:34 +01:00
|
|
|
recvd = dev->disconnected ? -ENOTCONN : ret;
|
2013-01-31 17:52:20 +01:00
|
|
|
#else
|
2023-03-24 06:44:34 +01:00
|
|
|
recvd = ret;
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-08-26 18:30:48 +02:00
|
|
|
leave_critical_section(flags);
|
2021-04-29 14:23:15 +02:00
|
|
|
|
2012-04-18 19:08:27 +02:00
|
|
|
uart_enablerxint(dev);
|
|
|
|
}
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 08:10:41 +01:00
|
|
|
if (echoed)
|
2023-03-01 16:25:35 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_SERIAL_TXDMA
|
|
|
|
uart_dmatxavail(dev);
|
|
|
|
#endif
|
|
|
|
uart_enabletxint(dev);
|
|
|
|
}
|
|
|
|
|
2019-04-24 20:11:40 +02:00
|
|
|
#ifdef CONFIG_SERIAL_RXDMA
|
2015-11-12 21:16:19 +01:00
|
|
|
/* Notify DMA that there is free space in the RX buffer */
|
|
|
|
|
2018-08-26 18:03:30 +02:00
|
|
|
flags = enter_critical_section();
|
2015-11-12 21:16:19 +01:00
|
|
|
uart_dmarxfree(dev);
|
2018-08-26 18:03:30 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
2015-11-12 21:16:19 +01:00
|
|
|
|
|
|
|
/* RX interrupt could be disabled by RX buffer overflow. Enable it now. */
|
|
|
|
|
|
|
|
uart_enablerxint(dev);
|
|
|
|
|
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
|
|
|
|
2021-03-04 08:02:21 +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
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
uart_rxflowcontrol(dev, nbuffered, false);
|
2014-12-27 14:43:06 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-10-18 14:49:11 +02:00
|
|
|
/* Is the RX buffer empty? */
|
2014-12-27 16:45:45 +01:00
|
|
|
|
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
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
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
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->recv.lock);
|
2008-10-09 21:20:02 +02:00
|
|
|
return recvd;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2017-08-01 18:13:59 +02:00
|
|
|
* Name: uart_write
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2017-08-01 18:13:59 +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
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
ssize_t nwritten = buflen;
|
|
|
|
bool oktoblock;
|
|
|
|
int ret;
|
|
|
|
char ch;
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* We may receive serial 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.
|
2013-08-11 03:14:05 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if (up_interrupt_context() || sched_idletask())
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
2018-08-26 17:47:25 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* If the removable device is no longer connected, refuse to write to
|
|
|
|
* the device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->disconnected)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
#endif
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2018-08-26 17:47:25 +02:00
|
|
|
flags = enter_critical_section();
|
|
|
|
ret = uart_irqwrite(dev, buffer, buflen);
|
|
|
|
leave_critical_section(flags);
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2018-08-26 17:47:25 +02:00
|
|
|
return ret;
|
2017-08-01 18:13:59 +02:00
|
|
|
}
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Only one user can access dev->xmit.head at a time */
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->xmit.lock);
|
2017-08-01 18:13:59 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* A signal received while waiting for access to the xmit.head will
|
2021-03-04 08:02:21 +01:00
|
|
|
* abort the transfer. After the transfer has started, we are
|
|
|
|
* committed and signals will be ignored.
|
2017-08-01 18:13:59 +02:00
|
|
|
*/
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
/* If the removable device is no longer connected, refuse to write to the
|
2022-09-06 08:18:45 +02:00
|
|
|
* device. This check occurs after taking the xmit.lock because the
|
2017-08-01 18:13:59 +02:00
|
|
|
* disconnection event might have occurred while we were waiting for
|
|
|
|
* access to the transmit buffers.
|
|
|
|
*/
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if (dev->disconnected)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->xmit.lock);
|
2017-08-01 18:13:59 +02:00
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
#endif
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Can the following loop block, waiting for space in the TX
|
|
|
|
* buffer?
|
|
|
|
*/
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
oktoblock = ((filep->f_oflags & O_NONBLOCK) == 0);
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2017-08-01 18:13:59 +02: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.
|
|
|
|
*/
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
uart_disabletxint(dev);
|
|
|
|
for (; buflen; buflen--)
|
|
|
|
{
|
|
|
|
ch = *buffer++;
|
|
|
|
ret = OK;
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Do output post-processing */
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if ((dev->tc_oflag & OPOST) != 0)
|
|
|
|
{
|
|
|
|
/* Mapping CR to NL? */
|
|
|
|
|
|
|
|
if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
|
2016-07-25 22:06:32 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
ch = '\n';
|
|
|
|
}
|
2016-07-25 22:06:32 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Are we interested in newline processing? */
|
2013-08-11 03:14:05 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
|
|
|
|
{
|
|
|
|
ret = uart_putxmitchar(dev, '\r', oktoblock);
|
2013-08-11 03:14:05 +02:00
|
|
|
}
|
2017-08-01 14:30:52 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Specifically not handled:
|
|
|
|
*
|
2018-08-26 17:56:02 +02:00
|
|
|
* OXTABS - primarily a full-screen terminal optimization
|
2017-08-01 18:13:59 +02:00
|
|
|
* ONOEOT - Unix interoperability hack
|
|
|
|
* OLCUC - Not specified by POSIX
|
2018-08-26 17:56:02 +02:00
|
|
|
* ONOCR - low-speed interactive optimization
|
2017-08-01 18:13:59 +02:00
|
|
|
*/
|
|
|
|
}
|
2017-08-01 14:30:52 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Put the character into the transmit buffer */
|
|
|
|
|
2018-08-26 17:56:02 +02:00
|
|
|
if (ret >= 0)
|
2017-08-01 18:13:59 +02:00
|
|
|
{
|
|
|
|
ret = uart_putxmitchar(dev, ch, oktoblock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* uart_putxmitchar() might return an error under one of two
|
|
|
|
* conditions: (1) The wait for buffer space might have been
|
|
|
|
* interrupted by a signal (ret should be -EINTR), (2) if
|
|
|
|
* CONFIG_SERIAL_REMOVABLE is defined, then uart_putxmitchar()
|
|
|
|
* might also return if the serial device was disconnected
|
|
|
|
* (with -ENOTCONN), or (3) if O_NONBLOCK is specified, then
|
|
|
|
* then uart_putxmitchar() might return -EAGAIN if the output
|
|
|
|
* TX buffer is full.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (buflen < (size_t)nwritten)
|
|
|
|
{
|
|
|
|
/* Some data was transferred. Return the number of bytes that
|
|
|
|
* were successfully transferred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nwritten -= buflen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No data was transferred. Return the negated errno value.
|
|
|
|
* The VFS layer will set the errno value appropriately).
|
|
|
|
*/
|
|
|
|
|
|
|
|
nwritten = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->xmit.head != dev->xmit.tail)
|
|
|
|
{
|
2019-04-24 20:11:40 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TXDMA
|
2017-08-01 18:13:59 +02:00
|
|
|
uart_dmatxavail(dev);
|
|
|
|
#endif
|
|
|
|
uart_enabletxint(dev);
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->xmit.lock);
|
2017-08-01 18:13:59 +02:00
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2017-08-01 18:13:59 +02:00
|
|
|
* Name: uart_ioctl
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
static int uart_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR uart_dev_t *dev = inode->i_private;
|
|
|
|
|
|
|
|
/* Handle TTY-level IOCTLs here */
|
2020-03-14 17:30:07 +01:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Let low-level driver handle the call first */
|
|
|
|
|
2022-02-07 03:45:18 +01:00
|
|
|
int ret = dev->ops->ioctl ? dev->ops->ioctl(filep, cmd, arg) : -ENOTTY;
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
/* Get the number of bytes that may be read from the RX buffer
|
|
|
|
* (without waiting)
|
|
|
|
*/
|
|
|
|
|
|
|
|
case FIONREAD:
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* Determine the number of bytes available in the RX buffer */
|
|
|
|
|
|
|
|
if (dev->recv.tail <= dev->recv.head)
|
2017-08-01 14:30:52 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
count = dev->recv.head - dev->recv.tail;
|
2017-08-01 14:30:52 +02:00
|
|
|
}
|
2017-08-01 18:13:59 +02:00
|
|
|
else
|
2017-08-01 14:30:52 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
count = dev->recv.size - (dev->recv.tail - dev->recv.head);
|
2017-08-01 14:30:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
|
|
|
ret = 0;
|
2017-08-01 14:30:52 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the number of bytes that have been written to the TX
|
|
|
|
* buffer.
|
|
|
|
*/
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
case FIONWRITE:
|
2017-08-01 14:30:52 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
int count;
|
|
|
|
irqstate_t flags = enter_critical_section();
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Determine the number of bytes waiting in the TX buffer */
|
|
|
|
|
|
|
|
if (dev->xmit.tail <= dev->xmit.head)
|
2017-08-01 15:39:08 +02:00
|
|
|
{
|
2017-08-01 18:13:59 +02:00
|
|
|
count = dev->xmit.head - dev->xmit.tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = dev->xmit.size - (dev->xmit.tail - dev->xmit.head);
|
|
|
|
}
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
leave_critical_section(flags);
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Get the number of free bytes in the TX buffer */
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
case FIONSPACE:
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
irqstate_t flags = enter_critical_section();
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
/* Determine the number of bytes free in the TX buffer */
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if (dev->xmit.head < dev->xmit.tail)
|
|
|
|
{
|
|
|
|
count = dev->xmit.tail - dev->xmit.head - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
count = dev->xmit.size -
|
|
|
|
(dev->xmit.head - dev->xmit.tail) - 1;
|
2017-08-01 18:13:59 +02:00
|
|
|
}
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
leave_critical_section(flags);
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCFLSH:
|
|
|
|
{
|
|
|
|
/* Empty the tx/rx buffers */
|
|
|
|
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
|
|
|
|
if (arg == TCIFLUSH || arg == TCIOFLUSH)
|
|
|
|
{
|
2018-08-26 18:03:30 +02:00
|
|
|
dev->recv.tail = dev->recv.head;
|
2018-08-26 17:39:16 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
/* De-activate RX flow control. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
uart_rxflowcontrol(dev, 0, false);
|
2018-08-26 17:39:16 +02:00
|
|
|
#endif
|
2017-08-01 18:13:59 +02:00
|
|
|
}
|
2017-08-01 15:39:08 +02:00
|
|
|
|
2017-08-01 18:13:59 +02:00
|
|
|
if (arg == TCOFLUSH || arg == TCIOFLUSH)
|
|
|
|
{
|
2018-08-26 18:03:30 +02:00
|
|
|
dev->xmit.tail = dev->xmit.head;
|
2018-08-26 17:39:16 +02:00
|
|
|
|
|
|
|
/* Inform any waiters there there is space available. */
|
|
|
|
|
|
|
|
uart_datasent(dev);
|
2017-08-01 15:39:08 +02:00
|
|
|
}
|
2017-08-01 18:13:59 +02:00
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2018-03-26 21:54:47 +02:00
|
|
|
ret = 0;
|
2017-08-01 18:13:59 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCDRN:
|
|
|
|
{
|
2021-12-17 15:02:43 +01:00
|
|
|
ret = uart_tcdrain(dev, true, 10 * TICK_PER_SEC);
|
2017-08-01 14:30:52 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2023-02-09 01:51:11 +01:00
|
|
|
case TCSBRK:
|
|
|
|
{
|
|
|
|
/* Non-standard Break specifies duration in milliseconds */
|
|
|
|
|
|
|
|
ret = uart_tcsendbreak(dev, filep, arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCSBRKP:
|
|
|
|
{
|
|
|
|
/* POSIX Break specifies duration in units of 100ms */
|
|
|
|
|
|
|
|
ret = uart_tcsendbreak(dev, filep, arg * 100);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-10-29 05:53:41 +01:00
|
|
|
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
|
2020-05-04 19:34:04 +02:00
|
|
|
/* Make the controlling terminal of the calling process */
|
2018-08-26 16:56:16 +02:00
|
|
|
|
2018-08-26 16:49:08 +02:00
|
|
|
case TIOCSCTTY:
|
|
|
|
{
|
2020-09-24 12:11:44 +02:00
|
|
|
/* Save the PID of the recipient of the SIGINT signal. */
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2021-06-17 17:04:04 +02:00
|
|
|
if ((int)arg < 0 || dev->pid >= 0)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->pid = (pid_t)arg;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCNOTTY:
|
|
|
|
{
|
2022-03-21 23:47:09 +01:00
|
|
|
dev->pid = INVALID_PROCESS_ID;
|
2021-06-17 17:04:04 +02:00
|
|
|
ret = 0;
|
2018-08-26 16:49:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2013-08-11 03:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append any higher level TTY flags */
|
|
|
|
|
2022-02-01 17:53:34 +01:00
|
|
|
if (ret == OK || ret == -ENOTTY)
|
2013-08-11 03:14:05 +02:00
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case TCGETS:
|
|
|
|
{
|
2023-02-08 04:27:44 +01:00
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)
|
|
|
|
(uintptr_t)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;
|
2023-03-24 06:44:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
termiosp->c_cc[VTIME] = dev->timeout;
|
|
|
|
termiosp->c_cc[VMIN] = dev->minread;
|
|
|
|
#endif
|
2022-02-07 03:45:18 +01:00
|
|
|
|
|
|
|
ret = 0;
|
2013-08-11 03:14:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCSETS:
|
|
|
|
{
|
2023-02-08 04:27:44 +01:00
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)
|
|
|
|
(uintptr_t)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;
|
2023-03-24 06:44:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
dev->timeout = termiosp->c_cc[VTIME];
|
|
|
|
dev->minread = termiosp->c_cc[VMIN];
|
2024-01-25 11:49:16 +01:00
|
|
|
dev->minrecv = dev->minread;
|
2023-03-24 06:44:34 +01:00
|
|
|
#endif
|
2022-02-07 03:45:18 +01:00
|
|
|
ret = 0;
|
2013-08-11 03:14:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2007-03-16 23:07:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-11-18 00:22:27 +01:00
|
|
|
* Name: uart_poll
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
static 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
|
2019-05-26 20:33:42 +02:00
|
|
|
if (dev == NULL || fds == NULL)
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->polllock);
|
2012-08-12 19:37:04 +02:00
|
|
|
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
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
dev->fds[i] = fds;
|
|
|
|
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
|
|
|
{
|
2021-06-02 05:16:34 +02:00
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
2008-11-19 19:43:50 +01:00
|
|
|
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
|
|
|
*
|
2021-03-04 08:02:21 +01: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;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&dev->xmit.lock);
|
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)
|
2020-03-14 17:30:07 +01:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLOUT;
|
2020-03-14 17:30:07 +01:00
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->xmit.lock);
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Check if the receive buffer is empty.
|
2012-08-12 19:37:04 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01: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).
|
2012-08-12 19:37:04 +02:00
|
|
|
*/
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&dev->recv.lock);
|
2008-11-18 00:22:27 +01:00
|
|
|
if (dev->recv.head != dev->recv.tail)
|
2020-03-14 17:30:07 +01:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLIN;
|
2020-03-14 17:30:07 +01:00
|
|
|
}
|
2012-08-12 19:37:04 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->recv.lock);
|
2008-11-18 00:22:27 +01:00
|
|
|
|
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
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
2019-05-26 20:33:42 +02:00
|
|
|
else if (fds->priv != NULL)
|
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
|
|
|
|
2019-05-26 20:33:42 +02:00
|
|
|
FAR struct pollfd **slot = (FAR 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)
|
|
|
|
{
|
2021-06-02 05:16:34 +02:00
|
|
|
ret = -EIO;
|
2008-11-19 19:43:50 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
2021-06-02 05:16:34 +02:00
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
2008-11-19 19:43:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->polllock);
|
2008-11-19 19:43:50 +01:00
|
|
|
return ret;
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
|
|
|
|
2023-12-11 14:11:41 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int uart_unlink(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR uart_dev_t *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode->i_private != NULL);
|
|
|
|
|
|
|
|
dev = inode->i_private;
|
|
|
|
ret = nxmutex_lock(&dev->closelock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* A signal received while waiting for the last close operation. */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->open_count <= 0)
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&dev->closelock);
|
|
|
|
nxmutex_destroy(&dev->xmit.lock);
|
|
|
|
nxmutex_destroy(&dev->recv.lock);
|
|
|
|
nxmutex_destroy(&dev->closelock);
|
|
|
|
nxmutex_destroy(&dev->polllock);
|
|
|
|
nxsem_destroy(&dev->xmitsem);
|
|
|
|
nxsem_destroy(&dev->recvsem);
|
|
|
|
uart_release(dev);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->unlinked = true;
|
|
|
|
nxmutex_unlock(&dev->closelock);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-15 06:40:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_nxsched_foreach_cb
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_TTY_LAUNCH
|
|
|
|
static void uart_launch_foreach(FAR struct tcb_s *tcb, FAR void *arg)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TTY_LAUNCH_ENTRY
|
|
|
|
if (!strcmp(tcb->name, CONFIG_TTY_LAUNCH_ENTRYNAME))
|
|
|
|
#else
|
|
|
|
if (!strcmp(tcb->name, CONFIG_TTY_LAUNCH_FILEPATH))
|
|
|
|
#endif
|
|
|
|
{
|
2024-08-25 22:37:15 +02:00
|
|
|
*(FAR int *)arg = 1;
|
2021-12-15 06:40:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_launch_worker(void *arg)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TTY_LAUNCH_ARGS
|
|
|
|
FAR char *const argv[] =
|
|
|
|
{
|
|
|
|
CONFIG_TTY_LAUNCH_ARGS,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
FAR char *const *argv = NULL;
|
|
|
|
#endif
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
nxsched_foreach(uart_launch_foreach, &found);
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
posix_spawnattr_t attr;
|
|
|
|
|
|
|
|
posix_spawnattr_init(&attr);
|
|
|
|
attr.priority = CONFIG_TTY_LAUNCH_PRIORITY;
|
|
|
|
attr.stacksize = CONFIG_TTY_LAUNCH_STACKSIZE;
|
2022-10-29 18:30:23 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_TTY_LAUNCH_ENTRY
|
|
|
|
task_spawn(CONFIG_TTY_LAUNCH_ENTRYNAME,
|
|
|
|
CONFIG_TTY_LAUNCH_ENTRYPOINT,
|
|
|
|
NULL, &attr, argv, NULL);
|
|
|
|
#else
|
2023-04-26 05:44:08 +02:00
|
|
|
exec_spawn(CONFIG_TTY_LAUNCH_FILEPATH,
|
|
|
|
argv, NULL, NULL, 0, NULL, &attr);
|
2021-12-15 06:40:45 +01:00
|
|
|
#endif
|
2022-10-29 18:30:23 +02:00
|
|
|
posix_spawnattr_destroy(&attr);
|
2021-12-15 06:40:45 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-20 10:44:08 +01:00
|
|
|
|
|
|
|
static void uart_launch(void)
|
|
|
|
{
|
|
|
|
work_queue(HPWORK, &g_serial_work, uart_launch_worker, NULL, 0);
|
|
|
|
}
|
2021-12-15 06:40:45 +01:00
|
|
|
#endif
|
|
|
|
|
2023-05-16 08:37:41 +02:00
|
|
|
static void uart_wakeup(FAR sem_t *sem)
|
|
|
|
{
|
|
|
|
int sval;
|
|
|
|
|
|
|
|
if (nxsem_get_value(sem, &sval) != OK)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes... wake up all waiting threads */
|
|
|
|
|
|
|
|
while (sval++ < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Public Functions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2007-03-16 23:07:14 +01:00
|
|
|
* Name: uart_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register serial console and serial ports.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2007-03-16 23:07:14 +01:00
|
|
|
|
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
|
|
|
{
|
2021-12-17 15:03:10 +01:00
|
|
|
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
|
2018-08-28 20:15:31 +02:00
|
|
|
/* Initialize of the task that will receive SIGINT signals. */
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
dev->pid = INVALID_PROCESS_ID;
|
2021-12-17 15:03:10 +01:00
|
|
|
#endif
|
2018-08-28 17:15:29 +02:00
|
|
|
|
2019-01-27 17:40:54 +01:00
|
|
|
/* If this UART is a serial console */
|
2018-08-28 17:15:29 +02:00
|
|
|
|
|
|
|
if (dev->isconsole)
|
|
|
|
{
|
2023-03-13 10:20:27 +01:00
|
|
|
/* Enable signals and echo by default */
|
2019-01-27 17:40:54 +01:00
|
|
|
|
2023-03-13 10:20:27 +01:00
|
|
|
dev->tc_lflag |= ISIG | ECHO;
|
2019-01-27 17:40:54 +01:00
|
|
|
|
|
|
|
/* Enable \n -> \r\n translation for the console */
|
|
|
|
|
|
|
|
dev->tc_oflag = OPOST | ONLCR;
|
2023-02-07 11:12:08 +01:00
|
|
|
|
|
|
|
/* Convert CR to LF by default for console */
|
|
|
|
|
2023-03-13 10:20:27 +01:00
|
|
|
dev->tc_iflag |= ICRNL;
|
2023-03-01 16:25:35 +01:00
|
|
|
|
|
|
|
/* Clear escape counter */
|
|
|
|
|
|
|
|
dev->escape = 0;
|
2018-08-28 17:15:29 +02:00
|
|
|
}
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize mutex & semaphores */
|
2016-11-03 15:16:59 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->xmit.lock);
|
|
|
|
nxmutex_init(&dev->recv.lock);
|
|
|
|
nxmutex_init(&dev->closelock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_init(&dev->xmitsem, 0, 0);
|
|
|
|
nxsem_init(&dev->recvsem, 0, 0);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->polllock);
|
2007-03-16 23:07:14 +01:00
|
|
|
|
2023-03-24 06:44:34 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
dev->timeout = 0;
|
|
|
|
dev->minread = 1;
|
|
|
|
#endif
|
|
|
|
|
2016-11-03 15:16:59 +01:00
|
|
|
/* Register the serial driver */
|
|
|
|
|
2023-10-31 13:12:02 +01:00
|
|
|
#ifdef CONFIG_SERIAL_GDBSTUB
|
|
|
|
if (strcmp(path, CONFIG_SERIAL_GDBSTUB_PATH) == 0)
|
|
|
|
{
|
|
|
|
return uart_gdbstub_register(dev);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-11-18 00:22:27 +01:00
|
|
|
* Name: uart_datareceived
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* This function is called from uart_recvchars when new serial data is
|
|
|
|
* place in the driver's circular buffer. This function will wake-up any
|
|
|
|
* stalled read() operations that are waiting for incoming data.
|
2008-11-18 00:22:27 +01:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-11-18 00:22:27 +01:00
|
|
|
|
|
|
|
void uart_datareceived(FAR uart_dev_t *dev)
|
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can read from the recv buffer */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->fds, CONFIG_SERIAL_NPOLLWAITERS, POLLIN);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Is there a thread waiting for read data? */
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2023-05-16 08:37:41 +02:00
|
|
|
uart_wakeup(&dev->recvsem);
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2019-11-04 02:56:28 +01:00
|
|
|
#if defined(CONFIG_PM) && defined(CONFIG_SERIAL_CONSOLE)
|
2019-01-27 17:50:15 +01:00
|
|
|
/* Call pm_activity when characters are received on the console device */
|
|
|
|
|
|
|
|
if (dev->isconsole)
|
|
|
|
{
|
|
|
|
pm_activity(CONFIG_SERIAL_PM_ACTIVITY_DOMAIN,
|
|
|
|
CONFIG_SERIAL_PM_ACTIVITY_PRIORITY);
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-11-18 00:22:27 +01:00
|
|
|
* Name: uart_datasent
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* 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.
|
2008-11-18 00:22:27 +01:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-11-18 00:22:27 +01:00
|
|
|
|
|
|
|
void uart_datasent(FAR uart_dev_t *dev)
|
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can write to xmit buffer */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->fds, CONFIG_SERIAL_NPOLLWAITERS, POLLOUT);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Is there a thread waiting for space in xmit.buffer? */
|
|
|
|
|
2023-05-16 08:37:41 +02:00
|
|
|
uart_wakeup(&dev->xmitsem);
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2013-01-31 17:52:20 +01:00
|
|
|
* Name: uart_connected
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* 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.
|
2013-01-31 17:52:20 +01:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
* 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.
|
2013-01-31 17:52:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2013-01-31 17:52:20 +01:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that a hangup occurred */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->fds, CONFIG_SERIAL_NPOLLWAITERS, POLLERR | POLLHUP);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* 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? */
|
|
|
|
|
2023-05-16 08:37:41 +02:00
|
|
|
uart_wakeup(&dev->xmitsem);
|
2013-01-31 17:52:20 +01:00
|
|
|
|
|
|
|
/* Is there a thread waiting for read data? */
|
|
|
|
|
2023-05-16 08:37:41 +02:00
|
|
|
uart_wakeup(&dev->recvsem);
|
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
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2018-08-26 16:49:08 +02:00
|
|
|
* Name: uart_reset_sem
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* This function is called when need reset uart semaphore, this may used in
|
|
|
|
* kill one process, but this process was reading/writing with the
|
|
|
|
* semaphore.
|
2018-08-26 16:49:08 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2018-08-26 16:49:08 +02:00
|
|
|
|
|
|
|
void uart_reset_sem(FAR uart_dev_t *dev)
|
|
|
|
{
|
|
|
|
nxsem_reset(&dev->xmitsem, 0);
|
|
|
|
nxsem_reset(&dev->recvsem, 0);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_reset(&dev->xmit.lock);
|
|
|
|
nxmutex_reset(&dev->recv.lock);
|
|
|
|
nxmutex_reset(&dev->polllock);
|
2018-08-26 16:49:08 +02:00
|
|
|
}
|
2021-12-15 06:40:45 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2021-12-20 10:44:08 +01:00
|
|
|
* Name: uart_check_special
|
2021-12-15 06:40:45 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2021-12-20 10:44:08 +01:00
|
|
|
* Check if the SIGINT or SIGTSTP character is in the contiguous Rx DMA
|
|
|
|
* buffer region. The first signal associated with the first such
|
|
|
|
* character is returned.
|
|
|
|
*
|
|
|
|
* If there multiple such characters in the buffer, only the signal
|
|
|
|
* associated with the first is returned (this a bug!)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 if a signal-related character does not appear in the. Otherwise,
|
|
|
|
* SIGKILL or SIGTSTP may be returned to indicate the appropriate signal
|
|
|
|
* action.
|
2021-12-15 06:40:45 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-12-20 10:44:08 +01:00
|
|
|
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP) || \
|
|
|
|
defined(CONFIG_TTY_FORCE_PANIC) || defined(CONFIG_TTY_LAUNCH)
|
2024-08-25 01:21:12 +02:00
|
|
|
int uart_check_special(FAR uart_dev_t *dev, FAR const char *buf, size_t size)
|
2021-12-15 06:40:45 +01:00
|
|
|
{
|
2021-12-20 10:44:08 +01:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if ((dev->tc_lflag & ISIG) == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TTY_FORCE_PANIC
|
|
|
|
if (buf[i] == CONFIG_TTY_FORCE_PANIC_CHAR)
|
|
|
|
{
|
|
|
|
PANIC();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_TTY_LAUNCH
|
|
|
|
if (buf[i] == CONFIG_TTY_LAUNCH_CHAR)
|
|
|
|
{
|
|
|
|
uart_launch();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_TTY_SIGINT
|
|
|
|
if (dev->pid > 0 && buf[i] == CONFIG_TTY_SIGINT_CHAR)
|
|
|
|
{
|
|
|
|
return SIGINT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_TTY_SIGTSTP
|
|
|
|
if (dev->pid > 0 && buf[i] == CONFIG_TTY_SIGTSTP_CHAR)
|
|
|
|
{
|
|
|
|
return SIGTSTP;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-12-15 06:40:45 +01:00
|
|
|
}
|
|
|
|
#endif
|