]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/busio.py
Renaming and refactoring typos. Experimental logic for board-based Pin naming, fallin...
[Adafruit_Blinka-hackapet.git] / src / busio.py
1 from adafruit_blinka import Enum, Lockable, agnostic
2
3
4 class SPI(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:
9                 self._spi = SPI(spiId)
10                 self._pinIds = (sck, mosi, miso)
11                 break
12         else:
13             raise NotImplementedError("No Hardware SPI on (clock, MOSI, MISO)={}\nValid SPI ports:{}".format((clock, MOSI, MISO), spiPorts))
14
15     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
16         if self._locked:
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]))
21         else:
22             raise RuntimeError("First call try_lock()")
23
24     def deinit(self):
25         self._spi = None
26         self._pinIds = None
27
28     def write(self, buf):
29         return self._spi.write(buf)
30
31     def readinto(self, buf):
32         return self.readinto(buf)
33
34     def write_readinto(self, buffer_out, buffer_in):
35         return self.write_readinto(buffer_out, buffer_in)
36
37 class UART(Lockable):
38
39     class Parity(Enum):
40         pass
41     Parity.ODD=Parity()
42     Parity.EVEN=Parity()
43
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
48
49         self.baudrate = baudrate
50
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))
57
58         # translate parity flag for Micropython
59         if parity is UART.Parity.ODD:
60             parity = 1
61         elif parity is UART.Parity.EVEN:
62             parity = 0
63         elif parity is None:
64             pass
65         else:
66             raise ValueError("Invalid parity")
67
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)
72                 break
73         else:
74             raise NotImplementedError("No Hardware UART on (tx,rx)={}\nValid UART ports".format((tx, rx), uartPorts))
75
76     def deinit(self):
77         self._uart = None
78
79     def read(self, nbytes=None):
80         return self._uart.read(nbytes)
81
82     def readinto(self, buf, nbytes=None):
83         return self._uart.readinto(buf, nbytes)
84
85     def readline(self):
86         return self._uart.readline()
87
88     def write(self, buf):
89         return self._uart.write(buf)