mirror of
https://github.com/apache/nuttx.git
synced 2026-09-11 21:20:10 +00:00
serial: Added APIs for receiving and sending multiple chars
Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
This commit is contained in:
parent
a9b8681a9f
commit
b319c27f03
3 changed files with 152 additions and 67 deletions
|
|
@ -294,13 +294,32 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
|
||||||
* Name: uart_putc
|
* Name: uart_putc
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static inline void uart_putc(FAR uart_dev_t *dev, int ch)
|
static inline void uart_putchars(FAR uart_dev_t *dev,
|
||||||
|
FAR const void *buf, size_t len)
|
||||||
{
|
{
|
||||||
while (!uart_txready(dev))
|
FAR const char *pbuf = buf;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
uart_send(dev, ch);
|
while (len > 0)
|
||||||
|
{
|
||||||
|
while (!uart_txready(dev))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dev->ops->sendbuf)
|
||||||
|
{
|
||||||
|
ssize_t ret = uart_sendbuf(dev, pbuf, len);
|
||||||
|
if (ret > 0)
|
||||||
|
{
|
||||||
|
pbuf += ret;
|
||||||
|
len -= ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uart_send(dev, *pbuf++);
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
@ -311,39 +330,41 @@ static inline ssize_t uart_irqwrite(FAR uart_dev_t *dev,
|
||||||
FAR const char *buffer,
|
FAR const char *buffer,
|
||||||
size_t buflen)
|
size_t buflen)
|
||||||
{
|
{
|
||||||
ssize_t ret = buflen;
|
size_t tail = 0;
|
||||||
|
size_t head = buflen;
|
||||||
|
|
||||||
/* Force each character through the low level interface */
|
/* Do output post-processing */
|
||||||
|
|
||||||
for (; buflen; buflen--)
|
if ((dev->tc_oflag & OPOST) != 0)
|
||||||
{
|
{
|
||||||
int ch = *buffer++;
|
for (head = 0; head < buflen; head++)
|
||||||
|
|
||||||
/* Do output post-processing */
|
|
||||||
|
|
||||||
if ((dev->tc_oflag & OPOST) != 0)
|
|
||||||
{
|
{
|
||||||
|
int ch = buffer[head];
|
||||||
|
|
||||||
/* Mapping CR to NL? */
|
/* Mapping CR to NL? */
|
||||||
|
|
||||||
if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
|
if ((ch == '\r') && (dev->tc_oflag & OCRNL) != 0)
|
||||||
{
|
{
|
||||||
ch = '\n';
|
uart_putchars(dev, &buffer[tail], head - tail);
|
||||||
|
uart_putchars(dev, "\n", 1);
|
||||||
|
tail = head + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Are we interested in newline processing? */
|
/* Are we interested in newline processing? */
|
||||||
|
|
||||||
if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
|
if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET)) != 0)
|
||||||
{
|
{
|
||||||
uart_putc(dev, '\r');
|
uart_putchars(dev, &buffer[tail], head - tail);
|
||||||
|
uart_putchars(dev, "\r", 1);
|
||||||
|
tail = head;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output the character, using the low-level direct UART interfaces */
|
|
||||||
|
|
||||||
uart_putc(dev, ch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
/* Output the character, using the low-level direct UART interfaces */
|
||||||
|
|
||||||
|
uart_putchars(dev, &buffer[tail], head - tail);
|
||||||
|
return buflen;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
|
||||||
|
|
@ -66,12 +66,37 @@ void uart_xmitchars(FAR uart_dev_t *dev)
|
||||||
{
|
{
|
||||||
/* Send the next byte */
|
/* Send the next byte */
|
||||||
|
|
||||||
uart_send(dev, dev->xmit.buffer[dev->xmit.tail]);
|
if (dev->ops->sendbuf)
|
||||||
nbytes++;
|
{
|
||||||
|
ssize_t sent;
|
||||||
|
|
||||||
|
if (dev->xmit.tail < dev->xmit.head)
|
||||||
|
{
|
||||||
|
sent = dev->xmit.head - dev->xmit.tail;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sent = dev->xmit.size - dev->xmit.tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
sent = uart_sendbuf(dev,
|
||||||
|
&dev->xmit.buffer[dev->xmit.tail],
|
||||||
|
sent);
|
||||||
|
if (sent > 0)
|
||||||
|
{
|
||||||
|
dev->xmit.tail += sent;
|
||||||
|
nbytes += sent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uart_send(dev, dev->xmit.buffer[dev->xmit.tail++]);
|
||||||
|
nbytes++;
|
||||||
|
}
|
||||||
|
|
||||||
/* Increment the tail index */
|
/* Increment the tail index */
|
||||||
|
|
||||||
if (++(dev->xmit.tail) >= dev->xmit.size)
|
if (dev->xmit.tail >= dev->xmit.size)
|
||||||
{
|
{
|
||||||
dev->xmit.tail = 0;
|
dev->xmit.tail = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -120,38 +145,28 @@ void uart_recvchars(FAR uart_dev_t *dev)
|
||||||
{
|
{
|
||||||
FAR struct uart_buffer_s *rxbuf = &dev->recv;
|
FAR struct uart_buffer_s *rxbuf = &dev->recv;
|
||||||
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
||||||
unsigned int watermark;
|
/* Pre-calculate the watermark level that we will need to test against. */
|
||||||
|
|
||||||
|
unsigned int watermark =
|
||||||
|
(CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK * rxbuf->size) / 100;
|
||||||
#endif
|
#endif
|
||||||
unsigned int status;
|
|
||||||
int nexthead = rxbuf->head + 1;
|
|
||||||
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP) || \
|
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP) || \
|
||||||
defined(CONFIG_TTY_FORCE_PANIC) || defined(CONFIG_TTY_LAUNCH)
|
defined(CONFIG_TTY_FORCE_PANIC) || defined(CONFIG_TTY_LAUNCH)
|
||||||
int signo = 0;
|
int signo = 0;
|
||||||
#endif
|
#endif
|
||||||
uint16_t nbytes = 0;
|
uint16_t nbytes = 0;
|
||||||
|
|
||||||
if (nexthead >= rxbuf->size)
|
|
||||||
{
|
|
||||||
nexthead = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
|
||||||
/* Pre-calculate the watermark level that we will need to test against. */
|
|
||||||
|
|
||||||
watermark = (CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK * rxbuf->size) /
|
|
||||||
100;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Loop putting characters into the receive buffer until there are no
|
/* Loop putting characters into the receive buffer until there are no
|
||||||
* further characters to available.
|
* further characters to available.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
while (uart_rxavailable(dev))
|
while (uart_rxavailable(dev))
|
||||||
{
|
{
|
||||||
|
int nexthead = rxbuf->head + 1 < rxbuf->size ? rxbuf->head + 1 : 0;
|
||||||
bool is_full = (nexthead == rxbuf->tail);
|
bool is_full = (nexthead == rxbuf->tail);
|
||||||
|
FAR char *pbuf;
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
||||||
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
|
||||||
unsigned int nbuffered;
|
unsigned int nbuffered;
|
||||||
|
|
||||||
|
|
@ -181,7 +196,7 @@ void uart_recvchars(FAR uart_dev_t *dev)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#elif defined(CONFIG_SERIAL_IFLOWCONTROL)
|
||||||
/* Check if RX buffer is full and allow serial low-level driver to
|
/* Check if RX buffer is full and allow serial low-level driver to
|
||||||
* pause processing. This allows proper utilization of hardware flow
|
* pause processing. This allows proper utilization of hardware flow
|
||||||
* control.
|
* control.
|
||||||
|
|
@ -196,41 +211,74 @@ void uart_recvchars(FAR uart_dev_t *dev)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Get this next character from the hardware */
|
/* Get this next character from the hardware */
|
||||||
|
|
||||||
ch = uart_receive(dev, &status);
|
if (!is_full && dev->ops->recvbuf)
|
||||||
|
{
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
if (rxbuf->tail > rxbuf->head)
|
||||||
|
{
|
||||||
|
nbytes = rxbuf->tail - rxbuf->head - 1;
|
||||||
|
}
|
||||||
|
else if (rxbuf->tail)
|
||||||
|
{
|
||||||
|
nbytes = rxbuf->size - rxbuf->head;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nbytes = rxbuf->size - rxbuf->head - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pbuf = &rxbuf->buffer[rxbuf->head];
|
||||||
|
ret = uart_recvbuf(dev, pbuf, nbytes);
|
||||||
|
if (ret <= 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
nbytes = ret;
|
||||||
|
rxbuf->head += nbytes;
|
||||||
|
if (rxbuf->head >= rxbuf->size)
|
||||||
|
{
|
||||||
|
rxbuf->head = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsigned int status;
|
||||||
|
|
||||||
|
ch = uart_receive(dev, &status);
|
||||||
|
pbuf = &ch;
|
||||||
|
nbytes = 1;
|
||||||
|
|
||||||
|
/* 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).
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!is_full)
|
||||||
|
{
|
||||||
|
/* Add the character to the buffer */
|
||||||
|
|
||||||
|
rxbuf->buffer[rxbuf->head] = ch;
|
||||||
|
|
||||||
|
/* Increment the head index */
|
||||||
|
|
||||||
|
rxbuf->head = nexthead;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP) || \
|
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP) || \
|
||||||
defined(CONFIG_TTY_FORCE_PANIC) || defined(CONFIG_TTY_LAUNCH)
|
defined(CONFIG_TTY_FORCE_PANIC) || defined(CONFIG_TTY_LAUNCH)
|
||||||
signo = uart_check_special(dev, &ch, 1);
|
signo = uart_check_special(dev, pbuf, nbytes);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* 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).
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (!is_full)
|
|
||||||
{
|
|
||||||
/* Add the character to the buffer */
|
|
||||||
|
|
||||||
rxbuf->buffer[rxbuf->head] = ch;
|
|
||||||
|
|
||||||
/* Increment the head index */
|
|
||||||
|
|
||||||
rxbuf->head = nexthead;
|
|
||||||
if (++nexthead >= rxbuf->size)
|
|
||||||
{
|
|
||||||
nexthead = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If any bytes were added to the buffer, inform any waiters there is new
|
/* If any bytes were added to the buffer, inform any waiters there is new
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,8 @@
|
||||||
#define uart_txempty(dev) dev->ops->txempty(dev)
|
#define uart_txempty(dev) dev->ops->txempty(dev)
|
||||||
#define uart_send(dev,ch) dev->ops->send(dev,ch)
|
#define uart_send(dev,ch) dev->ops->send(dev,ch)
|
||||||
#define uart_receive(dev,s) dev->ops->receive(dev,s)
|
#define uart_receive(dev,s) dev->ops->receive(dev,s)
|
||||||
|
#define uart_recvbuf(dev,b,l) dev->ops->recvbuf(dev,b,l)
|
||||||
|
#define uart_sendbuf(dev,b,l) dev->ops->sendbuf(dev,b,l)
|
||||||
|
|
||||||
#define uart_release(dev) \
|
#define uart_release(dev) \
|
||||||
((dev)->ops->release ? (dev)->ops->release(dev) : -ENOSYS)
|
((dev)->ops->release ? (dev)->ops->release(dev) : -ENOSYS)
|
||||||
|
|
@ -263,6 +265,20 @@ struct uart_ops_s
|
||||||
*/
|
*/
|
||||||
|
|
||||||
CODE int (*release)(FAR struct uart_dev_s *dev);
|
CODE int (*release)(FAR struct uart_dev_s *dev);
|
||||||
|
|
||||||
|
/* Receive multiple bytes.
|
||||||
|
* Returns the actual number of characters received.
|
||||||
|
*/
|
||||||
|
|
||||||
|
CODE ssize_t (*recvbuf)(FAR struct uart_dev_s *dev,
|
||||||
|
FAR void *buf, size_t len);
|
||||||
|
|
||||||
|
/* This method will send multiple bytes.
|
||||||
|
* Returns the actual number of characters sent.
|
||||||
|
*/
|
||||||
|
|
||||||
|
CODE ssize_t (*sendbuf)(FAR struct uart_dev_s *dev,
|
||||||
|
FAR const void *buf, size_t len);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* This is the device structure used by the driver. The caller of
|
/* This is the device structure used by the driver. The caller of
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue