]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rp2040/uart.py
Merge pull request #837 from brentru/fix-spi-generic-board
[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
10 # pylint: disable=protected-access, no-self-use
11 class UART:
12     """Custom UART Class for RP2040"""
13
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:
19                 self._uart = _UART(
20                     portId,
21                     baudrate,
22                     bits=bits,
23                     parity=parity,
24                     stop=stop,
25                     tx=Pin(txPin.id),
26                     rx=Pin(rxPin.id),
27                 )
28                 break
29         else:
30             raise ValueError(
31                 "No Hardware UART on (tx,rx)={}\nValid UART ports: {}".format(
32                     (tx.id, rx.id), uartPorts
33                 )
34             )
35
36     # pylint: enable=too-many-arguments
37
38     def read(self, nbytes=None):
39         """Read from the UART"""
40         return self._uart.read(nbytes)
41
42     def readinto(self, buf, nbytes=None):
43         """Read from the UART into a buffer"""
44         return self._uart.readinto(buf, nbytes)
45
46     def readline(self):
47         """Read a line of characters up to a newline charater from the UART"""
48         return self._uart.readline()
49
50     def write(self, buf):
51         """Write to the UART from a buffer"""
52         return self._uart.write(buf)