Skip to content

[bsp][esp32-c6] compile SPI when it is selected - #11846

Merged
Rbb666 merged 3 commits into
RT-Thread:masterfrom
cms19859230182-lang:bsp/esp32c6-spi-link
Oct 16, 2026
Merged

Rbb666 merged 3 commits into
RT-Thread:masterfrom
cms19859230182-lang:bsp/esp32c6-spi-link

Conversation

@cms19859230182-lang

@cms19859230182-lang cms19859230182-lang commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Selecting BSP_USING_SPI on ESP32-C6 now links the IDF SPI master. The default image still leaves SPI off.
  • The shared ESP driver no longer falls through SPI modes, returns the transferred byte count, and takes the chip-select pin from SPI2_IOMUX_PIN_NUM_CS. C6 asks for SPI_DMA_DISABLED. Other chips keep SPI_DMA_CH_AUTO.
  • spi_configure() returns -RT_EINVAL when configuration is null. The unused esp32_spi_init() call that passed NULL is gone.
  • The peripheral table is unchanged. SPI stays unsupported. The GPIO7 to GPIO2 loopback was not run again.

Test plan

  • ESP32-C6 with RT_USING_SPI, BSP_USING_SPI and BSP_USING_SPI2 added to rtconfig.h, RT_USING_ADC kept: scons exit 0. rtthread.bin is 466416 bytes. text is 371342. The link includes drv_spi, spi_common, spi_master, spi_hal and gdma.
  • Those three lines removed, then scons again: exit 0. rtthread.bin is 445408 bytes. text is 340218. The link does not include drv_spi.
  • Not flashed. The loopback was not run again.

Selecting BSP_USING_SPI on C6 links the IDF SPI master. The default image still leaves SPI off, and the peripheral table stays unsupported.
@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

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 bsp/esp32c6-spi-link
    Enter PR branch bsp/esp32c6-spi-link in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 bsp/esp32c6-spi-link 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the bsp/esp32c6-spi-link 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 BSP: ESP32 BSP related with ESP labels Sep 29, 2026
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

cppcheck follows spi_configure(NULL, NULL) into a dereference. clang-format also wants the macro backslashes left-aligned and a single space around the enum equals.
memcpys the low byte of each 32-bit data_buf word to the start of the
buffer -- its length is (len + 7) / 8, which is 1 for an 8-bit word -- and
that is what fills the staging array above. */
err = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_DISABLED);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

AI给的建议:关闭 DMA 后,IDF 将单次传输上限设为 64 字节,忽略配置中的 4092。但 spixfer() 仍整包提交,65 字节消息就会返回 ESP_ERR_INVALID_ARG,随后触发 ESP_ERROR_CHECK 中止。应分段传输并保持正确片选时序,至少应返回错误而非终止系统。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

感谢指出。ai说的没错,关闭dma以后单次传输确实会收到限制;我目前的想法是在驱动里边,把超过64字节的分段循环发送;如果分段发送的过程中有bug直接弹错误码。改完以后我会做一轮验证

Comment thread bsp/ESP/ESP32_C6/SConstruct Outdated
with open(gdma, 'wb') as f:
f.write(fixed)

_adapt_idf_gdma_to_c6_ll()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

当 C3/C6 共用 bsp/ESP/libraries/ESP-IDF-latest 时,构建 C6 会永久把 gdma.c 改成四参数调用,即使未启用 SPI 也会执行。之后构建 C3,其 HAL 仍是三参数接口,会报 too many arguments。应使用 C6 独立副本,或通过目标宏保留两套调用。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

感谢指出,这里确实有问题,构建脚本会改掉 ESP-IDF-latest/.../gdma.c ,破坏掉c3的构建。所以我的想法是在 SConstruct 里把这段改写去掉;我现在打算在 gdma.c 里按目标宏保留两套调用;改完我会依次编译 C6 和 C3,确认两边都能过。之所以选择是目标宏,我觉得应该会比 C6 单独复制一份好维护。

Comment thread bsp/ESP/libraries/drivers/drv_spi.c Outdated
Comment on lines +149 to +152
else
{
LOG_E("data width %u is not supported\n", (rt_uint32_t)configuration->data_width);
return -RT_ENOSYS;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

位宽检查发生在 spi_bus_initialize() 成功之后。首次请求 16 位返回 -RT_ENOSYS,随后改用 8 位重试,会因总线已初始化而触发中止。应先校验位宽,再申请资源。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

感谢指出,经过检查,现在的顺序确实有问题。我打算把位宽校验挪到 spi_bus_initialize() 之前,不支持的位宽直接返回错误,不申请任何资源。

@cms19859230182-lang

Copy link
Copy Markdown
Contributor Author

@Rbb666 感谢评审,三处意见已在 d373b11 中修改:

  • SPI 单次传输超过 64 字节硬件上限时改为分段发送,期间片选保持有效;出错返回 -RT_EIO,不再直接断言。
  • 构建时不再修改共享包里的 gdma.c,改为 BSP 自带一份 idf_port/src/gdma.c。
  • 位宽检查提前到总线初始化之前。

已在 ESP32-C6 上验证开启和关闭 SPI 两种配置均编译通过;手头的 C6 板子坏了,暂时没法上板测试。

@Rbb666

Rbb666 commented Oct 16, 2026

Copy link
Copy Markdown
Member

@Rbb666 感谢评审,三处意见已在 d373b11 中修改:

  • SPI 单次传输超过 64 字节硬件上限时改为分段发送,期间片选保持有效;出错返回 -RT_EIO,不再直接断言。
  • 构建时不再修改共享包里的 gdma.c,改为 BSP 自带一份 idf_port/src/gdma.c。
  • 位宽检查提前到总线初始化之前。

已在 ESP32-C6 上验证开启和关闭 SPI 两种配置均编译通过;手头的 C6 板子坏了,暂时没法上板测试。

好的,我先合并进去

@Rbb666
Rbb666 merged commit 338eb83 into RT-Thread:master Oct 16, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: ESP32 BSP related with ESP BSP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants