2 from mcp import Enum, Lockable
 
   5     def __init__(self, clock, MOSI=None, MISO=None):
 
   6         from microcontroller import spiPorts
 
   7         for spiId, sck, mosi, miso in spiPorts:
 
   8             if sck == clock.id and mosi == MOSI.id and miso == MISO.id:
 
  10                 self._pinIds = (sck, mosi, miso)
 
  13             raise NotImplementedError("No Hardware SPI on (clock, MOSI, MISO)={}\nValid SPI ports:{}".format((clock, MOSI, MISO), spiPorts))
 
  15     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
 
  17             from machine import Pin
 
  18             from microcontroller import spiPorts
 
  19             # TODO check if #init ignores MOSI=None rather than unsetting, to save _pinIds attribute
 
  20             self._spi.init(baudrate=baudrate, polarity=polarity, phase=phase, bits = bits, firstbit = SPI.MSB, sck = Pin(self._pinIds[0]), mosi=Pin(self._pinIds[1]), miso=Pin(self._pinIds[2]))
 
  22             raise RuntimeError("First call try_lock()")
 
  29         return self._spi.write(buf)
 
  31     def readinto(self, buf):
 
  32         return self.readinto(buf)
 
  34     def write_readinto(self, buffer_out, buffer_in)
 
  35         return self.write_readinto(buffer_out, buffer_in)
 
  44     # TODO investigate UART receiver_buffer_size as e.g. read_buf_len in https://github.com/micropython/micropython/blob/3eb0694b97c6a8f0e93b874549aac40d8b78b0e5/ports/stm32/uart.c
 
  45     def __init__(self, tx, rx, baudrate=9600, bits=8, parity=None, stop=1, timeout=None, receiver_buffer_size=None, flow=None):
 
  46         from microcontroller import uartPorts
 
  47         from machine import UART
 
  49         self.baudrate = baudrate
 
  51         if timeout is not None: # default 1000
 
  52             raise NotImplementedError("Parameter '{}' unsupported on {}".format("timeout", agnostic.board))
 
  53         if receiver_buffer_size is not None: # default 64
 
  54             raise NotImplementedError("Parameter '{}' unsupported on {}".format("receiver_buffer_size", agnostic.board))
 
  55         if flow is not None: # default 0
 
  56             raise NotImplementedError("Parameter '{}' unsupported on {}".format("flow", agnostic.board))
 
  58         # translate parity flag for Micropython
 
  59         if parity is UART.Parity.ODD:
 
  61         elif parity is UART.Parity.EVEN:
 
  66             raise ValueError("Invalid parity")
 
  68         # check tx and rx have hardware support
 
  69         for portId, portTx, portRx in uartPorts:#
 
  70             if portTx == tx.id and portRx == rx.id:
 
  71                 self._uart = UART(portId, baudrate, bits=bits, parity=parity, stop=stop, timeout=timeout)
 
  74             raise NotImplementedError("No Hardware UART on (tx,rx)={}\nValid UART ports".format((tx, rx), uartPorts))
 
  79     def read(self, nbytes=None):
 
  80         return self._uart.read(nbytes)
 
  82     def readinto(self, buf, nbytes=None):
 
  83         return self._uart.readinto(buf, nbytes)
 
  86         return self._uart.readline()
 
  89         return self._uart.write(buf)