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