Protocol

CRUMBS protocol 1.1. The frame below has been the wire format since library 0.7; SET_REPLY (opcode 0xFE) was added in 0.10.0, receivers have rejected over-long buffers since 0.12.4, and protocol 1.1 adds the SET acknowledgement (GET_ACK, opcode 0xFD). Nothing on the wire changes incompatibly without a new protocol major version; an addition that older peripherals and controllers can ignore is a minor version. This document is normative; the library version (CRUMBS_VERSION) is a separate number and moves independently.

CRUMBS is a message layer over plain I²C. The I²C address selects the device; everything inside the transaction is one CRUMBS frame.

Frame

The CRUMBS frame: type_id, opcode and data_len of one byte each at offsets 0 to 2, then 0 to 27 data bytes from offset 3, then the crc8 byte at offset 3 + data_len. The CRC covers every byte before it; the frame is 4 + data_len bytes, 4 to 31.

The frame.

Field Meaning
type_id Device class the frame is addressed to or comes from. 0x00 = any.
opcode Command or query identifier, scoped to the type_id. 0xFD and 0xFE are reserved.
data_len Payload length, 0–27.
data Opaque bytes; the application defines the layout.
crc8 CRC-8 over the preceding 3 + data_len bytes.

Frame length is 4 + data_len: 4 bytes minimum, 31 maximum (CRUMBS_MESSAGE_MAX_SIZE). The 31-byte ceiling is chosen so a frame fits the 32-byte buffer of the AVR Arduino Wire library.

A receiver rejects a frame when the buffer is shorter than 4 bytes, data_len exceeds 27, the buffer is shorter or longer than 4 + data_len, or the CRC does not match. Only a CRC mismatch counts as a CRC error in the receiver’s statistics; structural failures do not.

CRC-8

Parameter Value
Width 8
Polynomial 0x07 (x⁸ + x² + x + 1)
Init 0x00
Reflect in/out no / no
XOR out 0x00
Check value crc8("123456789") = 0xF4

These are the parameters of CRC-8/SMBUS. The CRC covers type_id, opcode, data_len and data, in that order, and excludes the CRC byte. The reference implementation is a 16-entry nibble table generated by pycrc (src/crc/crc8_nibble.c); crumbs_crc8() is the public entry point.

Multi-byte values

The payload is opaque to the protocol. The library’s helpers (crumbs_msg_add_u16() and friends) write integers little-endian and floats as their 4 native bytes; a family that uses them inherits that convention.

Transactions

Two I²C transaction shapes carry every CRUMBS exchange.

SET — one write. The controller writes a frame; the peripheral validates it and dispatches on opcode. Nothing comes back on the bus; a controller that needs to know the SET arrived reads the acknowledgement afterwards (GET_ACK).

GET — a write, then a read.

A GET in two steps. Step 1, write: the controller sends the five-byte SET_REPLY frame 0x00, 0xFE, 0x01, op, crc; after the stop, the peripheral's receive callback decodes it and stores op. Then the controller waits 10 ms. Step 2, read: the controller reads 31 bytes. After the address, on AVR, SCL is held low while the peripheral's request callback builds the reply; then come the reply frame of 4 + len bytes, which is decoded, and a tail of 0xFF bytes, which the controller trims first.

A GET as the generated getters send it: two I2C transactions, one after the other, each read left to right and not to scale. The wait is CRUMBS_DEFAULT_QUERY_DELAY_US. A is the 7-bit address; S and P are start and stop. The clock stretch and where the receive callback runs on megaAVR are described below.

The peripheral stores op as its requested opcode and builds the reply when the read arrives. The requested opcode persists until the next SET_REPLY, so a controller can read the same value repeatedly with one SET_REPLY.

SET_REPLY (0xFE)

Reserved. The library intercepts it before any user code runs: it is never delivered to on_message or to a handler, and a handler registered for 0xFE is never called.

  • The generated wrappers send it with type_id 0x00 and a one-byte payload (hand-written senders may use the target’s type). A receiver accepts any data_len ≥ 1 and uses data[0]; an empty payload leaves the requested opcode unchanged.
  • The initial requested opcode after initialisation is 0x00.
  • A SET_REPLY frame is subject to the type check below like any other frame.

The read

A controller reads 31 bytes and trims to the length the reply’s own header declares; on both Arduino and Linux a peripheral that has finished its frame leaves the remaining bytes reading as 0xFF, and a decoder that receives the untrimmed buffer rejects it as over-long. A peripheral with neither a reply handler for the requested opcode nor an on_request callback gives the HAL nothing to write (the AVR Wire core then clocks out a single 0x00), and the read fails to decode either way; a handler that returns without filling the reply sends the empty frame 00 00 00 00.

