]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/ftdi_mpsse/mpsse/i2c.py
Add GPIO support for Rock 5
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / ftdi_mpsse / mpsse / i2c.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
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 (
7     get_ft232h_url,
8     get_ft2232h_url,
9 )
10
11
12 class I2C:
13     """Custom I2C Class for FTDI MPSSE"""
14
15     MASTER = 0
16     SLAVE = 1
17     _mode = None
18
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!")
23         _mode = self.MASTER
24
25         # change GPIO controller to I2C
26         # pylint: disable=import-outside-toplevel
27         from pyftdi.i2c import I2cController
28
29         # pylint: enable=import-outside-toplevel
30
31         self._i2c = I2cController()
32         if i2c_id is None:
33             self._i2c.configure(get_ft232h_url(), frequency=frequency)
34         else:
35             self._i2c.configure(get_ft2232h_url(i2c_id), frequency=frequency)
36         Pin.mpsse_gpio = self._i2c.get_gpio()
37
38     def scan(self):
39         """Perform an I2C Device Scan"""
40         return [addr for addr in range(0x79) if self._i2c.poll(addr)]
41
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)
47
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):
54             buffer[start + i] = b
55
56     # pylint: disable=unused-argument
57     def writeto_then_readfrom(
58         self,
59         address,
60         buffer_out,
61         buffer_in,
62         *,
63         out_start=0,
64         out_end=None,
65         in_start=0,
66         in_end=None,
67         stop=False,
68     ):
69         """Write data from buffer_out to an address and then
70         read data from an address and into buffer_in
71         """
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
77         )
78         for i, b in enumerate(result):
79             buffer_in[in_start + i] = b
80
81     # pylint: enable=unused-argument