[opt]add i2c config in stm32 lib
Resolves #10635
拉取/合并请求描述:(PR description)
[ 本PR增强了STM32硬件I2C驱动(drv_hard_i2c),支持在运行时通过控制接口动态调整I2C速率,并在开启DMA的情况下安全地重建DMA与中断配置,避免通信异常。同时清理了无效/未实现的占空比设置项,统一英文注释,并将控制宏移动到头文件,整体风格与现有代码保持一致。
为什么提交这份PR (why to submit this PR)
- 现有驱动在初始化时固定速率,运行期无法调整;
你的解决方案是什么 (what is your solution)
- 新增运行时速率设置路径:通过
i2c_bus->ops->i2c_bus_control(bus, I2C_CTRL_SET_SPEED, &speed)调整速率。 - 在
HAL_I2C_Init之后,若启用DMA,按系统启动流程完整重建 DMA RX/TX 句柄、__HAL_LINKDMA、DMA NVIC 以及 I2C 事件中断 NVIC,保证DMA场景稳定。 - 将
I2C_CTRL_SET_SPEED宏迁移至头文件drv_hard_i2c.h,便于外部调用与统一管理。 - 将新增、修改处的注释改为英文,并按文件原有风格格式化。
主要变更点:
- drv_hard_i2c.h
- 新增宏:
#define I2C_CTRL_SET_SPEED (0x01U)
- 新增宏:
- drv_hard_i2c.c
- 新增函数:
stm32_i2c_set_speed()(含DMA/IRQ重配置) - 控制接口:
stm32_i2c_control()仅支持I2C_CTRL_SET_SPEED - 删除占空比控制分支与相关注释(未实现路径)
- 注释英文化、代码风格统一
- 新增函数:
请提供验证的bsp和config (provide the config and bsp)
-
BSP:
- [请填写实际验证BSP路径,如] bsp/stm32/stm32f411-weact 或 bsp/stm32/stm32f429-st-discovery
-
.config:
- CONFIG_BSP_USING_I2C=y
- CONFIG_BSP_USING_HARD_I2C1=y
- CONFIG_BSP_I2C1_SPEED=400000
- [如启用DMA则] CONFIG_BSP_I2C1_RX_USING_DMA=y、CONFIG_BSP_I2C1_TX_USING_DMA=y
-
action:
- [请填写你仓库该PR分支的CI编译链接] ]
当前拉取/合并请求的状态 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:
- [x] 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
- [x] 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
- [x] 没有垃圾代码,代码尽量精简,不包含
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up - [x] 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
- [x] 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
- [x] 代码是高质量的 Code in this PR is of high quality
- [x] 已经使用formatting等工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
- [ ] 如果是新增bsp, 已经添加ci检查到 .github/ALL_BSP_COMPILE.json
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.
📌 Code Review Assignment
🏷️ Tag: bsp_stm32
Reviewers: Liang1795 hamburger-os wdfk-prog
Changed Files (Click to expand)
- bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c
- bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.h
📊 Current Review Status (Last Updated: 2025-08-25 18:28 CST)
- ⌛ Liang1795 Pending Review
- ⌛ hamburger-os Pending Review
- ⌛ wdfk-prog Pending Review
📝 Review Instructions
-
维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态 Maintainers can refresh the review status by clicking here: 🔄 Refresh Status
-
确认审核通过后评论
LGTM/lgtmCommentLGTM/lgtmafter confirming approval -
PR合并前需至少一位维护者确认 PR must be confirmed by at least one maintainer before merging
ℹ️ 刷新CI状态操作需要具备仓库写入权限。 ℹ️ Refresh CI status operation requires repository Write permission.
https://github.com/RT-Thread/rt-thread/blob/fabe5646ab94d1b104d12ec27b8767232f53eab7/bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c#L203
- 这里可以加一个断言判断是否空指针
https://github.com/RT-Thread/rt-thread/blob/fabe5646ab94d1b104d12ec27b8767232f53eab7/bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c#L367
https://github.com/RT-Thread/rt-thread/blob/fabe5646ab94d1b104d12ec27b8767232f53eab7/bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c#L392
- 这个格式好像没对齐,可以帮忙对齐一下