The user learned that for STM32 SPI master, data reception requires using the transmit-receive API because the clock is only generated during transmission. The receive API is ineffective for masters as it doesn't produce a clock, making it suitable only for slave mode. This leads to buffer inefficiencies during large reads, as dummy data must be sent, reflecting SPI's bidirectional communication design.
How the SPI Peripheral Generates Its Clock Line
Today I finally figured out how to use the SPI peripheral.
Let's discuss it with these three typical APIs:
A problem I ran into earlier
When operating as an SPI master, calling the
receive APInever actually got me any data, so I used thetransmit/receive APIeverywhere I needed to receive.I don't use SPI much, and the use case has always been simple, so I never really thought about why. I just assumed it was a problem with the API implementation (there's a compatibility layer wrapped around the lower level, after all).
Rewriting the SPI flash driver today made me pick the question up again.
In short
SPI is a hardware communication protocol that shifts data on the edges of the clock line. Without a clock, neither the master nor the slave actually sends anything—unless it's some weird non-standard implementation.
And normally, only the master is allowed to generate the clock.
So the answer is obvious: at least on STM32, the clock is only generated while transmitting. That means even a pure receive still has to use the
transmit/receive API.The
receive APIonly makes sense when acting as a slave, or in the case GPT mentioned where external hardware generates the clock (which sounds terrifying—does anyone actually do that?).Thoughts
SPI communication seems suited to bidirectional communication without exception, and not to one-way communication. That becomes a problem when you're a master trying to do a large read, because the transmit side still needs a buffer of the same size.
It might be an STM32 HAL implementation issue, but I haven't looked into it. I'll deal with it if I actually run into it.
You can, but you need to distinguish between “hardware automatically supplying the clock” and “software explicitly supplying the clock.” Given your current STM32H743 + HAL configuration, here's the conclusion:
Got it — I'll focus only on the STM32 SPI implementation itself. Here's the approach and conclusion.
does not guarantee clock generation in master mode, so it can't serve as a generic “read-only” interface (unless the hardware/library supports automatic RX-only clocking) — which isn't present in your current configuration.