1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """I2C Class for FTDI MPSSE"""
5 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.pin import Pin
6 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.url import (
13 """Custom I2C Class for FTDI MPSSE"""
19 # pylint: disable=unused-argument
20 def __init__(self, i2c_id=None, mode=MASTER, baudrate=None, frequency=400000):
21 if mode != self.MASTER:
22 raise NotImplementedError("Only I2C Master supported!")
25 # change GPIO controller to I2C
26 # pylint: disable=import-outside-toplevel
27 from pyftdi.i2c import I2cController
29 # pylint: enable=import-outside-toplevel
31 self._i2c = I2cController()
33 self._i2c.configure(get_ft232h_url(), frequency=frequency)
35 self._i2c.configure(get_ft2232h_url(i2c_id), frequency=frequency)
36 Pin.mpsse_gpio = self._i2c.get_gpio()
39 """Perform an I2C Device Scan"""
40 return [addr for addr in range(0x79) if self._i2c.poll(addr)]
42 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
43 """Write data from the buffer to an address"""
44 end = end if end else len(buffer)
45 port = self._i2c.get_port(address)
46 port.write(buffer[start:end], relax=stop)
48 def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
49 """Read data from an address and into the buffer"""
50 end = end if end else len(buffer)
51 port = self._i2c.get_port(address)
52 result = port.read(len(buffer[start:end]), relax=stop)
53 for i, b in enumerate(result):
56 # pylint: disable=unused-argument
57 def writeto_then_readfrom(
69 """Write data from buffer_out to an address and then
70 read data from an address and into buffer_in
72 out_end = out_end if out_end else len(buffer_out)
73 in_end = in_end if in_end else len(buffer_in)
74 port = self._i2c.get_port(address)
75 result = port.exchange(
76 buffer_out[out_start:out_end], in_end - in_start, relax=True
78 for i, b in enumerate(result):
79 buffer_in[in_start + i] = b
81 # pylint: enable=unused-argument