]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/bitbangio.py
e63168fbbf0484e96b6d99acb1474bc2a6a2dab9
[Adafruit_Blinka-hackapet.git] / src / bitbangio.py
1 """
2 `bitbangio` - Bitbanged bus protocols
3 ==============================================================
4
5 See `CircuitPython:bitbangio` in CircuitPython for more details.
6
7 * Author(s): cefn
8 """
9
10
11 __version__ = "0.0.0-auto.0"
12 __repo__ = "https://github.com/adafruit/Adafruit_Blinka.git"
13
14
15 import adafruit_platformdetect.constants.boards as ap_board
16 from adafruit_blinka import Lockable, agnostic
17
18 # pylint: disable=import-outside-toplevel,too-many-arguments
19
20
21 class I2C(Lockable):
22     """Bitbang/Software I2C implementation"""
23
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"
32             )
33         self.init(scl, sda, frequency)
34
35     def init(self, scl, sda, frequency):
36         """Initialization"""
37         from machine import Pin
38         from machine import I2C as _I2C
39
40         self.deinit()
41         id = (  # pylint: disable=redefined-builtin
42             -1
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)
46
47     def deinit(self):
48         """Deinitialization"""
49         try:
50             del self._i2c
51         except AttributeError:
52             pass
53
54     def __enter__(self):
55         return self
56
57     def __exit__(self, exc_type, exc_value, traceback):
58         self.deinit()
59
60     def scan(self):
61         """Scan for attached devices"""
62         return self._i2c.scan()
63
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:
67             if end is None:
68                 end = len(buffer)
69             buffer = memoryview(buffer)[start:end]
70         stop = True  # remove for efficiency later
71         return self._i2c.readfrom_into(address, buffer, stop)
72
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:
76             if end is 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)
80
81
82 # TODO untested, as actually busio.SPI was on
83 # tasklist https://github.com/adafruit/Adafruit_Micropython_Blinka/issues/2 :(
84 class SPI(Lockable):
85     """Bitbang/Software SPI implementation"""
86
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"
92             )
93         from machine import SPI as _SPI
94
95         self._spi = _SPI(-1)
96         self._pins = (clock, MOSI, MISO)
97
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
102
103         if self._locked:
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
107             self._spi.init(
108                 baudrate=baudrate,
109                 polarity=polarity,
110                 phase=phase,
111                 bits=bits,
112                 firstbit=_SPI.MSB,
113                 sck=Pin(self._pins[0].id),
114                 mosi=Pin(self._pins[1].id),
115                 miso=Pin(self._pins[2].id),
116             )
117         else:
118             raise RuntimeError("First call try_lock()")
119
120     def write(self, buf):
121         """Write to the SPI device"""
122         return self._spi.write(buf)
123
124     def readinto(self, buf):
125         """Read from the SPI device into a buffer"""
126         return self.readinto(buf)
127
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)