]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rp2040_u2if/i2c.py
initial qt2040 trinkey
[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 class I2C_Pico(I2C):
58     """I2C Class for Pico u2if"""
59
60     def __init__(self, scl, sda, *, frequency=100000):
61         index = None
62         if scl.id == 5 and sda.id == 4:
63             index = 0
64         if scl.id == 15 and sda.id == 14:
65             index = 1
66         if index is None:
67             raise ValueError("I2C not found on specified pins.")
68         self._index = index
69
70         super().__init__(index, frequency=frequency)
71
72 class I2C_Feather(I2C):
73     """I2C Class for Feather u2if"""
74
75     def __init__(self, scl, sda, *, frequency=100000):
76         index = None
77         if scl.id == 3 and sda.id == 2:
78             index = 1
79         if index is None:
80             raise ValueError("I2C not found on specified pins.")
81         self._index = index
82
83         super().__init__(index, frequency=frequency)
84
85 class I2C_QT2040_Trinkey(I2C):
86     """I2C Class for QT2040 Trinkey u2if"""
87
88     def __init__(self, scl, sda, *, frequency=100000):
89         index = None
90         if scl.id == 17 and sda.id == 16:
91             index = 0
92         if index is None:
93             raise ValueError("I2C not found on specified pins.")
94         self._index = index
95
96         super().__init__(index, frequency=frequency)
97
98