-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[component][drivers][serial_v2] 优化serial_v2 #10603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…部的时间绕回判断,写满丢弃策略rx接收数据错乱修复,clang-tidy和cppcheck审查优化
📌 Code Review Assignment🏷️ Tag: bsp_stm32Reviewers: Liang1795 hamburger-os wdfk-prog Changed Files (Click to expand)
🏷️ Tag: componentsReviewers: Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_driver_serial_v2Reviewers: Ryan-CW-Code Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2025-08-14 11:23 CST)
📝 Review Instructions
|
|
rt-thread/components/drivers/serial/dev_serial_v2.c Lines 390 to 391 in cb5bacf
rt-thread/components/drivers/serial/dev_serial_v2.c Lines 579 to 582 in cb5bacf
rt-thread/components/drivers/serial/dev_serial_v2.c Lines 648 to 651 in cb5bacf
rt-thread/components/drivers/serial/dev_serial_v2.c Lines 602 to 607 in cb5bacf
rt-thread/components/drivers/serial/dev_serial_v2.c Lines 1193 to 1195 in cb5bacf
rt-thread/components/drivers/serial/dev_serial_v2.c Lines 1213 to 1229 in cb5bacf
|
|
返回值这里我还是按规范统一返回接收或者读取的数据大小吧,不再返回 |
|
中断下19个测试例程,dma下18个测试例程,都跑过了 |
There was a problem hiding this 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; |
Copilot
AI
Aug 13, 2025
There was a problem hiding this comment.
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.
| struct rt_serial_device *serial; |
| { | ||
| getc_element = serial->ops->getc(serial); | ||
| if (getc_element == -1) break; | ||
| if (getc_element < 0) break; |
Copilot
AI
Aug 13, 2025
There was a problem hiding this comment.
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'.
| if (getc_element < 0) break; | |
| if (getc_element == -1) break; |
There was a problem hiding this comment.
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; |
Copilot
AI
Aug 13, 2025
There was a problem hiding this comment.
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.
| 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; | |
| } |
| struct rt_serial_device *serial; | ||
| struct rt_serial_tx_fifo *tx_fifo; | ||
| rt_uint8_t *put_ptr; | ||
| rt_uint8_t *put_ptr = RT_NULL; |
Copilot
AI
Aug 13, 2025
There was a problem hiding this comment.
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.
| rt_uint8_t *put_ptr = RT_NULL; | |
| rt_uint8_t *put_ptr; |
There was a problem hiding this comment.
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 { |
Copilot
AI
Aug 13, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
Copilot
AI
Aug 13, 2025
There was a problem hiding this comment.
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.
| return 0; | |
| { | |
| LOG_E("HAL_UART_Transmit_DMA failed in stm32_transmit"); | |
| return -1; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stm32_transmit的返回值是传输成功的字节数,不需要负数进行判断
There was a problem hiding this comment.
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++) |
Copilot
AI
Aug 13, 2025
There was a problem hiding this comment.
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.
| 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++) |
wdfk-prog
left a comment
There was a problem hiding this 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; |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 这种while循环没有超时退出机制的总是有点担心.
- 虽然也没啥就是了,锦上添花罢了
There was a problem hiding this comment.
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标志。
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 这个control没有做返回值判断
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是关闭设备,就算失败了也需要继续执行清理操作。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
获取control失败后没有什么好的处理措施
|
跟改完后,dma和中断的utest都跑过 |
| size, | ||
| RT_SERIAL_TX_BLOCKING); | ||
| if (send_size == 0) | ||
| if (send_size <= 0) |
There was a problem hiding this comment.
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的
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里还必须返回send_size因为是循环调用的嘛
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
你的解决方案是什么 (what is your solution)
请提供验证的bsp和config (provide the config and bsp)
DMA模式写满丢弃和写满覆盖和中断方式都跑过utest

]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up