Skip to content

Conversation

@Ryan-CW-Code
Copy link
Contributor

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

[

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

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

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

DMA模式写满丢弃和写满覆盖和中断方式都跑过utest
c8053d614376f0273637154e6eb4813f

]

当前拉取/合并请求的状态 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自查

@github-actions
Copy link

github-actions bot commented Aug 12, 2025

📌 Code Review Assignment

🏷️ Tag: bsp_stm32

Reviewers: Liang1795 hamburger-os wdfk-prog

Changed Files (Click to expand)
  • bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart_v2.c

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/drivers/include/drivers/dev_serial_v2.h
  • components/drivers/serial/dev_serial_v2.c

🏷️ Tag: components_driver_serial_v2

Reviewers: Ryan-CW-Code

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

📊 Current Review Status (Last Updated: 2025-08-14 11:23 CST)

  • Liang1795 Pending Review
  • Maihuanyi Pending Review
  • Ryan-CW-Code Pending Review
  • hamburger-os Pending Review
  • wdfk-prog Pending Review

📝 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
Copy link
Contributor

wdfk-prog commented Aug 12, 2025

getc_element = serial->ops->getc(serial);
if (getc_element == -1) break;

  • 这里不止-1要break吧?负数都得break;rt_ssize_t _serial_poll_rx函数返回值可以负值,可以将错误码也返回

serial->ops->putc(serial, *putc_buffer);

  • 这里应该也一样,可以判断返回值

if (rt_atomic_flag_test_and_set(&tx_fifo->activated))
{
return 0;
}

  • 这里可以返回负值错误码

if (rt_atomic_flag_test_and_set(&tx_fifo->activated))
{
return 0;
}

  • 这里可以返回负值错误码

if (ret == -RT_ETIMEOUT)
{
return ret;
}
return 0;

  • 这里直接返回ret既可吧

