decode_spi
Builtin index · Signal processing
result = decode_spi(clk, [mosi], [miso], [mode=], [cpol=0], [cpha=0], [bits=8], [bit_order="msb"], [cs=], [cs_active="low"])
Input: clk, mosi, miso, mode, cpol, cpha, bits, bit_order, cs, cs_active · Output: result
What it is
clk: a Digital clock. Decodes clocked SPI traffic into bytes, sampling each data line at whichever clock edge CPOL and CPHA select. SPI carries its own clock, so unlike decode_uart there is no bit rate to state — but there are multiple input signals. At least one data line is required, given positionally (decode_spi(clk, mosi) or decode_spi(clk, mosi, miso)) or by name (miso= alone, for a capture that only tapped the peripheral's output). Every line must be the same length; a mismatch is an error rather than a truncation to the shortest, since two captures of different lengths were not taken together. mode (optional, 0–3) is the datasheet spelling of cpol/cpha, with mode = 2*CPOL + CPHA; passing mode= and cpol=/cpha= is an error rather than a precedence rule, because the two can contradict. CPHA=0 samples on each clock cycle's leading edge and CPHA=1 on its trailing edge, with CPOL fixing which physical direction that is — so modes 0 and 3 sample rising, modes 1 and 2 falling. bits (optional, 2–32, default 8); bit_order (optional string) "msb" (default) or "lsb", both genuinely in use on real peripherals. cs (optional Digital) segments the capture into frames: each assertion starts a new frame and bumps frame, clock edges while CS is idle are ignored entirely, and a partial byte left over when CS releases is discarded; cs_active (optional string) is "low" (default) or "high". Without cs= the capture decodes as one continuous byte stream with frame 0 throughout — frame boundaries are deliberately not guessed from clock gaps, which has no correct answer on a bus whose idle time is unspecified.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = decode_spi(clk, [mosi], [miso], [mode=], [cpol=0], [cpha=0], [bits=8], [bit_order="msb"], [cs=], [cs_active="low"]) | clk —clk: a Digital clock.mosi optionalAt least one data line is required, given positionally (decode_spi(clk, mosi) or decode_spi(clk, mosi, miso)) or by name (miso= alone, for a capture that only tapped the peripheral's output).miso optionalAt least one data line is required, given positionally (decode_spi(clk, mosi) or decode_spi(clk, mosi, miso)) or by name (miso= alone, for a capture that only tapped the peripheral's output).mode optionalmode (optional, 0–3) is the datasheet spelling of cpol/cpha, with mode = 2*CPOL + CPHA.cpol optional (default 0)mode (optional, 0–3) is the datasheet spelling of cpol/cpha, with mode = 2*CPOL + CPHA.cpha optional (default 0)mode (optional, 0–3) is the datasheet spelling of cpol/cpha, with mode = 2*CPOL + CPHA.bits optional (default 8)bits (optional, 2–32, default 8).bit_order string, optional (default "msb")bit_order (optional string) "msb" (default) or "lsb", both genuinely in use on real peripherals.cs optionalcs (optional Digital) segments the capture into frames: each assertion starts a new frame and bumps frame, clock edges while CS is idle are ignored entirely, and a partial byte left over when CS releases is discarded.cs_active string, optional (default "low")cs_active (optional string) is "low" (default) or "high". | start Signalthe sampling edge of the byte's first bit — seconds for a Signal, samples otherwise | Returns a Table with columns frame, start (the sampling edge of the byte's first bit — seconds for a Signal, samples otherwise), and mosi/mosi_hex and/or miso/miso_hex for whichever data lines were given. A trailing partial byte is not reported. Added in v0.3.0. |
Examples
No documentation example calls this form yet. The Signal processing chapter is where one belongs.
How it works
decode_spi(clk, [mosi], [miso], [mosi=], [miso=], [mode=], [cpol=0], [cpha=0], [bits=8], [bit_order="msb"], [cs=], [cs_active="low"]) -- synchronous serial. Unlike UART this takes MULTIPLE digitized signals, because SPI carries its own clock: there is no baud rate to state and nothing to derive timing from -- every bit is sampled at a clock edge, so the clock line is a required second input rather than a keyword. The data lines may be given positionally (decode_spi(clk, mosi), decode_spi(clk, mosi, miso)) or by name (miso= alone, for a capture that only tapped the peripheral's output); at least one is required, since a clock with no data decodes to nothing. WHICH EDGE SAMPLES is the whole of CPOL/CPHA, and reduces to one line. CPHA=0 samples on the LEADING edge of each clock cycle, CPHA=1 on the TRAILING edge; CPOL fixes which physical direction "leading" is (idle low -> leading is rising, idle high -> falling). Composing those gives sample_on_rising == (cpol == cpha), i.e. modes 0 and 3 sample rising, modes 1 and 2 falling. FRAMING. With a cs= chip-select line, bytes are segmented by it: each assertion starts a new frame and bumps the frame column, clock edges while CS is idle are ignored entirely, and a partial byte left over when CS releases is discarded (a frame is not obliged to be a whole number of bytes, and padding one out with invented bits would be a fabricated row). Without cs= the capture is decoded as ONE continuous byte stream with frame 0 throughout -- deliberately not guessing frame boundaries from clock gaps, which is a heuristic with no correct answer on a bus whose idle time is unspecified.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.