/ examples / boardtest_simpletest.py
boardtest_simpletest.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  `BoardTest Suite`
 24  ====================================================
 25  CircuitPython board hardware test suite
 26  
 27  * Author(s): Shawn Hymel
 28  * Date: December 8, 2018
 29  
 30  Implementation Notes
 31  --------------------
 32  Run this to test various input/output abilities of a board. Tests the
 33  following:
 34  
 35  * Onboard LEDs
 36  * GPIO output
 37  * Onboard battery voltage monitor
 38  * SPI
 39  * I2C
 40  
 41  You will need the following components:
 42  
 43  * Multimeter
 44  * LED
 45  * 1x 330 Ohm resistor or 220 Ohm resistor
 46  * 2x 4.7k Ohm resistor
 47  * Microchip 25AA040A SPI EEPROM
 48  * Microchip AT24HC04B I2C EEPROM
 49  * Breadboard
 50  * Wires
 51  
 52  Copy the following files to the adafruit_boardtest folder on your CIRCUITPY drive:
 53  
 54  * __init__.py
 55  * boardtest_gpio.mpy
 56  * boardtest_i2c.mpy
 57  * boardtest_led.mpy
 58  * boardtest_spi.mpy
 59  * boardtest_uart.mpy
 60  * boardtest_voltage_monitor
 61  
 62  Copy this file to the root directory of your CIRCUITPY drive and rename the
 63  filename to code.py. Open a serial terminal, and follow the prompts to run
 64  the various tests.
 65  """
 66  
 67  import board
 68  
 69  from adafruit_boardtest import boardtest_led
 70  from adafruit_boardtest import boardtest_gpio
 71  from adafruit_boardtest import boardtest_voltage_monitor
 72  from adafruit_boardtest import boardtest_uart
 73  from adafruit_boardtest import boardtest_spi
 74  from adafruit_boardtest import boardtest_i2c
 75  
 76  # Constants
 77  UART_TX_PIN_NAME = "TX"
 78  UART_RX_PIN_NAME = "RX"
 79  UART_BAUD_RATE = 9600
 80  SPI_MOSI_PIN_NAME = "MOSI"
 81  SPI_MISO_PIN_NAME = "MISO"
 82  SPI_SCK_PIN_NAME = "SCK"
 83  SPI_CS_PIN_NAME = "D2"
 84  I2C_SDA_PIN_NAME = "SDA"
 85  I2C_SCL_PIN_NAME = "SCL"
 86  
 87  # Results dictionary
 88  TEST_RESULTS = {}
 89  
 90  # Save tested pins
 91  PINS_TESTED = []
 92  
 93  # Print welcome message
 94  print()
 95  print("                            ....                                      ")
 96  print("                        #@@%%%%%%&@@/                                 ")
 97  print("                     (&@%%%%%%%%%%%%%@&                               ")
 98  print("                  .(@&%%%@*    *&%%%%%%@.                             ")
 99  print("            ,@@&&%%%%%%%%//@%,/ /&%%%%%%@                             ")
100  print("            %@%%%&%%%%%%%#(@@@&&%%%%%%%%@*                            ")
101  print("             @&%%&%%%%%%%%%%%%%%%%%%%%%%@/                            ")
102  print("               &@@&%%%%&&&%%%%%%%%%%%%%%@,                            ")
103  print("                ,/ &@&&%%%%%%%%%%%%%%%%%@                             ")
104  print("               ,*        *@&%%%%%%%%%%%%#                             ")
105  print("               (           @%%%%%%%%%%%@                              ")
106  print("              ,            @%%%%%%%%%%&@                              ")
107  print("                          #&%%%%%%%%%%@.                              ")
108  print("                         #@###%%%%%%%@/                               ")
109  print("                        (@##(%%%%%%%@%                                ")
110  print("                       /@###(#%%%%%&@                                 ")
111  print("                      #@####%%%%%%%@                                  ")
112  print("                     (@###(%%%%%%%@,                                  ")
113  print("                    .@##(((#%%%%%&(         .,,.                      ")
114  print("                   ,@#####%%%%%%%@    ,%@@%%%%%%%&@%                  ")
115  print("                ,#&@####(%%%%%%%@@@@@&%%%%%%%%%%%###&                 ")
116  print("               @%%@%####(#%%%%%&@%%%%%%%%%%%%%%##/((@@@@&*            ")
117  print("              (##@%#####%%%%%%%@(#%%%(/####(/####(%@%%%%%%@/          ")
118  print("           (@&%@@###(#%%%%%%@&/####(/#####/#&@@&%%%%%%%##@            ")
119  print("          #@%%%%@#####(#%%%%%%@@@@@@@@@@@@@&%%%%%%%%%%%%#/(@@@@@/     ")
120  print("          @%(/#@%######%%%%%%%@%%%%%%%%%%%%%%%%%%%%%(/(###@%%%%%%@%   ")
121  print("         .@@#(#@#####(#%%%%%%&@###//#####/#####/(####/#%@&%%%%%%%%&&  ")
122  print("        /@%%&@@@(#((((#%%%%%%&@###((#####/#####((##%@@&%%%%%%%%%%%/@. ")
123  print("       ,@%%%%%%#####%%%%%%%%@@@@&&&&&&&%&@@@@@@&%%%%%%%%%%%%%%%##@,   ")
124  print("       %%%%%%%%@######(%%%%%%%@&%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#/(#&&  ")
125  print("       (@###/(%@##((##(%%%%%%%%@%%%%%%%%%%%%%%%%%%%%%%%%%##%###/(&&   ")
126  print("    ,@@%@%##((#%@#######%%%%%%%%@&%%%%##%%%%##%%%%#/#####((####(@*    ")
127  print("  *&(,    %@@%##%@#######(%%%%%%%%@#/#####((#####(#####(/#&@&.        ")
128  print("                 .@###((#%%%%%%%%%&@@###((#####(###%@@&,              ")
129  print("                   #@#(#######%&@@&* .*#&@@@@@@@%(,                   ")
130  print("                          .,,,..                                      ")
131  print()
132  print("**********************************************************************")
133  print("*           Welcome to the CircuitPython board test suite!           *")
134  print("*              Follow the directions to run each test.               *")
135  print("**********************************************************************")
136  print()
137  
138  # List out all the pins available to us
139  PINS = list(dir(board))
140  print("All pins found:", end=" ")
141  
142  # Print pins
143  for pin in PINS:
144      print(pin, end=" ")
145  print("\n")
146  
147  # Run LED test
148  print("@)}---^-----  LED TEST  -----^---{(@")
149  print()
150  RESULT = boardtest_led.run_test(PINS)
151  TEST_RESULTS["LED Test"] = RESULT[0]
152  PINS_TESTED.append(RESULT[1])
153  print()
154  print(RESULT[0])
155  print()
156  
157  # Run GPIO test
158  print("@)}---^-----  GPIO TEST  -----^---{(@")
159  print()
160  RESULT = boardtest_gpio.run_test(PINS)
161  TEST_RESULTS["GPIO Test"] = RESULT[0]
162  PINS_TESTED.append(RESULT[1])
163  print()
164  print(RESULT[0])
165  print()
166  
167  # Run voltage monitor test
168  print("@)}---^-----  VOLTAGE MONITOR TEST  -----^---{(@")
169  print()
170  RESULT = boardtest_voltage_monitor.run_test(PINS)
171  TEST_RESULTS["Voltage Monitor Test"] = RESULT[0]
172  PINS_TESTED.append(RESULT[1])
173  print()
174  print(RESULT[0])
175  print()
176  
177  # Run UART test
178  print("@)}---^-----  UART TEST  -----^---{(@")
179  print()
180  RESULT = boardtest_uart.run_test(
181      PINS, UART_TX_PIN_NAME, UART_RX_PIN_NAME, UART_BAUD_RATE
182  )
183  TEST_RESULTS["UART Test"] = RESULT[0]
184  PINS_TESTED.append(RESULT[1])
185  print()
186  print(RESULT[0])
187  print()
188  
189  # Run SPI test
190  print("@)}---^-----  SPI TEST  -----^---{(@")
191  print()
192  RESULT = boardtest_spi.run_test(
193      PINS,
194      mosi_pin=SPI_MOSI_PIN_NAME,
195      miso_pin=SPI_MISO_PIN_NAME,
196      sck_pin=SPI_SCK_PIN_NAME,
197      cs_pin=SPI_CS_PIN_NAME,
198  )
199  TEST_RESULTS["SPI Test"] = RESULT[0]
200  PINS_TESTED.append(RESULT[1])
201  print()
202  print(RESULT[0])
203  print()
204  
205  # Run I2C test
206  print("@)}---^-----  I2C TEST  -----^---{(@")
207  print()
208  RESULT = boardtest_i2c.run_test(
209      PINS, sda_pin=I2C_SDA_PIN_NAME, scl_pin=I2C_SCL_PIN_NAME
210  )
211  TEST_RESULTS["I2C Test"] = RESULT[0]
212  PINS_TESTED.append(RESULT[1])
213  print()
214  print(RESULT[0])
215  print()
216  
217  # Print out test results
218  print("@)}---^-----  TEST RESULTS  -----^---{(@")
219  print()
220  
221  # Find appropriate spaces for printing test results
222  NUM_SPACES = 0
223  for key in TEST_RESULTS:
224      if len(key) > NUM_SPACES:
225          NUM_SPACES = len(key)
226  
227  # Print test results
228  for key in TEST_RESULTS:
229      print(key + ":", end=" ")
230      for i in range(NUM_SPACES - len(key)):
231          print(end=" ")
232      print(TEST_RESULTS[key])
233  print()
234  
235  # Figure out which pins were tested and not tested
236  TESTED = []
237  for sublist in PINS_TESTED:
238      for pin in sublist:
239          TESTED.append(pin)
240  NOT_TESTED = list(set(PINS).difference(set(TESTED)))
241  
242  # Print tested pins
243  print("The following pins were tested:", end=" ")
244  for pin in TESTED:
245      print(pin, end=" ")
246  print("\n")
247  
248  # Print pins not tested
249  print("The following pins were NOT tested:", end=" ")
250  for pin in NOT_TESTED:
251      print(pin, end=" ")
252  print("\n")