2019-11-06 02:39:36 +01:00
|
|
|
/****************************************************************************
|
2015-11-15 14:58:08 +01:00
|
|
|
* drivers/serial/serial_io.c
|
2008-09-24 17:42:13 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +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
|
2008-09-24 17:42:13 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-09-24 17:42:13 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +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.
|
2008-09-24 17:42:13 +02:00
|
|
|
*
|
2019-11-06 02:39:36 +01:00
|
|
|
****************************************************************************/
|
2008-09-24 17:42:13 +02:00
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
/****************************************************************************
|
2008-09-24 17:42:13 +02:00
|
|
|
* Included Files
|
2019-11-06 02:39:36 +01:00
|
|
|
****************************************************************************/
|
2008-09-24 17:42:13 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
|
2019-10-16 15:09:03 +02:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
# include <nuttx/irq.h>
|
|
|
|
#endif
|
|
|
|
|
2021-04-29 11:02:34 +02:00
|
|
|
#include <assert.h>
|
2008-09-24 17:42:13 +02:00
|
|
|
#include <sys/types.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
2008-09-24 17:42:13 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
#include <nuttx/signal.h>
|
2015-11-15 14:58:08 +01:00
|
|
|
#include <nuttx/serial/serial.h>
|
2008-09-24 17:42:13 +02:00
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
/****************************************************************************
|
2008-09-24 17:42:13 +02:00
|
|
|
* Public Functions
|
2019-11-06 02:39:36 +01:00
|
|
|
****************************************************************************/
|
2008-09-24 17:42:13 +02:00
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
/****************************************************************************
|
2008-09-24 17:42:13 +02:00
|
|
|
* Name: uart_xmitchars
|
|
|
|
*
|
|
|
|
* Description:
|
2019-11-06 02:39:36 +01:00
|
|
|
* This function is called from the UART interrupt handler when an
|
|
|
|
* interrupt is received indicating that there is more space in the
|
|
|
|
* transmit FIFO. This function will send characters from the tail of
|
|
|
|
* the xmit buffer while the driver write() logic adds data to the head
|
|
|
|
* of the xmit buffer.
|
2008-09-24 17:42:13 +02:00
|
|
|
*
|
2019-11-06 02:39:36 +01:00
|
|
|
****************************************************************************/
|
2008-09-24 17:42:13 +02:00
|
|
|
|
|
|
|
void uart_xmitchars(FAR uart_dev_t *dev)
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t nbytes = 0;
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2019-10-16 15:09:03 +02:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
#endif
|
|
|
|
|
2013-06-14 00:18:25 +02:00
|
|
|
/* Send while we still have data in the TX buffer & room in the fifo */
|
2008-09-24 17:42:13 +02:00
|
|
|
|
|
|
|
while (dev->xmit.head != dev->xmit.tail && uart_txready(dev))
|
|
|
|
{
|
2008-11-13 15:46:10 +01:00
|
|
|
/* Send the next byte */
|
|
|
|
|
2008-09-24 17:42:13 +02:00
|
|
|
uart_send(dev, dev->xmit.buffer[dev->xmit.tail]);
|
2008-11-18 00:22:27 +01:00
|
|
|
nbytes++;
|
2008-09-24 17:42:13 +02:00
|
|
|
|
2008-11-13 15:46:10 +01:00
|
|
|
/* Increment the tail index */
|
|
|
|
|
2008-09-24 17:42:13 +02:00
|
|
|
if (++(dev->xmit.tail) >= dev->xmit.size)
|
|
|
|
{
|
|
|
|
dev->xmit.tail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-12 16:29:08 +02:00
|
|
|
/* When all of the characters have been sent from the buffer disable the TX
|
|
|
|
* interrupt.
|
2013-06-14 00:18:25 +02:00
|
|
|
*
|
|
|
|
* Potential bug? If nbytes == 0 && (dev->xmit.head == dev->xmit.tail) &&
|
|
|
|
* dev->xmitwaiting == true, then disabling the TX interrupt will leave
|
|
|
|
* the uart_write() logic waiting to TX to complete with no TX interrupts.
|
|
|
|
* Can that happen?
|
2008-09-24 17:42:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->xmit.head == dev->xmit.tail)
|
|
|
|
{
|
|
|
|
uart_disabletxint(dev);
|
|
|
|
}
|
2008-11-18 00:22:27 +01:00
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
/* If any bytes were removed from the buffer, inform any waiters that
|
|
|
|
* there is space available.
|
2008-11-18 00:22:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (nbytes)
|
|
|
|
{
|
|
|
|
uart_datasent(dev);
|
|
|
|
}
|
2019-10-16 15:09:03 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
2008-09-24 17:42:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
/****************************************************************************
|
2008-09-24 17:42:13 +02:00
|
|
|
* Name: uart_receivechars
|
|
|
|
*
|
|
|
|
* Description:
|
2019-11-06 02:39:36 +01:00
|
|
|
* This function is called from the UART interrupt handler when an
|
|
|
|
* interrupt is received indicating that are bytes available in the
|
2019-11-06 14:06:36 +01:00
|
|
|
* receive FIFO. This function will add chars to head of receive buffer.
|
2019-11-06 02:39:36 +01:00
|
|
|
* Driver read() logic will take characters from the tail of the buffer.
|
2008-09-24 17:42:13 +02:00
|
|
|
*
|
2019-11-06 02:39:36 +01:00
|
|
|
****************************************************************************/
|
2008-09-24 17:42:13 +02:00
|
|
|
|
|
|
|
void uart_recvchars(FAR uart_dev_t *dev)
|
|
|
|
{
|
2014-12-27 15:15:41 +01:00
|
|
|
FAR struct uart_buffer_s *rxbuf = &dev->recv;
|
2014-12-27 14:43:06 +01:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
|
|
|
unsigned int watermark;
|
|
|
|
#endif
|
2008-09-24 17:42:13 +02:00
|
|
|
unsigned int status;
|
2014-12-27 15:15:41 +01:00
|
|
|
int nexthead = rxbuf->head + 1;
|
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)
|
2018-09-02 23:36:25 +02:00
|
|
|
int signo = 0;
|
|
|
|
#endif
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t nbytes = 0;
|
2008-09-24 17:42:13 +02:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
if (nexthead >= rxbuf->size)
|
2008-09-24 17:42:13 +02:00
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
|
|
|
|
2014-12-27 14:43:06 +01:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
2018-09-02 23:36:25 +02:00
|
|
|
/* Pre-calculate the watermark level that we will need to test against. */
|
2014-12-27 14:43:06 +01:00
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
watermark = (CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK * rxbuf->size) /
|
|
|
|
100;
|
2014-12-27 14:43:06 +01:00
|
|
|
#endif
|
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
/* Loop putting characters into the receive buffer until there are no
|
|
|
|
* further characters to available.
|
2008-11-13 02:02:56 +01:00
|
|
|
*/
|
|
|
|
|
2011-07-12 16:29:08 +02:00
|
|
|
while (uart_rxavailable(dev))
|
2008-09-24 17:42:13 +02:00
|
|
|
{
|
2015-11-15 15:39:01 +01:00
|
|
|
bool is_full = (nexthead == rxbuf->tail);
|
2014-05-08 17:00:33 +02:00
|
|
|
char ch;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
2014-12-27 15:00:48 +01:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
2015-11-15 15:39:01 +01:00
|
|
|
unsigned int nbuffered;
|
|
|
|
|
|
|
|
/* How many bytes are buffered */
|
|
|
|
|
|
|
|
if (rxbuf->head >= rxbuf->tail)
|
|
|
|
{
|
|
|
|
nbuffered = rxbuf->head - rxbuf->tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbuffered = rxbuf->size - rxbuf->tail + rxbuf->head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the level now above the watermark level that we need to report? */
|
|
|
|
|
|
|
|
if (nbuffered >= watermark)
|
2014-12-27 14:43:06 +01:00
|
|
|
{
|
2020-09-24 12:11:44 +02:00
|
|
|
/* Let the lower level driver know that the watermark level has
|
|
|
|
* been crossed. It will probably activate RX flow control.
|
2015-11-15 15:39:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (uart_rxflowcontrol(dev, nbuffered, true))
|
|
|
|
{
|
|
|
|
/* Low-level driver activated RX flow control, exit loop now. */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-12-27 14:43:06 +01:00
|
|
|
}
|
|
|
|
#else
|
2020-09-24 12:11:44 +02:00
|
|
|
/* Check if RX buffer is full and allow serial low-level driver to
|
|
|
|
* pause processing. This allows proper utilization of hardware flow
|
|
|
|
* control.
|
2015-11-15 15:39:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (is_full)
|
2014-05-08 17:00:33 +02:00
|
|
|
{
|
2015-11-15 15:39:01 +01:00
|
|
|
if (uart_rxflowcontrol(dev, rxbuf->size, true))
|
|
|
|
{
|
|
|
|
/* Low-level driver activated RX flow control, exit loop now. */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-05-08 17:00:33 +02:00
|
|
|
}
|
2014-12-27 14:43:06 +01:00
|
|
|
#endif
|
2014-05-08 17:00:33 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-26 16:49:08 +02:00
|
|
|
/* Get this next character from the hardware */
|
|
|
|
|
2014-05-08 17:00:33 +02:00
|
|
|
ch = uart_receive(dev, &status);
|
2014-04-13 22:32:20 +02: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)
|
|
|
|
signo = uart_check_special(dev, &ch, 1);
|
2021-12-15 06:40:45 +01:00
|
|
|
#endif
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2019-11-06 02:39:36 +01:00
|
|
|
/* If the RX buffer becomes full, then the serial data is discarded.
|
|
|
|
* This is necessary because on most serial hardware, you must read
|
|
|
|
* the data in order to clear the RX interrupt. An option on some
|
|
|
|
* hardware might be to simply disable RX interrupts until the RX
|
|
|
|
* buffer becomes non-FULL. However, that would probably just cause
|
|
|
|
* the overrun to occur in hardware (unless it has some large internal
|
|
|
|
* buffering).
|
2011-07-12 16:29:08 +02:00
|
|
|
*/
|
|
|
|
|
2014-05-08 17:00:33 +02:00
|
|
|
if (!is_full)
|
2011-07-12 16:29:08 +02:00
|
|
|
{
|
|
|
|
/* Add the character to the buffer */
|
2008-11-13 02:02:56 +01:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
rxbuf->buffer[rxbuf->head] = ch;
|
2011-07-12 16:29:08 +02:00
|
|
|
nbytes++;
|
2008-09-24 17:42:13 +02:00
|
|
|
|
2011-07-12 16:29:08 +02:00
|
|
|
/* Increment the head index */
|
2008-11-13 02:02:56 +01:00
|
|
|
|
2014-12-27 15:15:41 +01:00
|
|
|
rxbuf->head = nexthead;
|
|
|
|
if (++nexthead >= rxbuf->size)
|
2011-07-12 16:29:08 +02:00
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
2008-09-24 17:42:13 +02:00
|
|
|
}
|
2008-11-18 00:22:27 +01:00
|
|
|
}
|
2008-09-24 17:42:13 +02:00
|
|
|
|
2015-11-12 21:16:19 +01:00
|
|
|
/* If any bytes were added to the buffer, inform any waiters there is new
|
2011-07-12 16:29:08 +02:00
|
|
|
* incoming data available.
|
2008-11-18 00:22:27 +01:00
|
|
|
*/
|
2008-11-13 02:02:56 +01:00
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
if (nbytes)
|
|
|
|
{
|
|
|
|
uart_datareceived(dev);
|
2008-09-24 17:42:13 +02:00
|
|
|
}
|
2018-08-26 16:49:08 +02: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)
|
2018-09-02 23:36:25 +02:00
|
|
|
/* Send the signal if necessary */
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2018-09-02 23:36:25 +02:00
|
|
|
if (signo != 0)
|
2018-08-26 16:49:08 +02:00
|
|
|
{
|
2020-05-02 16:30:58 +02:00
|
|
|
nxsig_kill(dev->pid, signo);
|
2018-08-26 16:49:08 +02:00
|
|
|
uart_reset_sem(dev);
|
|
|
|
}
|
|
|
|
#endif
|
2008-09-24 17:42:13 +02:00
|
|
|
}
|