3 * The readall functions - getting a bit big, so split them out.
4 * Copyright (c) 2012-2013 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
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.
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.
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 ***********************************************************************
33 #include <sys/types.h>
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 *********************************************************************************
53 static void doReadallExternal (void)
57 printf ("+------+---------+--------+\n") ;
58 printf ("| Pin | Digital | Analog |\n") ;
59 printf ("+------+---------+--------+\n") ;
61 for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
62 printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
64 printf ("+------+---------+--------+\n") ;
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 *********************************************************************************
78 static char *pinNames [] =
80 "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
82 "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ",
84 "GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
87 static char *alts [] =
89 "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
92 static int wpiToPhys [64] =
94 11, 12, 13, 15, 16, 18, 22, 7, // 0...7
96 24, 26, 19, 21, 23, // 10..14
98 53, 54, 55, 56, // 17..20
99 0,0,0,0,0,0,0,0,0,0,0, // 20..31
100 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47
101 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63
104 // The other mappings needed are in wiringPi.c
106 static int physToWpi [64] =
122 30, 31, // Actually I2C, but not used
129 -1, -1, -1, -1, -1, -1, -1, // ... 47
130 -1, -1, -1, -1, -1, // ... 52
131 17, 18, 19, 20, // ... 53, 54, 55, 56 - P5
132 -1, -1, -1, -1, -1, -1, -1, // ... 63
135 static char *physNames [64] =
144 "GPIO. 0", "GPIO. 1",
146 "GPIO. 3", "GPIO. 4",
154 "GPIO.22", "GPIO.26",
156 "GPIO.24", "GPIO.27",
157 "GPIO.25", "GPIO.28",
164 "GPIO.17", "GPIO.18",
165 "GPIO.19", "GPIO.20",
166 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
169 static void readallPhys (int physPin)
173 /**/ if (wpMode == WPI_MODE_GPIO)
175 if (physPinToGpio (physPin) == -1)
178 printf (" | %3d", physPinToGpio (physPin)) ;
180 else if (wpMode != WPI_MODE_PHYS)
182 if (physToWpi [physPin] == -1)
185 printf (" | %3d", physToWpi [physPin]) ;
188 printf (" | %s", physNames [physPin]) ;
190 if (physToWpi [physPin] == -1)
194 /**/ if (wpMode == WPI_MODE_GPIO)
195 pin = physPinToGpio (physPin) ;
196 else if (wpMode == WPI_MODE_PHYS)
199 pin = physToWpi [physPin] ;
201 printf (" | %4s", alts [getAlt (pin)]) ;
202 printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ;
207 printf (" | %2d", physPin) ;
209 printf (" || %-2d", physPin) ;
213 if (physToWpi [physPin] == -1)
217 /**/ if (wpMode == WPI_MODE_GPIO)
218 pin = physPinToGpio (physPin) ;
219 else if (wpMode == WPI_MODE_PHYS)
222 pin = physToWpi [physPin] ;
224 printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ;
225 printf (" | %-4s", alts [getAlt (pin)]) ;
228 printf (" | %-5s", physNames [physPin]) ;
230 /**/ if (wpMode == WPI_MODE_GPIO)
232 if (physPinToGpio (physPin) == -1)
235 printf (" | %-3d", physPinToGpio (physPin)) ;
237 else if (wpMode != WPI_MODE_PHYS)
239 if (physToWpi [physPin] == -1)
242 printf (" | %-3d", physToWpi [physPin]) ;
251 int model, rev, mem ;
255 piBoardId (&model, &rev, &mem, &maker) ;
256 if (model != PI_MODEL_CM)
259 printf ("+-----+------+-------+ +-----+------+-------+\n") ;
260 printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ;
261 printf ("+-----+------+-------+ +-----+------+-------+\n") ;
263 for (pin = 0 ; pin < 28 ; ++pin)
265 printf ("| %3d ", pin) ;
266 printf ("| %-4s ", alts [getAlt (pin)]) ;
267 printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
269 printf ("| %3d ", pin + 28) ;
270 printf ("| %-4s ", alts [getAlt (pin + 28)]) ;
271 printf ("| %s ", digitalRead (pin + 28) == HIGH ? "High" : "Low ") ;
275 printf ("+-----+------+-------+ +-----+------+-------+\n") ;
283 * Read all the pins on the model B+
284 *********************************************************************************
287 int bPlusReadall (void)
289 int model, rev, mem ;
294 piBoardId (&model, &rev, &mem, &maker) ;
295 if (model != PI_MODEL_BPLUS)
298 /**/ if (wpMode == WPI_MODE_GPIO)
303 printf (" +-----+---------+------+----+--B Plus--+----+------+---------+-----+\n") ;
304 printf (" | %s | Name | Mode | Val| Physical |Val | Mode | Name | %s |\n", name, name) ;
305 printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ;
306 for (pin = 1 ; pin <= 40 ; pin += 2)
308 printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ;
314 void doReadall (void)
318 if (wiringPiNodes != NULL) // External readall
320 doReadallExternal () ;
330 /**/ if (wpMode == WPI_MODE_GPIO)
332 printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
333 printf (" | BCM | Name | Mode | Val| Physical |Val | Mode | Name | BCM |\n") ;
334 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
335 for (pin = 1 ; pin <= 26 ; pin += 2)
337 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
339 else if (wpMode == WPI_MODE_PHYS)
341 printf (" +-------+------+----+-Rev%d-----+----+------+-------+\n", piBoardRev ()) ;
342 printf (" | Name | Mode | Val| Physical |Val | Mode | Name |\n") ;
343 printf (" +-------+------+----+----++----+----+------+-------+\n") ;
344 for (pin = 1 ; pin <= 26 ; pin += 2)
346 printf (" +-------+------+----+----++----+----+------+-------+\n") ;
350 printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
351 printf (" | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi |\n") ;
352 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
353 for (pin = 1 ; pin <= 26 ; pin += 2)
355 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
360 void doReadallOld (void)
364 if (wiringPiNodes != NULL) // External readall
366 doReadallExternal () ;
376 printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
377 printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ;
378 printf ("+----------+------+------+--------+------+-------+\n") ;
380 for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
382 if (wpiPinToGpio (pin) == -1)
385 printf ("| %6d | %3d | %3d | %s | %-4s | %-4s |\n",
386 pin, wpiPinToGpio (pin), wpiToPhys [pin],
389 digitalRead (pin) == HIGH ? "High" : "Low ") ;
392 printf ("+----------+------+------+--------+------+-------+\n") ;