1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
5 `bitbangio` - Bitbanged bus protocols
6 ==============================================================
8 See `CircuitPython:bitbangio` in CircuitPython for more details.
13 import adafruit_platformdetect.constants.boards as ap_board
14 from adafruit_blinka import Lockable, agnostic
16 # pylint: disable=import-outside-toplevel,too-many-arguments
20 """Bitbang/Software I2C implementation"""
22 def __init__(self, scl, sda, frequency=400000):
23 # TODO: This one is a bit questionable:
24 if agnostic.board_id == ap_board.PYBOARD:
25 raise NotImplementedError("No software I2C on {}".format(agnostic.board_id))
26 if agnostic.detector.board.any_embedded_linux:
27 # TODO: Attempt to load this library automatically
28 raise NotImplementedError(
29 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
31 self.init(scl, sda, frequency)
33 def init(self, scl, sda, frequency):
35 from machine import Pin
36 from machine import I2C as _I2C
39 id = ( # pylint: disable=redefined-builtin
41 ) # force bitbanging implementation - in future
42 # introspect platform if SDA/SCL matches hardware I2C
43 self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
46 """Deinitialization"""
49 except AttributeError:
55 def __exit__(self, exc_type, exc_value, traceback):
59 """Scan for attached devices"""
60 return self._i2c.scan()
62 def readfrom_into(self, address, buffer, start=0, end=None):
63 """Read from a device at specified address into a buffer"""
64 if start != 0 or end is not None:
67 buffer = memoryview(buffer)[start:end]
68 stop = True # remove for efficiency later
69 return self._i2c.readfrom_into(address, buffer, stop)
71 def writeto(self, address, buffer, start=0, end=None, stop=True):
72 """Write to a device at specified address from a buffer"""
73 if start != 0 or end is not None:
75 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
76 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
77 return self._i2c.writeto(address, buffer, stop)
80 # TODO untested, as actually busio.SPI was on
81 # tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
83 """Bitbang/Software SPI implementation"""
85 def __init__(self, clock, MOSI=None, MISO=None):
86 if agnostic.detector.board.any_embedded_linux:
87 # TODO: Attempt to load this library automatically
88 raise NotImplementedError(
89 "For bitbangio on Linux, please use Adafruit_CircuitPython_BitbangIO"
91 from machine import SPI as _SPI
94 self._pins = (clock, MOSI, MISO)
96 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
97 """Update the configuration"""
98 from machine import Pin
99 from machine import SPI as _SPI
102 # TODO verify if _spi obj 'caches' sck, mosi, miso to
103 # avoid storing in _attributeIds (duplicated in busio)
104 # i.e. #init ignores MOSI=None rather than unsetting
111 sck=Pin(self._pins[0].id),
112 mosi=Pin(self._pins[1].id),
113 miso=Pin(self._pins[2].id),
116 raise RuntimeError("First call try_lock()")
118 def write(self, buf):
119 """Write to the SPI device"""
120 return self._spi.write(buf)
122 def readinto(self, buf):
123 """Read from the SPI device into a buffer"""
124 return self.readinto(buf)
126 def write_readinto(self, buffer_out, buffer_in):
127 """Write to the SPI device and read from the SPI device into a buffer"""
128 return self.write_readinto(buffer_out, buffer_in)