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

Output:

34928829895030
34928906677252 🟒 READ 794 b'29e59ddd' bytearray(b'29e59ddd') bytearray(b'E3E860EA30C96C08F71ECEC17D667606434FD3D4C02A4B221455FB4991FAA0D10A2E569D825C48D22334F50095F61D362481DA7779CA23BCC14EC65E794FF1D2EDB47FDEC2D37D1D99C33BA435899EA501F805D32EA663B3268CDD63700B55BE1D75A479C93B43E808073E3EEB3E440F17011F6EA29DD1454426CF44C96EC8A84927E2922F5FEF7FBD7E73C9EE3C90B16FEBB7956ADDF36F24212EF5242E29F948FFBD476AF723C5CB9F2080C15071E47F056934A6AD532875AFFC9D8473FF6B25E6D9D11723E871CEB40E53853C7DFA11344BED8EEA4E3B5CA2CF665B256754C86D52EAEB6346017602C05B40E2B646A2CFADB6B04083920A66E7E30B94EEBF7B6932D15DB5434E247D9CB9AB4FDA90404B7730D5DB2FCF0F1EAAFBEBE2120EC269C6738FFA05D78F3C70B54A75EDA78651C676AD27D31DA579B7260782F2931A633AD618215D258C1C380FBE82289EBE0762330F430775BADB53C605A56DF024D47877C7A16499DF58B0E804A31512C1449791BBA04F3EF0B472D3AE8B7AFFC7B28D252FAC223E4829e59ddd')
34929311706545
34929321838386 🟒 READ 104 b'd8e1c65e' bytearray(b'd8e1c65e') bytearray(b'18A124B71FAB49141AA7E67D883C8DDC1675C34AEDCDC0C8EDA940D48BC36D43DAAFD925084D396382AC0927B1671474d8e1c65e')
34930941955574

@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