Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / gpio / readall.c @ 32:cadb9025f1e0

History | View | Annotate | Download (10.7 KB)

1
/*
2
 * readall.c:
3
 *        The readall functions - getting a bit big, so split them out.
4
 *        Copyright (c) 2012-2018 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

    
38
extern int wpMode ;
39

    
40
#ifndef TRUE
41
#  define       TRUE    (1==1)
42
#  define       FALSE   (1==2)
43
#endif
44

    
45
/*
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

    
53
static 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

    
78
static char *alts [] =
79
{
80
  "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
81
} ;
82

    
83
static 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
99
  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,
106
  -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
114
} ;
115

    
116
static char *physNames [64] = 
117
{
118
  NULL,
119

    
120
  "   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    ",
133
  "  SDA.0", "SCL.0  ",
134
  "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,
148
} ;
149

    
150

    
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

    
158
static void readallPhys (int physPin)
159
{
160
  int pin ;
161

    
162
  if (physPinToGpio (physPin) == -1)
163
    printf (" |     |    ") ;
164
  else
165
    printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi [physPin]) ;
166

    
167
  printf (" | %s", 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 (" | %d", digitalRead (pin)) ;
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 (" | %d", digitalRead (pin)) ;
204
    printf (" | %-4s", alts [getAlt (pin)]) ;
205
  }
206

    
207
  printf (" | %-5s", physNames [physPin]) ;
208

    
209
  if (physToWpi     [physPin] == -1)
210
    printf (" |     |    ") ;
211
  else
212
    printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio (physPin)) ;
213

    
214
  printf (" |\n") ;
215
}
216

    
217

    
218
/*
219
 * allReadall:
220
 *        Read all the pins regardless of the model. Primarily of use for
221
 *        the compute module, but handy for other fiddling...
222
 *********************************************************************************
223
 */
224

    
225
static void allReadall (void)
226
{
227
  int pin ;
228

    
229
  printf ("+-----+------+-------+      +-----+------+-------+\n") ;
230
  printf ("| Pin | Mode | Value |      | Pin | Mode | Value |\n") ;
231
  printf ("+-----+------+-------+      +-----+------+-------+\n") ;
232

    
233
  for (pin = 0 ; pin < 27 ; ++pin)
234
  {
235
    printf ("| %3d ", pin) ;
236
    printf ("| %-4s ", alts [getAlt (pin)]) ;
237
    printf ("| %s  ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
238
    printf ("|      ") ;
239
    printf ("| %3d ", pin + 27) ;
240
    printf ("| %-4s ", alts [getAlt (pin + 27)]) ;
241
    printf ("| %s  ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ;
242
    printf ("|\n") ;
243
  }
244

    
245
  printf ("+-----+------+-------+      +-----+------+-------+\n") ;
246

    
247
}
248

    
249

    
250
/*
251
 * abReadall:
252
 *        Read all the pins on the model A or B.
253
 *********************************************************************************
254
 */
255

    
256
void abReadall (int model, int rev)
257
{
258
  int pin ;
259
  char *type ;
260

    
261
  if (model == PI_MODEL_A)
262
    type = " A" ;
263
  else
264
    if (rev == PI_VERSION_2)
265
      type = "B2" ;
266
    else
267
      type = "B1" ;
268

    
269
  printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
270
  printf (" | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |\n") ;
271
  printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
272
  for (pin = 1 ; pin <= 26 ; pin += 2)
273
    readallPhys (pin) ;
274

    
275
  if (rev == PI_VERSION_2) // B version 2
276
  {
277
    printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
278
    for (pin = 51 ; pin <= 54 ; pin += 2)
279
      readallPhys (pin) ;
280
  }
281

    
282
  printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
283
  printf (" | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |\n") ;
284
  printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
285
}
286

    
287

    
288
/*
289
 * piPlusReadall:
290
 *        Read all the pins on the model A+ or the B+ or actually, all 40-pin Pi's
291
 *********************************************************************************
292
 */
293

    
294
static void plus2header (int model)
295
{
296
  /**/ if (model == PI_MODEL_AP)
297
    printf (" +-----+-----+---------+------+---+---Pi A+--+---+------+---------+-----+-----+\n") ;
298
  else if (model == PI_MODEL_BP)
299
    printf (" +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+\n") ;
300
  else if (model == PI_MODEL_ZERO)
301
    printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ;
302
  else if (model == PI_MODEL_ZERO_W)
303
    printf (" +-----+-----+---------+------+---+-Pi ZeroW-+---+------+---------+-----+-----+\n") ;
304
  else if (model == PI_MODEL_2)
305
    printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
306
  else if (model == PI_MODEL_3B)
307
    printf (" +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+\n") ;
308
  else if (model == PI_MODEL_3BP)
309
    printf (" +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+\n") ;
310
  else if (model == PI_MODEL_3AP)
311
    printf (" +-----+-----+---------+------+---+---Pi 3A+-+---+------+---------+-----+-----+\n") ;
312
  else
313
    printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ;
314
}
315

    
316

    
317
static void piPlusReadall (int model)
318
{
319
  int pin ;
320

    
321
  plus2header (model) ;
322

    
323
  printf (" | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |\n") ;
324
  printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
325
  for (pin = 1 ; pin <= 40 ; pin += 2)
326
    readallPhys (pin) ;
327
  printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
328
  printf (" | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |\n") ;
329

    
330
  plus2header (model) ;
331
}
332

    
333

    
334
/*
335
 * doReadall:
336
 *        Generic read all pins called from main program. Works out the Pi type
337
 *        and calls the appropriate function.
338
 *********************************************************************************
339
 */
340

    
341
void doReadall (void)
342
{
343
  int model, rev, mem, maker, overVolted ;
344

    
345
  if (wiringPiNodes != NULL)        // External readall
346
  {
347
    doReadallExternal () ;
348
    return ;
349
  }
350

    
351
  piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
352

    
353
  /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
354
    abReadall (model, rev) ;
355
  else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) ||
356
        (model == PI_MODEL_2)    ||
357
        (model == PI_MODEL_3AP)  ||
358
        (model == PI_MODEL_3B)   || (model == PI_MODEL_3BP) ||
359
        (model == PI_MODEL_ZERO) || (model == PI_MODEL_ZERO_W))
360
    piPlusReadall (model) ;
361
  else if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3) || ((model == PI_MODEL_CM3P)))
362
    allReadall () ;
363
  else
364
    printf ("Oops - unable to determine board type... model: %d\n", model) ;
365
}
366

    
367

    
368
/*
369
 * doAllReadall:
370
 *        Force reading of all pins regardless of Pi model
371
 *********************************************************************************
372
 */
373

    
374
void doAllReadall (void)
375
{
376
  allReadall () ;
377
}
378

    
379

    
380
/*
381
 * doQmode:
382
 *        Query mode on a pin
383
 *********************************************************************************
384
 */
385

    
386
void doQmode (int argc, char *argv [])
387
{
388
  int pin ;
389

    
390
  if (argc != 3)
391
  {
392
    fprintf (stderr, "Usage: %s qmode pin\n", argv [0]) ;
393
    exit (EXIT_FAILURE) ;
394
  }
395

    
396
  pin = atoi (argv [2]) ;
397
  printf ("%s\n", alts [getAlt (pin)]) ;
398
}