decode_i2c
Builtin index · Signal processing
result = decode_i2c(scl, sda, [hysteresis=0])
Input: scl, sda, hysteresis · Output: result
What it is
Added in v0.3.0. scl, sda: two Digital values from to_digital, clock first, data second — I2C framing is defined by SDA moving while SCL is held high, so neither wire decodes alone and a single-argument call is an error, as is a clock and data of different lengths (they are two channels of one capture; differing lengths mean they are not aligned in time). hysteresis as for find_edges. There is deliberately no bit-rate keyword: I2C carries its own clock, and a rate keyword would invite stating something the decoder must not believe over SCL itself.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = decode_i2c(scl, sda, [hysteresis=0]) | scl —Added in v0.3.0. scl, sda: two Digital values from to_digital, clock first, data second — I2C framing is defined by SDA moving while SCL is held high, so neither wire decodes alone and a single-argument call is an error, as is a clock and data of different lengths (they are two channels of one capture.sda —Added in v0.3.0. scl, sda: two Digital values from to_digital, clock first, data second — I2C framing is defined by SDA moving while SCL is held high, so neither wire decodes alone and a single-argument call is an error, as is a clock and data of different lengths (they are two channels of one capture.hysteresis optional (default 0)hysteresis as for find_edges. | Model —kind "i2c"transactions —a listaddresses vectora parallel vectorModel —kind "i2c_transaction"read boolbooldirection —"read"/"write"address_acks —one entry per address bytedata vectora vector of bytesacks —1/0 per data byte, where 1 means the receiver pulled SDA low to acknowledgerepeated_start —whether *this* segment was preceded by an Sr | Returns a Model (kind "i2c") with transactions (a list), addresses (a parallel vector) and count. Each transaction is a Model (kind "i2c_transaction") with address, read (bool), direction ("read"/"write"), ten_bit, address_complete, address_ack, address_acks (one entry per address byte), data (a vector of bytes), acks (1/0 per data byte, where 1 means the receiver pulled SDA low to acknowledge), byte_count, repeated_start (whether *this* segment was preceded by an Sr), terminator, partial_bits, start_index/stop_index and start_time/stop_time. Two things that look like bugs and are not: a repeated START ends one transaction record and begins another — the bus is never released, but the address and direction are restated, and flattening both halves of a register read into one record would discard exactly the framing this decoder exists to preserve, so terminator is a string ("stop", "repeated_start", "truncated") rather than a boolean; and partial_bits is normally 1 after a clean STOP, because generating STOP requires a real SCL rising edge that is sampled as a data bit before SDA rises to end the transfer. A transaction the capture cut off is still reported, with terminator = "truncated" and whatever bytes did decode — a clipped capture is ordinary, and dropping it silently would look identical to there having been no traffic. 10-bit addressing is handled, including the asymmetry that trips naive decoders: a 10-bit write sends the reserved 11110xx0 prefix byte *and* a second byte of low address bits, but the read half (after a repeated START) sends only 11110xx1 — so the number of address bytes is decided by the R/W bit, not fixed, and the low byte is carried forward from the write phase, with address_complete reporting false if no such phase was captured. |
Examples
No documentation example calls this form yet. The Signal processing chapter is where one belongs.
How it works
decode_i2c(scl, sda, [hysteresis=]) -- §8's I2C protocol decoder. TWO positional arguments, both Digital, because I2C is a two-wire bus and neither wire alone carries a transaction: the clock says WHEN a data bit is valid and the data line says WHAT it is, and the START/STOP conditions are defined by their relationship (SDA moving while SCL is held high), so there is no way to express this as a method on one signal. Clock first, data second, matching how every datasheet, scope decoder and the bus's own name orders them. Unlike CAN below, there is no bitrate=: I2C is a clocked bus and the clock is right there in the capture. A bit rate keyword would be an invitation to state something the decoder must not believe over SCL itself. The decode walks the two level streams in lockstep rather than working from an edge list, per digital_levels. Three things can happen at sample i: * SCL high and steady, SDA falls -> START (or, mid transaction, a repeated START) * SCL high and steady, SDA rises -> STOP * SCL rises -> a data bit, sample SDA These are mutually exclusive by construction: the first two require SCL high at both i-1 and i, which is exactly what an SCL rising edge is not. That is the whole reason the protocol reserves SDA transitions during SCL-high for framing -- data is only allowed to move while the clock is low -- and it is why this ordering is safe rather than a priority guess.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.