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
11 from adafruit_blinka.agnostic import board_id
14 def __init__(self, scl, sda, frequency=400000):
15 self.init(scl, sda, frequency)
17 def init(self, scl, sda, frequency):
19 if board_id == "raspi_3" or board_id == "raspi_2":
20 from adafruit_blinka.microcontroller.raspi_23.i2c import I2C as _I2C
21 if board_id == "beaglebone_black":
22 from adafruit_blinka.microcontroller.raspi_23.i2c import I2C as _I2C
24 from machine import I2C as _I2C
25 from microcontroller.pin import i2cPorts
26 for portId, portScl, portSda in i2cPorts:
27 if scl == portScl and sda == portSda:
28 self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
31 raise NotImplementedError("No Hardware I2C on (scl,sda)={}\nValid UART ports".format(
32 (scl, sda), i2cPorts))
37 except AttributeError:
43 def __exit__(self, exc_type, exc_value, traceback):
47 return self._i2c.scan()
49 def readfrom_into(self, address, buffer, *, start=0, end=None):
50 if start is not 0 or end is not None:
53 buffer = memoryview(buffer)[start:end]
54 stop = True # remove for efficiency later
55 return self._i2c.readfrom_into(address, buffer, stop=stop)
57 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
58 if isinstance(buffer, str):
59 buffer = bytes([ord(x) for x in buffer])
60 if start is not 0 or end is not None:
62 return self._i2c.writeto(address, memoryview(buffer)[start:], stop=stop)
64 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop=stop)
65 return self._i2c.writeto(address, buffer, stop=stop)
67 def writeto_then_readfrom(self, address, buffer_out, buffer_in, *, out_start=0, out_end=None, in_start=0, in_end=None, stop=False):
68 return self._i2c.writeto_then_readfrom(address, buffer_out, buffer_in,
69 out_start=out_start, out_end=out_end,
70 in_start=in_start, in_end=in_end, stop=stop)
73 def __init__(self, clock, MOSI=None, MISO=None):
75 if board_id == "raspi_3" or board_id == "raspi_2":
76 from adafruit_blinka.microcontroller.raspi_23.spi import SPI as _SPI
77 elif board_id == "beaglebone_black":
78 from adafruit_blinka.microcontroller.beaglebone_black.spi import SPI as _SPI
80 from machine import SPI as _SPI
81 from microcontroller.pin import spiPorts
82 for portId, portSck, portMosi, portMiso in spiPorts:
83 if ((clock == portSck) and # Clock is required!
84 (MOSI == portMosi or MOSI == None) and # But can do with just output
85 (MISO == portMiso or MISO == None)): # Or just input
86 self._spi = _SPI(portId)
87 self._pins = (portSck, portMosi, portMiso)
90 raise NotImplementedError(
91 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".
92 format((clock, MOSI, MISO), spiPorts))
94 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
95 if board_id == "raspi_3" or board_id == "raspi_2":
96 from adafruit_blinka.microcontroller.raspi_23.spi import SPI as _SPI
97 from adafruit_blinka.microcontroller.raspi_23.pin import Pin
98 elif board_id == "beaglebone_black":
99 from adafruit_blinka.microcontroller.beaglebone_black.spi import SPI as _SPI
100 from adafruit_blinka.microcontroller.beaglebone_black.pin import Pin
102 from machine import SPI as _SPI
103 from machine import Pin
106 # TODO check if #init ignores MOSI=None rather than unsetting, to save _pinIds attribute
113 sck=Pin(self._pins[0].id),
114 mosi=Pin(self._pins[1].id),
115 miso=Pin(self._pins[2].id)
118 raise RuntimeError("First call try_lock()")
124 def write(self, buf, start=0, end=None):
125 return self._spi.write(buf, start, end)
127 def readinto(self, buf, start=0, end=None, write_value=0):
128 return self._spi.readinto(buf, start, end)
130 def write_readinto(self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None):
131 return self._spi.write_readinto(buffer_out, buffer_in, out_start, out_end, in_start, in_end)
134 class UART(Lockable):
138 Parity.ODD = Parity()
139 Parity.EVEN = Parity()
149 receiver_buffer_size=64,
151 from machine import UART as _UART
152 from microcontroller.pin import uartPorts
154 self.baudrate = baudrate
156 if flow is not None: # default 0
157 raise NotImplementedError(
158 "Parameter '{}' unsupported on {}".format(
159 "flow", agnostic.board))
161 # translate parity flag for Micropython
162 if parity is UART.Parity.ODD:
164 elif parity is UART.Parity.EVEN:
169 raise ValueError("Invalid parity")
171 # check tx and rx have hardware support
172 for portId, portTx, portRx in uartPorts: #
173 if portTx == tx and portRx == rx:
181 read_buf_len=receiver_buffer_size
185 raise NotImplementedError(
186 "No Hardware UART on (tx,rx)={}\nValid UART ports".format(
187 (tx, rx), uartPorts))
192 def read(self, nbytes=None):
193 return self._uart.read(nbytes)
195 def readinto(self, buf, nbytes=None):
196 return self._uart.readinto(buf, nbytes)
199 return self._uart.readline()
201 def write(self, buf):
202 return self._uart.write(buf)