Skip to content

Conversation

@lhxj
Copy link
Contributor

@lhxj lhxj commented Dec 25, 2025

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

[

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

Context: 此问题是在 Spacemit K1 BSP 精简版上发现的。由于在该版本中存在更频繁或特定的设备树(OFW)节点枚举逻辑,触发了跨总线冲突。

Problem:
在枚举设备树节点时,平台总线(platform bus)和原生总线(I2C/SPI)可能会为同一个 OFW 节点创建重复的设备。这会导致在 rt_bus_reload_driver_device() 中触发断言失败 (dev->bus != new_bus)。

Root Cause:
平台总线尝试通过调用 rt_bus_reload_driver_device(dev->bus, dev) 来重新加载已经属于其他总线的设备,这违反了 API 约束。

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

Solution:
本次修改的核心在于 rt_platform_ofw_request。现在,当检测到节点已经绑定了设备(np->dev 已存在)时,直接返回 RT_EOK。这一改动直接从路径上阻断了平台总线对已占用节点的再次注册尝试,从而杜绝了跨总线重复创建设备的问题。仅凭此项平台侧的改动即可消除断言失败。

  • components/drivers/core/platform_ofw.c (核心修改): 当 np->dev 存在时直接返回 RT_EOK。这确保了如果原生总线(如 I2C/SPI)已经接管了该节点,平台总线将不再干预,让原生总线负责设备生命周期。
  • components/drivers/i2c/dev_i2c_bus.c & components/drivers/spi/dev_spi_bus.c (辅助修改): 在扫描阶段为 np->dev 赋值。这属于规范化操作,虽无害但不是消除断言的充分条件。

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

修改前运行结果:
Pasted image 20251229101108
修改后运行结果:
Pasted image 20251229100422

  • .config:https://github.com/lhxj/rt-thread/blob/spacemit-k1/bsp/spacemit/k1pr/.config
    根据对两个 .config 文件的对比,k1pr(精简版)相比 k1(完整版)的主要改动如下:
  • Add minimal k1pr BSP based on k1 with reduced config
  • Simplified priority levels (32 vs 256)
  • Removed storage subsystems (SDIO/SPI/NVMe/ATA/SCSI)
  • Removed networking stack (lwIP/SAL)
  • Removed graphics/display drivers
  • Removed PCIe subsystem
  • Keep core DM framework (CLK/RESET/PIN/HWTIMER/PMDOMAIN)
  • Keep serial driver stack for console
  • Keep minimal I2C for PMIC communication
  • Binary size reduced ~42% (3.1MB -> 1.8MB estimated)
  • 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/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions
Copy link

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:fix-ofw-bus-conflict
  • 设置PR number为 \ Set the PR number to:11090
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 fix-ofw-bus-conflict 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix-ofw-bus-conflict branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions
Copy link

github-actions bot commented Dec 25, 2025

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/drivers/core/platform_ofw.c
  • components/drivers/i2c/dev_i2c_bus.c
  • components/drivers/spi/dev_spi_bus.c

🏷️ Tag: components_driver_i2c

Reviewers: wdfk-prog

Changed Files (Click to expand)
  • components/drivers/i2c/dev_i2c_bus.c

🏷️ Tag: components_driver_spi

Reviewers: Liang1795 wdfk-prog

Changed Files (Click to expand)
  • components/drivers/spi/dev_spi_bus.c

📊 Current Review Status (Last Updated: 2025-12-25 18:48 CST)

  • Liang1795 Pending Review
  • Maihuanyi 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.

@lhxj lhxj force-pushed the fix-ofw-bus-conflict branch from e3e10fd to ff03e9c Compare December 25, 2025 10:45
Problem:
When enumerating device tree nodes, platform bus and native buses (I2C/SPI)
may create duplicate devices for the same OFW node, causing cross-bus conflicts.
This triggers assertion failure '(dev->bus != new_bus)' in
rt_bus_reload_driver_device() during boot on minimal DM-enabled systems.

Root Cause:
1. Platform bus tries to reload devices that already belong to other buses
   by calling rt_bus_reload_driver_device(dev->bus, dev), which violates
   the API contract (requires dev->bus != new_bus).
2. Native buses (I2C/SPI) do not mark OFW nodes as occupied, so platform
   bus creates duplicate platform devices for I2C/SPI client nodes.

Solution:
1. components/drivers/core/platform_ofw.c: Return RT_EOK when np->dev exists,
   letting the native bus handle device lifecycle instead of cross-bus reload.
2. components/drivers/i2c/dev_i2c_bus.c: Mark i2c_client_np->dev during scan
   to prevent platform bus from duplicating I2C client devices.
3. components/drivers/spi/dev_spi_bus.c: Mark spi_dev_np->dev during scan
   to prevent platform bus from duplicating SPI devices.

Tested on Spacemit K1 RISC-V platform with minimal DM configuration.

Signed-off-by: lhxj <[email protected]>
@lhxj lhxj force-pushed the fix-ofw-bus-conflict branch from ff03e9c to 5b18fe8 Compare December 25, 2025 10:48
@Rbb666
Copy link
Member

Rbb666 commented Dec 27, 2025

@GuEe-GUI 帮忙看看

}
else
{
err = rt_bus_reload_driver_device(dev->bus, dev);
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的目的是为了舍弃之前匹配上的驱动,重新根据设备状态进行驱动匹配,看上去并没有影响 I2C/SPI 呀,如果会发生错误,那应该是调用方的方法不对。

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