Skip to content

refactor[STM32][DMA]: unify DMA helpers and descriptors for HAL drivers#11329

Open
wdfk-prog wants to merge 2 commits intoRT-Thread:masterfrom
wdfk-prog:stm32_dma
Open

refactor[STM32][DMA]: unify DMA helpers and descriptors for HAL drivers#11329
wdfk-prog wants to merge 2 commits intoRT-Thread:masterfrom
wdfk-prog:stm32_dma

Conversation

@wdfk-prog
Copy link
Copy Markdown
Contributor

@wdfk-prog wdfk-prog commented Apr 14, 2026

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

[

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

当前 STM32 HAL_Drivers 中 DMA 相关实现分散在两层:

  1. 外设驱动层分别维护 DMA 初始化、时钟使能、IRQ 配置、回滚和反初始化逻辑;
  2. 各系列配置头文件分别维护 DMA 端点描述,存在大量重复字段展开和系列差异分支。

这种实现方式导致 DMA 接入路径和配置表达方式不统一,增加了维护成本,也不利于后续扩展更多 STM32 系列或外设 DMA 场景。

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

本次修改统一了驱动侧和配置侧两部分 DMA 逻辑:

  1. 在驱动侧新增 drv_dma.c / drv_dma.h,引入 stm32_dma_config 描述结构以及 stm32_dma_init()stm32_dma_setup()stm32_dma_deinit() 等公共 helper,用于统一处理 DMA 控制器时钟开启、DMAMUX 使能、HAL DMA 参数下发、NVIC 配置和反初始化流程。

  2. drv_hard_i2c.cdrv_spi.cdrv_usart.cdrv_usart_v2.cdrv_qspi.cdrv_sdio.c 中原有分散的 DMA 初始化和回滚逻辑切换为复用公共 helper,并将驱动内部相关 DMA 配置改为 const struct stm32_dma_config

  3. 在配置侧,将多个 STM32 系列下 SPI/UART/I2C/SDIO/QSPI 的 DMA 配置宏迁移为统一的描述符初始化宏,如 STM32_DMA_*_CONFIG_INIT_EXSTM32_DMA_CONFIG_INIT_FIFO_EX,并补充可覆写的 DMA_PRIORITYDMA_PREEMPT_PRIORITYDMA_SUB_PRIORITY 字段,以统一不同系列配置头的表达方式。

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

  • BSP:

    • 已验证F4 I2C SPI UART DMA RX TX可用
  • .config:

    • 需根据实际验证情况补充 DMA 相关配置项
    • 例如:BSP_SPIx_TX_USING_DMABSP_SPIx_RX_USING_DMABSP_UARTx_TX_USING_DMABSP_UARTx_RX_USING_DMABSP_I2Cx_TX_USING_DMABSP_I2Cx_RX_USING_DMABSP_QSPI_USING_DMABSP_USING_SDIO
  • action:

    • 当前未提供 PR 分支 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
Copy Markdown

👋 感谢您对 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:stm32_dma
  • 设置PR number为 \ Set the PR number to:11329
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 stm32_dma 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the stm32_dma 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 github-actions bot added BSP: STM32 BSP related with ST/STM32 BSP labels Apr 14, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 14, 2026

📌 Code Review Assignment

🏷️ Tag: bsp_stm32

Reviewers: Liang1795 hamburger-os wdfk-prog

Changed Files (Click to expand)
  • bsp/stm32/libraries/HAL_Drivers/drivers/SConscript
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f0/spi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f0/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/i2c_hard_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/sdio_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/spi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f2/sdio_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f2/spi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f2/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f3/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f4/i2c_hard_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f4/qspi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f4/sdio_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f4/spi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f4/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f7/qspi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f7/sdio_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f7/spi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/f7/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/g0/spi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/g0/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/g4/qspi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/g4/sdio_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/g4/spi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/g4/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/h5/uart_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/i2c_hard_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/qspi_config.h
  • bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/sdio_config.h

📊 Current Review Status (Last Updated: 2026-04-14 16:59 CST)

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

- add drv_dma common helpers and stm32_dma_config descriptors for STM32 series
- refactor i2c, spi, usart, qspi, and sdio DMA setup and deinit paths to reuse the common layer
- unify DMA IRQ enable, rollback handling, and const board-level DMA configuration usage
- migrate STM32 HAL driver DMA config macros to shared descriptor init helpers
- add overridable DMA priority and NVIC priority fields for board-level configs
- align SPI, UART, I2C, SDIO, and QSPI DMA descriptors across visible STM32 series configs
- simplify per-series DMA config definitions and reduce duplicated field assignments
Copy link
Copy Markdown
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 refactors STM32 HAL_Drivers DMA integration by introducing a shared DMA descriptor (stm32_dma_config) plus common helper APIs, and migrates peripheral/config headers to use the unified descriptors/macros.
本次 PR 通过引入统一的 DMA 描述符(stm32_dma_config)与公共 helper 接口,重构 STM32 HAL_Drivers 中分散的 DMA 初始化/配置逻辑,并将多系列配置头迁移到统一的描述符宏。

Changes:

  • Add drv_dma.c/.h to centralize DMA clock/DMAMUX enable, HAL DMA init, NVIC setup, and deinit/rollback.
    新增 drv_dma.c/.h,集中 DMA 时钟/DMAMUX、HAL 初始化、NVIC 配置与反初始化/回滚。
  • Switch USART/SPI/I2C/SDIO/QSPI drivers and configs to stm32_dma_config (mostly const) and unified STM32_DMA_*_CONFIG_INIT_EX macros.
    将 USART/SPI/I2C/SDIO/QSPI 驱动与配置统一切换到 stm32_dma_config(多为 const)及统一初始化宏。
  • Update build script to compile the new DMA helper module.
    更新构建脚本以编译新的 DMA helper 模块。

Reviewed changes

Copilot reviewed 70 out of 70 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
bsp/stm32/libraries/HAL_Drivers/drivers/drv_dma.c New common DMA helper implementation (init/setup/deinit).
bsp/stm32/libraries/HAL_Drivers/drivers/drv_config.h Includes drv_dma.h so config headers can use unified DMA descriptors.
bsp/stm32/libraries/HAL_Drivers/drivers/SConscript Adds drv_dma.c to build sources.
bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart.h UART config DMA fields switched to const struct stm32_dma_config *; Change Logs line edited.
bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart_v2.h UART v2 config DMA fields switched to const struct stm32_dma_config *.
bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.h SPI config DMA fields switched to const struct stm32_dma_config *.
bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.h I2C config DMA fields switched to const struct stm32_dma_config *.
bsp/stm32/libraries/HAL_Drivers/drivers/drv_sdio.h SDIO config DMA members switched to struct stm32_dma_config.
bsp/stm32/libraries/HAL_Drivers/drivers/drv_sdio.c SDIO DMA (Tx/Rx) paths refactored to use stm32_dma_init/deinit and improved error handling.
bsp/stm32/libraries/HAL_Drivers/drivers/drv_qspi.c QSPI DMA setup refactored to stm32_dma_setup and unified descriptor usage.
bsp/stm32/libraries/HAL_Drivers/drivers/config/wl/uart_config.h Migrates WL UART DMA macros to unified STM32_DMA_*_CONFIG_INIT_EX + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/wl/spi_config.h Migrates WL SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/wb/uart_config.h Migrates WB UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/wb/spi_config.h Migrates WB SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/wb/qspi_config.h Migrates WB QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/u5/uart_config.h Migrates U5 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/u5/spi_config.h Migrates U5 SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/u5/sdio_config.h Migrates U5 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/u5/qspi_config.h Migrates U5 QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/mp1/qspi_config.h Migrates MP1 QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l5/uart_config.h Migrates L5 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l5/spi_config.h Migrates L5 SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l5/sdio_config.h Migrates L5 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l5/qspi_config.h Migrates L5 QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l4/uart_config.h Migrates L4 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l4/spi_config.h Migrates L4 SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l4/sdio_config.h Migrates L4 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l4/qspi_config.h Migrates L4 QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l1/uart_config.h Migrates L1 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l1/spi_config.h Migrates L1 SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l1/sdio_config.h Migrates L1 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/l0/uart_config.h Migrates L0 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/g0/uart_config.h Migrates G0 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/g0/spi_config.h Migrates G0 SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/g4/sdio_config.h Migrates G4 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/g4/qspi_config.h Migrates G4 QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f7/uart_config.h Migrates F7 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/uart_config.h Migrates H7 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/sdio_config.h Migrates H7 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/qspi_config.h Migrates H7 QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/h5/uart_config.h Migrates H5 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f4/sdio_config.h Migrates F4 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f4/qspi_config.h Migrates F4 QSPI DMA macro to unified init macro + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f3/uart_config.h Migrates F3 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f2/spi_config.h Migrates F2 SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f2/sdio_config.h Migrates F2 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/spi_config.h Migrates F1 SPI DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/sdio_config.h Migrates F1 SDIO DMA config to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/i2c_hard_config.h Migrates F1 I2C DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f0/uart_config.h Migrates F0 UART DMA macros to unified init macros + priority fields.
bsp/stm32/libraries/HAL_Drivers/drivers/config/f0/spi_config.h Migrates F0 SPI DMA macros to unified init macros + priority fields.

* 2018-10-30 SummerGift first version
* 2019-03-05 whj4674672 add stm32h7
* 2020-10-14 PeakRacing Porting for stm32wbxx
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

[documentation/文档]: Historical Change Logs entry author was altered.

English: This PR changes the 2020-10-14 Change Logs author name from PeakRacing to Dozingfiretruck. Unless there is a verified attribution correction, please avoid modifying historical authorship lines because it breaks traceability and credit. If the intent is to add your own contribution, add a new Change Logs entry instead.

中文:此处将 2020-10-14 的 Change Logs 作者从 PeakRacing 改成了 Dozingfiretruck。除非这是经过确认的署名更正,否则不建议修改历史作者信息,这会影响可追溯性与贡献归属;如果是新增贡献,请新增一条 Change Logs 记录即可。

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +46
#if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F3)
#define STM32_DMA_USES_CHANNEL
#endif /* defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F3) */
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

[bug/缺陷]: STM32_DMA_USES_CHANNEL incorrectly includes SOC_SERIES_STM32F3.

English: In drv_dma.h, SOC_SERIES_STM32F3 is classified as DMA_Channel_TypeDef (channel-based DMA), but it is also included in STM32_DMA_USES_CHANNEL, which makes the helper write dma_handle->Init.Channel. For channel-based DMA (like the DMA_Channel_TypeDef path), HAL DMA_InitTypeDef typically has no Channel member, so this will break builds for STM32F3 or configure a non-existent field. Please remove SOC_SERIES_STM32F3 from STM32_DMA_USES_CHANNEL (keep it only for stream-based families), or gate it on the actual presence of DMA_InitTypeDef.Channel.

中文:在 drv_dma.h 里,SOC_SERIES_STM32F3 被归类为 DMA_Channel_TypeDef(通道式 DMA),但同时又被放进了 STM32_DMA_USES_CHANNEL,这会导致 helper 去写 dma_handle->Init.Channel。对于通道式 DMA(DMA_Channel_TypeDef 路径),HAL 的 DMA_InitTypeDef 通常没有 Channel 成员,这会导致 F3 编译失败或写入不存在的字段。建议把 SOC_SERIES_STM32F3STM32_DMA_USES_CHANNEL 中移除(仅保留给 stream-based 系列),或基于 DMA_InitTypeDef.Channel 是否存在来做条件编译。

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: STM32 BSP related with ST/STM32 BSP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants