Qu

decode_can

Builtin index · Signal processing

result = decode_can(bus, [bitrate=], [samples_per_bit=], [polarity="normal"], [hysteresis=0])

Input: bus, bitrate, samples_per_bit, polarity, hysteresis · Output: result

What it is

Added in v0.3.0. bus: one Digital value — the differential pair has already been resolved to a logic level by the transceiver or by the scope's own threshold. Bit length is required and comes from either bitrate= (125000, 250000, 500000, 1e6, …), which needs a Signal whose sample rate supplies the rest, or samples_per_bit=, which works on a plain vector too; with neither, or with fewer than 2 samples per bit, it errors rather than guessing. polarity (optional string): "normal" (default) means bus dominant = logical 0 = LOW, which is what a transceiver's RX pin gives you and leaves the bus recessive/high when idle; "inverted" for a capture taken off an inverting buffer or the other line of the pair. Getting polarity wrong does not reliably yield zero frames — the complemented stream still offers candidate start bits — but it cannot produce a CRC-valid one, so check crc_ok rather than the frame count. hysteresis as for find_edges. Handles both base (11-bit) and extended (29-bit) identifiers; which one a frame uses is not knowable up front, since both open with SOF and 11 identifier bits and it is the IDE bit — the *thirteenth* — that finally says, so the fields are read strictly sequentially.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = decode_can(bus, [bitrate=], [samples_per_bit=], [polarity="normal"], [hysteresis=0])
bus —Added in v0.3.0. bus: one Digital value — the differential pair has already been resolved to a logic level by the transceiver or by the scope's own threshold.
bitrate Signal or vector, optionalBit length is required and comes from either bitrate= (125000, 250000, 500000, 1e6, …), which needs a Signal whose sample rate supplies the rest, or samples_per_bit=, which works on a plain vector too.
samples_per_bit Signal or vector, optionalBit length is required and comes from either bitrate= (125000, 250000, 500000, 1e6, …), which needs a Signal whose sample rate supplies the rest, or samples_per_bit=, which works on a plain vector too.
polarity string, optional (default "normal")polarity (optional string): "normal" (default) means bus dominant = logical 0 = LOW, which is what a transceiver's RX pin gives you and leaves the bus recessive/high when idle.
hysteresis optional (default 0)hysteresis as for find_edges.
Model —kind "can"
frames —a list
ids vectora parallel vector
error_count —candidate frames abandoned on a stuff error
samples_per_bit —the resolved value, worth printing when a bitrate= is suspected wrong
Model —kind "can_frame"
Returns a Model (kind "can") with frames (a list), ids (a parallel vector), count, error_count (candidate frames abandoned on a stuff error) and samples_per_bit (the resolved value, worth printing when a bitrate= is suspected wrong). Each frame is a Model (kind "can_frame") with id, extended, rtr, dlc, byte_count, data, crc, crc_ok, crc_delim_ok, ack, ack_delim_ok, eof_ok, stuff_bits, start_index and start_time. dlc is reported raw and byte_count separately, rather than silently reconciling the two: a remote-transmission-request frame states a DLC but carries no payload at all, and classical CAN caps the payload at 8 bytes while DLC values 9–15 are legal on the wire and still mean 8. ack is true when the ACK slot was driven dominant, i.e. some node on the bus acknowledged. The decoder resynchronizes on every recessive-to-dominant edge, the way a real CAN controller does — on an ideal capture this changes nothing, and on a drifting one it is what stops the sample point walking off the bit over a ~130-bit frame.

Examples

No documentation example calls this form yet. The Signal processing chapter is where one belongs.

How it works

decode_can(bus, [bitrate=], [samples_per_bit=], [polarity="normal"], [hysteresis=]) -- §8's CAN decoder, base (11-bit) and extended (29-bit) frame formats. One positional argument, unlike I2C: CAN is a single-wire bus as far as a digitized capture is concerned (the differential pair has already been resolved to a logic level by the transceiver, or by the scope's own threshold), and it carries no clock -- which is exactly why this one needs a bitrate= and I2C does not. LEVELS: CAN is defined on dominant/recessive, not high/low. Dominant is logical 0 and, on a transceiver's RX pin, LOW; recessive is logical 1, HIGH, and is the idle state. That is polarity="normal". A capture taken off an inverting buffer -- or off CAN_L instead of CAN_H -- needs polarity="inverted", and gets a clear error rather than a silent stream of nonsense frames if it is left unset, because with the polarity backwards the bus never looks idle and no SOF is ever found. FIELD ORDER, which is where base and extended diverge and the divergence is not knowable up front: both open with SOF and 11 identifier bits, then one bit (RTR for base, SRR for extended) and then IDE -- and it is IDE, the THIRTEENTH bit, that finally says which format has been arriving all along. Base then runs r0, DLC(4); extended runs 18 more identifier bits, RTR, r1, r0, DLC(4). Hence the read is strictly sequential and cannot be a fixed-offset slice.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.