TT Lab
Get started
Learn Learning paths Courses

The Sensor Is Fine. The Board Cannot Understand It

The Wiring Is Right, but the Value Is Twice as Large

Continue in TT Lab

In one line

SPI has a clock line, so there is no speed problem. Instead, which edge you read on remains a setting, and if that setting is off, you get a value shifted by one bit.

Why this was needed

You read the manufacturer ID from a new flash chip and got an absurd value. You checked the wiring three times, the clock goes in, and the chip responds. Only the value is strange. A pattern like this is usually not the wiring but the mode. And the mode is written in just one cell of a table in the datasheet, so it is easy to overlook.

SPI is a de facto standard without a specification, so even the terminology differs a little by manufacturer. So much the more, "what the waveform says" is faster than "what the document says". Fortunately, an SPI waveform tells you its mode by itself.

How it works

There are four lines. The select line (CS or SS), the clock (SCLK), MOSI sent by the master, and MISO sent by the slave. A transfer is only while the select line is low, and one bit goes each way at every clock edge. Both directions happen at the same time, so even when sending a read command, something must be carried on MOSI.

There are only two settings. CPOL is whether the clock is high or low when idle, and CPHA is whether to read on the leading edge or the trailing edge. Together they are called modes 0 to 3. TI's SPI user guide also treats these two values as separate settings (SPRUGP2). The settings sigrok's SPI decoder accepts are also five, cs_polarity, cpol, cpha, bitorder, and wordsize (documentation) — this list tells you what cannot be determined from the waveform alone.

CPOL=0 CPHA=0 (모드 0)            CPOL=1 CPHA=1 (모드 3)
SCLK  __/‾\_/‾\_/‾\__            SCLK  ‾‾\_/‾\_/‾\_/‾‾
읽는 곳  ^   ^   ^                읽는 곳   ^   ^   ^
         상승 모서리                        상승 모서리

Here is a fact that trips up beginners. Mode 0 and mode 3 read on the same edge. In the figure above, both are the rising edge. Modes 1 and 2 likewise use the same edge as each other (falling). So you cannot tell the four modes apart from the clock edges alone. The other half of the information is where the clock sits when idle, that is, CPOL. To determine the mode from a waveform, you must look at two things separately.

The way to determine CPHA is more interesting. A real device does not change data immediately at the clock edge but changes it after an output valid delay. So if, on the waveform, you find the clock edge just before the time MOSI moves, that edge is the side that is not the reading edge. The reading edge is the opposite one, and if it is the leading edge that leaves idle, CPHA=0, otherwise CPHA=1. If you read at the edge where the data changes, you pick up the previous bit, so the whole thing comes out shifted by one slot — this is the identity of the "absurd ID" mentioned earlier.

The timing of the select line also creates quiet faults. If CS falls later than the first clock edge, that edge is not included in the transfer. The slave did not receive the first bit, but the master believes it sent it. On the waveform it shows up as the number of sample edges while CS is low not being a multiple of 8. In this course's capture, there should be 24 edges but there are 23, and 7 bits are left over. That there are leftover bits means the byte boundary is off, and it is the count rather than the value that tells you first.

The byte order is also an item to check. Most send the high-order bit first, but some devices send the low-order bit first, and you cannot tell which from the waveform alone. This is an item you have to look up in the document.

Finally, the clock frequency is rarely the cause of a fault. SPI sends the clock line along with the data, so if the master wiggles it slowly, the slave follows slowly too. Unlike UART, the phrase "the speed doesn't match" does not apply, and instead the datasheet lists only the maximum frequency the device can handle. So when a value is strange in SPI, look first at the edges and the select line rather than the speed.

What it looks like in the field

The most common report is "the value I read is double or half". A value shifted by one bit looks exactly like that in hexadecimal. Shifting 0x9F one slot to the left gives 0x3E, and to the right gives 0x4F. If a value gives you the feeling "it's about double", it is faster to suspect the mode first.

On boards where several slaves share one bus, an accident of mixed modes happens. Each slave demands its own mode, and if the driver does not change the mode at every transfer, some chips read and some do not. A good share of reports that "only this sensor doesn't work" are this.

What to check in the next quiz

It asks what CPOL and CPHA each determine, why you cannot tell the four modes apart from the edges alone, how to determine CPHA from the edge where data changes, and which number on the waveform becomes abnormal first when the select line is late.