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 VOLTS] [--scl PIN] [--sda PIN]
                              -A I2C-ADDR [--pulls]
-h, --help

show this help message and exit

-V <volts>, --voltage <volts>

bus power (range: 1.8..5 V, or 'S<port>' to follow the voltage sensed on a port; default: unchanged)

--scl <pin>

bind I²C clock to PIN (default: 'A0', required)

--sda <pin>

bind I²C data 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.I2CTargetPorts(*, supply: glasgow.abstract.GlasgowSupply, scl: amaranth.lib.io.PortLike, sda: amaranth.lib.io.PortLike)
supply: GlasgowSupply

bus power

scl: PortLike

I²C clock

sda: PortLike

I²C data

class glasgow.applet.interface.i2c_target.I2CTargetInterface(logger: Logger, assembly: AbstractAssembly, ports: I2CTargetPorts, *, address: int)

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, an I2CTargetPorts, and the address keyword argument inside the assembly's add_applet() context. Call read_event() repeatedly to process transactions.

The interface does not enable pull-ups; to use the integrated ones, declare pull="high" on the pins, or use AbstractAssembly.use_pulls().

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.