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
12 import adafruit_platformdetect.board as ap_board
14 EMBEDDED_LINUX_BOARDS = (
15 ap_board.RASPBERRY_PI_2B,
16 ap_board.RASPBERRY_PI_3B,
17 ap_board.RASPBERRY_PI_3B_PLUS,
18 ap_board.BEAGLEBONE_BLACK,
23 def __init__(self, scl, sda, frequency=400000):
24 self.init(scl, sda, frequency)
26 def init(self, scl, sda, frequency):
28 if board_id in EMBEDDED_LINUX_BOARDS:
29 from adafruit_blinka.microcontroller.generic_linux.i2c import I2C as _I2C
31 from machine import I2C as _I2C
32 from microcontroller.pin import i2cPorts
33 for portId, portScl, portSda in i2cPorts:
34 if scl == portScl and sda == portSda:
35 self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
38 raise NotImplementedError(
39 "No Hardware I2C on (scl,sda)={}\nValid UART ports: {}".format((scl, sda), i2cPorts)
45 except AttributeError:
51 def __exit__(self, exc_type, exc_value, traceback):
55 return self._i2c.scan()
57 def readfrom_into(self, address, buffer, *, start=0, end=None):
58 if start is not 0 or end is not None:
61 buffer = memoryview(buffer)[start:end]
62 stop = True # remove for efficiency later
63 return self._i2c.readfrom_into(address, buffer, stop=stop)
65 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
66 if isinstance(buffer, str):
67 buffer = bytes([ord(x) for x in buffer])
68 if start is not 0 or end is not None:
70 return self._i2c.writeto(address, memoryview(buffer)[start:], stop=stop)
72 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop=stop)
73 return self._i2c.writeto(address, buffer, stop=stop)
75 def writeto_then_readfrom(self, address, buffer_out, buffer_in, *, out_start=0, out_end=None, in_start=0, in_end=None, stop=False):
76 return self._i2c.writeto_then_readfrom(address, buffer_out, buffer_in,
77 out_start=out_start, out_end=out_end,
78 in_start=in_start, in_end=in_end, stop=stop)
81 def __init__(self, clock, MOSI=None, MISO=None):
83 if board_id in EMBEDDED_LINUX_BOARDS:
84 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
86 from machine import SPI as _SPI
87 from microcontroller.pin import spiPorts
88 for portId, portSck, portMosi, portMiso in spiPorts:
89 if ((clock == portSck) and # Clock is required!
90 (MOSI == portMosi or MOSI == None) and # But can do with just output
91 (MISO == portMiso or MISO == None)): # Or just input
92 self._spi = _SPI(portId)
93 self._pins = (portSck, portMosi, portMiso)
96 raise NotImplementedError(
97 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".
98 format((clock, MOSI, MISO), spiPorts))
100 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
101 if board_id in ap_board.ANY_RASPBERRY_PI_2_OR_3:
102 from adafruit_blinka.microcontroller.bcm283x.pin import Pin
103 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
104 elif board_id == ap_board.BEAGLEBONE_BLACK:
105 from adafruit_blinka.microcontroller.am335x.pin import Pin
106 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
107 elif board_id == ap_board.ORANGEPI_PC:
108 from adafruit_blinka.microcontroller.allwinner_h3.pin import Pin
109 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
111 from machine import SPI as _SPI
112 from machine import Pin
115 # TODO check if #init ignores MOSI=None rather than unsetting, to save _pinIds attribute
122 sck=Pin(self._pins[0].id),
123 mosi=Pin(self._pins[1].id),
124 miso=Pin(self._pins[2].id)
127 raise RuntimeError("First call try_lock()")
133 def write(self, buf, start=0, end=None):
134 return self._spi.write(buf, start, end)
136 def readinto(self, buf, start=0, end=None, write_value=0):
137 return self._spi.readinto(buf, start, end)
139 def write_readinto(self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None):
140 return self._spi.write_readinto(buffer_out, buffer_in, out_start, out_end, in_start, in_end)
143 class UART(Lockable):
147 Parity.ODD = Parity()
148 Parity.EVEN = Parity()
158 receiver_buffer_size=64,
160 from machine import UART as _UART
161 from microcontroller.pin import uartPorts
163 self.baudrate = baudrate
165 if flow is not None: # default 0
166 raise NotImplementedError(
167 "Parameter '{}' unsupported on {}".format(
168 "flow", agnostic.board_id))
170 # translate parity flag for Micropython
171 if parity is UART.Parity.ODD:
173 elif parity is UART.Parity.EVEN:
178 raise ValueError("Invalid parity")
180 # check tx and rx have hardware support
181 for portId, portTx, portRx in uartPorts: #
182 if portTx == tx and portRx == rx:
190 read_buf_len=receiver_buffer_size
194 raise NotImplementedError(
195 "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format((tx, rx), uartPorts)
201 def read(self, nbytes=None):
202 return self._uart.read(nbytes)
204 def readinto(self, buf, nbytes=None):
205 return self._uart.readinto(buf, nbytes)
208 return self._uart.readline()
210 def write(self, buf):
211 return self._uart.write(buf)