]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/bitbangio.py
Handle also CircuitPython error from missing pull capability.
[Adafruit_Blinka-hackapet.git] / src / bitbangio.py
1 from adafruit_blinka import Lockable, agnostic
2
3
4 class I2C(Lockable):
5     def __init__(self, scl, sda, frequency=400000):
6         if agnostic.microcontroller == "stm32":
7             raise NotImplementedError("No software I2C on {}".format(agnostic.board))
8         self.init(scl, sda, frequency)
9
10     def init(self, scl, sda, frequency):
11         from machine import Pin
12         from machine import I2C as _I2C
13         self.deinit()
14         id = -1  # force bitbanging implementation - in future introspect platform if SDA/SCL matches hardware I2C
15         self._i2c = _I2C(id, Pin(scl.id), Pin(sda.id), freq=frequency)
16
17     def deinit(self):
18         try:
19             del self._i2c
20         except AttributeError:
21             pass
22
23     def __enter__(self):
24         return self
25
26     def __exit__(self, exc_type, exc_value, traceback):
27         self.deinit()
28
29     def scan(self):
30         return self._i2c.scan()
31
32     def readfrom_into(self, address, buffer, start=0, end=None):
33         if start is not 0 or end is not None:
34             if end is None:
35                 end = len(buffer)
36             buffer = memoryview(buffer)[start:end]
37         stop = True  # remove for efficiency later
38         return self._i2c.readfrom_into(address, buffer, stop)
39
40     def writeto(self, address, buffer, start=0, end=None, stop=True):
41         if start is not 0 or end is not None:
42             if end is None:
43                 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
44             else:
45                 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
46         return self._i2c.writeto(address, buffer, stop)
47
48
49 # TODO untested, as actually busio.SPI was on tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
50 class SPI(Lockable):
51     def __init__(self, clock, MOSI=None, MISO=None):
52         from machine import SPI
53         self._spi = SPI(-1)
54         self._pins = (clock, MOSI, MISO)
55
56     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
57         from machine import SPI,Pin
58         if self._locked:
59             # TODO verify if _spi obj 'caches' sck, mosi, miso to avoid storing in _attributeIds (duplicated in busio)
60             # i.e. #init ignores MOSI=None rather than unsetting
61             self._spi.init(
62                 baudrate=baudrate,
63                 polarity=polarity,
64                 phase=phase,
65                 bits=bits,
66                 firstbit=SPI.MSB,
67                 sck=Pin(self._pins[0].id),
68                 mosi=Pin(self._pins[1].id),
69                 miso=Pin(self._pins[2].id))
70         else:
71             raise RuntimeError("First call try_lock()")
72
73     def write(self, buf):
74         return self._spi.write(buf)
75
76     def readinto(self, buf):
77         return self.readinto(buf)
78
79     def write_readinto(self, buffer_out, buffer_in):
80         return self.write_readinto(buffer_out, buffer_in)