Skip to content

Conversation

@wdfk-prog
Copy link
Contributor

@wdfk-prog wdfk-prog commented Aug 9, 2025

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.

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

https://club.rt-thread.org/ask/question/381d6f99ad1e8dbe.html

#10584

你的解决方案是什么 (what is your solution)

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/workflows/bsp_buildings.yml 详细请参考链接BSP自查

… 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.
@github-actions
Copy link

github-actions bot commented Aug 9, 2025

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/drivers/serial/dev_serial.c

📊 Current Review Status (Last Updated: 2025-08-09 09:50 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@wdfk-prog wdfk-prog changed the title [driver][serial] V1]: fix correct data loss logic when RX ring buffer is full [driver][serial V1]: fix correct data loss logic when RX ring buffer is full Aug 9, 2025
@Rbb666 Rbb666 requested a review from Copilot August 10, 2025 03:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request fixes a bug in the serial driver's receive interrupt handling where the ring buffer logic was incorrectly discarding data and reducing usable buffer capacity. The fix changes from incrementing get_index to setting it equal to put_index when the buffer is full, implementing proper overwriting ring buffer behavior.

  • Corrects data loss logic in the RX ring buffer when full
  • Ensures full buffer capacity utilization (N bytes instead of N-1)
  • Implements standard overwriting ring buffer strategy

if (rx_fifo->put_index == rx_fifo->get_index)
{
rx_fifo->get_index += 1;
rx_fifo->get_index = rx_fifo->put_index;
Copy link

Copilot AI Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting get_index equal to put_index when the buffer is full creates an empty buffer state (get_index == put_index typically indicates empty). This contradicts the is_full flag being set to RT_TRUE on the next line and may cause inconsistent buffer state interpretation elsewhere in the code.

Suggested change
rx_fifo->get_index = rx_fifo->put_index;
rx_fifo->get_index = (rx_fifo->get_index + 1) % serial->config.bufsz;

Copilot uses AI. Check for mistakes.
@Rbb666 Rbb666 merged commit 3023707 into RT-Thread:master Aug 10, 2025
61 checks passed
@wdfk-prog wdfk-prog deleted the serail branch August 18, 2025 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants