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 boardtest_led
 73from adafruit_boardtest import boardtest_gpio
 74from adafruit_boardtest import boardtest_voltage_monitor
 75from adafruit_boardtest import boardtest_uart
 76from adafruit_boardtest import boardtest_spi
 77from adafruit_boardtest import boardtest_i2c
 78
 79# Constants
 80UART_TX_PIN_NAME = "TX"
 81UART_RX_PIN_NAME = "RX"
 82UART_BAUD_RATE = 9600
 83SPI_MOSI_PIN_NAME = "MOSI"
 84SPI_MISO_PIN_NAME = "MISO"
 85SPI_SCK_PIN_NAME = "SCK"
 86SPI_CS_PIN_NAME = "D2"
 87I2C_SDA_PIN_NAME = "SDA"
 88I2C_SCL_PIN_NAME = "SCL"
 89
 90# Results dictionary
 91TEST_RESULTS = {}
 92
 93# Save tested pins
 94PINS_TESTED = []
 95
 96# Print welcome message
 97print()
 98print("                            ....                                      ")
 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("*           Welcome to the CircuitPython board test suite!           *")
137print("*              Follow the directions to run each test.               *")
138print("**********************************************************************")
139print()
140
141# List out all the pins available to us
142PINS = list(dir(board))
143print("All pins found:", end=" ")
144
145# Print pins
146for pin in PINS:
147    print(pin, end=" ")
148print("\n")
149
150# Run LED test
151print("@)}---^-----  LED TEST  -----^---{(@")
152print()
153RESULT = boardtest_led.run_test(PINS)
154TEST_RESULTS["LED Test"] = RESULT[0]
155PINS_TESTED.append(RESULT[1])
156print()
157print(RESULT[0])
158print()
159
160# Run GPIO test
161print("@)}---^-----  GPIO TEST  -----^---{(@")
162print()
163RESULT = boardtest_gpio.run_test(PINS)
164TEST_RESULTS["GPIO Test"] = RESULT[0]
165PINS_TESTED.append(RESULT[1])
166print()
167print(RESULT[0])
168print()
169
170# Run voltage monitor test
171print("@)}---^-----  VOLTAGE MONITOR TEST  -----^---{(@")
172print()
173RESULT = boardtest_voltage_monitor.run_test(PINS)
174TEST_RESULTS["Voltage Monitor Test"] = RESULT[0]
175PINS_TESTED.append(RESULT[1])
176print()
177print(RESULT[0])
178print()
179
180# Run UART test
181print("@)}---^-----  UART TEST  -----^---{(@")
182print()
183RESULT = boardtest_uart.run_test(
184    PINS, UART_TX_PIN_NAME, UART_RX_PIN_NAME, UART_BAUD_RATE
185)
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(
212    PINS, sda_pin=I2C_SDA_PIN_NAME, scl_pin=I2C_SCL_PIN_NAME
213)
214TEST_RESULTS["I2C Test"] = RESULT[0]
215PINS_TESTED.append(RESULT[1])
216print()
217print(RESULT[0])
218print()
219
220# Print out test results
221print("@)}---^-----  TEST RESULTS  -----^---{(@")
222print()
223
224# Find appropriate spaces for printing test results
225NUM_SPACES = 0
226for key in TEST_RESULTS:
227    if len(key) > NUM_SPACES:
228        NUM_SPACES = len(key)
229
230# Print test results
231for key, val in TEST_RESULTS.items():
232    print(key + ":", end=" ")
233    for i in range(NUM_SPACES - len(key)):
234        print(end=" ")
235    print(val)
236print()
237
238# Figure out which pins were tested and not tested
239TESTED = []
240for sublist in PINS_TESTED:
241    for pin in sublist:
242        TESTED.append(pin)
243NOT_TESTED = list(set(PINS).difference(set(TESTED)))
244
245# Print tested pins
246print("The following pins were tested:", end=" ")
247for pin in TESTED:
248    print(pin, end=" ")
249print("\n")
250
251# Print pins not tested
252print("The following pins were NOT tested:", end=" ")
253for pin in NOT_TESTED:
254    print(pin, end=" ")
255print("\n")