chiark / gitweb /
Updated mostly to the gpio readall command to support the Raspberry Pi B+
[wiringPi.git] / gpio / readall.c
1 /*
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
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 *pinNames [] =
79 {
80   "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
81   "SDA   ", "SCL   ",
82   "CE0   ", "CE1   ", "MOSI  ", "MISO  ", "SCLK  ",
83   "TxD   ", "RxD   ",
84   "GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
85 } ;
86
87 static char *alts [] =
88 {
89   "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
90 } ;
91
92 static int wpiToPhys [64] =
93 {
94   11, 12, 13, 15, 16, 18, 22,  7,       //  0...7
95    3,  5,                               //  8...9
96   24, 26, 19, 21, 23,                   // 10..14
97    8, 10,                               // 15..16
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
102 } ;
103
104 // The other mappings needed are in wiringPi.c
105
106 static int physToWpi [64] = 
107 {
108   -1,           // 0
109   -1, -1,       // 1, 2
110    8, -1,
111    9, -1,
112    7, 15,
113   -1, 16,
114    0,  1,
115    2, -1,
116    3,  4,
117   -1,  5,
118   12, -1,
119   13,  6,
120   14, 10,
121   -1, 11,       // 25, 26
122   30, 31,       // Actually I2C, but not used
123   21, -1,
124   22, 26,
125   23, -1,
126   24, 27,
127   25, 28,
128   -1, 29,
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
133 } ;
134
135 static char *physNames [64] = 
136 {
137   NULL,
138
139   "   3.3v", "5v     ",
140   "  SDA.1", "5V     ",
141   "  SCL.1", "0v     ",
142   "GPIO. 7", "TxD    ",
143   "     0v", "RxD    ",
144   "GPIO. 0", "GPIO. 1",
145   "GPIO. 2", "0v     ",
146   "GPIO. 3", "GPIO. 4",
147   "   3.3v", "GPIO. 5",
148   "   MOSI", "0v     ",
149   "   MISO", "GPIO. 6",
150   "   SCLK", "CE0    ",
151   "     0v", "CE1    ",
152   "  SDA.0", "SCL0   ",
153   "GPIO.21", "0v     ",
154   "GPIO.22", "GPIO.26",
155   "GPIO.23", "0v     ",
156   "GPIO.24", "GPIO.27",
157   "GPIO.25", "GPIO.28",
158   "     0v", "GPIO.29",
159        NULL, NULL,
160        NULL, NULL,
161        NULL, NULL,
162        NULL, NULL,
163        NULL, NULL,
164   "GPIO.17", "GPIO.18",
165   "GPIO.19", "GPIO.20",
166    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
167 } ;
168
169 static void readallPhys (int physPin)
170 {
171   int pin ;
172
173   /**/ if (wpMode == WPI_MODE_GPIO)
174   {
175     if (physPinToGpio (physPin) == -1)
176       printf (" |    ") ;
177     else
178       printf (" | %3d", physPinToGpio (physPin)) ;
179   }
180   else if (wpMode != WPI_MODE_PHYS)
181   {
182     if (physToWpi     [physPin] == -1)
183       printf (" |    ") ;
184     else
185       printf (" | %3d", physToWpi     [physPin]) ;
186   }
187
188   printf (" | %s", physNames [physPin]) ;
189
190   if (physToWpi [physPin] == -1)
191     printf (" |      |   ") ;
192   else
193   {
194     /**/ if (wpMode == WPI_MODE_GPIO)
195       pin = physPinToGpio (physPin) ;
196     else if (wpMode == WPI_MODE_PHYS)
197       pin = physPin ;
198     else
199       pin = physToWpi [physPin] ;
200
201     printf (" | %4s", alts [getAlt (pin)]) ;
202     printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ;
203   }
204
205 // Pin numbers:
206
207   printf (" | %2d", physPin) ;
208   ++physPin ;
209   printf (" || %-2d", physPin) ;
210
211 // Same, reversed
212
213   if (physToWpi [physPin] == -1)
214     printf (" |    |     ") ;
215   else
216   {
217     /**/ if (wpMode == WPI_MODE_GPIO)
218       pin = physPinToGpio (physPin) ;
219     else if (wpMode == WPI_MODE_PHYS)
220       pin = physPin ;
221     else
222       pin = physToWpi [physPin] ;
223
224     printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ;
225     printf (" | %-4s", alts [getAlt (pin)]) ;
226   }
227
228   printf (" | %-5s", physNames [physPin]) ;
229
230   /**/ if (wpMode == WPI_MODE_GPIO)
231   {
232     if (physPinToGpio (physPin) == -1)
233       printf (" |    ") ;
234     else
235       printf (" | %-3d", physPinToGpio (physPin)) ;
236   }
237   else if (wpMode != WPI_MODE_PHYS)
238   {
239     if (physToWpi     [physPin] == -1)
240       printf (" |    ") ;
241     else
242       printf (" | %-3d", physToWpi     [physPin]) ;
243   }
244
245   printf (" |\n") ;
246 }
247
248
249 int cmReadall (void)
250 {
251   int model, rev, mem ;
252   int pin ;
253   char *maker ;
254
255   piBoardId (&model, &rev, &mem, &maker) ;
256   if (model != PI_MODEL_CM)
257     return FALSE ;
258
259   printf ("+-----+------+-------+      +-----+------+-------+\n") ;
260   printf ("| Pin | Mode | Value |      | Pin | Mode | Value |\n") ;
261   printf ("+-----+------+-------+      +-----+------+-------+\n") ;
262
263   for (pin = 0 ; pin < 28 ; ++pin)
264   {
265     printf ("| %3d ", pin) ;
266     printf ("| %-4s ", alts [getAlt (pin)]) ;
267     printf ("| %s  ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
268     printf ("|      ") ;
269     printf ("| %3d ", pin + 28) ;
270     printf ("| %-4s ", alts [getAlt (pin + 28)]) ;
271     printf ("| %s  ", digitalRead (pin + 28) == HIGH ? "High" : "Low ") ;
272     printf ("|\n") ;
273   }
274
275   printf ("+-----+------+-------+      +-----+------+-------+\n") ;
276
277   return TRUE ;
278 }
279
280
281 /*
282  * bPlusReadall:
283  *      Read all the pins on the model B+
284  *********************************************************************************
285  */
286
287 int bPlusReadall (void)
288 {
289   int model, rev, mem ;
290   int pin ;
291   char *maker ;
292   char *name ;
293
294   piBoardId (&model, &rev, &mem, &maker) ;
295   if (model != PI_MODEL_BPLUS)
296     return FALSE ;
297
298   /**/ if (wpMode == WPI_MODE_GPIO)
299     name = "BCM" ;
300   else
301     name = "wPi" ;
302
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)
307     readallPhys (pin) ;
308   printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ;
309
310   return TRUE ;
311 }
312
313
314 void doReadall (void)
315 {
316   int pin ;
317
318   if (wiringPiNodes != NULL)    // External readall
319   {
320     doReadallExternal () ;
321     return ;
322   }
323
324   if (cmReadall ())
325     return ;
326
327   if (bPlusReadall ())
328     return ;
329
330   /**/ if (wpMode == WPI_MODE_GPIO)
331   {
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)
336       readallPhys (pin) ;
337     printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
338   }
339   else if (wpMode == WPI_MODE_PHYS)
340   {
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)
345       readallPhys (pin) ;
346     printf (" +-------+------+----+----++----+----+------+-------+\n") ;
347   }
348   else  // wiringPi
349   {
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)
354       readallPhys (pin) ;
355     printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
356   }
357 }
358
359
360 void doReadallOld (void)
361 {
362   int pin ;
363
364   if (wiringPiNodes != NULL)    // External readall
365   {
366     doReadallExternal () ;
367     return ;
368   }
369
370   if (cmReadall ())
371     return ;
372
373   if (bPlusReadall ())
374     return ;
375
376   printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
377   printf ("| wiringPi | GPIO | Phys | Name   | Mode | Value |\n") ;
378   printf ("+----------+------+------+--------+------+-------+\n") ;
379
380   for (pin = 0 ; pin < 64 ; ++pin)      // Crude, but effective
381   {
382     if (wpiPinToGpio (pin) == -1)
383       continue ;
384
385     printf ("| %6d   | %3d  | %3d  | %s | %-4s | %-4s  |\n",
386         pin, wpiPinToGpio (pin), wpiToPhys [pin],
387         pinNames [pin], 
388         alts [getAlt (pin)], 
389         digitalRead (pin) == HIGH ? "High" : "Low ") ;
390   }
391
392   printf ("+----------+------+------+--------+------+-------+\n") ;
393 }