Browse Source

Raise erro rwhen attempting software I2C on platform without. Minimise imports on load (import within function definitions). Remove spurious positional and keyword arg containers. Eliminate locally-scoped temporary variables. Switched to use of pin references rather than IDs in microcontroller port lists

master^2
Cefn Hoile 3 years ago
parent
commit
5d0b207bdb
1 changed files with 15 additions and 10 deletions
  1. +15
    -10
      src/bitbangio.py

+ 15
- 10
src/bitbangio.py View File

@ -1,13 +1,15 @@
from adafruit_blinka import Lockable
from machine import I2C as _I2C
from machine import Pin
from adafruit_blinka import Lockable, agnostic
class I2C(Lockable):
def __init__(self, scl, sda, frequency=400000):
if agnostic.microcontroller == "stm32":
raise NotImplementedError("No software I2C on {}".format(agnostic.board))
self.init(scl, sda, frequency)
def init(self, scl, sda, frequency):
from machine import Pin
from machine import I2C as _I2C
self.deinit()
id = -1 # force bitbanging implementation - in future introspect platform if SDA/SCL matches hardware I2C
self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
@ -35,21 +37,24 @@ class I2C(Lockable):
stop = True # remove for efficiency later
return self._i2c.readfrom_into(address, buffer, stop)
def writeto(self, address, buffer, start=0, end=None, stop=True, *a, **k):
def writeto(self, address, buffer, start=0, end=None, stop=True):
if start is not 0 or end is not None:
if end is None:
end = len(buffer)
buffer = memoryview(buffer)[start:end]
return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
else:
return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
return self._i2c.writeto(address, buffer, stop)
# TODO untested, as actually busio.SPI was on tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
class SPI(Lockable):
def __init__(self, clock, MOSI=None, MISO=None):
from machine import SPI
self._spi = SPI(-1)
self._pins = (clock.id, MOSI.id, MISO.id)
self._pins = (clock, MOSI, MISO)
def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
from machine import SPI,Pin
if self._locked:
# TODO verify if _spi obj 'caches' sck, mosi, miso to avoid storing in _attributeIds (duplicated in busio)
# i.e. #init ignores MOSI=None rather than unsetting
@ -59,9 +64,9 @@ class SPI(Lockable):
phase=phase,
bits=bits,
firstbit=SPI.MSB,
sck=Pin(self._pins[0]),
mosi=Pin(self._pins[1]),
miso=Pin(self._pins[2]))
sck=Pin(self._pins[0].id),
mosi=Pin(self._pins[1].id),
miso=Pin(self._pins[2].id))
else:
raise RuntimeError("First call try_lock()")


Loading…
Cancel
Save