]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rp2040_u2if/i2c.py
Restructure and move pins to generic linux
[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_CAN(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_Feather_EPD(I2C):
106     """I2C Class for Feather EPD u2if"""
107
108     def __init__(self, scl, sda, *, frequency=100000):
109         index = None
110         if scl.id == 3 and sda.id == 2:
111             index = 1
112         if index is None:
113             raise ValueError("I2C not found on specified pins.")
114         self._index = index
115
116         super().__init__(index, frequency=frequency)
117
118
119 class I2C_Feather_RFM(I2C):
120     """I2C Class for Feather EPD u2if"""
121
122     def __init__(self, scl, sda, *, frequency=100000):
123         index = None
124         if scl.id == 3 and sda.id == 2:
125             index = 1
126         if index is None:
127             raise ValueError("I2C not found on specified pins.")
128         self._index = index
129
130         super().__init__(index, frequency=frequency)
131
132
133 class I2C_QTPY(I2C):
134     """I2C Class for QT Py 2if"""
135
136     def __init__(self, scl, sda, *, frequency=100000):
137         index = None
138         if scl.id == 25 and sda.id == 24:
139             index = 0
140         if scl.id == 23 and sda.id == 22:
141             index = 1
142         if index is None:
143             raise ValueError("I2C not found on specified pins.")
144         self._index = index
145
146         super().__init__(index, frequency=frequency)
147
148
149 class I2C_ItsyBitsy(I2C):
150     """I2C Class for ItsyBitsy u2if"""
151
152     def __init__(self, scl, sda, *, frequency=100000):
153         index = None
154         if scl.id == 3 and sda.id == 2:
155             index = 1
156         if index is None:
157             raise ValueError("I2C not found on specified pins.")
158         self._index = index
159
160         super().__init__(index, frequency=frequency)
161
162
163 class I2C_MacroPad(I2C):
164     """I2C Class for MacroPad u2if"""
165
166     def __init__(self, scl, sda, *, frequency=100000):
167         index = None
168         if scl.id == 21 and sda.id == 20:
169             index = 0
170         if index is None:
171             raise ValueError("I2C not found on specified pins.")
172         self._index = index
173         super().__init__(index, frequency=frequency)
174
175
176 class I2C_QT2040_Trinkey(I2C):
177     """I2C Class for QT2040 Trinkey u2if"""
178
179     def __init__(self, scl, sda, *, frequency=100000):
180         index = None
181         if scl.id == 17 and sda.id == 16:
182             index = 0
183         if index is None:
184             raise ValueError("I2C not found on specified pins.")
185         self._index = index
186
187         super().__init__(index, frequency=frequency)
188
189
190 class I2C_KB2040(I2C):
191     """I2C Class for KB2040 u2if"""
192
193     def __init__(self, scl, sda, *, frequency=100000):
194         index = None
195         if scl.id == 13 and sda.id == 12:
196             index = 0
197         if index is None:
198             raise ValueError("I2C not found on specified pins.")
199         self._index = index
200
201         super().__init__(index, frequency=frequency)