3 * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
5 * Copyright (c) 2012-2015 Gordon Henderson
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
10 * wiringPi is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * wiringPi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
34 #include <sys/types.h>
38 #include <wpiExtensions.h>
40 #include <gertboard.h>
44 extern int wiringPiDebug ;
46 // External functions I can't be bothered creating a separate .h file for:
48 extern void doReadall (void) ;
49 extern void doPins (void) ;
56 #define VERSION "2.23"
57 #define PI_USB_POWER_CONTROL 38
58 #define I2CDETECT "/usr/sbin/i2cdetect"
62 char *usage = "Usage: gpio -v\n"
64 " gpio [-g|-1] [-x extension:params] ...\n"
65 " gpio [-p] <read/write/wb> ...\n"
66 " gpio <read/write/aread/awritewb/pwm/clock/mode> ...\n"
67 " gpio readall/reset\n"
68 " gpio unexportall/exports\n"
69 " gpio export/edge/unexport ...\n"
70 " gpio wfi <pin> <mode>\n"
71 " gpio drive <group> <value>\n"
72 " gpio pwm-bal/pwm-ms \n"
73 " gpio pwmr <range> \n"
74 " gpio pwmc <divider> \n"
75 " gpio load spi/i2c\n"
76 " gpio unload spi/i2c\n"
77 " gpio i2cd/i2cdetect\n"
78 " gpio usbp high/low\n"
79 " gpio gbr <channel>\n"
80 " gpio gbw <channel> <value>" ; // No trailing newline needed here.
86 * Decode a pin "number" which can actually be a pin name to represent
87 * one of the Pi's on-board pins.
88 *********************************************************************************
91 static int decodePin (const char *str)
94 // The first case - see if it's a number:
96 if (isdigit (str [0]))
106 * Change the ownership of the file to the real userId of the calling
107 * program so we can access it.
108 *********************************************************************************
111 static void changeOwner (char *cmd, char *file)
113 uid_t uid = getuid () ;
114 uid_t gid = getgid () ;
116 if (chown (file, uid, gid) != 0)
118 if (errno == ENOENT) // Warn that it's not there
119 fprintf (stderr, "%s: Warning (not an error): File not present: %s\n", cmd, file) ;
121 fprintf (stderr, "%s: Warning (not an error): Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
128 * Return true/false if the supplied module is loaded
129 *********************************************************************************
132 static int moduleLoaded (char *modName)
134 int len = strlen (modName) ;
136 FILE *fd = fopen ("/proc/modules", "r") ;
141 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
145 while (fgets (line, 80, fd) != NULL)
147 if (strncmp (line, modName, len) != 0)
162 * Load either the spi or i2c modules and change device ownerships, etc.
163 *********************************************************************************
166 static void _doLoadUsage (char *argv [])
168 fprintf (stderr, "Usage: %s load <spi/i2c> [I2C baudrate in Kb/sec]\n", argv [0]) ;
172 static void doLoad (int argc, char *argv [])
174 char *module1, *module2 ;
176 char *file1, *file2 ;
177 char args1 [32], args2 [32] ;
180 _doLoadUsage (argv) ;
182 args1 [0] = args2 [0] = 0 ;
184 /**/ if (strcasecmp (argv [2], "spi") == 0)
187 module2 = "spi_bcm2708" ;
188 file1 = "/dev/spidev0.0" ;
189 file2 = "/dev/spidev0.1" ;
192 fprintf (stderr, "%s: Unable to set the buffer size now. Load aborted. Please see the man page.\n", argv [0]) ;
196 _doLoadUsage (argv) ;
198 else if (strcasecmp (argv [2], "i2c") == 0)
200 module1 = "i2c_dev" ;
201 module2 = "i2c_bcm2708" ;
202 file1 = "/dev/i2c-0" ;
203 file2 = "/dev/i2c-1" ;
205 sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
207 _doLoadUsage (argv) ;
210 _doLoadUsage (argv) ;
212 if (!moduleLoaded (module1))
214 sprintf (cmd, "/sbin/modprobe %s%s", module1, args1) ;
218 if (!moduleLoaded (module2))
220 sprintf (cmd, "/sbin/modprobe %s%s", module2, args2) ;
224 if (!moduleLoaded (module2))
226 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
230 sleep (1) ; // To let things get settled
232 changeOwner (argv [0], file1) ;
233 changeOwner (argv [0], file2) ;
239 * Un-Load either the spi or i2c modules and change device ownerships, etc.
240 *********************************************************************************
243 static void _doUnLoadUsage (char *argv [])
245 fprintf (stderr, "Usage: %s unload <spi/i2c>\n", argv [0]) ;
249 static void doUnLoad (int argc, char *argv [])
251 char *module1, *module2 ;
255 _doUnLoadUsage (argv) ;
257 /**/ if (strcasecmp (argv [2], "spi") == 0)
260 module2 = "spi_bcm2708" ;
262 else if (strcasecmp (argv [2], "i2c") == 0)
264 module1 = "i2c_dev" ;
265 module2 = "i2c_bcm2708" ;
268 _doUnLoadUsage (argv) ;
270 if (moduleLoaded (module1))
272 sprintf (cmd, "/sbin/rmmod %s", module1) ;
276 if (moduleLoaded (module2))
278 sprintf (cmd, "/sbin/rmmod %s", module2) ;
286 * Run the i2cdetect command with the right runes for this Pi revision
287 *********************************************************************************
290 static void doI2Cdetect (int argc, char *argv [])
292 int port = piBoardRev () == 1 ? 0 : 1 ;
294 struct stat statBuf ;
296 if (stat (I2CDETECT, &statBuf) < 0)
298 fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
302 if (!moduleLoaded ("i2c_dev"))
304 fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
308 sprintf (command, "%s -y %d", I2CDETECT, port) ;
309 if (system (command) < 0)
310 fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
317 * List all GPIO exports
318 *********************************************************************************
321 static void doExports (int argc, char *argv [])
328 for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
331 // Try to read the direction
333 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
334 if ((fd = open (fName, O_RDONLY)) == -1)
340 printf ("GPIO Pins exported:\n") ;
343 printf ("%4d: ", i) ;
345 if ((l = read (fd, buf, 16)) == 0)
346 sprintf (buf, "%s", "?") ;
349 if ((buf [strlen (buf) - 1]) == '\n')
350 buf [strlen (buf) - 1] = 0 ;
352 printf ("%-3s", buf) ;
356 // Try to Read the value
358 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
359 if ((fd = open (fName, O_RDONLY)) == -1)
361 printf ("No Value file (huh?)\n") ;
365 if ((l = read (fd, buf, 16)) == 0)
366 sprintf (buf, "%s", "?") ;
369 if ((buf [strlen (buf) - 1]) == '\n')
370 buf [strlen (buf) - 1] = 0 ;
372 printf (" %s", buf) ;
374 // Read any edge trigger file
376 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
377 if ((fd = open (fName, O_RDONLY)) == -1)
383 if ((l = read (fd, buf, 16)) == 0)
384 sprintf (buf, "%s", "?") ;
387 if ((buf [strlen (buf) - 1]) == '\n')
388 buf [strlen (buf) - 1] = 0 ;
390 printf (" %-8s\n", buf) ;
399 * gpio export pin mode
400 * This uses the /sys/class/gpio device interface.
401 *********************************************************************************
404 void doExport (int argc, char *argv [])
413 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
417 pin = atoi (argv [2]) ;
421 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
423 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
427 fprintf (fd, "%d\n", pin) ;
430 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
431 if ((fd = fopen (fName, "w")) == NULL)
433 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
437 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
438 fprintf (fd, "in\n") ;
439 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
440 fprintf (fd, "out\n") ;
443 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
449 // Change ownership so the current user can actually use it!
451 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
452 changeOwner (argv [0], fName) ;
454 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
455 changeOwner (argv [0], fName) ;
463 * Wait for Interrupt on a given pin.
464 * Slight cheat here - it's easier to actually use ISR now (which calls
465 * gpio to set the pin modes!) then we simply sleep, and expect the thread
466 * to exit the program. Crude but effective.
467 *********************************************************************************
470 static void wfi (void)
473 void doWfi (int argc, char *argv [])
479 fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
483 pin = atoi (argv [2]) ;
485 /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
486 else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
487 else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
490 fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
494 if (wiringPiISR (pin, mode, &wfi) < 0)
496 fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
509 * Easy access to changing the edge trigger on a GPIO pin
510 * This uses the /sys/class/gpio device interface.
511 *********************************************************************************
514 void doEdge (int argc, char *argv [])
523 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
527 pin = atoi (argv [2]) ;
530 // Export the pin and set direction to input
532 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
534 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
538 fprintf (fd, "%d\n", pin) ;
541 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
542 if ((fd = fopen (fName, "w")) == NULL)
544 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
548 fprintf (fd, "in\n") ;
551 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
552 if ((fd = fopen (fName, "w")) == NULL)
554 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
558 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
559 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
560 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
561 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
564 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
568 // Change ownership of the value and edge files, so the current user can actually use it!
570 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
571 changeOwner (argv [0], fName) ;
573 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
574 changeOwner (argv [0], fName) ;
583 * This uses the /sys/class/gpio device interface.
584 *********************************************************************************
587 void doUnexport (int argc, char *argv [])
594 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
598 pin = atoi (argv [2]) ;
600 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
602 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
606 fprintf (fd, "%d\n", pin) ;
614 * Un-Export all the GPIO pins.
615 * This uses the /sys/class/gpio device interface.
616 *********************************************************************************
619 void doUnexportall (char *progName)
624 for (pin = 0 ; pin < 63 ; ++pin)
626 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
628 fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
631 fprintf (fd, "%d\n", pin) ;
639 * Reset the GPIO pins - as much as we can do
640 *********************************************************************************
643 static void doReset (char *progName)
645 printf ("GPIO Reset is dangerous and has been removed from the gpio command.\n") ;
646 printf (" - Please write a shell-script to reset the GPIO pins into the state\n") ;
647 printf (" that you need them in for your applications.\n") ;
653 * gpio mode pin mode ...
654 *********************************************************************************
657 void doMode (int argc, char *argv [])
664 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
668 pin = atoi (argv [2]) ;
672 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
673 else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
674 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
675 else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
676 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
677 else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ;
678 else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
679 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
680 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
681 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
682 else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
683 else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
684 else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
685 else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
686 else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
687 else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
688 else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
691 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
699 * gpio drive group value
700 *********************************************************************************
703 static void doPadDrive (int argc, char *argv [])
709 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
713 group = atoi (argv [2]) ;
714 val = atoi (argv [3]) ;
716 if ((group < 0) || (group > 2))
718 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
722 if ((val < 0) || (val > 7))
724 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
728 setPadDrive (group, val) ;
734 * Control USB Power - High (1.2A) or Low (600mA)
736 *********************************************************************************
739 static void doUsbP (int argc, char *argv [])
741 int model, rev, mem, maker, overVolted ;
745 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
749 // Make sure we're on a B+
751 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
753 if (model != PI_MODEL_BP)
755 fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ;
759 // Need to force BCM_GPIO mode:
761 wiringPiSetupGpio () ;
763 if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0))
765 digitalWrite (PI_USB_POWER_CONTROL, 1) ;
766 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
767 printf ("Switched to HIGH current USB (1.2A)\n") ;
771 if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0))
773 digitalWrite (PI_USB_POWER_CONTROL, 0) ;
774 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
775 printf ("Switched to LOW current USB (600mA)\n") ;
779 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
786 * gpio gbw channel value
787 * Gertboard Write - To the Analog output
788 *********************************************************************************
791 static void doGbw (int argc, char *argv [])
797 fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
801 channel = atoi (argv [2]) ;
802 value = atoi (argv [3]) ;
804 if ((channel < 0) || (channel > 1))
806 fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
810 if ((value < 0) || (value > 255))
812 fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
816 if (gertboardAnalogSetup (64) < 0)
818 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
822 analogWrite (64 + channel, value) ;
829 * From the analog input
830 *********************************************************************************
833 static void doGbr (int argc, char *argv [])
839 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
843 channel = atoi (argv [2]) ;
845 if ((channel < 0) || (channel > 1))
847 fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
851 if (gertboardAnalogSetup (64) < 0)
853 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
857 printf ("%d\n", analogRead (64 + channel)) ;
863 * gpio write pin value
864 *********************************************************************************
867 static void doWrite (int argc, char *argv [])
873 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
877 pin = atoi (argv [2]) ;
879 /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
881 else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
884 val = atoi (argv [3]) ;
887 digitalWrite (pin, LOW) ;
889 digitalWrite (pin, HIGH) ;
895 * gpio awrite pin value
896 *********************************************************************************
899 static void doAwrite (int argc, char *argv [])
905 fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
909 pin = atoi (argv [2]) ;
911 val = atoi (argv [3]) ;
913 analogWrite (pin, val) ;
920 *********************************************************************************
923 static void doWriteByte (int argc, char *argv [])
929 fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
933 val = (int)strtol (argv [2], NULL, 0) ;
935 digitalWriteByte (val) ;
941 * Read a pin and return the value
942 *********************************************************************************
945 void doRead (int argc, char *argv [])
951 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
955 pin = atoi (argv [2]) ;
956 val = digitalRead (pin) ;
958 printf ("%s\n", val == 0 ? "0" : "1") ;
964 * Read an analog pin and return the value
965 *********************************************************************************
968 void doAread (int argc, char *argv [])
972 fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
976 printf ("%d\n", analogRead (atoi (argv [2]))) ;
983 *********************************************************************************
986 void doToggle (int argc, char *argv [])
992 fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
996 pin = atoi (argv [2]) ;
998 digitalWrite (pin, !digitalRead (pin)) ;
1004 * Output a tone in a PWM pin
1005 *********************************************************************************
1008 void doPwmTone (int argc, char *argv [])
1014 fprintf (stderr, "Usage: %s pwmTone <pin> <freq>\n", argv [0]) ;
1018 pin = atoi (argv [2]) ;
1019 freq = atoi (argv [3]) ;
1021 pwmToneWrite (pin, freq) ;
1027 * Output a clock on a pin
1028 *********************************************************************************
1031 void doClock (int argc, char *argv [])
1037 fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
1041 pin = atoi (argv [2]) ;
1043 freq = atoi (argv [3]) ;
1045 gpioClockSet (pin, freq) ;
1051 * Output a PWM value on a pin
1052 *********************************************************************************
1055 void doPwm (int argc, char *argv [])
1061 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
1065 pin = atoi (argv [2]) ;
1067 val = atoi (argv [3]) ;
1069 pwmWrite (pin, val) ;
1074 * doPwmMode: doPwmRange: doPwmClock:
1075 * Change the PWM mode, range and clock divider values
1076 *********************************************************************************
1079 static void doPwmMode (int mode)
1084 static void doPwmRange (int argc, char *argv [])
1086 unsigned int range ;
1090 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
1094 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
1098 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
1102 pwmSetRange (range) ;
1105 static void doPwmClock (int argc, char *argv [])
1107 unsigned int clock ;
1111 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
1115 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
1117 if ((clock < 1) || (clock > 4095))
1119 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
1123 pwmSetClock (clock) ;
1130 *********************************************************************************
1133 int main (int argc, char *argv [])
1136 int model, rev, mem, maker, overVolted ;
1138 if (getenv ("WIRINGPI_DEBUG") != NULL)
1140 printf ("gpio: wiringPi debug mode enabled\n") ;
1141 wiringPiDebug = TRUE ;
1146 fprintf (stderr, "%s\n", usage) ;
1152 if (strcasecmp (argv [1], "-h") == 0)
1154 printf ("%s: %s\n", argv [0], usage) ;
1158 // Sort of a special:
1160 if (strcmp (argv [1], "-R") == 0)
1162 printf ("%d\n", piBoardRev ()) ;
1166 // Version & Warranty
1168 if (strcmp (argv [1], "-V") == 0)
1170 printf ("%d\n", piBoardRev ()) ;
1174 if (strcmp (argv [1], "-v") == 0)
1176 printf ("gpio version: %s\n", VERSION) ;
1177 printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
1178 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
1179 printf ("For details type: %s -warranty\n", argv [0]) ;
1181 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
1182 if (model == PI_MODEL_UNKNOWN)
1184 printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ;
1185 printf (" projects@drogon.net\n") ;
1186 printf ("with a copy of your /proc/cpuinfo if possible\n") ;
1190 printf ("Raspberry Pi Details:\n") ;
1191 printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
1192 piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
1197 if (strcasecmp (argv [1], "-warranty") == 0)
1199 printf ("gpio version: %s\n", VERSION) ;
1200 printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
1202 printf (" This program is free software; you can redistribute it and/or modify\n") ;
1203 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
1204 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
1205 printf (" (at your option) any later version.\n") ;
1207 printf (" This program is distributed in the hope that it will be useful,\n") ;
1208 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
1209 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
1210 printf (" GNU Lesser General Public License for more details.\n") ;
1212 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
1213 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
1218 if (geteuid () != 0)
1220 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
1224 // Initial test for /sys/class/gpio operations:
1226 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
1227 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
1228 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
1229 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
1230 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
1232 // Check for load command:
1234 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
1235 if (strcasecmp (argv [1], "unload" ) == 0) { doUnLoad (argc, argv) ; return 0 ; }
1237 // Gertboard commands
1239 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
1240 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
1242 // Check for -g argument
1244 /**/ if (strcasecmp (argv [1], "-g") == 0)
1246 wiringPiSetupGpio () ;
1248 for (i = 2 ; i < argc ; ++i)
1249 argv [i - 1] = argv [i] ;
1251 wpMode = WPI_MODE_GPIO ;
1254 // Check for -1 argument
1256 else if (strcasecmp (argv [1], "-1") == 0)
1258 wiringPiSetupPhys () ;
1260 for (i = 2 ; i < argc ; ++i)
1261 argv [i - 1] = argv [i] ;
1263 wpMode = WPI_MODE_PHYS ;
1266 // Check for -p argument for PiFace
1268 else if (strcasecmp (argv [1], "-p") == 0)
1272 for (i = 2 ; i < argc ; ++i)
1273 argv [i - 1] = argv [i] ;
1275 wpMode = WPI_MODE_PIFACE ;
1278 // Default to wiringPi mode
1283 wpMode = WPI_MODE_PINS ;
1286 // Check for -x argument to load in a new extension
1288 if (strcasecmp (argv [1], "-x") == 0)
1292 fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
1293 exit (EXIT_FAILURE) ;
1296 if (!loadWPiExtension (argv [0], argv [2], TRUE)) // Prints its own error messages
1297 exit (EXIT_FAILURE) ;
1299 for (i = 3 ; i < argc ; ++i)
1300 argv [i - 2] = argv [i] ;
1306 fprintf (stderr, "%s: no command given\n", argv [0]) ;
1307 exit (EXIT_FAILURE) ;
1310 // Core wiringPi functions
1312 /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
1313 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
1314 else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
1315 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
1316 else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
1317 else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
1321 else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
1325 else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
1326 else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
1327 else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
1328 else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
1329 else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
1330 else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
1331 else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ;
1332 else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
1333 else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
1334 else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ;
1335 else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
1336 else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
1337 else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
1338 else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
1339 else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
1340 else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
1343 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
1344 exit (EXIT_FAILURE) ;