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
9 # pylint: disable=protected-access, no-self-use
11 """Custom UART Class for RP2040"""
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:
30 "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format(
31 (tx.id, rx.id), uartPorts
35 # pylint: enable=too-many-arguments
37 def read(self, nbytes=None):
38 """Read from the UART"""
39 return self._uart.read(nbytes)
41 def readinto(self, buf, nbytes=None):
42 """Read from the UART into a buffer"""
43 return self._uart.readinto(buf, nbytes)
46 """Read a line of characters up to a newline charater from the UART"""
47 return self._uart.readline()
50 """Write to the UART from a buffer"""
51 return self._uart.write(buf)