]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/busio.py
Added agnostic as shared dependency between micropython and circuitpython test config...
[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(
14                 "No Hardware SPI on (clock, MOSI, MISO)={}\nValid SPI ports:{}".
15                 format((clock, MOSI, MISO), spiPorts))
16
17     def configure(self, baudrate=100000, polarity=0, phase=0, bits=8):
18         if self._locked:
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
22             self._spi.init(
23                 baudrate=baudrate,
24                 polarity=polarity,
25                 phase=phase,
26                 bits=bits,
27                 firstbit=SPI.MSB,
28                 sck=Pin(self._pinIds[0]),
29                 mosi=Pin(self._pinIds[1]),
30                 miso=Pin(self._pinIds[2]))
31         else:
32             raise RuntimeError("First call try_lock()")
33
34     def deinit(self):
35         self._spi = None
36         self._pinIds = None
37
38     def write(self, buf):
39         return self._spi.write(buf)
40
41     def readinto(self, buf):
42         return self.readinto(buf)
43
44     def write_readinto(self, buffer_out, buffer_in):
45         return self.write_readinto(buffer_out, buffer_in)
46
47
48 class UART(Lockable):
49     class Parity(Enum):
50         pass
51
52     Parity.ODD = Parity()
53     Parity.EVEN = Parity()
54
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
56     def __init__(self,
57                  tx,
58                  rx,
59                  baudrate=9600,
60                  bits=8,
61                  parity=None,
62                  stop=1,
63                  timeout=None,
64                  receiver_buffer_size=None,
65                  flow=None):
66         from microcontroller import uartPorts
67         from machine import UART
68
69         self.baudrate = baudrate
70
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))
83
84         # translate parity flag for Micropython
85         if parity is UART.Parity.ODD:
86             parity = 1
87         elif parity is UART.Parity.EVEN:
88             parity = 0
89         elif parity is None:
90             pass
91         else:
92             raise ValueError("Invalid parity")
93
94         # check tx and rx have hardware support
95         for portId, portTx, portRx in uartPorts:  #
96             if portTx == tx.id and portRx == rx.id:
97                 self._uart = UART(
98                     portId,
99                     baudrate,
100                     bits=bits,
101                     parity=parity,
102                     stop=stop,
103                     timeout=timeout)
104                 break
105         else:
106             raise NotImplementedError(
107                 "No Hardware UART on (tx,rx)={}\nValid UART ports".format(
108                     (tx, rx), uartPorts))
109
110     def deinit(self):
111         self._uart = None
112
113     def read(self, nbytes=None):
114         return self._uart.read(nbytes)
115
116     def readinto(self, buf, nbytes=None):
117         return self._uart.readinto(buf, nbytes)
118
119     def readline(self):
120         return self._uart.readline()
121
122     def write(self, buf):
123         return self._uart.write(buf)