Note: If you have multiple gateways, all commands can also take a --gateway option to specify which one. For brevity, that option is omitted from the below examples.

How to communicate with peripherals on the I2C bus with lager CLI


In order to debug hardware designs with chips that utilize an I2C bus, it's often handy to have a way of communicating with the bus directly. The Lager Gateway has a built-in I2C peripheral that you can use to communicate with your I2C chips or microcontrollers.


Install the latest version of the lager-cli tool, and confirm that you are able to communicate with your Lager Gateway.

Sending Bytes

lager-cli includes a i2c write command for transmitting bytes via I2C. This command has a mandatory device argument, which is the I2C address of the device you're writing to (in either 0x hex or integer notation), as well as one or more bytes of data to transmit. An optional --reg argument can be provided with the address of the register to send the bytes to. The datatype of the provided bytes can be set with the --type argument.

# Write 0xFFFF to device 0x12 (18)
> lager i2c write --type hex 0x12 0xFF 0xFF

# Write 0x12 0x34 to device 0x12 (18) register 0x10 (16)
> lager i2c write --type hex --reg 0x10 0x12 0x34

# Write "hello" to device 0x12 (18)
> lager i2c write --type string 18 test

Requesting Bytes

lager-cli also includes a i2c read command for requesting bytes from a specific I2C address. A --length argument can be used to set the number of bytes to request, otherwise one byte will be requested.

# Read 1 byte from device 0x12 (18)
> lager i2c read 0x12

# Read 2 bytes from device 0x12 (18) register 0x10 (16) in hex format
> lager i2c read --length 2 --type hex --reg 16 0x12

# Read a 5-byte string from device 0x12 (18)
> lager i2c read --length 5 --type string 0x12