Skip to content

Conversation

@wdfk-prog
Copy link
Contributor

@wdfk-prog wdfk-prog commented Oct 30, 2025

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

[

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

  • 当前的 SPI 驱动框架使用 rt_mutex 作为总线和设备的保护锁。由于互斥锁在获取不到时会导致线程挂起,因此它不能在中断服务程序(ISR)或其他禁止调度的上下文中使用。这限制了 SPI 设备(如 SPI Flash)在某些需要高实时性响应(例如,在中断中紧急写入日志或数据)的场景下的应用。

  • 此外,SFUD 驱动中的 retry_delay 函数使用了 rt_thread_delay,该函数同样依赖于线程调度器,无法在中断中调用。

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

  • 为了使 SPI 驱动框架能够安全地在中断上下文中使用,本次提交引入了一套上下文感知的混合锁机制,并对相关代码进行了适应性修改。

主要变更 (Key Changes)

  1. 引入混合锁机制:
  • 通过新增的 spi_lock 和 spi_unlock 辅助函数,封装了锁的获取与释放逻辑。
  • 函数内部通过 rt_scheduler_is_available() 判断当前环境:
    • 线程环境: 继续使用 rt_mutex,允许任务调度。
    • 中断环境: 切换为使用 rt_spin_lock_irqsave,通过禁用中断和忙等来保证资源访问的原子性。
  1. 数据结构更新:
  • 为 rt_spi_bus 和 spi_flash_device 结构体增加了 rt_spinlock 成员,并通过 RT_USING_SPI_ISR 宏进行条件编译。
  1. SFUD 驱动适配:
  • 对 SFUD 驱动中的 spi_lock/spi_unlock 回调进行了同样的改造,使其支持中断调用。
  • 修改了 retry_delay_100us 函数,使其在中断上下文中改用 rt_hw_us_delay 进行硬件忙等延时。
  1. 新增 Kconfig 选项:
  • 增加了 RT_USING_SPI_ISR 选项,用户可以按需开启或关闭此功能。

  • 通过这些修改,SPI 驱动现在是完全的线程安全和中断安全的,极大地扩展了其应用范围。

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

[2025/10/30 15:48:38 253] msh />
[2025/10/30 15:48:38 460] msh />
[2025/10/30 15:48:42 356] msh />cmb
[2025/10/30 15:48:42 358] cmb_test
[2025/10/30 15:48:42 893] msh />cmb_test
[2025/10/30 15:48:42 896] Please input 'cmb_test <DIVBYZERO|UNALIGNED|ASSERT>' 
[2025/10/30 15:48:47 737] msh />cmb_test UNALIGNED
[2025/10/30 15:48:47 739] addr:0x00 value:0x20002A28
[2025/10/30 15:48:47 739] addr:0x04 value:0x08000229
[2025/10/30 15:48:47 739] 01-01 00:00:00 cmb:  
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb: Firmware name: 5axis, hardware version: 1.0, software version: 1.0
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb: Fault on interrupt or bare metal(no OS) environment
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb: ===== Thread stack information =====
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb:   addr: 20003cf4    data: 2000415c
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb:   addr: 20003cf8    data: 00000000
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb:   addr: 20003cfc    data: 08027db9
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb:   addr: 20003d00    data: 08020270
[2025/10/30 15:48:47 925] 01-01 00:00:00 cmb:   addr: 20003d04    data: 01000200
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb:   addr: 20003d08    data: 080554e7
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb: ====================================
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb: =================== Registers information ====================
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb:   R0 : 2000c5d4  R1 : 2000c5f8  R2 : 2000c618  R3 : 080202c3
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb:   R12: fffffffd  LR : 00000000  PC : 10000000  PSR: f0000000
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb: ==============================================================
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb: Usage fault is caused by indicates that an unaligned access fault has taken place
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb: Show more call stack info by run: addr2line -e 5axis.elf -afpiC 10000000 08027db8 080554e6 
[2025/10/30 15:48:47 926] 01-01 00:00:00 cmb: Current system tick: 11837
[2025/10/30 15:48:57 939] rcc_csr = 0x1e000000(Reset flag for system reset through CPU)

[2025/10/30 15:49:04 968] cd f
[2025/10/30 15:49:05 624] msh />cd flash/l
[2025/10/30 15:49:05 847] msh />cd flash/log/
[2025/10/30 15:49:06 453] msh /flash/log>ls
[2025/10/30 15:49:06 469] Directory /flash/log:
[2025/10/30 15:49:06 549] cmb.log              1685                     
[2025/10/30 15:49:06 549] flash_sys.log        14296                    
[2025/10/30 15:49:07 789] msh /flash/log>cat c
[2025/10/30 15:49:08 124] msh /flash/log>cat cmb.log
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb:  
[2025/10/30 15:49:08 309] ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿXÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ01-01 00:00:00 cmb:  
[2025/10/30 15:49:08 309] ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿXÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ01-01 00:00:00 cmb:  
[2025/10/30 15:49:08 309] ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿXÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ01-01 00:00:00 cmb:  
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb: Firmware name: 5axis, hardware version: 1.0, software version: 1.0
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb: Fault on interrupt or bare metal(no OS) environment
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb: ===== Thread stack information =====
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb:   addr: 20003cf4    data: 2000415c
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb:   addr: 20003cf8    data: 00000000
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb:   addr: 20003cfc    data: 08027db9
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb:   addr: 20003d00    data: 08020270
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb:   addr: 20003d04    data: 01000200
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb:   addr: 20003d08    data: 080554e7
[2025/10/30 15:49:08 309] 01-01 00:00:00 cmb: ====================================
[2025/10/30 15:49:08 310] 01-01 00:00:00 cmb: =================== Registers information ====================
[2025/10/30 15:49:08 310] 01-01 00:00:00 cmb:   R0 : 2000c5d4  R1 : 2000c5f8  R2 : 2000c618  R3 : 080202c3
[2025/10/30 15:49:08 310] 01-01 00:00:00 cmb:   R12: fffffffd  LR : 00000000  PC : 10000000  PSR: f0000000
[2025/10/30 15:49:08 310] 01-01 00:00:00 cmb: ==============================================================
[2025/10/30 15:49:08 310] 01-01 00:00:00 cmb: Usage fault is caused by indicates that an unaligned access fault has taken place
[2025/10/30 15:49:08 310] 01-01 00:00:00 cmb: Show more call stack info by run: addr2line -e 5axis.elf -afpiC 10000000 08027db8 080554e6 
[2025/10/30 15:49:08 310] 01-01 00:00:00 cmb: Current system tick: 11837
[2025/10/30 15:49:08 310] 
  • .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/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:spi_interrupt-safe
  • 设置PR number为 \ Set the PR number to:10873
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 spi_interrupt-safe 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the spi_interrupt-safe 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 Oct 30, 2025

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/drivers/include/drivers/dev_spi.h
  • components/drivers/spi/Kconfig
  • components/drivers/spi/dev_spi_core.c
  • components/drivers/spi/dev_spi_flash.h
  • components/drivers/spi/dev_spi_flash_sfud.c

🏷️ Tag: components_driver_spi

Reviewers: Liang1795 wdfk-prog

Changed Files (Click to expand)
  • components/drivers/spi/Kconfig
  • components/drivers/spi/dev_spi_core.c
  • components/drivers/spi/dev_spi_flash.h
  • components/drivers/spi/dev_spi_flash_sfud.c

📊 Current Review Status (Last Updated: 2025-10-30 17:20 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.

@supperthomas supperthomas requested a review from Copilot October 30, 2025 11:11
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 adds interrupt-safe operations to the SPI driver using spinlocks, enabling SPI operations in both interrupt context and thread context. The implementation adds a runtime check using rt_scheduler_is_available() to determine whether to use mutex (for thread context) or spinlock (for interrupt context).

Key changes:

  • Added a new Kconfig option RT_USING_SPI_ISR (enabled by default) to control ISR-safe SPI functionality
  • Introduced spinlock fields (_spinlock, _isr_lvl) to SPI bus and SPI flash device structures
  • Refactored locking mechanisms in SPI core and SFUD driver to support both mutex and spinlock based on scheduler availability

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
components/drivers/spi/Kconfig Adds configuration option RT_USING_SPI_ISR to enable interrupt-safe SPI operations
components/drivers/include/drivers/dev_spi.h Adds spinlock and interrupt level fields to rt_spi_bus structure
components/drivers/spi/dev_spi_flash.h Adds spinlock and interrupt level fields to spi_flash_device structure
components/drivers/spi/dev_spi_core.c Implements new spi_lock/spi_unlock helper functions that choose between mutex and spinlock based on scheduler availability
components/drivers/spi/dev_spi_flash_sfud.c Updates SFUD lock/unlock functions and retry_delay to support interrupt context operations

{
RT_ASSERT(bus);

rt_err_t ret = -RT_ERROR;
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

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

English: When RT_USING_SPI_ISR is not defined and the scheduler is unavailable (line 181-187), the function returns -RT_ERROR without taking any lock. This creates an inconsistent lock state and will cause the corresponding spi_unlock() to fail or behave incorrectly. Either return an error to prevent the operation, or implement a fallback locking mechanism.
中文:当 RT_USING_SPI_ISR 未定义且调度器不可用时(181-187行),函数返回 -RT_ERROR 而未获取任何锁。这会导致锁状态不一致,并使对应的 spi_unlock() 失败或行为异常。应返回错误以阻止操作,或实现回退锁机制。

Copilot uses AI. Check for mistakes.
if RT_USING_SPI

menuconfig RT_USING_SPI_ISR
bool "Enable ISR SPI"
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

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

English: The configuration option description "Enable ISR SPI" is too brief and unclear. It should clearly explain the feature's purpose and implications. Consider: "Enable interrupt-safe SPI operations (using spinlocks in ISR context)".
中文:配置选项描述 "Enable ISR SPI" 过于简短且不清晰。应明确说明功能目的和影响。建议改为:"Enable interrupt-safe SPI operations (using spinlocks in ISR context)"(启用中断安全的SPI操作,在中断上下文中使用自旋锁)。

Suggested change
bool "Enable ISR SPI"
bool "Enable interrupt-safe SPI operations (using spinlocks in ISR context)"

Copilot uses AI. Check for mistakes.
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.

1 participant