2 `busio` - Bus protocol support like I2C and SPI
3 =================================================
5 See `CircuitPython:busio` in CircuitPython for more details.
10 from adafruit_blinka import Enum, Lockable, agnostic
13 def __init__(self, scl, sda, frequency=400000):
14 self.init(scl, sda, frequency)
16 def init(self, scl, sda, frequency):
18 from machine import I2C as _I2C
19 from microcontroller.pin import i2cPorts
20 for portId, portScl, portSda in i2cPorts:
21 if scl == portScl and sda == portSda:
22 self._i2c = I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
25 raise NotImplementedError("No Hardware I2C on (scl,sda)={}\nValid UART ports".format(
26 (scl, sda), i2cPorts))
31 except AttributeError:
37 def __exit__(self, exc_type, exc_value, traceback):
41 return self._i2c.scan()
43 def readfrom_into(self, address, buffer, start=0, end=None):
44 if start is not 0 or end is not None:
47 buffer = memoryview(buffer)[start:end]
48 stop = True # remove for efficiency later
49 return self._i2c.readfrom_into(address, buffer, stop)
51 def writeto(self, address, buffer, start=0, end=None, stop=True):
52 if start is not 0 or end is not None:
54 return self._i2c.writeto(address, memoryview(buffer)[start:], stop)
56 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop)
57 return self._i2c.writeto(address, buffer, stop)
61 def __init__(self, clock, MOSI=None, MISO=None):
62 from microcontroller.pin import spiPorts
63 for portId, portSck, portMosi, portMiso in spiPorts:
64 if clock == portSck and MOSI == portMosi and MISO == portMiso:
65 self._spi = SPI(portId)
66 self._pins = (portSck, portMosi, portMiso)
69 raise NotImplementedError(
70 "No Hardware SPI on (clock, MOSI, MISO)={}\nValid SPI ports:{}".
71 format((clock, MOSI, MISO), spiPorts))
73 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
75 from machine import Pin
76 # TODO check if #init ignores MOSI=None rather than unsetting, to save _pinIds attribute
83 sck=Pin(self._pins[0].id),
84 mosi=Pin(self._pins[1].id),
85 miso=Pin(self._pins[2].id)
88 raise RuntimeError("First call try_lock()")
95 return self._spi.write(buf)
97 def readinto(self, buf):
98 return self.readinto(buf)
100 def write_readinto(self, buffer_out, buffer_in):
101 return self.write_readinto(buffer_out, buffer_in)
104 class UART(Lockable):
108 Parity.ODD = Parity()
109 Parity.EVEN = Parity()
119 receiver_buffer_size=64,
121 from machine import UART as _UART
122 from microcontroller.pin import uartPorts
124 self.baudrate = baudrate
126 if flow is not None: # default 0
127 raise NotImplementedError(
128 "Parameter '{}' unsupported on {}".format(
129 "flow", agnostic.board))
131 # translate parity flag for Micropython
132 if parity is UART.Parity.ODD:
134 elif parity is UART.Parity.EVEN:
139 raise ValueError("Invalid parity")
141 # check tx and rx have hardware support
142 for portId, portTx, portRx in uartPorts: #
143 if portTx == tx and portRx == rx:
151 read_buf_len=receiver_buffer_size
155 raise NotImplementedError(
156 "No Hardware UART on (tx,rx)={}\nValid UART ports".format(
157 (tx, rx), uartPorts))
162 def read(self, nbytes=None):
163 return self._uart.read(nbytes)
165 def readinto(self, buf, nbytes=None):
166 return self._uart.readinto(buf, nbytes)
169 return self._uart.readline()
171 def write(self, buf):
172 return self._uart.write(buf)