blob: 278e6ee2ad24a92a1dd18d6d9aef277ac04d2391 [file] [log] [blame]
Gordon Henderson02a3bd82013-06-27 21:57:09 +01001/*
2 * readall.c:
3 * The readall functions - getting a bit big, so split them out.
Gordon Hendersoneb1fc2c2015-01-30 18:14:49 +00004 * Copyright (c) 2012-2015 Gordon Henderson
Gordon Henderson02a3bd82013-06-27 21:57:09 +01005 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
8 *
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
21 ***********************************************************************
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdint.h>
28#include <ctype.h>
29#include <string.h>
30#include <unistd.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35
36#include <wiringPi.h>
37
38extern int wpMode ;
39
Gordon Henderson6fba4032014-06-24 19:23:31 +010040#ifndef TRUE
41# define TRUE (1==1)
42# define FALSE (1==2)
43#endif
44
Gordon Henderson02a3bd82013-06-27 21:57:09 +010045/*
46 * doReadallExternal:
47 * A relatively crude way to read the pins on an external device.
48 * We don't know the input/output mode of pins, but we can tell
49 * if it's an analog pin or a digital one...
50 *********************************************************************************
51 */
52
53static void doReadallExternal (void)
54{
55 int pin ;
56
57 printf ("+------+---------+--------+\n") ;
58 printf ("| Pin | Digital | Analog |\n") ;
59 printf ("+------+---------+--------+\n") ;
60
61 for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
62 printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
63
64 printf ("+------+---------+--------+\n") ;
65}
66
67
68/*
69 * doReadall:
70 * Read all the GPIO pins
71 * We also want to use this to read the state of pins on an externally
72 * connected device, so we need to do some fiddling with the internal
73 * wiringPi node structures - since the gpio command can only use
74 * one external device at a time, we'll use that to our advantage...
75 *********************************************************************************
76 */
77
Gordon Henderson02a3bd82013-06-27 21:57:09 +010078static char *alts [] =
79{
80 "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
81} ;
82
Gordon Henderson02a3bd82013-06-27 21:57:09 +010083static int physToWpi [64] =
84{
85 -1, // 0
86 -1, -1, // 1, 2
87 8, -1,
88 9, -1,
89 7, 15,
90 -1, 16,
91 0, 1,
92 2, -1,
93 3, 4,
94 -1, 5,
95 12, -1,
96 13, 6,
97 14, 10,
98 -1, 11, // 25, 26
Gordon Hendersondca8a192014-07-14 08:39:38 +010099 30, 31, // Actually I2C, but not used
100 21, -1,
101 22, 26,
102 23, -1,
103 24, 27,
104 25, 28,
105 -1, 29,
Gordon Hendersondf453882014-07-17 22:23:57 +0100106 -1, -1,
107 -1, -1,
108 -1, -1,
109 -1, -1,
110 -1, -1,
111 17, 18,
112 19, 20,
113 -1, -1, -1, -1, -1, -1, -1, -1, -1
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100114} ;
115
116static char *physNames [64] =
117{
118 NULL,
119
Gordon Hendersondca8a192014-07-14 08:39:38 +0100120 " 3.3v", "5v ",
121 " SDA.1", "5V ",
122 " SCL.1", "0v ",
123 "GPIO. 7", "TxD ",
124 " 0v", "RxD ",
125 "GPIO. 0", "GPIO. 1",
126 "GPIO. 2", "0v ",
127 "GPIO. 3", "GPIO. 4",
128 " 3.3v", "GPIO. 5",
129 " MOSI", "0v ",
130 " MISO", "GPIO. 6",
131 " SCLK", "CE0 ",
132 " 0v", "CE1 ",
Gordon Hendersondf453882014-07-17 22:23:57 +0100133 " SDA.0", "SCL.0 ",
Gordon Hendersondca8a192014-07-14 08:39:38 +0100134 "GPIO.21", "0v ",
135 "GPIO.22", "GPIO.26",
136 "GPIO.23", "0v ",
137 "GPIO.24", "GPIO.27",
138 "GPIO.25", "GPIO.28",
139 " 0v", "GPIO.29",
140 NULL, NULL,
141 NULL, NULL,
142 NULL, NULL,
143 NULL, NULL,
144 NULL, NULL,
145 "GPIO.17", "GPIO.18",
146 "GPIO.19", "GPIO.20",
147 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100148} ;
149
Gordon Hendersondf453882014-07-17 22:23:57 +0100150
151/*
152 * readallPhys:
153 * Given a physical pin output the data on it and the next pin:
154 *| BCM | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi | BCM |
155 *********************************************************************************
156 */
157
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100158static void readallPhys (int physPin)
159{
160 int pin ;
161
Gordon Hendersondf453882014-07-17 22:23:57 +0100162 if (physPinToGpio (physPin) == -1)
163 printf (" | | ") ;
164 else
165 printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi [physPin]) ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100166
Gordon Hendersondca8a192014-07-14 08:39:38 +0100167 printf (" | %s", physNames [physPin]) ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100168
169 if (physToWpi [physPin] == -1)
Gordon Hendersondf453882014-07-17 22:23:57 +0100170 printf (" | | ") ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100171 else
172 {
173 /**/ if (wpMode == WPI_MODE_GPIO)
174 pin = physPinToGpio (physPin) ;
175 else if (wpMode == WPI_MODE_PHYS)
176 pin = physPin ;
177 else
178 pin = physToWpi [physPin] ;
179
180 printf (" | %4s", alts [getAlt (pin)]) ;
Gordon Hendersondf453882014-07-17 22:23:57 +0100181 printf (" | %d", digitalRead (pin)) ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100182 }
183
184// Pin numbers:
185
186 printf (" | %2d", physPin) ;
187 ++physPin ;
188 printf (" || %-2d", physPin) ;
189
190// Same, reversed
191
192 if (physToWpi [physPin] == -1)
Gordon Hendersondf453882014-07-17 22:23:57 +0100193 printf (" | | ") ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100194 else
195 {
196 /**/ if (wpMode == WPI_MODE_GPIO)
197 pin = physPinToGpio (physPin) ;
198 else if (wpMode == WPI_MODE_PHYS)
199 pin = physPin ;
200 else
201 pin = physToWpi [physPin] ;
202
Gordon Hendersondf453882014-07-17 22:23:57 +0100203 printf (" | %d", digitalRead (pin)) ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100204 printf (" | %-4s", alts [getAlt (pin)]) ;
205 }
206
207 printf (" | %-5s", physNames [physPin]) ;
208
Gordon Hendersondf453882014-07-17 22:23:57 +0100209 if (physToWpi [physPin] == -1)
210 printf (" | | ") ;
211 else
212 printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio (physPin)) ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100213
214 printf (" |\n") ;
215}
216
217
Gordon Hendersondf453882014-07-17 22:23:57 +0100218void cmReadall (void)
Gordon Henderson6fba4032014-06-24 19:23:31 +0100219{
Gordon Henderson6fba4032014-06-24 19:23:31 +0100220 int pin ;
Gordon Henderson6fba4032014-06-24 19:23:31 +0100221
222 printf ("+-----+------+-------+ +-----+------+-------+\n") ;
223 printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ;
224 printf ("+-----+------+-------+ +-----+------+-------+\n") ;
225
Gordon Henderson143b3832016-01-28 19:20:31 +0000226 for (pin = 0 ; pin < 27 ; ++pin)
Gordon Henderson6fba4032014-06-24 19:23:31 +0100227 {
228 printf ("| %3d ", pin) ;
229 printf ("| %-4s ", alts [getAlt (pin)]) ;
230 printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
231 printf ("| ") ;
Gordon Henderson143b3832016-01-28 19:20:31 +0000232 printf ("| %3d ", pin + 27) ;
233 printf ("| %-4s ", alts [getAlt (pin + 27)]) ;
234 printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ;
Gordon Henderson6fba4032014-06-24 19:23:31 +0100235 printf ("|\n") ;
236 }
237
238 printf ("+-----+------+-------+ +-----+------+-------+\n") ;
Gordon Henderson143b3832016-01-28 19:20:31 +0000239
Gordon Hendersondf453882014-07-17 22:23:57 +0100240}
Gordon Henderson6fba4032014-06-24 19:23:31 +0100241
Gordon Hendersondf453882014-07-17 22:23:57 +0100242
243/*
244 * abReadall:
245 * Read all the pins on the model A or B.
246 *********************************************************************************
247 */
248
249void abReadall (int model, int rev)
250{
251 int pin ;
252 char *type ;
253
254 if (model == PI_MODEL_A)
255 type = " A" ;
256 else
257 if (rev == PI_VERSION_2)
258 type = "B2" ;
259 else
260 type = "B1" ;
261
262 printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
263 printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
264 printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
265 for (pin = 1 ; pin <= 26 ; pin += 2)
266 readallPhys (pin) ;
267
268 if (rev == PI_VERSION_2) // B version 2
269 {
270 printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
271 for (pin = 51 ; pin <= 54 ; pin += 2)
272 readallPhys (pin) ;
273 }
274
275 printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
276 printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
277 printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
Gordon Henderson6fba4032014-06-24 19:23:31 +0100278}
279
280
Gordon Hendersondca8a192014-07-14 08:39:38 +0100281/*
Gordon Henderson0a9fdeb2014-11-10 10:55:23 +0000282 * piPlusReadall:
283 * Read all the pins on the model A+ or the B+
Gordon Hendersondca8a192014-07-14 08:39:38 +0100284 *********************************************************************************
285 */
286
Gordon Hendersoneb1fc2c2015-01-30 18:14:49 +0000287static void plus2header (int model)
288{
289 /**/ if (model == PI_MODEL_AP)
290 printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ;
291 else if (model == PI_MODEL_BP)
292 printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ;
Gordon Henderson31a8a2c2015-12-02 15:13:01 +0000293 else if (model == PI_MODEL_ZERO)
294 printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ;
Gordon Hendersoneb1fc2c2015-01-30 18:14:49 +0000295 else
296 printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
297}
298
299
Gordon Henderson0a9fdeb2014-11-10 10:55:23 +0000300void piPlusReadall (int model)
Gordon Hendersondca8a192014-07-14 08:39:38 +0100301{
Gordon Hendersondca8a192014-07-14 08:39:38 +0100302 int pin ;
Gordon Hendersondca8a192014-07-14 08:39:38 +0100303
Gordon Hendersoneb1fc2c2015-01-30 18:14:49 +0000304 plus2header (model) ;
Gordon Henderson0a9fdeb2014-11-10 10:55:23 +0000305
Gordon Hendersondf453882014-07-17 22:23:57 +0100306 printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
307 printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
Gordon Hendersondca8a192014-07-14 08:39:38 +0100308 for (pin = 1 ; pin <= 40 ; pin += 2)
309 readallPhys (pin) ;
Gordon Hendersondf453882014-07-17 22:23:57 +0100310 printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
311 printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
Gordon Henderson0a9fdeb2014-11-10 10:55:23 +0000312
Gordon Hendersoneb1fc2c2015-01-30 18:14:49 +0000313 plus2header (model) ;
Gordon Hendersondca8a192014-07-14 08:39:38 +0100314}
315
316
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100317void doReadall (void)
318{
Gordon Hendersondf453882014-07-17 22:23:57 +0100319 int model, rev, mem, maker, overVolted ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100320
321 if (wiringPiNodes != NULL) // External readall
322 {
323 doReadallExternal () ;
324 return ;
325 }
326
Gordon Hendersondf453882014-07-17 22:23:57 +0100327 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
Gordon Henderson6fba4032014-06-24 19:23:31 +0100328
Gordon Hendersondf453882014-07-17 22:23:57 +0100329 /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
330 abReadall (model, rev) ;
Gordon Henderson31a8a2c2015-12-02 15:13:01 +0000331 else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_ZERO))
Gordon Henderson0a9fdeb2014-11-10 10:55:23 +0000332 piPlusReadall (model) ;
Gordon Hendersondf453882014-07-17 22:23:57 +0100333 else if (model == PI_MODEL_CM)
334 cmReadall () ;
335 else
336 printf ("Oops - unable to determine board type... model: %d\n", model) ;
Gordon Henderson02a3bd82013-06-27 21:57:09 +0100337}