From cc1552284d3c25b2414a2c496e59e7f784fb931e Mon Sep 17 00:00:00 2001 From: wdfk-prog <1425075683@qq.com> Date: Sat, 9 Aug 2025 09:48:31 +0800 Subject: [PATCH] [driver][serial] V1]: fix correct data loss logic when RX ring buffer is full In the serial ISR (`rt_hw_serial_isr`), the previous logic for handling a full RX FIFO was flawed. When the buffer was filled, it would increment `get_index` (`get_index += 1`). This had two negative consequences: 1. It effectively discarded the oldest byte of data prematurely. 2. It reduced the usable capacity of a buffer of size N to N-1. For example, a 64-byte buffer could only ever hold 63 readable bytes after becoming full. This patch corrects the behavior by implementing a standard overwriting ring buffer strategy. When the buffer is full, the logic is changed to `get_index = put_index`. This ensures that: - When new data arrives, it correctly overwrites the oldest data. - The `get_index` is advanced along with the `put_index`, correctly marking the new start of the buffer. - The full N-byte capacity of the buffer is utilized, always storing the N most recent bytes. This change resolves the unexpected data loss and makes the buffer behavior correct and predictable. --- components/drivers/serial/dev_serial.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/drivers/serial/dev_serial.c b/components/drivers/serial/dev_serial.c index 458a5b736ce..eb7c5275ec0 100644 --- a/components/drivers/serial/dev_serial.c +++ b/components/drivers/serial/dev_serial.c @@ -1463,9 +1463,8 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event) /* if the next position is read index, discard this 'read char' */ if (rx_fifo->put_index == rx_fifo->get_index) { - rx_fifo->get_index += 1; + rx_fifo->get_index = rx_fifo->put_index; rx_fifo->is_full = RT_TRUE; - if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0; _serial_check_buffer_size(); }