diff --git a/artiq/coredevice/i2c.py b/artiq/coredevice/i2c.py index b406b34ea..074a9aabe 100644 --- a/artiq/coredevice/i2c.py +++ b/artiq/coredevice/i2c.py @@ -46,6 +46,19 @@ class PCA9548: finally: i2c_stop(self.busno) + @kernel + def readback(self): + i2c_init(self.busno) + i2c_start(self.busno) + r = 0 + try: + if not i2c_write(self.busno, self.address | 1): + raise I2CError("PCA9548 failed to ack address") + r = i2c_read(self.busno, False) + finally: + i2c_stop(self.busno) + return r + class TCA6424A: def __init__(self, dmgr, busno=0, address=0x44): diff --git a/artiq/test/coredevice/test_i2c.py b/artiq/test/coredevice/test_i2c.py new file mode 100644 index 000000000..45955c132 --- /dev/null +++ b/artiq/test/coredevice/test_i2c.py @@ -0,0 +1,25 @@ +import os, unittest + +from artiq.experiment import * +from artiq.test.hardware_testbench import ExperimentCase + + +class I2CSwitch(EnvExperiment): + def build(self): + self.setattr_device("core") + self.setattr_device("i2c_switch") + + @kernel + def run(self): + passed = True + for i in range(8): + self.i2c_switch.set(i) + if self.i2c_switch.readback() != 1 << i: + passed = False + self.set_dataset("passed", passed) + + +class I2CTest(ExperimentCase): + def test_i2c_switch(self): + self.execute(I2CSwitch) + self.assertTrue(self.dataset_mgr.get("passed"))