1 """I2C Class for RP2040"""
2 from machine import I2C as _I2C
3 from machine import Pin
4 from microcontroller.pin import i2cPorts
8 """Custom I2C Class for RP2040"""
10 def __init__(self, scl, sda, *, frequency=100000):
11 for portId, portScl, portSda in i2cPorts:
13 if scl == portScl and sda == portSda:
15 portId, sda=Pin(sda.id), scl=Pin(scl.id), freq=frequency
22 "No Hardware I2C on (scl,sda)={}\nValid I2C ports: {}".format(
28 """Perform an I2C Device Scan"""
29 return self._i2c.scan()
31 def writeto(self, address, buffer, *, stop=True):
32 "Write data to the address from the buffer"
33 return self._i2c.writeto(address, buffer, stop)
35 def readfrom_into(self, address, buffer, *, stop=True):
36 """Read data from an address and into the buffer"""
37 return self._i2c.readfrom_into(address, buffer, stop)
39 def writeto_then_readfrom(
51 """Write data from buffer_out to an address and then
52 read data from an address and into buffer_in
55 self.writeto(address, buffer_out[out_start:out_end], stop=stop)
57 self.writeto(address, buffer_out[out_start:], stop=stop)
58 read_buffer = buffer_in
59 self.readfrom_into(address, read_buffer, stop=stop)
61 buffer_in[in_start:in_end] = read_buffer[in_start:in_end]
63 buffer_in[in_start:] = read_buffer[in_start:]