1 from adafruit_blinka import Lockable, agnostic
5 def __init__(self, scl, sda, frequency=400000):
6 if agnostic.microcontroller == "stm32":
7 raise NotImplementedError("No software I2C on {}".format(agnostic.board))
8 self.init(scl, sda, frequency)
10 def init(self, scl, sda, frequency):
11 from machine import Pin
12 from machine import I2C as _I2C
14 id = -1 # force bitbanging implementation - in future introspect platform if SDA/SCL matches hardware I2C
15 self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
20 except AttributeError:
26 def __exit__(self, exc_type, exc_value, traceback):
30 return self._i2c.scan()
32 def readfrom_into(self, address, buffer, start=0, end=None):
33 if start is not 0 or end is not None:
36 buffer = memoryview(buffer)[start:end]
37 stop = True # remove for efficiency later
38 return self._i2c.readfrom_into(address, buffer, stop)
40 def writeto(self, address, buffer, start=0, end=None, stop=True):
41 if start is not 0 or end is not None:
43 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
45 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
46 return self._i2c.writeto(address, buffer, stop)
49 # TODO untested, as actually busio.SPI was on tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
51 def __init__(self, clock, MOSI=None, MISO=None):
52 from machine import SPI
54 self._pins = (clock, MOSI, MISO)
56 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
57 from machine import SPI,Pin
59 # TODO verify if _spi obj 'caches' sck, mosi, miso to avoid storing in _attributeIds (duplicated in busio)
60 # i.e. #init ignores MOSI=None rather than unsetting
67 sck=Pin(self._pins[0].id),
68 mosi=Pin(self._pins[1].id),
69 miso=Pin(self._pins[2].id))
71 raise RuntimeError("First call try_lock()")
74 return self._spi.write(buf)
76 def readinto(self, buf):
77 return self.readinto(buf)
79 def write_readinto(self, buffer_out, buffer_in):
80 return self.write_readinto(buffer_out, buffer_in)