1 """I2C Class for FTDI MPSSE"""
2 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.pin import Pin
3 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.url import (
10 """Custom I2C Class for FTDI MPSSE"""
16 # pylint: disable=unused-argument
17 def __init__(self, i2c_id=None, mode=MASTER, baudrate=None, frequency=400000):
18 if mode != self.MASTER:
19 raise NotImplementedError("Only I2C Master supported!")
22 # change GPIO controller to I2C
23 # pylint: disable=import-outside-toplevel
24 from pyftdi.i2c import I2cController
26 # pylint: enable=import-outside-toplevel
28 self._i2c = I2cController()
30 self._i2c.configure(get_ft232h_url(), frequency=frequency)
32 self._i2c.configure(get_ft2232h_url(i2c_id), frequency=frequency)
33 Pin.mpsse_gpio = self._i2c.get_gpio()
36 """Perform an I2C Device Scan"""
37 return [addr for addr in range(0x79) if self._i2c.poll(addr)]
39 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
40 """Write data from the buffer to an address"""
41 end = end if end else len(buffer)
42 port = self._i2c.get_port(address)
43 port.write(buffer[start:end], relax=stop)
45 def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
46 """Read data from an address and into the buffer"""
47 end = end if end else len(buffer)
48 port = self._i2c.get_port(address)
49 result = port.read(len(buffer[start:end]), relax=stop)
50 for i, b in enumerate(result):
53 # pylint: disable=unused-argument
54 def writeto_then_readfrom(
66 """Write data from buffer_out to an address and then
67 read data from an address and into buffer_in
69 out_end = out_end if out_end else len(buffer_out)
70 in_end = in_end if in_end else len(buffer_in)
71 port = self._i2c.get_port(address)
72 result = port.exchange(
73 buffer_out[out_start:out_end], in_end - in_start, relax=True
75 for i, b in enumerate(result):
76 buffer_in[in_start + i] = b
78 # pylint: enable=unused-argument