]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rp2040_u2if/i2c.py
adding support for RFM and CAN feathers
[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 class I2C_Feather_CAN(I2C):
91     """I2C Class for Feather EPD u2if"""
92
93     def __init__(self, scl, sda, *, frequency=100000):
94         index = None
95         if scl.id == 3 and sda.id == 2:
96             index = 1
97         if index is None:
98             raise ValueError("I2C not found on specified pins.")
99         self._index = index
100
101         super().__init__(index, frequency=frequency)
102
103 class I2C_Feather_EPD(I2C):
104     """I2C Class for Feather EPD u2if"""
105
106     def __init__(self, scl, sda, *, frequency=100000):
107         index = None
108         if scl.id == 3 and sda.id == 2:
109             index = 1
110         if index is None:
111             raise ValueError("I2C not found on specified pins.")
112         self._index = index
113
114         super().__init__(index, frequency=frequency)
115
116 class I2C_Feather_RFM(I2C):
117     """I2C Class for Feather EPD u2if"""
118
119     def __init__(self, scl, sda, *, frequency=100000):
120         index = None
121         if scl.id == 3 and sda.id == 2:
122             index = 1
123         if index is None:
124             raise ValueError("I2C not found on specified pins.")
125         self._index = index
126
127         super().__init__(index, frequency=frequency)
128
129 class I2C_QTPY(I2C):
130     """I2C Class for QT Py 2if"""
131
132     def __init__(self, scl, sda, *, frequency=100000):
133         index = None
134         if scl.id == 25 and sda.id == 24:
135             index = 0
136         if scl.id == 23 and sda.id == 22:
137             index = 1
138         if index is None:
139             raise ValueError("I2C not found on specified pins.")
140         self._index = index
141
142         super().__init__(index, frequency=frequency)
143
144
145 class I2C_ItsyBitsy(I2C):
146     """I2C Class for ItsyBitsy u2if"""
147
148     def __init__(self, scl, sda, *, frequency=100000):
149         index = None
150         if scl.id == 3 and sda.id == 2:
151             index = 1
152         if index is None:
153             raise ValueError("I2C not found on specified pins.")
154         self._index = index
155
156         super().__init__(index, frequency=frequency)
157
158
159 class I2C_MacroPad(I2C):
160     """I2C Class for MacroPad u2if"""
161
162     def __init__(self, scl, sda, *, frequency=100000):
163         index = None
164         if scl.id == 21 and sda.id == 20:
165             index = 0
166         if index is None:
167             raise ValueError("I2C not found on specified pins.")
168         self._index = index
169         super().__init__(index, frequency=frequency)
170
171
172 class I2C_QT2040_Trinkey(I2C):
173     """I2C Class for QT2040 Trinkey u2if"""
174
175     def __init__(self, scl, sda, *, frequency=100000):
176         index = None
177         if scl.id == 17 and sda.id == 16:
178             index = 0
179         if index is None:
180             raise ValueError("I2C not found on specified pins.")
181         self._index = index
182
183         super().__init__(index, frequency=frequency)