/ adafruit_boardtest / boardtest_sd_cd.py
boardtest_sd_cd.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_sd_cd` 24 ==================================================== 25 Reports the output of an SD card's chip detect (CD) 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 **Hardware:** 36 37 * `SD Card <https://www.adafruit.com/product/1294>`_ 38 39 **Software and Dependencies:** 40 41 * Adafruit CircuitPython firmware for the supported boards: 42 https://github.com/adafruit/circuitpython/releases 43 44 """ 45 46 import board 47 import digitalio 48 49 __version__ = "0.0.0-auto.0" 50 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" 51 52 # Constants 53 SD_CD_PIN_NAME = "SD_CD" 54 55 # Test result strings 56 PASS = "PASS" 57 FAIL = "FAIL" 58 NA = "N/A" 59 60 61 def run_test(pins, cd_pin=SD_CD_PIN_NAME): 62 63 """ 64 Checks status of CD pin as user inserts and removes SD card. 65 66 :param list[str] pins: list of pins to run the test on 67 :param str cd_pin: pin name of chip detect (CD) line 68 :return: tuple(str, list[str]): test result followed by list of pins tested 69 """ 70 71 # Ask user to insert and remove SD card 72 if list(set(pins).intersection(set([cd_pin]))): 73 74 # Configure CD pin as input with pullup 75 cdt = digitalio.DigitalInOut(getattr(board, cd_pin)) 76 cdt.direction = digitalio.Direction.INPUT 77 cdt.pull = digitalio.Pull.UP 78 79 # Tell user to insert SD card 80 print("Connect " + cd_pin + " to CD pin on SD card holder.") 81 print("Insert SD card into holder.") 82 print("Press enter to continue.") 83 input() 84 85 # Make sure we see that the pin is low 86 if cdt.value: 87 print("Error: Card not detected") 88 return FAIL, [cd_pin] 89 90 # Tell user to remove SD card 91 print("Card detected. Remove card and press enter to continue.") 92 input() 93 94 # Make sure we see that the pin is high 95 if not cdt.value: 96 print("Error: Card detected") 97 return FAIL, [cd_pin] 98 99 # Test passed 100 print("Card removed") 101 return PASS, [cd_pin] 102 103 # Else (no pins found) 104 print("No CD pin found") 105 return NA, [] 106 107 108 def _main(): 109 110 # List out all the pins available to us 111 pins = list(dir(board)) 112 print() 113 print("All pins found:", end=" ") 114 115 # Print pins 116 for pin in pins: 117 print(pin, end=" ") 118 print("\n") 119 120 # Run test 121 result = run_test(pins) 122 print() 123 print(result[0]) 124 print("Pins tested: " + str(result[1])) 125 126 127 # Execute only if run as main.py or code.py 128 if __name__ == "__main__": 129 _main()