-
Notifications
You must be signed in to change notification settings - Fork 8
/
readall.c
393 lines (331 loc) · 11.1 KB
/
readall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* readall.c:
* The readall functions - getting a bit big, so split them out.
* Copyright (c) 2012-2017 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <wiringPi.h>
extern int wpMode ;
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
/*
* doReadallExternal:
* A relatively crude way to read the pins on an external device.
* We don't know the input/output mode of pins, but we can tell
* if it's an analog pin or a digital one...
*********************************************************************************
*/
static void doReadallExternal (void)
{
int pin ;
printf ("+------+---------+--------+\n") ;
printf ("| Pin | Digital | Analog |\n") ;
printf ("+------+---------+--------+\n") ;
for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
printf ("+------+---------+--------+\n") ;
}
/*
* doReadall:
* Read all the GPIO pins
* We also want to use this to read the state of pins on an externally
* connected device, so we need to do some fiddling with the internal
* wiringPi node structures - since the gpio command can only use
* one external device at a time, we'll use that to our advantage...
*********************************************************************************
*/
static char *alts [] =
{
"IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
} ;
static int physToWpi [64] =
{
-1, // 0
-1, -1, // 1, 2
8, -1,
9, -1,
7, 15,
-1, 16,
0, 1,
2, -1,
3, 4,
-1, 5,
12, -1,
13, 6,
14, 10,
-1, 11, // 25, 26
30, 31, // Actually I2C, but not used
21, -1,
22, 26,
23, -1,
24, 27,
25, 28,
-1, 29,
-1, -1,
-1, -1,
-1, -1,
-1, -1,
-1, -1,
17, 18,
19, 20,
-1, -1, -1, -1, -1, -1, -1, -1, -1
} ;
static char *physNames [64] =
{
NULL,
" 3.3v", "5v ",
" SDA.1", "5v ",
" SCL.1", "0v ",
"GPIO. 7", "TxD ",
" 0v", "RxD ",
"GPIO. 0", "GPIO. 1",
"GPIO. 2", "0v ",
"GPIO. 3", "GPIO. 4",
" 3.3v", "GPIO. 5",
" MOSI", "0v ",
" MISO", "GPIO. 6",
" SCLK", "CE0 ",
" 0v", "CE1 ",
" SDA.0", "SCL.0 ",
"GPIO.21", "0v ",
"GPIO.22", "GPIO.26",
"GPIO.23", "0v ",
"GPIO.24", "GPIO.27",
"GPIO.25", "GPIO.28",
" 0v", "GPIO.29",
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
"GPIO.17", "GPIO.18",
"GPIO.19", "GPIO.20",
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
} ;
/*
* physPinToGpio4M2Z:
* Translate a physical Pin number to native GPIO pin number.
* Banana Pi M2 Zero version, used for visualise in 'gpio readall' only
*********************************************************************************
*/
extern int physToGpio_BPI_M2Z[64];
static int physPinToGpio4M2Z (int physPin)
{
return physToGpio_BPI_M2Z [physPin & 63] ;
}
/*
* readallPhys:
* Given a physical pin output the data on it and the next pin:
*| BCM | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi | BCM |
*********************************************************************************
*/
static void readallPhys (int physPin, int model, int bpi)
{
int pin ;
if (physPinToGpio (physPin) == -1)
printf (" | | ") ;
else if ( ( model == BPI_MODEL_M2Z ) & ( bpi == 0))
printf (" | %3d | %3d", physPinToGpio4M2Z (physPin), physToWpi [physPin]) ;
else
printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi [physPin]) ;
printf (" | %s", physNames [physPin]) ;
if (physToWpi [physPin] == -1)
printf (" | | ") ;
else
{
/**/ if (wpMode == WPI_MODE_GPIO)
pin = physPinToGpio (physPin) ;
else if (wpMode == WPI_MODE_PHYS)
pin = physPin ;
else
pin = physToWpi [physPin] ;
printf (" | %4s", alts [getAlt (pin)]) ;
printf (" | %d", digitalRead (pin)) ;
}
// Pin numbers:
printf (" | %2d", physPin) ;
++physPin ;
printf (" || %-2d", physPin) ;
// Same, reversed
if (physToWpi [physPin] == -1)
printf (" | | ") ;
else
{
/**/ if (wpMode == WPI_MODE_GPIO)
pin = physPinToGpio (physPin) ;
else if (wpMode == WPI_MODE_PHYS)
pin = physPin ;
else
pin = physToWpi [physPin] ;
printf (" | %d", digitalRead (pin)) ;
printf (" | %-4s", alts [getAlt (pin)]) ;
}
printf (" | %-5s", physNames [physPin]) ;
if (physToWpi [physPin] == -1)
printf (" | | ") ;
else if ( ( model == BPI_MODEL_M2Z ) & ( bpi == 0))
printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio4M2Z (physPin)) ;
else
printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio (physPin)) ;
printf (" |\n") ;
}
/*
* allReadall:
* Read all the pins regardless of the model. Primarily of use for
* the compute module, but handy for other fiddling...
*********************************************************************************
*/
static void allReadall (void)
{
int pin ;
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ;
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
for (pin = 0 ; pin < 27 ; ++pin)
{
printf ("| %3d ", pin) ;
printf ("| %-4s ", alts [getAlt (pin)]) ;
printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
printf ("| ") ;
printf ("| %3d ", pin + 27) ;
printf ("| %-4s ", alts [getAlt (pin + 27)]) ;
printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ;
printf ("|\n") ;
}
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
}
/*
* abReadall:
* Read all the pins on the model A or B.
*********************************************************************************
*/
void abReadall (int model, int rev)
{
int pin ;
char *type ;
if (model == PI_MODEL_A)
type = " A" ;
else
if (rev == PI_VERSION_2)
type = "B2" ;
else
type = "B1" ;
printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
for (pin = 1 ; pin <= 26 ; pin += 2)
readallPhys (pin,model,0) ;
if (rev == PI_VERSION_2) // B version 2
{
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
for (pin = 51 ; pin <= 54 ; pin += 2)
readallPhys (pin,model,0) ;
}
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
}
/*
* piPlusReadall:
* Read all the pins on the model A+ or the B+
*********************************************************************************
*/
static void plus2header (int model)
{
/**/ if (model == PI_MODEL_AP)
printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ;
else if (model == PI_MODEL_BP)
printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ;
else if (model == PI_MODEL_ZERO)
printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ;
else if (model == PI_MODEL_ZERO_W)
printf (" +-----+-----+---------+------+---+-Pi ZeroW-+---+------+---------+-----+-----+\n") ;
else if (model == PI_MODEL_2)
printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
else if (model == PI_MODEL_3)
printf (" +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+\n") ;
else if (model >= BPI_MODEL_MIN)
printf (" +-----+-----+---------+------+---+--Pi-M2Z--+---+------+---------+-----+-----+\n") ;
//printf (" +-----+-----+---------+%-15s+------+---------+-----+-----+\n",piModelNames[model]) ;
else
printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ;
}
static void piPlusReadall (int model, int bpi)
{
int pin ;
plus2header (model) ;
if (bpi) {
printf (" | BPI | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BPI |\n") ; }
else {
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; }
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
for (pin = 1 ; pin <= 40 ; pin += 2)
readallPhys (pin,model,bpi) ;
printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
if (bpi) {
printf (" | BPI | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BPI |\n") ; }
else {
printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; }
plus2header (model) ;
}
/*
* doReadall:
* Generic read all pins called from main program. Works out the Pi type
* and calls the appropriate function.
*********************************************************************************
*/
void doReadall (int bpi)
{
int model, rev, mem, maker, overVolted ;
if (wiringPiNodes != NULL) // External readall
{
doReadallExternal () ;
return ;
}
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
/**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
abReadall (model, rev) ;
else if ((model >= BPI_MODEL_MIN) || (model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_3) || (model == PI_MODEL_ZERO) || (model == PI_MODEL_ZERO_W))
piPlusReadall (model,bpi) ;
else if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3))
allReadall () ;
else
printf ("Oops - unable to determine board type... model: %d\n", model) ;
}
/*
* doAllReadall:
* Force reading of all pins regardless of Pi model
*********************************************************************************
*/
void doAllReadall (void)
{
allReadall () ;
}