25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

194 lines
4.0 KiB

  1. #!/bin/bash
  2. #
  3. # pintest
  4. # Test the Pi's GPIO port
  5. # Copyright (c) 2013 Gordon Henderson
  6. #################################################################################
  7. # This file is part of wiringPi:
  8. # Wiring Compatable library for the Raspberry Pi
  9. #
  10. # wiringPi is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Lesser General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # wiringPi is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License
  21. # along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  22. #################################################################################
  23. # logErr pin, expected got
  24. ################################################################################
  25. logErr ()
  26. {
  27. if [ $errs = 0 ]; then
  28. echo ""
  29. fi
  30. echo " --> Pin $1 failure. Expected $2, got $3"
  31. let errs+=1
  32. }
  33. # printErrorCount
  34. ################################################################################
  35. printErrCount()
  36. {
  37. if [ $errs = 0 ]; then
  38. echo "No faults detected."
  39. elif [ $errs = 1 ]; then
  40. echo "One fault detected."
  41. else
  42. echo "$errs faults detected"
  43. fi
  44. }
  45. # testPins start end
  46. ################################################################################
  47. testPins()
  48. {
  49. start=$1
  50. end=$2
  51. errs=0
  52. printf "%30s %2d:%2d: " "$3" $1 $2
  53. # Set range to inputs
  54. for i in `seq $start $end`; do
  55. gpio mode $i in
  56. done
  57. # Enable internal pull-ups and expect to read high
  58. for i in `seq $start $end`; do
  59. gpio mode $i up
  60. if [ `gpio read $i` = 0 ]; then
  61. logErr $i 1 0
  62. fi
  63. done
  64. # Enable internal pull-downs and expect to read low
  65. for i in `seq $start $end`; do
  66. gpio mode $i down
  67. if [ `gpio read $i` = 1 ]; then
  68. echo "Pin $i failure - expected 0, got 1"
  69. let errs+=1
  70. fi
  71. done
  72. # Remove the internal pull up/downs
  73. for i in `seq $start $end`; do
  74. gpio mode $i tri
  75. done
  76. if [ $errs = 0 ]; then
  77. echo " OK"
  78. else
  79. printErrCount
  80. fi
  81. let totErrs+=errs
  82. }
  83. intro()
  84. {
  85. revision=`gpio -V`
  86. cat <<EOF
  87. PinTest
  88. =======
  89. This is a simple utility to test the GPIO pins on your revision $revision
  90. Raspberry Pi.
  91. NOTE: All GPIO peripherals must be removed to perform this test. This
  92. includes serial, I2C and SPI connections. You may get incorrect results
  93. if something is connected and it interferes with the test.
  94. This test can only test the input side of things. It uses the internal
  95. pull-up and pull-down resistors to simulate inputs. It does not test
  96. the output drivers.
  97. You will need to reboot your Pi after this test if you wish to use the
  98. serial port as it will be left in GPIO mode rather than serial mode.
  99. Please make sure everything is removed and press the ENTER key to continue,
  100. EOF
  101. echo -n "or Control-C to abort... "
  102. read a
  103. }
  104. # Start here
  105. ################################################################################
  106. intro
  107. gpio unexportall
  108. gpio reset
  109. errs=0
  110. totErrs=0
  111. echo ""
  112. # Main pins
  113. testPins 0 7 "The main 8 GPIO pins"
  114. # P5 pins, if a rev 2:
  115. if [ $revision = 2 ]; then
  116. testPins 17 20 "The 4 pins on the P5 connector"
  117. fi
  118. # SPI
  119. testPins 10 14 "The 5 SPI pins"
  120. # Serial
  121. testPins 15 16 "The serial pins"
  122. # I2C - Needs somewhat different testing
  123. # due to the on-board pull-up's
  124. echo -n " The I2C pins 8: 9: "
  125. errs=0
  126. gpio mode 8 in
  127. gpio mode 9 in
  128. if [ `gpio read 8` = 0 ]; then
  129. echo "Pin 8 failure - expected 1, got 0"
  130. let errs+=1
  131. fi
  132. if [ `gpio read 9` = 0 ]; then
  133. echo "Pin 9 failure - expected 1, got 0"
  134. let errs+=1
  135. fi
  136. if [ $errs = 0 ]; then
  137. echo " OK"
  138. else
  139. printErrCount
  140. fi
  141. echo ""
  142. if [ $totErrs != 0 ]; then
  143. echo ""
  144. echo "Faults detected! Output of 'readall':"
  145. gpio readall
  146. fi