]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/nxp_lpc4330/uart.py
Update orangepizero2.py
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nxp_lpc4330 / uart.py
1 """UART Class for NXP LPC4330"""
2 from greatfet import GreatFET
3 from greatfet.interfaces.uart import UART as _UART
4
5
6 class UART:
7     """Custom UART Class for NXP LPC4330"""
8
9     PARITY_NONE = 0
10     PARITY_ODD = 1
11     PARITY_EVEN = 2
12     PARITY_STUCK_AT_ONE = 3
13     PARITY_STUCK_AT_ZERO = 4
14
15     # pylint: disable=too-many-arguments,unused-argument
16     def __init__(
17         self,
18         portid,
19         baudrate=9600,
20         bits=8,
21         parity=None,
22         stop=1,
23         timeout=1000,
24         read_buf_len=None,
25         flow=None,
26     ):
27         self._gf = GreatFET()
28         self._uart = _UART(
29             self._gf,
30             baud=baudrate,
31             data_bits=bits,
32             stop_bits=stop,
33             parity=parity,
34             uart_number=portid,
35         )
36
37         if flow is not None:  # default None
38             raise NotImplementedError(
39                 "Parameter '{}' unsupported on GreatFET One".format("flow")
40             )
41
42     # pylint: enable=too-many-arguments,unused-argument
43
44     def deinit(self):
45         """Deinitialize"""
46         self._uart.initialized = False
47
48     def read(self, nbytes=None):
49         """Read data from UART and return it"""
50         if nbytes is None:
51             return None
52         return self._uart.read(nbytes)
53
54     def readinto(self, buf, nbytes=None):
55         """Read data from UART and into the buffer"""
56         if nbytes is None:
57             return None
58         result = self.read(nbytes)
59         for _ in range(nbytes):
60             buf.append(result)
61         return buf
62
63     def readline(self):
64         """Read a single line of data from UART"""
65         out = self.read(nbytes=1)
66         line = out
67         while out != "\r":
68             out = self.read(nbytes=1)
69             line += out
70         return line
71
72     def write(self, buf):
73         """Write data from the buffer to UART"""
74         return self._uart.write(buf)