-
Notifications
You must be signed in to change notification settings - Fork 5.3k
feat[spi]: enable interrupt-safe operations using spinlocks #10873
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
base: master
Are you sure you want to change the base?
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_driver_spiReviewers: Liang1795 wdfk-prog Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2025-10-30 17:20 CST)
📝 Review Instructions
|
df73830 to
cfe15d8
Compare
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 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; |
Copilot
AI
Oct 30, 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.
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() 失败或行为异常。应返回错误以阻止操作,或实现回退锁机制。
| if RT_USING_SPI | ||
|
|
||
| menuconfig RT_USING_SPI_ISR | ||
| bool "Enable ISR SPI" |
Copilot
AI
Oct 30, 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.
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操作,在中断上下文中使用自旋锁)。
| bool "Enable ISR SPI" | |
| bool "Enable interrupt-safe SPI operations (using spinlocks in ISR context)" |
拉取/合并请求描述:(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)
主要变更 (Key Changes)
增加了 RT_USING_SPI_ISR 选项,用户可以按需开启或关闭此功能。
通过这些修改,SPI 驱动现在是完全的线程安全和中断安全的,极大地扩展了其应用范围。
请提供验证的bsp和config (provide the config and bsp)
BSP: STM32F4
STM32需要修改适配可以在中断环境下使用
参考此处修改实现:2bbee8b
[components][SPI][spi-bit-ops]修复可能的异常操作 #9124
feat(CmBacktrace): enhance reliability, configurability, and testability armink-rtt-pkgs/CmBacktrace#22
验证结果
]
当前拉取/合并请求的状态 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