When the reply is built

The peripheral builds its reply inside the I²C request callback — after the controller has addressed it for reading and before the first byte is clocked out. On AVR that callback runs in the TWI interrupt with SCL held low by the hardware (clock stretching), so the controller’s read waits for the reply to be encoded.

Incoming frames are processed in the receive callback, also in the TWI interrupt, once the controller’s write has ended. A transfer that addresses the peripheral while that is still running, whether the read or the next write, is stretched until the interrupt returns. Where the stretch starts depends on the core. The ATmega328P core releases the bus before it calls the receive callback, so the hardware acknowledges the next address and then holds SCL. The megaAVR core (Nano Every; TWI_SlaveInterruptHandler in the Wire library’s utility/twi.c) acknowledges an address in software, after the callback returns, so it holds SCL before the acknowledgement. It also calls the receive callback from the interrupt for the STOP or for the next address. After a repeated start that is the read’s own address match, so the whole of receive processing runs inside the read’s stretch. The megaAVR description is of core version 1.8.8, from version= in that core’s platform.txt.

Three cases, each read left to right. ATmega328P, a write, STOP, then the next transfer: the receive callback starts after the STOP; the hardware ACKs the next address, then holds SCL low until the callback returns. megaAVR, the same sequence: the callback starts at the STOP, and SCL is held low after the address, before the ACK, until the callback returns. megaAVR, a write then a repeated-start read: the callback starts at the read's address match, so SCL is held low for all of it, then the ACK.

Where the stretch starts, when a transfer addresses the peripheral before its receive callback has returned. Not to scale.

The controller must therefore tolerate clock stretching. The generated getters pause CRUMBS_DEFAULT_QUERY_DELAY_US (10 ms) between the SET_REPLY write and the read, so the SET_REPLY’s processing has normally finished before the read starts. A transfer sent straight after a write waits for that write’s processing in the stretch: for example the SET_REPLY staging write that crumbs_controller_send_confirmed() and the Python binding’s Controller.send_confirmed() send, with no pause, after the SET they confirm. The pause is margin, chosen conservatively; the header says to reduce it only with an oscilloscope on the bus.

Two limits apply. SMBus, whose CRC this protocol shares, caps a target’s cumulative stretching at 25 ms per message (T_LOW:SEXT) and a controller’s at 10 ms per byte (T_LOW:MEXT). A read sent straight after a write is stretched for receive processing as well as reply construction, so keeping the receive and reply handlers well inside 25 ms between them keeps CRUMBS usable on SMBus-timed controllers.

And a Raspberry Pi 4 controller (i2c-bcm2835) loses bytes around a peripheral’s clock stretch unless the bus is slow. Measured on one rig on 2026-10-07 (Pi 4, kernel 6.18, core clock pinned with core_freq_min=500, two ATmega328P peripherals on CRUMBS 0.12.5), with 300 GET_STATE queries and 100 SET frames per board at each rate:

Measured SCL SETs applied Bad GET_STATE replies
98 kHz 96/100, 96/100; every loss a write that NACKed and was retried not measured
79 kHz 200/200 33/300, 29/300
66 kHz 200/200 92/300, 10/300
49.7 kHz 200/200 0/300, 0/300

Both failures fit the peripheral missing the first SCL edge after its stretch and ending up one bit behind. A bad reply is correct up to some byte, then 0xFF, and fails the CRC. A SET whose write NACKs is not delivered by a controller retry, though the controller sees the retry succeed: the peripheral’s ACK masks the controller’s STOP, the retry arrives in the same receive buffer as the failed frame, and the decoder rejects the result. It is stretch timing, not signal margin: at 49.7 kHz, masking interrupts on the peripheral for 10–30 µs per loop brought both failures back (8–28 of 300 replies bad, up to 5 write NACKs). Whether the edge is lost in the Pi’s controller or in the AVR’s TWI is not established, and other peripheral firmware was not swept. The Pi 4 setting is in platform-setup.md.

GET_ACK (0xFD): the SET acknowledgement

Since protocol 1.1 (library 0.16.0) a peripheral keeps a record of the command frames it receives, and the library answers the reserved query 0xFD with it. A controller stages 0xFD with SET_REPLY like any GET and reads:

