Skip to content
systemcoding.dev
Hardware

Read SPI and I2C Waveforms Bit by Bit on a Logic Analyser

Decode an SPI flash ID read and an I2C TMP102 temperature read by hand from logic analyser captures, with sample points, start, stop and ACK bits explained.

7 min read
Summarize in AI
Flat illustration of an oscilloscope screen showing digital signals, with the C, Arduino and KiCad logos.

A logic analyser records digital signals against time, and the software that comes with it can decode SPI and I2C for you. That is convenient until the decoder shows garbage, or nothing, or a plausible value that is wrong. Then you have to read the waveform yourself, and the skill is the same whatever the tool: know where the data is sampled, know which edge is special, and walk the bits in order. This post teaches that for the two buses you will meet first on a microcontroller board. The SPI example reads the JEDEC ID of a flash chip, and the I2C example reads a temperature register from a TMP102 sensor at address 0x48. Both are decoded by hand from the logic levels, and a short C program shows the bit-to-byte step so you can check your reading. You need only a basic idea of what a bus is.

What the analyser actually gives you

A logic analyser samples each input at a fixed rate and stores a stream of ones and zeros. It does not know what the signals mean. Two settings decide whether the capture is usable. The sample rate must be well above the bus clock, because the analyser can only see an edge at the sample that follows it. A common rule of thumb is to sample at four to ten times the fastest signal you want to decode, and more is safer; at too low a rate, edges move by a sample or two and the decoded bits shift. The voltage threshold decides what counts as high: a 3.3 V bus read with a threshold meant for 5 V logic can look permanently low.

Connect a ground wire from the analyser to the board first, then one channel per signal: for SPI, the clock (SCK), chip select (CS), controller out (MOSI) and controller in (MISO); for I2C, the clock (SCL) and data (SDA). Name the channels in the software before you capture; reading D0, D1, D2 three weeks later is not a pleasure.

SPI: sample on the right edge

SPI has no addresses and no acknowledgements. A transfer is bracketed by chip select going low and then high, and in between the controller clocks bits out on MOSI while the peripheral clocks bits out on MISO at the same time. Whether a bit is valid on the rising or the falling clock edge depends on the mode, which is two settings: clock polarity (CPOL), the idle level of the clock, and clock phase (CPHA), which edge samples.

ModeCPOLCPHAClock idlesData is sampled on
000lowrising edge
101lowfalling edge
210highfalling edge
311highrising edge

Mode 0 is the most common, so decode with it first. Read the clock's idle level in the capture to find CPOL, then try each phase until the bytes make sense.

For the example, the controller reads the JEDEC ID from a typical NOR flash: it sends the command byte 0x9F and then clocks out three more bytes while the chip answers with its manufacturer, memory type and capacity codes. The capture, with chip select low for 32 clocks, in mode 0, looks like this when you write down MOSI and MISO at each rising SCK edge:

Clock123456789 to 1617 to 2425 to 32
MOSI10011111000000000000000000000000
MISO00000000111011110100000000011000

Group the bits into bytes, most significant bit first, which is the usual SPI order. MOSI reads 10011111, which is 0x9F, the Read JEDEC ID command. MISO is high impedance or low during the command and then carries three bytes: 11101111 is 0xEF, 01000000 is 0x40 and 00011000 is 0x18. That is the identity of a Winbond W25Q128 part: manufacturer 0xEF, type 0x40, capacity code 0x18 for 128 Mbit. The chip started answering only after the command's last bit, which is why the first MISO byte is meaningless.

Checking the arithmetic in C

The step from bits to bytes is where hand decoding goes wrong, usually by reading the bits in the wrong order. A few lines of C make the conversion explicit:

spi_decode.cc
#include <stdint.h>
#include <stdio.h>

static uint8_t bits_to_byte_msb_first(const char *bits) {
    uint8_t v = 0;
    for (int i = 0; i < 8; i++)
        v = (uint8_t)((v << 1) | (bits[i] == '1'));
    return v;
}

