]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rp2040/uart.py
Initial update for Lichee RV (w/Dock)
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / rp2040 / uart.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """UART Class for RP2040"""
5 from machine import UART as _UART
6 from machine import Pin
7 from microcontroller.pin import uartPorts
8
9 # pylint: disable=protected-access, no-self-use
10 class UART:
11     """Custom UART Class for RP2040"""
12
13     # pylint: disable=too-many-arguments
14     def __init__(self, tx, rx, baudrate=9600, bits=8, parity=None, stop=1):
15         # check tx and rx have hardware support
16         for portId, txPin, rxPin in uartPorts:
17             if txPin == tx and rxPin == rx:
18                 self._uart = _UART(
19                     portId,
20                     baudrate,
21                     bits=bits,
22                     parity=parity,
23                     stop=stop,
24                     tx=Pin(txPin.id),
25                     rx=Pin(rxPin.id),
26                 )
27                 break
28         else:
29             raise ValueError(
30                 "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format(
31                     (tx.id, rx.id), uartPorts
32                 )
33             )
34
35     # pylint: enable=too-many-arguments
36
37     def read(self, nbytes=None):
38         """Read from the UART"""
39         return self._uart.read(nbytes)
40
41     def readinto(self, buf, nbytes=None):
42         """Read from the UART into a buffer"""
43         return self._uart.readinto(buf, nbytes)
44
45     def readline(self):
46         """Read a line of characters up to a newline charater from the UART"""
47         return self._uart.readline()
48
49     def write(self, buf):
50         """Write to the UART from a buffer"""
51         return self._uart.write(buf)