1 from adafruit_blinka import Enum, Lockable, agnostic
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(
14 "No Hardware SPI on (clock, MOSI, MISO)={}\nValid SPI ports:{}".
15 format((clock, MOSI, MISO), spiPorts))
17 def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
19 from machine import Pin
20 from microcontroller import spiPorts
21 # TODO check if #init ignores MOSI=None rather than unsetting, to save _pinIds attribute
28 sck=Pin(self._pinIds[0]),
29 mosi=Pin(self._pinIds[1]),
30 miso=Pin(self._pinIds[2]))
32 raise RuntimeError("First call try_lock()")
39 return self._spi.write(buf)
41 def readinto(self, buf):
42 return self.readinto(buf)
44 def write_readinto(self, buffer_out, buffer_in):
45 return self.write_readinto(buffer_out, buffer_in)
53 Parity.EVEN = Parity()
55 # TODO investigate UART receiver_buffer_size as e.g. read_buf_len in https://github.com/micropython/micropython/blob/3eb0694b97c6a8f0e93b874549aac40d8b78b0e5/ports/stm32/uart.c
64 receiver_buffer_size=None,
66 from microcontroller import uartPorts
67 from machine import UART
69 self.baudrate = baudrate
71 if timeout is not None: # default 1000
72 raise NotImplementedError(
73 "Parameter '{}' unsupported on {}".format(
74 "timeout", agnostic.board))
75 if receiver_buffer_size is not None: # default 64
76 raise NotImplementedError(
77 "Parameter '{}' unsupported on {}".format(
78 "receiver_buffer_size", agnostic.board))
79 if flow is not None: # default 0
80 raise NotImplementedError(
81 "Parameter '{}' unsupported on {}".format(
82 "flow", agnostic.board))
84 # translate parity flag for Micropython
85 if parity is UART.Parity.ODD:
87 elif parity is UART.Parity.EVEN:
92 raise ValueError("Invalid parity")
94 # check tx and rx have hardware support
95 for portId, portTx, portRx in uartPorts: #
96 if portTx == tx.id and portRx == rx.id:
106 raise NotImplementedError(
107 "No Hardware UART on (tx,rx)={}\nValid UART ports".format(
108 (tx, rx), uartPorts))
113 def read(self, nbytes=None):
114 return self._uart.read(nbytes)
116 def readinto(self, buf, nbytes=None):
117 return self._uart.readinto(buf, nbytes)
120 return self._uart.readline()
122 def write(self, buf):
123 return self._uart.write(buf)