int main(void) {
    const char *mosi[4] = { "10011111", "00000000", "00000000", "00000000" };
    const char *miso[4] = { "00000000", "11101111", "01000000", "00011000" };

    printf("MOSI:");
    for (int i = 0; i < 4; i++)
        printf(" %02X", bits_to_byte_msb_first(mosi[i]));
    printf("\nMISO:");
    for (int i = 0; i < 4; i++)
        printf(" %02X", bits_to_byte_msb_first(miso[i]));
    printf("\n");
    return 0;
}
Text
MOSI: 9F 00 00 00
MISO: 00 EF 40 18

If your decoder shows F9 instead of 9F, the bits are being assembled least significant first, which is a setting in the decoder, not a fault in the bus. If it shows CF for 0xEF, a single sample point is off by one edge, and the likely cause is the wrong clock phase.

I2C: two wires, and the data changes while the clock is low

I2C uses one clock line and one data line, both open drain with pull-up resistors, so a high level is the line released and a low level is a device pulling it down. Every transfer follows rules about when SDA may change relative to SCL:

  • Data bits are stable while SCL is high, and SDA changes only while SCL is low.

  • A start condition is SDA falling while SCL is high.

  • A stop condition is SDA rising while SCL is high.

  • Each byte is eight data bits, most significant first, followed by a ninth clock in which the receiver pulls SDA low to acknowledge (ACK) or leaves it high for no acknowledgement (NACK).

Those are the only two moments SDA changes while SCL is high, which is exactly why start and stop are easy to find in a waveform: they are the odd edges.

Reading a temperature from a TMP102 at 7-bit address 0x48 takes two phases. The controller writes the pointer register to select the temperature register, then issues a repeated start and reads two bytes. The decoded sequence, one row per byte, is:

PhaseByte on SDAMeaningDriven byACK bit
STARTnoneSDA falls with SCL highcontrollernone
110010000 = 0x90address 0x48 shifted left, bit 0 = 0 for writecontrollerACK from sensor
200000000 = 0x00pointer register 0, the temperature registercontrollerACK from sensor
REPEATED STARTnoneSDA falls again, no stop betweencontrollernone
310010001 = 0x91address 0x48 shifted left, bit 0 = 1 for readcontrollerACK from sensor
400011001 = 0x19temperature high bytesensorACK from controller
500000000 = 0x00temperature low bytesensorNACK from controller
STOPnoneSDA rises with SCL highcontrollernone

The address byte is the 7-bit address followed by the read/write bit, so 0x48 becomes 1001000 and then a 0 or a 1, which gives 0x90 and 0x91. That is the most common source of confusion: a datasheet that says "address 0x48" and a capture that shows 0x90 are the same device. The controller ends a read by sending a NACK on the last byte, telling the sensor to stop driving SDA, and then a stop condition.

The two data bytes together are 0x1900. The TMP102 returns a 12-bit two's complement temperature in the upper bits of that 16-bit word, with a resolution of 0.0625 degrees Celsius per least significant bit. Shifting right by four gives 0x190 which is 400, and 400 times 0.0625 is 25.0 degrees Celsius.

tmp102_decode.cc
#include <stdint.h>
#include <stdio.h>

int main(void) {
    uint8_t hi = 0x19;
    uint8_t lo = 0x00;
    int16_t raw = (int16_t)((hi << 8) | lo);
    raw >>= 4;
    printf("raw=%d temperature=%.4f C\n", raw, raw * 0.0625);
    return 0;
}
Text
raw=400 temperature=25.0000 C

The arithmetic right shift keeps the sign for negative temperatures, which matters because the sensor reports below zero in two's complement.

What the decoder is telling you when it fails

Match the symptom to a cause before opening the code.

SymptomLikely cause
SPI bytes are bit-reverseddecoder set to LSB first
SPI bytes are shifted by one bitwrong CPHA, or wrong sampling edge
SPI MISO is all ones or all zerosperipheral not selected, wrong mode, or MISO not wired
I2C shows NACK on the address bytewrong address, device not powered, or missing pull-ups
I2C start never appearsSDA and SCL swapped on the analyser channels
both lines look noisy and slow-edgedpull-up resistors too large for the bus capacitance

The NACK on the address byte is the single most common I2C failure on a new board. Check the address with the device's read and write form, and check that both lines are pulled up. A bus with no pull-ups sits low or floats; a decoder that finds nothing is reporting that accurately.

Also inDebugging
Taggedcbeginner
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.