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