Test Suite

Run through several tests at once.

examples/boardtest_simpletest.py
  1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  2# SPDX-License-Identifier: MIT
  3
  4# The MIT License (MIT)
  5#
  6# Copyright (c) 2018 Shawn Hymel for Adafruit Industries
  7#
  8# Permission is hereby granted, free of charge, to any person obtaining a copy
  9# of this software and associated documentation files (the "Software"), to deal
 10# in the Software without restriction, including without limitation the rights
 11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12# copies of the Software, and to permit persons to whom the Software is
 13# furnished to do so, subject to the following conditions:
 14#
 15# The above copyright notice and this permission notice shall be included in
 16# all copies or substantial portions of the Software.
 17#
 18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24# THE SOFTWARE.
 25"""
 26`BoardTest Suite`
 27====================================================
 28CircuitPython board hardware test suite
 29
 30* Author(s): Shawn Hymel
 31* Date: December 8, 2018
 32
 33Implementation Notes
 34--------------------
 35Run this to test various input/output abilities of a board. Tests the
 36following:
 37
 38* Onboard LEDs
 39* GPIO output
 40* Onboard battery voltage monitor
 41* SPI
 42* I2C
 43
 44You will need the following components:
 45
 46* Multimeter
 47* LED
 48* 1x 330 Ohm resistor or 220 Ohm resistor
 49* 2x 4.7k Ohm resistor
 50* Microchip 25AA040A SPI EEPROM
 51* Microchip AT24HC04B I2C EEPROM
 52* Breadboard
 53* Wires
 54
 55Copy the following files to the adafruit_boardtest folder on your CIRCUITPY drive:
 56
 57* __init__.py
 58* boardtest_gpio.mpy
 59* boardtest_i2c.mpy
 60* boardtest_led.mpy
 61* boardtest_spi.mpy
 62* boardtest_uart.mpy
 63* boardtest_voltage_monitor
 64
 65Copy this file to the root directory of your CIRCUITPY drive and rename the
 66filename to code.py. Open a serial terminal, and follow the prompts to run
 67the various tests.
 68"""
 69
 70import board
 71
 72from adafruit_boardtest import (
 73    boardtest_gpio,
 74    boardtest_i2c,
 75    boardtest_led,
 76    boardtest_spi,
 77    boardtest_uart,
 78    boardtest_voltage_monitor,
 79)
 80
 81# Constants
 82UART_TX_PIN_NAME = "TX"
 83UART_RX_PIN_NAME = "RX"
 84UART_BAUD_RATE = 9600
 85SPI_MOSI_PIN_NAME = "MOSI"
 86SPI_MISO_PIN_NAME = "MISO"
 87SPI_SCK_PIN_NAME = "SCK"
 88SPI_CS_PIN_NAME = "D2"
 89I2C_SDA_PIN_NAME = "SDA"
 90I2C_SCL_PIN_NAME = "SCL"
 91
 92# Results dictionary
 93TEST_RESULTS = {}
 94
 95# Save tested pins
 96PINS_TESTED = []
 97
 98# Print welcome message
 99print()
