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, detector
 
  12 import adafruit_platformdetect.board as ap_board
 
  15     def __init__(self, scl, sda, frequency=400000):
 
  16         self.init(scl, sda, frequency)
 
  18     def init(self, scl, sda, frequency):
 
  20         if detector.board.any_embedded_linux:
 
  21             from adafruit_blinka.microcontroller.generic_linux.i2c import I2C as _I2C
 
  23             from machine import I2C as _I2C
 
  24         from microcontroller.pin import i2cPorts
 
  25         for portId, portScl, portSda in i2cPorts:
 
  26             if scl == portScl and sda == portSda:
 
  27                 self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
 
  30             raise NotImplementedError(
 
  31                 "No Hardware I2C on (scl,sda)={}\nValid UART ports: {}".format((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 detector.board.any_embedded_linux:
 
  76             from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
 
  78             from machine import SPI as _SPI
 
  79         from microcontroller.pin import spiPorts
 
  80         for portId, portSck, portMosi, portMiso in spiPorts:
 
  81             if ((clock == portSck) and                   # Clock is required!
 
  82                 (MOSI == portMosi or MOSI == None) and   # But can do with just output
 
  83                 (MISO == portMiso or MISO == None)):      # Or just input
 
  84                 self._spi = _SPI(portId)
 
  85                 self._pins = (portSck, portMosi, portMiso)
 
  88             raise NotImplementedError(
 
  89                 "No Hardware SPI on (SCLK, MOSI, MISO)={}\nValid SPI ports:{}".
 
  90                 format((clock, MOSI, MISO), spiPorts))
 
  92     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
 
  93         if detector.board.any_raspberry_pi:
 
  94             from adafruit_blinka.microcontroller.bcm283x.pin import Pin
 
  95             from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
 
  96         elif board_id == ap_board.BEAGLEBONE_BLACK:
 
  97             from adafruit_blinka.microcontroller.am335x.pin import Pin
 
  98             from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
 
  99         elif board_id == ap_board.ORANGE_PI_PC:
 
 100             from adafruit_blinka.microcontroller.allwinner_h3.pin import Pin
 
 101             from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
 
 102         elif board_id == ap_board.GIANT_BOARD:
 
 103             from adafruit_blinka.microcontroller.sama5.pin import Pin
 
 104             from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
 
 106             from machine import SPI as _SPI
 
 107             from machine import Pin
 
 110             # TODO check if #init ignores MOSI=None rather than unsetting, to save _pinIds attribute
 
 117                 sck=Pin(self._pins[0].id),
 
 118                 mosi=Pin(self._pins[1].id),
 
 119                 miso=Pin(self._pins[2].id)
 
 122             raise RuntimeError("First call try_lock()")
 
 128     def write(self, buf, start=0, end=None):
 
 129         return self._spi.write(buf, start, end)
 
 131     def readinto(self, buf, start=0, end=None, write_value=0):
 
 132         return self._spi.readinto(buf, start, end, write_value=write_value)
 
 134     def write_readinto(self, buffer_out, buffer_in,  out_start=0, out_end=None, in_start=0, in_end=None):
 
 135         return self._spi.write_readinto(buffer_out, buffer_in, out_start, out_end, in_start, in_end)
 
 138 class UART(Lockable):
 
 142     Parity.ODD = Parity()
 
 143     Parity.EVEN = Parity()
 
 153                  receiver_buffer_size=64,
 
 155         if detector.board.any_embedded_linux:
 
 156             raise RuntimeError('busio.UART not supported on this platform. Please use pyserial instead.')
 
 158             from machine import UART as _UART
 
 159         from microcontroller.pin import uartPorts
 
 161         self.baudrate = baudrate
 
 163         if flow is not None:  # default 0
 
 164             raise NotImplementedError(
 
 165                 "Parameter '{}' unsupported on {}".format(
 
 166                     "flow", agnostic.board_id))
 
 168         # translate parity flag for Micropython
 
 169         if parity is UART.Parity.ODD:
 
 171         elif parity is UART.Parity.EVEN:
 
 176             raise ValueError("Invalid parity")
 
 178         # check tx and rx have hardware support
 
 179         for portId, portTx, portRx in uartPorts:  #
 
 180             if portTx == tx and portRx == rx:
 
 188                     read_buf_len=receiver_buffer_size
 
 192             raise NotImplementedError(
 
 193                 "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format((tx, rx), uartPorts)
 
 199     def read(self, nbytes=None):
 
 200         return self._uart.read(nbytes)
 
 202     def readinto(self, buf, nbytes=None):
 
 203         return self._uart.readinto(buf, nbytes)
 
 206         return self._uart.readline()
 
 208     def write(self, buf):
 
 209         return self._uart.write(buf)