1 """I2C Class for FTDI MPSSE"""
2 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.pin import Pin
5 """Custom I2C Class for FTDI MPSSE"""
11 # pylint: disable=unused-argument
12 def __init__(self, id=None, mode=MASTER, baudrate=None, frequency=400000):
13 if mode != self.MASTER:
14 raise NotImplementedError("Only I2C Master supported!")
17 # change GPIO controller to I2C
18 # pylint: disable=import-outside-toplevel
19 from pyftdi.i2c import I2cController
21 # pylint: enable=import-outside-toplevel
23 self._i2c = I2cController()
25 self._i2c.configure("ftdi://ftdi:ft232h/1", frequency=frequency)
27 self._i2c.configure("ftdi://ftdi:ft2232h/{}".format(id+1), frequency=frequency)
28 Pin.mpsse_gpio = self._i2c.get_gpio()
31 """Perform an I2C Device Scan"""
32 return [addr for addr in range(0x79) if self._i2c.poll(addr)]
34 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
35 """Write data from the buffer to an address"""
36 end = end if end else len(buffer)
37 port = self._i2c.get_port(address)
38 port.write(buffer[start:end], relax=stop)
40 def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
41 """Read data from an address and into the buffer"""
42 end = end if end else len(buffer)
43 port = self._i2c.get_port(address)
44 result = port.read(len(buffer[start:end]), relax=stop)
45 for i, b in enumerate(result):
48 # pylint: disable=unused-argument
49 def writeto_then_readfrom(
61 """Write data from buffer_out to an address and then
62 read data from an address and into buffer_in
64 out_end = out_end if out_end else len(buffer_out)
65 in_end = in_end if in_end else len(buffer_in)
66 port = self._i2c.get_port(address)
67 result = port.exchange(
68 buffer_out[out_start:out_end], in_end - in_start, relax=True
70 for i, b in enumerate(result):
71 buffer_in[in_start + i] = b
73 # pylint: enable=unused-argument