feat[spi]: enable interrupt-safe operations using spinlocks#10873
feat[spi]: enable interrupt-safe operations using spinlocks#10873Rbb666 merged 1 commit intoRT-Thread:masterfrom
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-11-13 11:24 CST)
📝 Review Instructions
|
df73830 to
cfe15d8
Compare
There was a problem hiding this comment.
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 |
cfe15d8 to
2621198
Compare
|
拉取/合并请求描述:(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