Skip to content

fix[stm32][spi]: add usage_freq and compute transfer timeout by frequency#11240

Open
wdfk-prog wants to merge 1 commit intoRT-Thread:masterfrom
wdfk-prog:pr/01-spi-config-usage-freq-timeout
Open

fix[stm32][spi]: add usage_freq and compute transfer timeout by frequency#11240
wdfk-prog wants to merge 1 commit intoRT-Thread:masterfrom
wdfk-prog:pr/01-spi-config-usage-freq-timeout

Conversation

@wdfk-prog
Copy link
Contributor

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

当前 STM32 SPI 驱动中,阻塞发送、接收、收发以及 DMA 等待完成时使用固定的 1000ms 超时。
当 SPI 传输数据量较大、配置频率较低,或者实际 SPI 工作频率低于预期时,固定超时可能过短,导致正常传输被误判为超时。
另外,驱动日志中计算 SPI 实际使用频率时使用了运行时表达式,结果不够直观,也不便于后续复用。

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

本 PR 对 STM32 SPI 驱动做了如下调整:

  1. 将局部变量 SPI_CLOCK 重命名为 spi_clock,使代码风格更符合规范;
  2. 在 SPI 初始化阶段,根据分频配置计算实际 SPI 使用频率,并保存到 cfg->usage_freq
  3. 将日志中的 SPI usage frequency 统一改为输出 cfg->usage_freq
  4. spixfer() 中根据本次传输长度和实际 SPI 使用频率动态计算超时时间:
    • 先根据 usage_freq / 8 计算理论字节传输速率;
    • 再根据报文长度推导所需传输时间;
    • 最后增加一定冗余量,生成本次传输使用的 timeout_ms
  5. HAL_SPI_TransmitReceive()HAL_SPI_Transmit()HAL_SPI_Receive() 的固定 1000ms 超时替换为动态超时;
  6. 将 DMA 完成等待 rt_completion_wait() 的固定超时替换为动态 tick 超时;
  7. 对非 DMA 场景下等待 HAL_SPI_STATE_READY 的死等逻辑增加超时退出机制,避免异常情况下无限阻塞。

这样可以让 SPI 超时与实际配置频率、传输长度相匹配,降低误超时风险,并提升驱动健壮性。

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

  • BSP:

    • bsp/stm32/xxx
  • .config:

    • CONFIG_RT_USING_SPI
    • 与目标板 SPI 外设相关的配置项
  • action:

    • 请填写你自己仓库 PR 分支触发的 CI / Action 链接

@github-actions
Copy link

github-actions bot commented Mar 7, 2026

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

完成后,提交将自动更新至 pr/01-spi-config-usage-freq-timeout 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the pr/01-spi-config-usage-freq-timeout 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 Mar 7, 2026

📌 Code Review Assignment

🏷️ Tag: bsp_stm32

Reviewers: Liang1795 hamburger-os wdfk-prog

Changed Files (Click to expand)
  • bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/drivers/include/drivers/dev_spi.h

📊 Current Review Status (Last Updated: 2026-03-09 08:36 CST)

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

@wdfk-prog wdfk-prog force-pushed the pr/01-spi-config-usage-freq-timeout branch from 99c16be to 9033c82 Compare March 9, 2026 00:36
@wdfk-prog
Copy link
Contributor Author

  • 已修复CI错误,解决H7系列报错问题
  • RTduino是警告,只是未初始化新增变量且与本仓库无关;不处理
