EEPROM Helpers (Lua API)
Read, program and verify a 24-series I2C EEPROM (AT24Cxx and clones) from Lua, over a CH347 USB bridge or the Linux
Peripherals transport (/dev/i2c-N). An EEPROM needs no erase: a write simply overwrites the cells.
eeprom_new{...} returns a chip handle bound to one device address and geometry. Each method queues one request and
returns true; the transport then streams results back (data chunks, progress, a final result) that the script sees in
on_receive(). Requests on one connection run one after another, so a :read() queued after a :program() reads the
new contents.
For a single register access on any I2C device, use the plain I2C helpers instead.
Creating a Chip Handle
eeprom_new{ dev, page, addr_bytes, chunk, write_ms, connection_id } → chip
All fields are optional. The geometry must match the chip: CycBox has no chip database on the Rust side.
Call it from a hook such as on_start(), not at the top level of the script: helper functions are registered after the
script body has run, so eeprom_new is still nil there.
dev(number) - 7-bit I2C device address (default:0x50, an AT24 with A2/A1/A0 grounded)page(number) - Page-write size in bytes (default:32). A write never crosses a page boundary, so a value smaller than the real page is safe but slower; a larger one corrupts data.addr_bytes(number) - Word-address width:1for chips up to 2 Kbit (24C01/24C02),2for 24C32 and larger (default:2)chunk(number) - Read/verify window in bytes; each window arrives as oneread_chunkmessage (default:4096)write_ms(number) - Settle time after each page write in ms (default:5).0polls the chip for an ACK instead, which is faster for bulk writes.connection_id(number) - Connection the requests are sent on (default:0)
Common geometries:
| Chip | Size | addr_bytes | page |
|---|---|---|---|
| 24C01 | 128 B | 1 | 8 |
| 24C02 | 256 B | 1 | 8 |
| 24C32/24C64 | 4/8 KB | 2 | 32 |
| 24C128/256 | 16/32 KB | 2 | 64 |
| 24C512 | 64 KB | 2 | 128 |
24C04/08/16 fold the high address bits into the device address: treat each 256-byte block as its own chip at
0x50, 0x51, … with addr_bytes = 1.
Chip Methods
chip:read(addr, length) → boolean
Read length bytes starting at word address addr. The data arrives as one or more read_chunk RX messages of at
most chunk bytes.
chip:program(addr, data, opts) → boolean
Write data starting at addr, split at page boundaries. By default the region is read back and compared afterwards.
addr(number) - Word address of the first bytedata(string, table or number) - Bytes to write: a binary string, a table of byte values ({0xDE, 0xAD}) or a single byteopts(table, optional) -{ verify = false }skips the read-back (default:verify = true)
chip:verify(addr, data) → boolean
Read the region back and compare it against data (same forms as :program), without writing.
chip:cancel() → boolean
Abort the operation in flight on this chip. It is handled immediately, not queued behind it; the aborted operation
ends with result = "cancelled".
Handling Responses
Every result carries eeprom_op and eeprom_dev metadata. Data chunks are RX messages; progress, completion and errors
are events (message.message_type == "event"), which also reach on_receive() but are not shown in the message list —
log what matters.
eeprom_op | Message kind | Content |
|---|---|---|
read_chunk | RX | Payload is the bytes read; eeprom_addr metadata is the word address of the first byte |
progress | event | eeprom_phase (read/program/verify), eeprom_done and eeprom_total (bytes) metadata |
complete | event | eeprom_phase metadata; result value ok, mismatch or cancelled; on mismatch mismatch_at value |
error | event | eeprom_error event; eeprom_phase metadata; the error value holds the message |
A :program() with verify ends with a single complete whose phase is verify; the read-back data also arrives as
read_chunk messages, so the message list shows what actually landed on the chip.
Example: write a pattern and check it
local chip
function on_start()
-- 24C02 on connection 0
chip = eeprom_new{ dev = 0x50, addr_bytes = 1, page = 8 }
-- 16 bytes: 0x00, 0x11, ..., 0xFF
local pattern = {}
for i = 0, 15 do pattern[#pattern + 1] = i * 0x11 end
chip:program(0x00, pattern) -- verifies by default
end
function on_receive()
if message:get_metadata("eeprom_op") ~= "complete" then return false end
local phase = message:get_metadata("eeprom_phase")
local result = message:get_value("result")
if result == "mismatch" then
log("error", string.format("EEPROM %s: mismatch at 0x%02X", phase, message:get_value("mismatch_at")))
else
log("info", string.format("EEPROM %s: %s", phase, result))
end
return false
end
Example: dump the chip as hex
function on_start()
local chip = eeprom_new{ dev = 0x50, addr_bytes = 1, page = 8, chunk = 16 }
chip:read(0x00, 256)
end
function on_receive()
if message:get_metadata("eeprom_op") ~= "read_chunk" then return false end
local addr = message:get_metadata("eeprom_addr")
local hex = {}
for i = 1, #message.payload do
hex[#hex + 1] = string.format("%02X", string.byte(message.payload, i))
end
log("info", string.format("%04X: %s", addr, table.concat(hex, " ")))
return false
end