chiark / gitweb /
Updates to the build process
[wiringPi.git] / gpio / pintest
1 #!/bin/bash
2 #
3 # pintest
4 #       Test the Pi's GPIO port
5 # Copyright (c) 2013-2015 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
24
25 # logErr pin, expected got
26 ################################################################################
27
28 logErr ()
29 {
30   if [ $errs = 0 ]; then
31     echo ""
32   fi
33   echo " --> Pin $1 failure. Expected $2, got $3"
34   let errs+=1
35 }
36
37
38 # printErrorCount
39 ################################################################################
40
41 printErrCount()
42 {
43   if [ $errs = 0 ]; then
44     echo "No faults detected."
45   elif [ $errs = 1 ]; then
46     echo "One fault detected."
47   else
48     echo "$errs faults detected"
49   fi
50 }
51
52
53 # testPins start end
54 ################################################################################
55
56 testPins()
57 {
58   start=$1
59     end=$2
60    errs=0
61
62   printf "%30s %2d:%2d: "  "$3" $1 $2
63
64 # Set range to inputs
65
66   for i in `seq $start $end`; do
67     gpio mode $i in
68   done
69
70 # Enable internal pull-ups and expect to read high
71
72   for i in `seq $start $end`; do
73     gpio mode $i up
74     if [ `gpio read $i` = 0 ]; then
75       logErr $i 1 0
76     fi
77   done
78
79 # Enable internal pull-downs and expect to read low
80
81   for i in `seq $start $end`; do
82     gpio mode $i down
83     if [ `gpio read $i` = 1 ]; then
84       echo "Pin $i failure - expected 0, got 1"
85       let errs+=1
86     fi
87   done
88
89 # Remove the internal pull up/downs
90
91   for i in `seq $start $end`; do
92     gpio mode $i tri
93   done
94
95   if [ $errs = 0 ]; then
96     echo " OK"
97   else
98     printErrCount
99   fi
100
101   let totErrs+=errs
102 }
103
104
105 intro()
106 {
107   cat <<EOF
108 PinTest
109 =======
110
111 This is a simple utility to test the GPIO pins on your Raspberry Pi.
112
113 NOTE: All GPIO peripherals must be removed to perform this test. This
114   includes serial, I2C and SPI connections. You may get incorrect results
115   if something is connected and it interferes with the test.
116
117 This test can only test the input side of things. It uses the internal
118 pull-up and pull-down resistors to simulate inputs. It does not test
119 the output drivers.
120
121 You will need to reboot your Pi after this test if you wish to use the 
122 serial port as it will be left in GPIO mode rather than serial mode.
123
124 This test only tests the original pins present on the Rev A and B. It
125 does not test the extra pins on the Revision A2, B2 nor the A+ or B+
126
127 Please make sure everything is removed and press the ENTER key to continue,
128 EOF
129
130   echo -n "or Control-C to abort... "
131   read a
132 }
133
134
135 # Start here
136 ################################################################################
137
138 intro
139 gpio unexportall
140
141    errs=0
142 totErrs=0
143
144 echo ""
145
146 # Main pins
147
148 testPins 0 7 "The main 8 GPIO pins"
149
150 # SPI
151
152 testPins 10 14 "The 5 SPI pins"
153
154 # Serial
155
156 testPins 15 16 "The serial pins"
157
158 # I2C - Needs somewhat different testing
159 #       due to the on-board pull-up's
160
161 echo -n "                  The I2C pins  8: 9: "
162 errs=0
163 gpio mode 8 in
164 gpio mode 9 in
165
166 if [ `gpio read 8` = 0 ]; then
167   echo "Pin 8 failure - expected 1, got 0"
168   let errs+=1
169 fi
170
171 if [ `gpio read 9` = 0 ]; then
172   echo "Pin 9 failure - expected 1, got 0"
173   let errs+=1
174 fi
175
176 if [ $errs = 0 ]; then
177   echo " OK"
178 else
179   printErrCount
180 fi
181
182 echo ""
183 if [ $totErrs != 0 ]; then
184   echo ""
185   echo "Faults detected! Output of 'readall':"
186   gpio readall
187 fi