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