i2c-target

CLI reference

glasgow run i2c-target

Process transactions on the I²C bus as a software-defined target.

This applet allows emulating any I²C device in Python, provided that the I²C initiator supports clock stretching and tolerates delays caused by host roundtrips. (Unfortunately, this excludes many I²C initiators.)

The default emulated device is a dummy device that logs all transactions, acknowledges all writes, and returns 0xFF in response to all reads. Use the global -v option to display the transaction log. For example, emulate address 0x50 on A0 (SCL) and A1 (SDA), with integrated pull-ups enabled:

$ glasgow run i2c-target -V 3.3 --scl A0 --sda A1 --address 0x50 --pulls
usage: glasgow run i2c-target [-h] [-V SPEC] [--scl PIN] [--sda PIN]
                              -A I2C-ADDR [--pulls]
-h, --help

show this help message and exit

-V <spec>, --voltage <spec>

configure I/O port voltage to SPEC (e.g.: ‘3.3’, ‘A=5.0,B=3.3’, ‘A=SA’)

--scl <pin>

bind the applet I/O line ‘scl’ to PIN (default: ‘A0’, required)

--sda <pin>

bind the applet I/O line ‘sda’ to PIN (default: ‘A1’, required)

-A <i2c-addr>, --address <i2c-addr>

I²C address of the target

--pulls

enable integrated pull-ups

API reference

class glasgow.applet.interface.i2c_target.I2CTargetInterface(logger: Logger, assembly: AbstractAssembly, *, scl: GlasgowPin, sda: GlasgowPin, address: int, pulls: bool = False)

Software-defined I²C target at a 7-bit address.

Subclass this interface and implement all five event callbacks. Construct it with a logger, an assembly, and the scl, sda, and address keyword arguments inside the assembly’s add_applet() context. Call read_event() repeatedly to process transactions.

An applet subclass can set I2CTargetApplet.interface_cls to its custom interface class to reuse the applet’s argument parsing and event loop.

async read_event()

Process one bus event, await its callback, and transmit any response.

abstractmethod async on_start()

Called when the initiator starts a transaction with this target.

abstractmethod async on_stop()

Called when the initiator ends a transaction.

abstractmethod async on_restart()

Called when the initiator issues a repeated START.

abstractmethod async on_write(data)

Receive one byte. Return True to acknowledge it or False to reject it.

abstractmethod async on_read()

Return the next byte to transmit as an integer from 0 through 255.