]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rp2040_u2if/i2c.py
179af826f95544cd8e6bd20ae5c8ca9cac794b2b
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / rp2040_u2if / i2c.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """I2C Classes for RP2040s with u2if firmware"""
5 from .rp2040_u2if import rp2040_u2if
6
7
8 class I2C:
9     """I2C Base Class for RP2040 u2if"""
10
11     def __init__(self, index, *, frequency=100000):
12         self._index = index
13         rp2040_u2if.i2c_set_port(self._index)
14         rp2040_u2if.i2c_configure(frequency)
15
16     def scan(self):
17         """Perform an I2C Device Scan"""
18         rp2040_u2if.i2c_set_port(self._index)
19         return rp2040_u2if.i2c_scan()
20
21     # pylint: disable=unused-argument
22     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
23         """Write data from the buffer to an address"""
24         rp2040_u2if.i2c_set_port(self._index)
25         rp2040_u2if.i2c_writeto(address, buffer, start=start, end=end)
26
27     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
28         """Read data from an address and into the buffer"""
29         rp2040_u2if.i2c_set_port(self._index)
30         rp2040_u2if.i2c_readfrom_into(address, buffer, start=start, end=end)
31
32     def writeto_then_readfrom(
33         self,
34         address,
35         buffer_out,
36         buffer_in,
37         *,
38         out_start=0,
39         out_end=None,
40         in_start=0,
41         in_end=None,
42         stop=False,
43     ):
44         """Write data from buffer_out to an address and then
45         read data from an address and into buffer_in
46         """
47         rp2040_u2if.i2c_set_port(self._index)
48         rp2040_u2if.i2c_writeto_then_readfrom(
49             address,
50             buffer_out,
51             buffer_in,
52             out_start=out_start,
53             out_end=out_end,
54             in_start=in_start,
55             in_end=in_end,
56         )
57
58     # pylint: enable=unused-argument
59
60
61 class I2C_Pico(I2C):
62     """I2C Class for Pico u2if"""
63
64     def __init__(self, scl, sda, *, frequency=100000):
65         index = None
66         if scl.id == 5 and sda.id == 4:
67             index = 0
68         if scl.id == 15 and sda.id == 14:
69             index = 1
70         if index is None:
71             raise ValueError("I2C not found on specified pins.")
72         self._index = index
73
74         super().__init__(index, frequency=frequency)
75
76
77 class I2C_Feather(I2C):
78     """I2C Class for Feather u2if"""
79
80     def __init__(self, scl, sda, *, frequency=100000):
81         index = None
82         if scl.id == 3 and sda.id == 2:
83             index = 1
84         if index is None:
85             raise ValueError("I2C not found on specified pins.")
86         self._index = index
87
88         super().__init__(index, frequency=frequency)
89
90
91 class I2C_Feather_EPD(I2C):
92     """I2C Class for Feather EPD u2if"""
93
94     def __init__(self, scl, sda, *, frequency=100000):
95         index = None
96         if scl.id == 3 and sda.id == 2:
97             index = 1
98         if index is None:
99             raise ValueError("I2C not found on specified pins.")
100         self._index = index
101
102         super().__init__(index, frequency=frequency)
103
104
105 class I2C_QTPY(I2C):
106     """I2C Class for QT Py 2if"""
107
108     def __init__(self, scl, sda, *, frequency=100000):
109         index = None
110         if scl.id == 25 and sda.id == 24:
111             index = 0
112         if scl.id == 23 and sda.id == 22:
113             index = 1
114         if index is None:
115             raise ValueError("I2C not found on specified pins.")
116         self._index = index
117
118         super().__init__(index, frequency=frequency)
119
120
121 class I2C_ItsyBitsy(I2C):
122     """I2C Class for ItsyBitsy u2if"""
123
124     def __init__(self, scl, sda, *, frequency=100000):
125         index = None
126         if scl.id == 3 and sda.id == 2:
127             index = 1
128         if index is None:
129             raise ValueError("I2C not found on specified pins.")
130         self._index = index
131
132         super().__init__(index, frequency=frequency)
133
134
135 class I2C_MacroPad(I2C):
136     """I2C Class for MacroPad u2if"""
137
138     def __init__(self, scl, sda, *, frequency=100000):
139         index = None
140         if scl.id == 21 and sda.id == 20:
141             index = 0
142         if index is None:
143             raise ValueError("I2C not found on specified pins.")
144         self._index = index
145         super().__init__(index, frequency=frequency)
146
147
148 class I2C_QT2040_Trinkey(I2C):
149     """I2C Class for QT2040 Trinkey u2if"""
150
151     def __init__(self, scl, sda, *, frequency=100000):
152         index = None
153         if scl.id == 17 and sda.id == 16:
154             index = 0
155         if index is None:
156             raise ValueError("I2C not found on specified pins.")
157         self._index = index
158
159         super().__init__(index, frequency=frequency)