blob: 0237110c317847c8b1cd119af72ccb89f64efda6 [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.
4 * Copyright (c) 2012-2013 Gordon Henderson
5 ***********************************************************************
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
40/*
41 * doReadallExternal:
42 * A relatively crude way to read the pins on an external device.
43 * We don't know the input/output mode of pins, but we can tell
44 * if it's an analog pin or a digital one...
45 *********************************************************************************
46 */
47
48static void doReadallExternal (void)
49{
50 int pin ;
51
52 printf ("+------+---------+--------+\n") ;
53 printf ("| Pin | Digital | Analog |\n") ;
54 printf ("+------+---------+--------+\n") ;
55
56 for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
57 printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
58
59 printf ("+------+---------+--------+\n") ;
60}
61
62
63/*
64 * doReadall:
65 * Read all the GPIO pins
66 * We also want to use this to read the state of pins on an externally
67 * connected device, so we need to do some fiddling with the internal
68 * wiringPi node structures - since the gpio command can only use
69 * one external device at a time, we'll use that to our advantage...
70 *********************************************************************************
71 */
72
73static char *pinNames [] =
74{
75 "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
76 "SDA ", "SCL ",
77 "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ",
78 "TxD ", "RxD ",
79 "GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
80} ;
81
82static char *alts [] =
83{
84 "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
85} ;
86
87static int wpiToPhys [64] =
88{
89 11, 12, 13, 15, 16, 18, 22, 7, // 0...7
90 3, 5, // 8...9
91 24, 26, 19, 21, 23, // 10..14
92 8, 10, // 15..16
93 3, 4, 5, 6, // 17..20
94 0,0,0,0,0,0,0,0,0,0,0, // 20..31
95 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47
96 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63
97} ;
98
99// The other mappings needed are in wiringPi.c
100
101static int physToWpi [64] =
102{
103 -1, // 0
104 -1, -1, // 1, 2
105 8, -1,
106 9, -1,
107 7, 15,
108 -1, 16,
109 0, 1,
110 2, -1,
111 3, 4,
112 -1, 5,
113 12, -1,
114 13, 6,
115 14, 10,
116 -1, 11, // 25, 26
117
118// Padding:
119
120 -1, -1, -1, -1, -1, // ... 31
121 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
122 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
123} ;
124
125static char *physNames [64] =
126{
127 NULL,
128
129 "3.3v", "5v",
130 "SDA", "5V",
131 "SCL", "0v",
132 "GPIO7", "TxD",
133 "0v", "RxD",
134 "GPIO0", "GPIO1",
135 "GPIO2", "0v",
136 "GPIO3", "GPIO4",
137 "3.3v", "GPIO5",
138 "MOSI", "0v",
139 "MISO", "GPIO6",
140 "SCLK", "CE1",
141 "0v", "CE1",
142
143 NULL,NULL,NULL,NULL,NULL,
144 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
145 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
146} ;
147
148static void readallPhys (int physPin)
149{
150 int pin ;
151
152 /**/ if (wpMode == WPI_MODE_GPIO)
153 {
154 if (physPinToGpio (physPin) == -1)
155 printf (" | ") ;
156 else
157 printf (" | %3d", physPinToGpio (physPin)) ;
158 }
159 else if (wpMode != WPI_MODE_PHYS)
160 {
161 if (physToWpi [physPin] == -1)
162 printf (" | ") ;
163 else
164 printf (" | %3d", physToWpi [physPin]) ;
165 }
166
167 printf (" | %5s", physNames [physPin]) ;
168
169 if (physToWpi [physPin] == -1)
170 printf (" | | ") ;
171 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)]) ;
181 printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ;
182 }
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)
193 printf (" | | ") ;
194 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
203 printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ;
204 printf (" | %-4s", alts [getAlt (pin)]) ;
205 }
206
207 printf (" | %-5s", physNames [physPin]) ;
208
209 /**/ if (wpMode == WPI_MODE_GPIO)
210 {
211 if (physPinToGpio (physPin) == -1)
212 printf (" | ") ;
213 else
214 printf (" | %-3d", physPinToGpio (physPin)) ;
215 }
216 else if (wpMode != WPI_MODE_PHYS)
217 {
218 if (physToWpi [physPin] == -1)
219 printf (" | ") ;
220 else
221 printf (" | %-3d", physToWpi [physPin]) ;
222 }
223
224 printf (" |\n") ;
225}
226
227
228void doReadall (void)
229{
230 int pin ;
231
232 if (wiringPiNodes != NULL) // External readall
233 {
234 doReadallExternal () ;
235 return ;
236 }
237
238 /**/ if (wpMode == WPI_MODE_GPIO)
239 {
240 printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
241 printf (" | BCM | Name | Mode | Val| Physical |Val | Mode | Name | BCM |\n") ;
242 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
243 for (pin = 1 ; pin <= 26 ; pin += 2)
244 readallPhys (pin) ;
245 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
246 }
247 else if (wpMode == WPI_MODE_PHYS)
248 {
249 printf (" +-------+------+----+-Rev%d-----+----+------+-------+\n", piBoardRev ()) ;
250 printf (" | Name | Mode | Val| Physical |Val | Mode | Name |\n") ;
251 printf (" +-------+------+----+----++----+----+------+-------+\n") ;
252 for (pin = 1 ; pin <= 26 ; pin += 2)
253 readallPhys (pin) ;
254 printf (" +-------+------+----+----++----+----+------+-------+\n") ;
255 }
256 else // wiringPi
257 {
258 printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
259 printf (" | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi |\n") ;
260 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
261 for (pin = 1 ; pin <= 26 ; pin += 2)
262 readallPhys (pin) ;
263 printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
264 }
265}
266
267
268void doReadallOld (void)
269{
270 int pin ;
271
272 if (wiringPiNodes != NULL) // External readall
273 {
274 doReadallExternal () ;
275 return ;
276 }
277
278 printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
279 printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ;
280 printf ("+----------+------+------+--------+------+-------+\n") ;
281
282 for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
283 {
284 if (wpiPinToGpio (pin) == -1)
285 continue ;
286
287 printf ("| %6d | %3d | %3d | %s | %-4s | %-4s |\n",
288 pin, wpiPinToGpio (pin), wpiToPhys [pin],
289 pinNames [pin],
290 alts [getAlt (pin)],
291 digitalRead (pin) == HIGH ? "High" : "Low ") ;
292 }
293
294 printf ("+----------+------+------+--------+------+-------+\n") ;
295}