Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created June 20, 2025 18:36
Show Gist options
  • Save anecdata/619bdae0df817175e68a86ff125b41b9 to your computer and use it in GitHub Desktop.
Save anecdata/619bdae0df817175e68a86ff125b41b9 to your computer and use it in GitHub Desktop.
CircuitPython i2ctarget with large transaction payloads
# atmel-samd i2ctarget (only reads):
import time
import binascii
import board
import busio
from i2ctarget import I2CTarget
I2C_ADDRESS = 0x40
# Feather M4 Express
with I2CTarget(scl=board.SCL, sda=board.SDA, addresses=(I2C_ADDRESS, 0x41)) as device:
print(f"{time.monotonic_ns()} START 0x{I2C_ADDRESS:02X}")
while True:
req = device.request(timeout=-1) # Zero means wait forever, a negative value means check once
if not req:
print(f"{time.monotonic_ns()}", end="\r")
continue
with req:
print()
if not req.is_read: # incoming request is a write # req.is_restart == False
inbuf = req.read(n=-1) # n – Number of bytes to read (negative means all)
body = inbuf[0:len(inbuf)-8]
crc_calc = f"{binascii.crc32(body):08x}".encode()
crc_read = inbuf[len(inbuf)-8:len(inbuf)]
if crc_calc == crc_read:
print(f"{time.monotonic_ns()} 🟢 READ {len(inbuf)} {crc_calc} {crc_read} {inbuf}")
else:
print(f"{time.monotonic_ns()} 🟠 READ {req.address} {len(inbuf)} {crc_calc} {crc_read} {inbuf}")
@anecdata
Copy link
Author

Multi-controller, multi-target:
https://fosstodon.org/@anecdata/114688874119367988

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment