chiark / gitweb /
More typos, added mcp3004/mcp3008
[wiringPi.git] / gpio / pintest
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
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   revision=`gpio -V`
108   cat <<EOF
109 PinTest
110 =======
111
112 This is a simple utility to test the GPIO pins on your revision $revision
113 Raspberry Pi.
114
115 NOTE: All GPIO peripherals must be removed to perform this test. This
116   includes serial, I2C and SPI connections. You may get incorrect results
117   if something is connected and it interferes with the test.
118
119 This test can only test the input side of things. It uses the internal
120 pull-up and pull-down resistors to simulate inputs. It does not test
121 the output drivers.
122
123 You will need to reboot your Pi after this test if you wish to use the 
124 serial port as it will be left in GPIO mode rather than serial mode.
125
126 Please make sure everything is removed and press the ENTER key to continue,
127 EOF
128
129   echo -n "or Control-C to abort... "
130   read a
131 }
132
133
134 # Start here
135 ################################################################################
136
137 intro
138 gpio unexportall
139 gpio reset
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 # P5 pins, if a rev 2:
151
152 if [ $revision = 2 ]; then
153   testPins 17 20 "The 4 pins on the P5 connector"
154 fi
155
156 # SPI
157
158 testPins 10 14 "The 5 SPI pins"
159
160 # Serial
161
162 testPins 15 16 "The serial pins"
163
164 # I2C - Needs somewhat different testing
165 #       due to the on-board pull-up's
166
167 echo -n "                  The I2C pins  8: 9: "
168 errs=0
169 gpio mode 8 in
170 gpio mode 9 in
171
172 if [ `gpio read 8` = 0 ]; then
173   echo "Pin 8 failure - expected 1, got 0"
174   let errs+=1
175 fi
176
177 if [ `gpio read 9` = 0 ]; then
178   echo "Pin 9 failure - expected 1, got 0"
179   let errs+=1
180 fi
181
182 if [ $errs = 0 ]; then
183   echo " OK"
184 else
185   printErrCount
186 fi
187
188 echo ""
189 if [ $totErrs != 0 ]; then
190   echo ""
191   echo "Faults detected! Output of 'readall':"
192   gpio readall
193 fi