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" or board_id == "beaglebone_black" or board_id == "orangepipc":
20 from adafruit_blinka.microcontroller.generic_linux.i2c import I2C as _I2C
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)
29 raise NotImplementedError("No Hardware I2C on (scl,sda)={}\nValid UART ports".format(
30 (scl, sda), i2cPorts))
35 except AttributeError:
41 def __exit__(self, exc_type, exc_value, traceback):
45 return self._i2c.scan()
47 def readfrom_into(self, address, buffer, *, start=0, end=None):
48 if start is not 0 or end is not None:
51 buffer = memoryview(buffer)[start:end]
52 stop = True # remove for efficiency later
53 return self._i2c.readfrom_into(address, buffer, stop=stop)
55 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
56 if isinstance(buffer, str):
57 buffer = bytes([ord(x) for x in buffer])
58 if start is not 0 or end is not None:
60 return self._i2c.writeto(address, memoryview(buffer)[start:], stop=stop)
62 return self._i2c.writeto(address, memoryview(buffer)[start:end], stop=stop)
63 return self._i2c.writeto(address, buffer, stop=stop)
65 def writeto_then_readfrom(self, address, buffer_out, buffer_in, *, out_start=0, out_end=None, in_start=0, in_end=None, stop=False):
66 return self._i2c.writeto_then_readfrom(address, buffer_out, buffer_in,
67 out_start=out_start, out_end=out_end,
68 in_start=in_start, in_end=in_end, stop=stop)
71 def __init__(self, clock, MOSI=None, MISO=None):
73 if board_id == "raspi_3" or board_id == "raspi_2" or board_id == "beaglebone_black" or board_id == "orangepipc":
74 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
76 from machine import SPI as _SPI
77 from microcontroller.pin import spiPorts
78 for portId, portSck, portMosi, portMiso in spiPorts:
79 if ((clock == portSck) and # Clock is required!
80 (MOSI == portMosi or MOSI == None) and # But can do with just output
81 (MISO == portMiso or MISO == None)): # Or just input
82 self._spi = _SPI(portId)
83 self._pins = (portSck, portMosi, portMiso)
86 raise NotImplementedError(
87 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".
88 format((clock, MOSI, MISO), spiPorts))
90 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
91 if board_id == "raspi_3" or board_id == "raspi_2":
92 from adafruit_blinka.microcontroller.raspi_23.pin import Pin
93 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
94 elif board_id == "beaglebone_black":
95 # reuse the raspberry pi class as both boards use Linux spidev
96 from adafruit_blinka.microcontroller.beaglebone_black.pin import Pin
97 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
98 elif board_id == "orangepipc":
99 from adafruit_blinka.microcontroller.allwinner_h3.pin import Pin
100 from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
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)