1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
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
10 # pylint: disable=protected-access, no-self-use
12 """Custom UART Class for RP2040"""
14 # pylint: disable=too-many-arguments
15 def __init__(self, tx, rx, baudrate=9600, bits=8, parity=None, stop=1):
16 # check tx and rx have hardware support
17 for portId, txPin, rxPin in uartPorts:
18 if txPin == tx and rxPin == rx:
31 "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format(
32 (tx.id, rx.id), uartPorts
36 # pylint: enable=too-many-arguments
38 def read(self, nbytes=None):
39 """Read from the UART"""
40 return self._uart.read(nbytes)
42 def readinto(self, buf, nbytes=None):
43 """Read from the UART into a buffer"""
44 return self._uart.readinto(buf, nbytes)
47 """Read a line of characters up to a newline charater from the UART"""
48 return self._uart.readline()
51 """Write to the UART from a buffer"""
52 return self._uart.write(buf)