2026-03-07T07:30:04.9015178Z packages/RTduino-latest/libraries/buildin/SPI/SPI.cpp: In member function 'void SPIClass::beginTransaction(SPISettings)':
2026-03-07T07:30:04.9016139Z packages/RTduino-latest/libraries/buildin/SPI/SPI.cpp:67:5: error: missing initializer for member 'rt_spi_configuration::usage_freq' [-Werror=missing-field-initializers]
2026-03-07T07:30:04.9016802Z    67 |     };
2026-03-07T07:30:04.9016971Z       |     ^
2026-03-07T07:30:04.9017195Z CC build/packages/perf_counter-v2.2.4.1/perf_counter.o
2026-03-07T07:30:04.9017520Z cc1plus: all warnings being treated as errors
2026-03-07T07:30:04.9017904Z scons: *** [build/packages/RTduino-latest/libraries/buildin/SPI/SPI.o] Error 1
2026-03-07T07:30:04.9018293Z scons: building terminated because of errors.
2026-03-07T07:30:04.9018927Z �[1;32mscons -c�[0m
2026-03-07T07:30:04.9040078Z ##[error]build stm32/stm32g474-st-nucleo peripheral.arduino failed.
  • 无关错误,不予处理
2026-03-07T07:31:35.1842835Z board/ports/drv_ov5640.c: In function 'ov5640_focus_init':
2026-03-07T07:31:35.1843468Z board/ports/drv_ov5640.c:265:20: warning: unsigned conversion from 'int' to 'rt_uint8_t' {aka 'unsigned char'} changes value from '-255' to '1' [-Woverflow]
2026-03-07T07:31:35.1844057Z   265 |             return -RT_ERROR;
2026-03-07T07:31:35.1844287Z       |                    ^
2026-03-07T07:31:35.1844563Z board/ports/drv_ov5640.c: In function 'ov5640_focus_constant':
2026-03-07T07:31:35.1845201Z board/ports/drv_ov5640.c:407:20: warning: unsigned conversion from 'int' to 'rt_uint8_t' {aka 'unsigned char'} changes value from '-255' to '1' [-Woverflow]
2026-03-07T07:31:35.1845789Z   407 |             return -RT_ERROR;
2026-03-07T07:31:35.1846008Z       |                    ^
2026-03-07T07:31:35.1846529Z board/ports/drv_ov5640.c:420:20: warning: unsigned conversion from 'int' to 'rt_uint8_t' {aka 'unsigned char'} changes value from '-255' to '1' [-Woverflow]
2026-03-07T07:31:35.1847100Z   420 |             return -RT_ERROR;
2026-03-07T07:31:35.1847317Z       |                    ^
2026-03-07T07:31:35.1847576Z board/ports/drv_ov5640.c: In function 'jpeg_data_process':
2026-03-07T07:31:35.1848229Z board/ports/drv_ov5640.c:483:14: warning: assignment to 'int *' from incompatible pointer type 'rt_int32_t *' {aka 'long int *'} [-Wincompatible-pointer-types]
2026-03-07T07:31:35.1849068Z   483 |         pbuf = JPEG_DATA_BUF + jpeg_data_len;

2026-03-07T07:33:05.1335395Z packages/btstack-latest/rtt_adapter/rtt_btstack_adapter.c: In function 'uart_sample':
2026-03-07T07:33:05.1336164Z packages/btstack-latest/rtt_adapter/rtt_btstack_adapter.c:107:8: warning: missing terminating " character
2026-03-07T07:33:05.1336747Z   107 | #error "Please set the reset pin
2026-03-07T07:33:05.1337023Z       |        ^
2026-03-07T07:33:05.1337423Z packages/btstack-latest/port/posix-h4-bcm/btstack_main.c: In function 'bt_stack_main':
2026-03-07T07:33:05.1338180Z packages/btstack-latest/port/posix-h4-bcm/btstack_main.c:217:8: warning: missing terminating " character
2026-03-07T07:33:05.1339032Z   217 | #error "Please set the reset pin

2026-03-07T07:34:38.4700595Z CC /home/runner/work/rt-thread/rt-thread/bsp/stm32/libraries/HAL_Drivers/drivers/drv_gpio.o
2026-03-07T07:34:38.4701177Z CC /home/runner/work/rt-thread/rt-thread/bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart.o
2026-03-07T07:34:38.4701584Z LINK rt-thread.elf
2026-03-07T07:34:38.4702464Z /opt/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/bin/ld:board/linker_scripts/link.lds:159: warning: memory region `RAM_SHARED' not declared
2026-03-07T07:34:38.4703285Z Memory region         Used Size  Region Size  %age Used
2026-03-07T07:34:38.4703590Z              ROM:       82976 B         1 MB      7.91%
2026-03-07T07:34:38.4703851Z              RAM:        5356 B       192 KB      2.72%
2026-03-07T07:34:38.4704182Z        *default*:   536876268 B 18446744073709551615 B      0.00%
2026-03-07T07:34:38.4704556Z arm-none-eabi-objcopy -O binary rt-thread.elf rtthread.bin
2026-03-07T07:34:38.4704877Z arm-none-eabi-size rt-thread.elf
2026-03-07T07:34:38.4705140Z    text	   data	    bss	    dec	    hex	filename
2026-03-07T07:34:38.4705517Z   82976	   1104	   4252	  88332	  1590c	rt-thread.elf
2026-03-07T07:34:38.4706002Z arm-none-eabi-objcopy -O ihex rt-thread.elf rt-thread.hex
2026-03-07T07:34:38.4706435Z scons: done building targets.
2026-03-07T07:34:38.4718850Z �[1;32mscons -c�[0m
2026-03-07T07:34:38.4719464Z ##[endgroup]
2026-03-07T07:34:38.4759375Z ##[error]Process completed with exit code 4.
2026-03-07T07:34:38.4816447Z ##[group]Run curl -X POST -H "Authorization: token " \
2026-03-07T07:34:38.4816855Z �[36;1mcurl -X POST -H "Authorization: token " \�[0m
2026-03-07T07:34:38.4817563Z �[36;1m-d '{"body":"@wdfk-prog, Thank you for your contribution, but there was an error with the action. Could you please help check the BSP compilation issue? Thank you."}' \�[0m
2026-03-07T07:34:38.4818371Z �[36;1m"https://api.github.com/repos/RT-Thread/rt-thread/issues/11240/comments"�[0m
2026-03-07T07:34:38.4855783Z shell: /usr/bin/bash -e {0}

@wdfk-prog
Copy link
Contributor Author

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