The GET_ACK reply, eight bytes: type_id at offset 0 (the peripheral's type, or 0x00), 0xFD, 0x04, then the four-byte ACK record — counter, opcode, status, reason — at offsets 3 to 6, and crc8 at offset 7 over bytes 0 to 6.

Byte Content
counter Command frames counted since the peripheral started, modulo 256.
opcode The last counted frame’s opcode.
status 0x00 none yet, 0x01 applied, 0x02 refused, 0x03 rejected.
reason 0x00 when applied. Refused: a byte the firmware chose (its meaning is the family’s). Rejected: 0x01 nothing took the opcode, 0x02 the type check failed.

Status 0x00 occurs only before the first command frame, and then all four bytes are 0x00. A reply with any other status, or with status 0x00 and a non-zero byte, is not an acknowledgement, and a controller treats it as one from a peripheral without GET_ACK.

“Applied” is the library’s view: the frame reached on_message or a handler and neither called crumbs_refuse(). A peripheral that dispatches only through on_message therefore reports every frame it receives as applied, including opcodes it ignores, unless it calls crumbs_refuse() for them (crumbs_has_handler() tells it which opcodes it registered handlers for); and a handler that only queues work reports the command applied when it returns.

The reply’s type_id is the peripheral’s declared type, or 0x00 if it has declared none.

A command frame is every frame that decodes, CRC included, other than SET_REPLY. Each one advances the counter and becomes the last command:

  • applied when on_message or a handler for its opcode received it and did not refuse it;
  • refused when one of them called crumbs_refuse(ctx, reason): the command arrived and was deliberately not acted on;
  • rejected when neither exists for its opcode (0x01), or when it failed the type check (0x02).

A frame that does not decode — a CRC error, a wrong length, or a retried write merged into a failed one (above) — is not counted: to the acknowledgement it never arrived. SET_REPLY is not counted, so reading the acknowledgement does not move it.

A controller reads the acknowledgement before a SET (the baseline) and after it. The counter is compared for change only, so it may wrap; a peripheral that restarts starts again from all zeros:

After the SET Means
all four bytes equal to the baseline, status not 0x00 not advanced: the frame did not arrive intact; send it again
status 0x00 unknown: the peripheral has counted nothing since it started, which is both a lost first frame and a frame applied before a restart; do not re-send
counter equal to the baseline, record different unknown: the counter came round, or the peripheral restarted and counted back to it; do not re-send
counter changed, same opcode the status says applied, refused or rejected
counter changed, another opcode mismatch: another frame was dispatched in between; do not re-send

So the first SET to a peripheral that has counted nothing since it started is never re-sent. A restart that brings the record back to exactly the baseline still reads as not advanced; the counter cannot rule that out.

flowchart TD
    S(["ACK record read<br/>after the SET,<br/>vs. the baseline"]) --> Astatus is<br/>none (0x00)?
    A -- yes --> U["unknown:<br/>do not re-send"]
    A -- no --> Bcounter same<br/>as baseline?
    B -- yes --> Call four bytes<br/>same as baseline?
    C -- yes --> N["not advanced:<br/>send again"]
    C -- no --> U
    B -- no --> Dopcode is<br/>the SET's?
    D -- no --> M["mismatch:<br/>do not re-send"]
    D -- yes --> SS["SET counted:<br/>the status says<br/>applied, refused<br/>or rejected"]

The table above, for two valid records, in the order crumbs_ack_classify() and the Python binding’s classify() test it. Unknown is CRUMBS_RX_ACK_UNKNOWN (Python raises AckUnknown), mismatch is CRUMBS_RX_ACK_MISMATCH (AckMismatch).

A failed read of the acknowledgement is read again, never answered by sending the SET again, so a frame is not applied twice because a read failed. crumbs_controller_send_confirmed() and the Python binding’s Controller.send_confirmed() do exactly this, with a bounded number of sends, after checking the peripheral’s version reply (below).

Compatibility. A peripheral built with a library older than 0.16.0 does not answer 0xFD: it returns whatever its reply handlers or on_request build for an unknown opcode, or nothing. What a controller then reads depends on that fallback:

The older peripheral’s fallback crumbs_controller_read_ack() returns
another opcode’s reply (a version reply from on_request) CRUMBS_RX_REPLY_MISMATCH (-7)
an echo of 0xFD with a short payload or one that is not a valid record (the basic_peripheral example’s on_request) -7
no reply -1: the read does not decode
an echo whose first four bytes happen to form a valid record success, with a counter that never moves

So only its version reply tells a controller reliably which it is: a CRUMBS_VERSION below 1600 (CRUMBS_ACK_MIN_VERSION) has no acknowledgement, and its SETs can only be sent unconfirmed. crumbs_controller_send_confirmed() reads the version reply first and sends nothing to such a peripheral (-7), so the echo in the last row is not re-sent to its bound; a controller that reads the acknowledgement itself checks the version the same way. Nothing else on the wire changed, so old controllers and new peripherals work together unchanged.

Type check

A peripheral may declare its own type_id (crumbs_set_type_id()). Once declared, an incoming frame is dropped — before SET_REPLY handling and before any callback — when its type_id is neither 0x00 nor the declared value. A peripheral that has not declared a type accepts every frame. The check runs after CRC validation, so a corrupt frame is reported as a CRC error whatever its type.

On the controller side, crumbs_controller_read_expect() applies the mirror check to a reply: opcode must match, and type_id must match unless the expected type is 0x00.

Number spaces

I²C address

CRUMBS uses 7-bit addressing. I²C reserves 0x00–0x07 and 0x78–0x7F, leaving 0x08–0x77. Within it, avoid addresses another standard on the same bus may drive.

The 7-bit address space 0x00 to 0x7F as a strip. 0x00–0x07 and 0x78–0x7F are reserved by I²C. Bands to avoid: 0x08–0x0B SMBus host, charger, battery; 0x0C SMBus Alert Response; 0x48–0x4B SMBus prototype; 0x50–0x57 EEPROM and DIMM SPD; 0x61 SMBus ARP default. 0x10–0x17 are the examples' defaults. The rest is free.

Why each band is one to avoid, worst first:

Avoid Why
0x0C SMBus Alert Response Address: a host reads it expecting every alerting device to answer
0x08–0x0B SMBus Host, Smart Battery Charger, Selector and Battery (SMBus 3.3.1, Table 17)
0x48–0x4B SMBus prototype addresses, “not intended for production parts and should never be assigned to any device” (SMBus §6.2.2.3); LM75-class temperature sensors occupy 0x48–0x4F
0x50–0x57 24Cxx EEPROMs and DIMM SPD, the most contested block on a real bus
0x61 SMBus Device Default Address (ARP); the Atlas Scientific EZO-DO also ships here

0x10–0x17 is reserved by neither the I²C specification nor SMBus, and is where the library’s examples default. Every address is a default: the integrator owns the map, and the controller-side scanners exist so a conflict is found on the bench rather than in the field.

type_id

0x00 is the wildcard and must not be assigned to a device. 0x01–0xFF identify a device class, not an individual — two devices of the same type are told apart by address. Each type owns an independent opcode space.

opcode

The opcode space of one type, not to scale: 0x00 version; 0x01–0x7F SET commands; 0x80–0xFC GET queries; 0xFD GET_ACK and 0xFE SET_REPLY, reserved by the library; 0xFF a GET query. Everything but 0xFD and 0xFE is free per type, and the names are the convention.

0xFE (SET_REPLY) and 0xFD (GET_ACK, the SET acknowledgement, since protocol 1.1) are reserved. Every other opcode is free per type. By convention 0x00 is version information, SET commands sit low and GET queries from 0x80 up, so a reply’s opcode says what it is.

0x00 is an ordinary opcode to the library; only the convention gives it meaning.

Opcode 0x00: version information

A peripheral should answer opcode 0x00 with five bytes: the CRUMBS_VERSION of the library it was built with, little-endian, then its module version as major, minor and patch.

The version reply, nine bytes: type_id, 0x00, 0x05, then the five-byte payload — CRUMBS_VERSION low byte and high byte at offsets 3 and 4, then the module's major, minor and patch at offsets 5 to 7 — and crc8 at offset 8 over bytes 0 to 7.

crumbs_build_version_reply(reply, type_id, major, minor, patch) produces exactly this frame. A controller that reads it can tell the device’s type, its firmware version and which CRUMBS release it runs; a peripheral that implements it answers a bare read with it, since the requested opcode starts at 0x00. For compatibility, treat the module version as semantic: a major mismatch is incompatible, and a peripheral’s minor must be at least the controller’s. crumbs_controller_scan_for_crumbs_with_types() reports the type_id of whatever frame each device returns.

CRUMBS_VERSION is major × 10000 + minor × 100 + patch (0.17.0 → 1700). For the module version, bump major for an incompatible opcode or payload change, minor for additions, patch for fixes.

Discovery

The controller-side scanners take one of two probes:

  • Read probe (crumbs_controller_scan_for_crumbs*, both modes): read the address and accept it if the bytes decode as a CRUMBS frame. The CRC is the discriminator, so a foreign device whose first bytes happen to be CRC-consistent is reported as present; the tests keep a deliberate example. In non-strict mode an address whose read did not decode is written an all-zero frame (00 00 00 00) and read once more. A CRUMBS peripheral dispatches that frame as an opcode-0x00 SET with no payload, so a family that binds SET opcode 0x00 will see it; a 24Cxx EEPROM takes it as a page write.
  • Address probe (crumbs_arduino_scan, crumbs_linux_scan): I²C-level only. Strict mode reads one byte, which consumes a byte from whatever device answers; non-strict mode is an address-only ACK check. Neither decodes a frame.

See api-reference.md for the signatures and return values.