2 `bitbangio` - Bitbanged bus protocols
3 ==============================================================
5 See `CircuitPython:bitbangio` in CircuitPython for more details.
11 __version__ = "0.0.0-auto.0"
12 __repo__ = "https://github.com/adafruit/Adafruit_Blinka.git"
15 import adafruit_platformdetect.constants.boards as ap_board
16 from adafruit_blinka import Lockable, agnostic
18 # pylint: disable=import-outside-toplevel,too-many-arguments
22 """Bitbang/Software I2C implementation"""
24 def __init__(self, scl, sda, frequency=400000):
25 # TODO: This one is a bit questionable:
26 if agnostic.board_id == ap_board.PYBOARD:
27 raise NotImplementedError("No software I2C on {}".format(agnostic.board_id))
28 if agnostic.detector.board.any_embedded_linux:
29 # TODO: Attempt to load this library automatically
30 raise NotImplementedError(
31 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
33 self.init(scl, sda, frequency)
35 def init(self, scl, sda, frequency):
37 from machine import Pin
38 from machine import I2C as _I2C
41 id = ( # pylint: disable=redefined-builtin
43 ) # force bitbanging implementation - in future
44 # introspect platform if SDA/SCL matches hardware I2C
45 self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
48 """Deinitialization"""
51 except AttributeError:
57 def __exit__(self, exc_type, exc_value, traceback):
61 """Scan for attached devices"""
62 return self._i2c.scan()
64 def readfrom_into(self, address, buffer, start=0, end=None):
65 """Read from a device at specified address into a buffer"""
66 if start != 0 or end is not None:
69 buffer = memoryview(buffer)[start:end]
70 stop = True # remove for efficiency later
71 return self._i2c.readfrom_into(address, buffer, stop)
73 def writeto(self, address, buffer, start=0, end=None, stop=True):
74 """Write to a device at specified address from a buffer"""
75 if start != 0 or end is not None:
77 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
78 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
79 return self._i2c.writeto(address, buffer, stop)
82 # TODO untested, as actually busio.SPI was on
83 # tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
85 """Bitbang/Software SPI implementation"""
87 def __init__(self, clock, MOSI=None, MISO=None):
88 if agnostic.detector.board.any_embedded_linux:
89 # TODO: Attempt to load this library automatically
90 raise NotImplementedError(
91 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
93 from machine import SPI as _SPI
96 self._pins = (clock, MOSI, MISO)
98 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
99 """Update the configuration"""
100 from machine import Pin
101 from machine import SPI as _SPI
104 # TODO verify if _spi obj 'caches' sck, mosi, miso to
105 # avoid storing in _attributeIds (duplicated in busio)
106 # i.e. #init ignores MOSI=None rather than unsetting
113 sck=Pin(self._pins[0].id),
114 mosi=Pin(self._pins[1].id),
115 miso=Pin(self._pins[2].id),
118 raise RuntimeError("First call try_lock()")
120 def write(self, buf):
121 """Write to the SPI device"""
122 return self._spi.write(buf)
124 def readinto(self, buf):
125 """Read from the SPI device into a buffer"""
126 return self.readinto(buf)
128 def write_readinto(self, buffer_out, buffer_in):
129 """Write to the SPI device and read from the SPI device into a buffer"""
130 return self.write_readinto(buffer_out, buffer_in)