Skip to main content
Associate
Question

Why does SPI1 master RXONLY keep an extra byte only with CPHA=1 + GPIO LOW speed?

STM32G474, SPI1 master RXONLY, 8-bit, MSB first, CPOL=0 / CPHA=1, NSS software, CS on a plain GPIO. Sensor: MT6701 in SSI mode, 24-bit frame. Read: HAL_SPI_Receive(&hspi1, buf, 3, ...).
Only one of four combinations fails:
SCK / MISO GPIO speed    CPOL / CPHA    3-byte read
LOW    0 / 1    24 SCK pulses, frame wraps by 8 bits (33 E8 20 read as E8 20 33)
MEDIUM / HIGH / VERY_HIGH    0 / 1    32 SCK pulses, correct
LOW    1 / 0    correct
full duplex    0 / 1    24 SCK pulses, correct
The wrap-around means one extra byte is returned first and the last byte of the frame is lost. Rotating the bytes back makes the CRC pass, so the data on the wire is correct and only the alignment is wrong.

 

frame wrpas when clk speed low
the correct when clk speed high

 

 

the logic analyzer when 3bytes and speed high

 

the logic analyzer when 3bytes and speed low

?code=MWVlMWYwYzM0ZmE3ZTIzYzA4MmU2ZWFjMWVlMzJiZTlfZnlVQUhQYkRWMVp3WkZjUDc0blRTcVlMN1ZsQTdha0JfVG9rZW46SkRaTWI0ZXByb1AxTTh4TEd6emN4Q00xbnVoXzE3OTYwNDk1OTQ6MTc5NjA1MzE5NF9WNA&add_watermark=true&scene_type=CCM

 

 

Two measurements:
- At LOW the first byte is available 0.27 us after SPE = 1; at MEDIUM it takes a full byte time (about 6.2 us). 0.27 us is far too short for a clocked byte.
- A 3-byte request produces 24 SCK pulses at LOW and 32 at the other speeds. A 4-byte request produces 32 at LOW and 40 at the others.
SR shows no overrun (OVR = 0) and no error flag in any of these cases. The wrap-around looks like an RX overrun or an extra buffered byte, but the registers do not show one. That is what makes this puzzling.


Question:

why does only the LOW + CPHA=1 combination leave an extra byte in the RX path? What is different inside the peripheral for that combination?

 

11 replies

waclawek.jan
Super User

> Read: HAL_SPI_Receive(&hspi1, buf, 3, ...).

> MEDIUM / HIGH / VERY_HIGH    0 / 1    32 SCK pulses, correct

Why would 32 SCK pulses be correct?

I don’t use Cube/HAL and don’t intend to decipher whatever is written there, but it’s open source so you can do it yourself.

Or, better, write your own. Note, that RXONLY mode generates clocks autonomously and it’s not trivial to time it properly, that’s why full-duplex is the first choice in this case even if you don’t use the transmitted data (and don’t need to set the Tx pin (MOSI for master) in GPIO).

JW

Associate

By "correct", I meant it aligns with the code logic. The HAL implementation essentially uses a while  loop to wait for the 3 bytes to be read and then manually clears SPE (SPE=0). This means it actually disables the SPI enable bit midway through the 4th byte. According to the reference manual, this should result in the transmission of 4 full bytes. Therefore, transmitting only 3 bytes would actually be the problematic case, as the first byte is received almost instantaneously rather than waiting for a full byte transmission cycle to complete. This effectively creates a "ghost byte" that cannot be observed in the data register. I have verified this hypothesis through software simulation. My only remaining question is: why does this ghost byte only appear in the LOW speed case?

 

TDK

RXONLY introduces a race condition. If the code can’t stop the SPI in time, it will clock another byte. This is the expected behavior. Use normal two-way mode and ignore the TX pin if this is a problem.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate

I am aware of this issue with RXONLY. I just want to know why the ghost byte only appears at low speed, but not at other speeds.

TDK

I just want to know why the ghost byte only appears at low speed, but not at other speeds.

As I interpret it, it doesn’t fail at LOW speed--that’s the only one that works (as ​@waclawek.jan pointed out). A 3-byte read should have 24 clocks, not 32. It fails at all higher speeds.

But regardless of which is “correct”, the speed of the pin will affect timing which can affect who wins the race condition. Even if this particular transaction works, the race condition issue is still there and there is no guarantee for future transactions.

"If you feel a post has answered your question, please click ""Accept as Solution""."