serial->ops->control(serial,

  • serial->ops->control的调用处都可以有返回值判断

while (serial->ops->getc(serial) != -1)
{
}

  • 这里没有超时退出机制,可以考虑加一个

static void _serial_tx_flush(struct rt_serial_device *serial)
{
struct rt_serial_tx_fifo *tx_fifo;
RT_ASSERT(serial != RT_NULL);
if (serial->config.tx_bufsz != 0)
{
tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
RT_ASSERT(tx_fifo != RT_NULL);
if (rt_atomic_load(&tx_fifo->activated))
{
rt_completion_wait(&tx_fifo->tx_cpt, RT_WAITING_FOREVER);
return;
}
}
}

  • 这个return是否没有必要

@Ryan-CW-Code
Copy link
Contributor Author

返回值这里我还是按规范统一返回接收或者读取的数据大小吧,不再返回-RT_ETIMEOUT,按照文档来实现更合理点。
小细节我再优化下

@Ryan-CW-Code
Copy link
Contributor Author

中断下19个测试例程,dma下18个测试例程,都跑过了

@Rbb666 Rbb666 requested a review from Copilot August 13, 2025 01:58
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 PR optimizes the serial_v2 driver component by adding enhanced timeout handling, POSIX mode support, and improving error handling mechanisms. The changes focus on making the serial driver more robust and compliant with POSIX standards.

  • Enhanced timeout handling for both RX and TX operations with proper timeout calculation
  • Added POSIX mode support for better compatibility with standard I/O operations
  • Improved error handling and return value validation across the driver

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.

File Description
components/drivers/serial/dev_serial_v2.c Core driver optimizations including timeout handling, POSIX mode support, and error handling improvements
components/drivers/include/drivers/dev_serial_v2.h Added new control command and POSIX mode flag to device structure
bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart_v2.c STM32-specific driver improvements with better interrupt handling and error checking
examples/utest/testcases/drivers/serial_v2/*.c Updated test cases with improved validation and reduced delay times for faster testing

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

rt_err_t ret = 0;
rt_uint16_t flags = 0;
rt_device_t device;
struct rt_serial_device *serial;
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

[nitpick] Variable 'serial' is declared but only used in a conditional block much later. Consider declaring it closer to its usage or within the conditional block to improve code readability.

Suggested change
struct rt_serial_device *serial;

Copilot uses AI. Check for mistakes.
{
getc_element = serial->ops->getc(serial);
if (getc_element == -1) break;
if (getc_element < 0) break;
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

Changed condition from '== -1' to '< 0', but this may break compatibility with drivers that return specific negative error codes other than -1. The original check for -1 was likely intentional as it's a common convention for 'no data available'.

Suggested change
if (getc_element < 0) break;
if (getc_element == -1) break;

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

实际串口的数据是uint8_t类型,getc和putc的返回值是int类型,所以这里判断是否为负数是可行的

}
serial->ops->putc(serial, *putc_buffer);
putc_result = serial->ops->putc(serial, *putc_buffer);
if (putc_result < 0) break;
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

The putc operation break on error may leave the transmission incomplete. Consider logging the error or implementing proper error handling to inform the caller about partial transmission.

Suggested change
if (putc_result < 0) break;
if (putc_result < 0)
{
LOG_E("Serial putc failed at buffer offset %d, error code: %d", (int)(putc_buffer - (rt_uint8_t *)buffer), putc_result);
break;
}

Copilot uses AI. Check for mistakes.
struct rt_serial_device *serial;
struct rt_serial_tx_fifo *tx_fifo;
rt_uint8_t *put_ptr;
rt_uint8_t *put_ptr = RT_NULL;
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

[nitpick] Initializing 'put_ptr' to RT_NULL is unnecessary since it's assigned immediately after declaration in the function logic.

Suggested change
rt_uint8_t *put_ptr = RT_NULL;
rt_uint8_t *put_ptr;

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是为了抑制clang-tidy的[clang-analyzer-core.CallAndMessage]的警告

char chr = UART_GET_RDR(&uart->handle, stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity));
rt_hw_serial_control_isr(serial, RT_HW_SERIAL_CTRL_PUTC, &chr);
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
do {
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

[nitpick] The do-while loop structure for reading RXNE flag should have proper spacing. Consider adding a space after 'do' for consistency with coding standards.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是误判吗? 检查了代码格式是带空格的,使用clang-format格式化

{
HAL_UART_Transmit_DMA(&uart->handle, buf, size);
if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) != HAL_OK)
return 0;
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

When HAL_UART_Transmit_DMA fails, returning 0 may not properly indicate the error condition to the caller. Consider returning a negative error code or logging the failure reason.

Suggested change
return 0;
{
LOG_E("HAL_UART_Transmit_DMA failed in stm32_transmit");
return -1;
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

stm32_transmit的返回值是传输成功的字节数,不需要负数进行判断

Copy link
Contributor

Choose a reason for hiding this comment

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

stm32_transmit的返回值是传输成功的字节数,不需要负数进行判断

  • rt_ssize_t类型支持返回负数.正数就是返回长度,负数就是错误码.rt_ssize_t的定义就是这么用的.最好可以都将rt_ssize_t的负值利用起来^_^

rt_int32_t tx_timeout = 1;
rt_int32_t tx_timeout = 10 * 1000;
uart_write_buffer = (rt_uint8_t *)rt_malloc(sizeof(rt_uint8_t) * (RT_SERIAL_TC_TXBUF_SIZE * 5 + 10));
for (rt_uint32_t count = 0; count < sizeof(rt_uint8_t) * (RT_SERIAL_TC_TXBUF_SIZE * 5 + 10); count++)
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

Similar to uart_flush_txnb.c, this loop uses 'RT_SERIAL_TC_TXBUF_SIZE' but the buffer allocation uses 'RT_SERIAL_TC_TXBUF_SIZE * 5 + 10'. The sizeof(rt_uint8_t) multiplication is redundant since rt_uint8_t is always 1 byte.

Suggested change
for (rt_uint32_t count = 0; count < sizeof(rt_uint8_t) * (RT_SERIAL_TC_TXBUF_SIZE * 5 + 10); count++)
uart_write_buffer = (rt_uint8_t *)rt_malloc(RT_SERIAL_TC_TXBUF_SIZE * 5 + 10);
for (rt_uint32_t count = 0; count < (RT_SERIAL_TC_TXBUF_SIZE * 5 + 10); count++)

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@wdfk-prog wdfk-prog left a comment

Choose a reason for hiding this comment

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

LGTM

{
HAL_UART_Transmit_DMA(&uart->handle, buf, size);
if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) != HAL_OK)
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

stm32_transmit的返回值是传输成功的字节数,不需要负数进行判断

  • rt_ssize_t类型支持返回负数.正数就是返回长度,负数就是错误码.rt_ssize_t的定义就是这么用的.最好可以都将rt_ssize_t的负值利用起来^_^

char chr = UART_GET_RDR(&uart->handle, stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity));
rt_hw_serial_control_isr(serial, RT_HW_SERIAL_CTRL_PUTC, (void *)&chr);
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
} while (__HAL_UART_GET_FLAG(&uart->handle, UART_FLAG_RXNE));
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 这种while循环没有超时退出机制的总是有点担心.
  • 虽然也没啥就是了,锦上添花罢了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

认知中这里应该不会有问题,UART_GET_RDR会清除UART_FLAG_RXNE标志。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里还是增加一个超时吧,最多处理1024字节就强制退出

rt_memset(&serial->rx_notify, RT_NULL, sizeof(struct rt_device_notify));

/* Call the control() API to close the serial device. disable ignore return value */
serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 这个control没有做返回值判断

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是关闭设备,就算失败了也需要继续执行清理操作。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

获取control失败后没有什么好的处理措施

@Ryan-CW-Code
Copy link
Contributor Author

跟改完后,dma和中断的utest都跑过

size,
RT_SERIAL_TX_BLOCKING);
if (send_size == 0)
if (send_size <= 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 这里应该返回send_size既可?函数返回值类型也是rt_ssize_t的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rt_device_read和write函数返回实际读或者写的字节数。 不返回错误值吧

RT_SERIAL_TX_BLOCKING);
if (transmit_size <= 0)
{
return send_size;
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 这里应该返回transmit_size既可?函数返回值类型也是rt_ssize_t

Copy link
Contributor Author

Choose a reason for hiding this comment

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

同上

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里还必须返回send_size因为是循环调用的嘛

@Rbb666 Rbb666 merged commit e0243e8 into RT-Thread:master Aug 16, 2025
63 checks passed
@Ryan-CW-Code Ryan-CW-Code deleted the serial_v2 branch August 20, 2025 02:39
@Rbb666 Rbb666 added this to the v5.2.2 milestone Sep 23, 2025
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.

3 participants