fix(spi): arm the SPI slave peripheral after its DMA channels start - #6291
Merged
Merged
Conversation
molqzone
requested review from
MabezDev,
bjoernQ,
bugadani and
playfulFence
as code owners
September 9, 2026 11:28
bugadani
approved these changes
Sep 17, 2026
bugadani
enabled auto-merge
September 17, 2026 07:01
|
Successfully created backport PR for |
MabezDev
pushed a commit
that referenced
this pull request
Sep 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thank you for your contribution!
We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:
Submission Checklist 📝
cargo xtask fmtcommand to ensure that all changed code is formatted correctly.skip-changelogormanual-changeloglabel as appropriate.Extra:
Pull Request Details 📖
Description
A GDMA-driven SPI slave has no control over when the master starts clocking:
SCLK and CS are owned by the master, and the first edge can arrive while the
driver is still setting up a transfer.
start_transfer_dmaarmed the peripheral (cmd().usr().set_bit()) beforestarting the RX/TX DMA channels. Any bits the master clocks in that window are
shifted without DMA service, so the first bytes of the transfer are lost or
corrupted.
This is easy to hit in practice but invisible to the current HIL test: in
test_basic_dmathe bitbang master only starts clocking after the slavetransfer has been armed, so the race window never opens. A real deployment
where the master owns the clock (in my case an ESP32-C5 receiving a continuous
video stream) hits it on every transfer.
The fix reorders the setup in
start_transfer_dma:channel,
USRis set only after both GDMA directions can service the first edge.The CPU buffer AFIFO (
buf_afifo_rst) was only reset as part of the oldcombined reset; it is not used by DMA slave transfers and is no longer
touched. The scope is unchanged: this only affects the AHB_GDMA/AXI_GDMA
engines (
spi_slave_dma_engine != "SPI_DMA"); the ESP32/ESP32-S2 slave pathis untouched.
ESP-IDF reference
ESP-IDF's GDMA SPI slave driver follows exactly the ordering this PR
restores, which is where this patch takes its cue from.
Per-direction FIFO reset, immediately before the corresponding channel
starts (
components/esp_driver_spi/src/gpspi/spi_slave.c,s_spi_slave_dma_prepare_data):The LL resets are per-direction toggles (write 1, then 0) and never touch
buf_afifo_rst— that bit belongs to the CPU-buffer FIFO path(
spi_ll_cpu_tx_fifo_reset, used by the non-DMA driver)(
components/esp_hal_gpspi/esp32c5/include/hal/spi_ll.h):And the transfer is kicked off only after both DMA directions have been
started, with a comment describing the very race this PR fixes
(
s_spi_slave_isr, same file):So in ESP-IDF terms, this PR replaces esp-hal's "set USR, then start DMA"
with IDF's "start DMA, then set USR", and replaces the combined
set-and-leave reset with IDF's per-direction toggling resets.
Happy to follow up with a HIL regression case (a master that does not wait for
the slave before clocking) if maintainers think that is worth the CI hardware
time.
Testing
byte stream from a master that owns SCLK/CS and does not wait for a
slave-ready signal. Before this change the first bytes of every transfer
were lost; after the change the stream is intact.
cargo checkpasses for theesp32c5andesp32c6targets.cargo xtask fmtlocally (nightly toolchain unavailablehere); the change was formatted to match the surrounding code. Happy to
reformat if CI flags anything.
Changelog
esp-hal/SPI slave
master starts clocking before the DMA channels are started.