100print("                            ....                                      ")
101print("                        #@@%%%%%%&@@/                                 ")
102print("                     (&@%%%%%%%%%%%%%@&                               ")
103print("                  .(@&%%%@*    *&%%%%%%@.                             ")
104print("            ,@@&&%%%%%%%%//@%,/ /&%%%%%%@                             ")
105print("            %@%%%&%%%%%%%#(@@@&&%%%%%%%%@*                            ")
106print("             @&%%&%%%%%%%%%%%%%%%%%%%%%%@/                            ")
107print("               &@@&%%%%&&&%%%%%%%%%%%%%%@,                            ")
108print("                ,/ &@&&%%%%%%%%%%%%%%%%%@                             ")
109print("               ,*        *@&%%%%%%%%%%%%#                             ")
110print("               (           @%%%%%%%%%%%@                              ")
111print("              ,            @%%%%%%%%%%&@                              ")
112print("                          #&%%%%%%%%%%@.                              ")
113print("                         #@###%%%%%%%@/                               ")
114print("                        (@##(%%%%%%%@%                                ")
115print("                       /@###(#%%%%%&@                                 ")
116print("                      #@####%%%%%%%@                                  ")
117print("                     (@###(%%%%%%%@,                                  ")
118print("                    .@##(((#%%%%%&(         .,,.                      ")
119print("                   ,@#####%%%%%%%@    ,%@@%%%%%%%&@%                  ")
120print("                ,#&@####(%%%%%%%@@@@@&%%%%%%%%%%%###&                 ")
121print("               @%%@%####(#%%%%%&@%%%%%%%%%%%%%%##/((@@@@&*            ")
122print("              (##@%#####%%%%%%%@(#%%%(/####(/####(%@%%%%%%@/          ")
123print("           (@&%@@###(#%%%%%%@&/####(/#####/#&@@&%%%%%%%##@            ")
124print("          #@%%%%@#####(#%%%%%%@@@@@@@@@@@@@&%%%%%%%%%%%%#/(@@@@@/     ")
125print("          @%(/#@%######%%%%%%%@%%%%%%%%%%%%%%%%%%%%%(/(###@%%%%%%@%   ")
126print("         .@@#(#@#####(#%%%%%%&@###//#####/#####/(####/#%@&%%%%%%%%&&  ")
127print("        /@%%&@@@(#((((#%%%%%%&@###((#####/#####((##%@@&%%%%%%%%%%%/@. ")
128print("       ,@%%%%%%#####%%%%%%%%@@@@&&&&&&&%&@@@@@@&%%%%%%%%%%%%%%%##@,   ")
129print("       %%%%%%%%@######(%%%%%%%@&%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#/(#&&  ")
130print("       (@###/(%@##((##(%%%%%%%%@%%%%%%%%%%%%%%%%%%%%%%%%%##%###/(&&   ")
131print("    ,@@%@%##((#%@#######%%%%%%%%@&%%%%##%%%%##%%%%#/#####((####(@*    ")
132print("  *&(,    %@@%##%@#######(%%%%%%%%@#/#####((#####(#####(/#&@&.        ")
133print("                 .@###((#%%%%%%%%%&@@###((#####(###%@@&,              ")
134print("                   #@#(#######%&@@&* .*#&@@@@@@@%(,                   ")
135print("                          .,,,..                                      ")
136print()
137print("**********************************************************************")
138print("*           Welcome to the CircuitPython board test suite!           *")
139print("*              Follow the directions to run each test.               *")
140print("**********************************************************************")
141print()
142
143# List out all the pins available to us
144PINS = list(dir(board))
145print("All pins found:", end=" ")
146
147# Print pins
148for pin in PINS:
149    print(pin, end=" ")
150print("\n")
151
152# Run LED test
153print("@)}---^-----  LED TEST  -----^---{(@")
154print()
155RESULT = boardtest_led.run_test(PINS)
156TEST_RESULTS["LED Test"] = RESULT[0]
157PINS_TESTED.append(RESULT[1])
158print()
159print(RESULT[0])
160print()
161
162# Run GPIO test
163print("@)}---^-----  GPIO TEST  -----^---{(@")
164print()
165RESULT = boardtest_gpio.run_test(PINS)
166TEST_RESULTS["GPIO Test"] = RESULT[0]
167PINS_TESTED.append(RESULT[1])
168print()
169print(RESULT[0])
170print()
171
172# Run voltage monitor test
173print("@)}---^-----  VOLTAGE MONITOR TEST  -----^---{(@")
174print()
175RESULT = boardtest_voltage_monitor.run_test(PINS)
176TEST_RESULTS["Voltage Monitor Test"] = RESULT[0]
177PINS_TESTED.append(RESULT[1])
178print()
179print(RESULT[0])
180print()
181
182# Run UART test
183print("@)}---^-----  UART TEST  -----^---{(@")
184print()
185RESULT = boardtest_uart.run_test(PINS, UART_TX_PIN_NAME, UART_RX_PIN_NAME, UART_BAUD_RATE)
186TEST_RESULTS["UART Test"] = RESULT[0]
187PINS_TESTED.append(RESULT[1])
188print()
189print(RESULT[0])
190print()
191
192# Run SPI test
193print("@)}---^-----  SPI TEST  -----^---{(@")
194print()
195RESULT = boardtest_spi.run_test(
196    PINS,
197    mosi_pin=SPI_MOSI_PIN_NAME,
198    miso_pin=SPI_MISO_PIN_NAME,
199    sck_pin=SPI_SCK_PIN_NAME,
200    cs_pin=SPI_CS_PIN_NAME,
201)
202TEST_RESULTS["SPI Test"] = RESULT[0]
203PINS_TESTED.append(RESULT[1])
204print()
205print(RESULT[0])
206print()
207
208# Run I2C test
209print("@)}---^-----  I2C TEST  -----^---{(@")
210print()
211RESULT = boardtest_i2c.run_test(PINS, sda_pin=I2C_SDA_PIN_NAME, scl_pin=I2C_SCL_PIN_NAME)
212TEST_RESULTS["I2C Test"] = RESULT[0]
213PINS_TESTED.append(RESULT[1])
214print()
215print(RESULT[0])
216print()
217
218# Print out test results
219print("@)}---^-----  TEST RESULTS  -----^---{(@")
220print()
221
222# Find appropriate spaces for printing test results
223NUM_SPACES = 0
224for key in TEST_RESULTS:
225    NUM_SPACES = max(NUM_SPACES, len(key))
226
227# Print test results
228for key, val in TEST_RESULTS.items():
229    print(key + ":", end=" ")
230    for i in range(NUM_SPACES - len(key)):
231        print(end=" ")
232    print(val)
233print()
234
235# Figure out which pins were tested and not tested
236TESTED = []
237for sublist in PINS_TESTED:
238    for pin in sublist:
239        TESTED.append(pin)
240NOT_TESTED = list(set(PINS).difference(set(TESTED)))
241
242# Print tested pins
243print("The following pins were tested:", end=" ")
244for pin in TESTED:
245    print(pin, end=" ")
246print("\n")
247
248# Print pins not tested
249print("The following pins were NOT tested:", end=" ")
250for pin in NOT_TESTED:
251    print(pin, end=" ")
252print("\n")