/ adafruit_boardtest / boardtest_uart.py
boardtest_uart.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2018 Shawn Hymel for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `adafruit_boardtest.boardtest_uart`
 24  ====================================================
 25  Performs random writes and reads across UART. Connect a wire from TX pin to RX pin.
 26  
 27  Run this script as its own main.py to individually run the test, or compile
 28  with mpy-cross and call from separate test script.
 29  
 30  * Author(s): Shawn Hymel for Adafruit Industries
 31  
 32  Implementation Notes
 33  --------------------
 34  
 35  **Software and Dependencies:**
 36  
 37  * Adafruit CircuitPython firmware for the supported boards:
 38    https://github.com/adafruit/circuitpython/releases
 39  * Adafruit's Bus Device library:
 40    https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
 41  
 42  """
 43  
 44  import random
 45  
 46  import board
 47  import busio
 48  
 49  __version__ = "0.0.0-auto.0"
 50  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
 51  
 52  # Constants
 53  TX_PIN_NAME = "TX"
 54  RX_PIN_NAME = "RX"
 55  BAUD_RATE = 9600
 56  NUM_UART_BYTES = 40  # Number of bytes to transmit over UART
 57  ASCII_MIN = 0x21  # '!' Lowest ASCII char in random range (inclusive)
 58  ASCII_MAX = 0x7E  # '~' Highest ASCII char in random range (inclusive)
 59  
 60  # Test result strings
 61  PASS = "PASS"
 62  FAIL = "FAIL"
 63  NA = "N/A"
 64  
 65  
 66  def run_test(pins, tx_pin=TX_PIN_NAME, rx_pin=RX_PIN_NAME, baud_rate=BAUD_RATE):
 67  
 68      """
 69      Performs random writes out of TX pin and reads on RX.
 70  
 71      :param list[str] pins: list of pins to run the test on
 72      :param str tx_pin: pin name of UART TX
 73      :param str rx_pin: pin name of UART RX
 74      :return: tuple(str, list[str]): test result followed by list of pins tested
 75      """
 76  
 77      # Echo some values over the UART
 78      if list(set(pins).intersection(set([tx_pin, rx_pin]))):
 79  
 80          # Tell user to create loopback connection
 81          print("Connect a wire from TX to RX. Press enter to continue.")
 82          input()
 83  
 84          # Initialize UART
 85          uart = busio.UART(
 86              getattr(board, tx_pin), getattr(board, rx_pin), baudrate=baud_rate
 87          )
 88          uart.reset_input_buffer()  # pylint: disable=no-member
 89  
 90          # Generate test string
 91          test_str = ""
 92          for _ in range(NUM_UART_BYTES):
 93              test_str += chr(random.randint(ASCII_MIN, ASCII_MAX))
 94  
 95          # Transmit test string
 96          uart.write(bytearray(test_str))
 97          print("Transmitting:\t" + test_str)
 98  
 99          # Wait for received string
100          data = uart.read(len(test_str))
101          recv_str = ""
102          if data is not None:
103              recv_str = "".join([chr(b) for b in data])
104              print("Received:\t" + recv_str)
105  
106          # Release UART pins
107          uart.deinit()
108  
109          # Compare strings
110          if recv_str == test_str:
111              return PASS, [tx_pin, rx_pin]
112  
113          return FAIL, [tx_pin, rx_pin]
114  
115      # Else (no pins found)
116      print("No UART pins found")
117      return NA, []
118  
119  
120  def _main():
121  
122      # List out all the pins available to us
123      pins = list(dir(board))
124      print()
125      print("All pins found:", end=" ")
126  
127      # Print pins
128      for pin in pins:
129          print(pin, end=" ")
130      print("\n")
131  
132      # Run test
133      result = run_test(pins)
134      print()
135      print(result[0])
136      print("Pins tested: " + str(result[1]))
137  
138  
139  # Execute only if run as main.py or code.py
140  if __name__ == "__main__":
141      _main()