GitHub repository: https://github.com/alexforencich/cocotbext-i2c
I2C simulation models for cocotb.
Installation from pip (release version, stable):
$ pip install cocotbext-i2c
Installation from git (latest development version, potentially unstable):
$ pip install https://github.com/alexforencich/cocotbext-i2c/archive/master.zip
Installation for active development:
$ git clone https://github.com/alexforencich/cocotbext-i2c
$ pip install -e cocotbext-i2c
See the tests
directory, taxi, and verilog-i2c for complete testbenches using these modules.
The I2cMaster
class can be used to issue read and write operations on an I2C bus.
Example:
from cocotbext.i2c import I2cMaster
i2c_master = I2cMaster(dut.sda_i, dut.sda_o, dut.scl_i, dut.scl_o, 400e3)
To issue I2C operations with an I2cMaster
, call read()
or write()
. These are the preferred methods, as they will manage the bus state automatically. Lower-level methods must be called in the appropriate order. The read()
and write()
methods will leave the bus active, so call send_stop()
to release the bus.
- sda: serial data in/out
- sda_o: serial data output
- scl: clock in/out
- scl_o: clock output
- speed: nominal data rate in bits per second (default
400e3
)
- speed: nominal data rate in bits per second
write(addr, data)
: send data to device at addressaddr
(blocking)read(addr, count)
: read count bytes from device at addressaddr
(blocking)send_start()
: send a start condition on the bussend_stop()
: send a stop condition and release the bussend_byte()
: send a byte on the busrecv_byte()
: read a byte from the bus
The I2cDevice
class emulates an I2C device. This class cannot be used directly, instead it should extended and the methods handle_start()
, handle_write()
, handle_read()
, and handle_stop()
implemented appropriately. See I2cMem
for an example.
- sda: serial data in/out
- sda_o: serial data output
- scl: clock in/out
- scl_o: clock output
The I2cMemory
class emulates a simple memory like an I2C EEPROM.
Example:
from cocotbext.i2c import I2cMemory
i2c_mem = I2cMemory(dut.sda_i, dut.sda_o, dut.scl_i, dut.scl_o, 0x50, 256)
The memory can then be read/written via I2C operations on the bus, with the first bytes written after a start bit setting the address, and subsequent reads/writes advancing the internal address and reading/writing the memory. The memory can also be accessed via read_mem()
and write_mem()
.
- sda: serial data in/out
- sda_o: serial data output
- scl: clock in/out
- scl_o: clock output
- addr: device address (default
0x50
) - size: size in bytes (default
256
)
read_mem(addr, count)
: read count bytes from memory, starting ataddr
write_mem(addr, data)
: write data to memory, starting ataddr