diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c index 7c1c57aa28..2ddafcb53c 100644 --- a/drivers/serial/serial.c +++ b/drivers/serial/serial.c @@ -431,8 +431,7 @@ static int uart_tcdrain(FAR uart_dev_t *dev, clock_t timeout) if (dev->disconnected) { - dev->xmit.head = 0; /* Drop the buffered TX data */ - dev->xmit.tail = 0; + dev->xmit.tail = dev->xmit.head; /* Drop the buffered TX data */ ret = -ENOTCONN; } else @@ -908,17 +907,11 @@ static ssize_t uart_read(FAR struct file *filep, FAR char *buffer, size_t buflen flags = enter_critical_section(); #ifdef CONFIG_SERIAL_DMA - /* If RX buffer is empty move tail and head to zero position */ - - if (rxbuf->head == rxbuf->tail) - { - rxbuf->head = rxbuf->tail = 0; - } - /* Notify DMA that there is free space in the RX buffer */ uart_dmarxfree(dev); #endif + /* Re-enable UART Rx interrupts */ uart_enablerxint(dev); @@ -992,25 +985,16 @@ static ssize_t uart_read(FAR struct file *filep, FAR char *buffer, size_t buflen } #ifdef CONFIG_SERIAL_DMA - flags = enter_critical_section(); - - /* If RX buffer is empty move tail and head to zero position */ - - if (rxbuf->head == rxbuf->tail) - { - rxbuf->head = rxbuf->tail = 0; - } - - leave_critical_section(flags); - /* Notify DMA that there is free space in the RX buffer */ + flags = enter_critical_section(); uart_dmarxfree(dev); + leave_critical_section(flags); +#endif /* RX interrupt could be disabled by RX buffer overflow. Enable it now. */ uart_enablerxint(dev); -#endif #ifdef CONFIG_SERIAL_IFLOWCONTROL #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS @@ -1340,8 +1324,7 @@ static int uart_ioctl(FAR struct file *filep, int cmd, unsigned long arg) if (arg == TCIFLUSH || arg == TCIOFLUSH) { - dev->recv.head = 0; - dev->recv.tail = 0; + dev->recv.tail = dev->recv.head; #ifdef CONFIG_SERIAL_IFLOWCONTROL /* De-activate RX flow control. */ @@ -1352,8 +1335,7 @@ static int uart_ioctl(FAR struct file *filep, int cmd, unsigned long arg) if (arg == TCOFLUSH || arg == TCIOFLUSH) { - dev->xmit.head = 0; - dev->xmit.tail = 0; + dev->xmit.tail = dev->xmit.head; /* Inform any waiters there there is space available. */ diff --git a/drivers/serial/serial_dma.c b/drivers/serial/serial_dma.c index 1238182bb8..17d6f6a56a 100644 --- a/drivers/serial/serial_dma.c +++ b/drivers/serial/serial_dma.c @@ -199,8 +199,21 @@ void uart_recvchars_dma(FAR uart_dev_t *dev) unsigned int watermark; #endif bool is_full; - int nexthead = rxbuf->head + 1; + int nexthead; + /* If RX buffer is empty move tail and head to zero position */ + + if (rxbuf->head == rxbuf->tail) + { + rxbuf->head = 0; + rxbuf->head = 0; + } + + /* Get the next head index and check if there is room to adding another + * byte to the buffer. + */ + + nexthead = rxbuf->head + 1; if (nexthead >= rxbuf->size) { nexthead = 0;