]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/busio.py
quiet gpio
[Adafruit_Blinka-hackapet.git] / src / busio.py
1 """
2 `busio` - Bus protocol support like I2C and SPI
3 =================================================
4
5 See `CircuitPython:busio` in CircuitPython for more details.
6
7 * Author(s): cefn
8 """
9
10 from adafruit_blinka import Enum, Lockable, agnostic
11 from adafruit_blinka.agnostic import board as boardId
12
13 class I2C(Lockable):
14     def __init__(self, scl, sda, frequency=400000):
15         self.init(scl, sda, frequency)
16
17     def init(self, scl, sda, frequency):
18         self.deinit()
19         if boardId == "raspi_3" or boardId == "raspi_2":
20             from adafruit_blinka.microcontroller.raspi_23.i2c import I2C as _I2C
21         else:
22             from machine import I2C as _I2C
23         from microcontroller.pin import i2cPorts
24         for portId, portScl, portSda in i2cPorts:
25             if scl == portScl and sda == portSda:
26                 self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
27                 break
28         else:
29             raise NotImplementedError("No Hardware I2C on (scl,sda)={}\nValid UART ports".format(
30         (scl, sda), i2cPorts))
31
32     def deinit(self):
33         try:
34             del self._i2c
35         except AttributeError:
36             pass
37
38     def __enter__(self):
39         return self
40
41     def __exit__(self, exc_type, exc_value, traceback):
42         self.deinit()
43
44     def scan(self):
45         return self._i2c.scan()
46
47     def readfrom_into(self, address, buffer, start=0, end=None):
48         if start is not 0 or end is not None:
49             if end is None:
50                 end = len(buffer)
51             buffer = memoryview(buffer)[start:end]
52         stop = True  # remove for efficiency later
53         return self._i2c.readfrom_into(address, buffer, stop)
54
55     def writeto(self, address, buffer, start=0, end=None, stop=True):
56         if start is not 0 or end is not None:
57             if end is None:
58                 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
59             else:
60                 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
61         return self._i2c.writeto(address, buffer, stop)
62
63
64 class SPI(Lockable):
65     def __init__(self, clock, MOSI=None, MISO=None):
66         self.deinit()
67         if boardId == "raspi_3" or boardId == "raspi_2":
68             from adafruit_blinka.microcontroller.raspi_23.spi import SPI as _SPI
69         else:
70             from machine import SPI as _SPI
71         from microcontroller.pin import spiPorts
72         for portId, portSck, portMosi, portMiso in spiPorts:
73             if clock == portSck and MOSI == portMosi and MISO == portMiso:
74                 self._spi = _SPI(portId)
75                 self._pins = (portSck, portMosi, portMiso)
76                 break
77         else:
78             raise NotImplementedError(
79                 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".
80                 format((clock, MOSI, MISO), spiPorts))
81
82     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
83         if boardId == "raspi_3" or boardId == "raspi_2":
84             from adafruit_blinka.microcontroller.raspi_23.spi import SPI as _SPI
85             from adafruit_blinka.microcontroller.raspi_23.pin import Pin
86         else:
87             from machine import SPI as _SPI
88             from machine import Pin
89
90         if self._locked:
91             # TODO check if #init ignores MOSI=None rather than unsetting, to save _pinIds attribute
92             self._spi.init(
93                 baudrate=baudrate,
94                 polarity=polarity,
95                 phase=phase,
96                 bits=bits,
97                 firstbit=_SPI.MSB,
98                 sck=Pin(self._pins[0].id),
99                 mosi=Pin(self._pins[1].id),
100                 miso=Pin(self._pins[2].id)
101             )
102         else:
103             raise RuntimeError("First call try_lock()")
104
105     def deinit(self):
106         self._spi = None
107         self._pinIds = None
108
109     def write(self, buf):
110         return self._spi.write(buf)
111
112     def readinto(self, buf):
113         return self.readinto(buf)
114
115     def write_readinto(self, buffer_out, buffer_in):
116         return self.write_readinto(buffer_out, buffer_in)
117
118
119 class UART(Lockable):
120     class Parity(Enum):
121         pass
122
123     Parity.ODD = Parity()
124     Parity.EVEN = Parity()
125
126     def __init__(self,
127                  tx,
128                  rx,
129                  baudrate=9600,
130                  bits=8,
131                  parity=None,
132                  stop=1,
133                  timeout=1000,
134                  receiver_buffer_size=64,
135                  flow=None):
136         from machine import UART as _UART
137         from microcontroller.pin import uartPorts
138
139         self.baudrate = baudrate
140
141         if flow is not None:  # default 0
142             raise NotImplementedError(
143                 "Parameter '{}' unsupported on {}".format(
144                     "flow", agnostic.board))
145
146         # translate parity flag for Micropython
147         if parity is UART.Parity.ODD:
148             parity = 1
149         elif parity is UART.Parity.EVEN:
150             parity = 0
151         elif parity is None:
152             pass
153         else:
154             raise ValueError("Invalid parity")
155
156         # check tx and rx have hardware support
157         for portId, portTx, portRx in uartPorts:  #
158             if portTx == tx and portRx == rx:
159                 self._uart = _UART(
160                     portId,
161                     baudrate,
162                     bits=bits,
163                     parity=parity,
164                     stop=stop,
165                     timeout=timeout,
166                     read_buf_len=receiver_buffer_size
167                 )
168                 break
169         else:
170             raise NotImplementedError(
171                 "No Hardware UART on (tx,rx)={}\nValid UART ports".format(
172                     (tx, rx), uartPorts))
173
174     def deinit(self):
175         self._uart = None
176
177     def read(self, nbytes=None):
178         return self._uart.read(nbytes)
179
180     def readinto(self, buf, nbytes=None):
181         return self._uart.readinto(buf, nbytes)
182
183     def readline(self):
184         return self._uart.readline()
185
186     def write(self, buf):
187         return self._uart.write(buf)