2 `bitbangio` - Bitbanged bus protocols
3 ==============================================================
5 See `CircuitPython:bitbangio` in CircuitPython for more details.
10 from adafruit_blinka import Lockable, agnostic
11 import adafruit_platformdetect.constants.boards as ap_board
15 def __init__(self, scl, sda, frequency=400000):
16 # TODO: This one is a bit questionable:
17 if agnostic.board_id == ap_board.PYBOARD:
18 raise NotImplementedError("No software I2C on {}".format(agnostic.board_id))
19 self.init(scl, sda, frequency)
21 def init(self, scl, sda, frequency):
22 from machine import Pin
23 from machine import I2C as _I2C
25 id = -1 # force bitbanging implementation - in future introspect platform if SDA/SCL matches hardware I2C
26 self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
31 except AttributeError:
37 def __exit__(self, exc_type, exc_value, traceback):
41 return self._i2c.scan()
43 def readfrom_into(self, address, buffer, start=0, end=None):
44 if start is not 0 or end is not None:
47 buffer = memoryview(buffer)[start:end]
48 stop = True # remove for efficiency later
49 return self._i2c.readfrom_into(address, buffer, stop)
51 def writeto(self, address, buffer, start=0, end=None, stop=True):
52 if start is not 0 or end is not None:
54 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
56 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
57 return self._i2c.writeto(address, buffer, stop)
60 # TODO untested, as actually busio.SPI was on tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
62 def __init__(self, clock, MOSI=None, MISO=None):
63 from machine import SPI
65 self._pins = (clock, MOSI, MISO)
67 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
68 from machine import SPI,Pin
70 # TODO verify if _spi obj 'caches' sck, mosi, miso to avoid storing in _attributeIds (duplicated in busio)
71 # i.e. #init ignores MOSI=None rather than unsetting
78 sck=Pin(self._pins[0].id),
79 mosi=Pin(self._pins[1].id),
80 miso=Pin(self._pins[2].id))
82 raise RuntimeError("First call try_lock()")
85 return self._spi.write(buf)
87 def readinto(self, buf):
88 return self.readinto(buf)
90 def write_readinto(self, buffer_out, buffer_in):
91 return self.write_readinto(buffer_out, buffer_in)