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
13 # pylint: disable=import-outside-toplevel,too-many-arguments
17 """Bitbang/Software I2C implementation"""
19 def __init__(self, scl, sda, frequency=400000):
20 # TODO: This one is a bit questionable:
21 if agnostic.board_id == ap_board.PYBOARD:
22 raise NotImplementedError("No software I2C on {}".format(agnostic.board_id))
23 if agnostic.detector.board.any_embedded_linux:
24 # TODO: Attempt to load this library automatically
25 raise NotImplementedError(
26 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
28 self.init(scl, sda, frequency)
30 def init(self, scl, sda, frequency):
32 from machine import Pin
33 from machine import I2C as _I2C
36 id = ( # pylint: disable=redefined-builtin
38 ) # force bitbanging implementation - in future
39 # introspect platform if SDA/SCL matches hardware I2C
40 self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
43 """Deinitialization"""
46 except AttributeError:
52 def __exit__(self, exc_type, exc_value, traceback):
56 """Scan for attached devices"""
57 return self._i2c.scan()
59 def readfrom_into(self, address, buffer, start=0, end=None):
60 """Read from a device at specified address into a buffer"""
61 if start != 0 or end is not None:
64 buffer = memoryview(buffer)[start:end]
65 stop = True # remove for efficiency later
66 return self._i2c.readfrom_into(address, buffer, stop)
68 def writeto(self, address, buffer, start=0, end=None, stop=True):
69 """Write to a device at specified address from a buffer"""
70 if start != 0 or end is not None:
72 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
73 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
74 return self._i2c.writeto(address, buffer, stop)
77 # TODO untested, as actually busio.SPI was on
78 # tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
80 """Bitbang/Software SPI implementation"""
82 def __init__(self, clock, MOSI=None, MISO=None):
83 if agnostic.detector.board.any_embedded_linux:
84 # TODO: Attempt to load this library automatically
85 raise NotImplementedError(
86 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
88 from machine import SPI as _SPI
91 self._pins = (clock, MOSI, MISO)
93 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
94 """Update the configuration"""
95 from machine import Pin
96 from machine import SPI as _SPI
99 # TODO verify if _spi obj 'caches' sck, mosi, miso to
100 # avoid storing in _attributeIds (duplicated in busio)
101 # i.e. #init ignores MOSI=None rather than unsetting
108 sck=Pin(self._pins[0].id),
109 mosi=Pin(self._pins[1].id),
110 miso=Pin(self._pins[2].id),
113 raise RuntimeError("First call try_lock()")
115 def write(self, buf):
116 """Write to the SPI device"""
117 return self._spi.write(buf)
119 def readinto(self, buf):
120 """Read from the SPI device into a buffer"""
121 return self.readinto(buf)
123 def write_readinto(self, buffer_out, buffer_in):
124 """Write to the SPI device and read from the SPI device into a buffer"""
125 return self.write_readinto(buffer_out, buffer_in)