3 * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
5 * Copyright (c) 2012-2014 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>
39 #include <gertboard.h>
42 #include "extensions.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.22"
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 i2cd/i2cdetect\n"
77 " gpio usbp high/low\n"
78 " gpio gbr <channel>\n"
79 " gpio gbw <channel> <value>" ; // No trailing newline needed here.
85 * Decode a pin "number" which can actually be a pin name to represent
86 * one of the Pi's on-board pins.
87 *********************************************************************************
90 static int decodePin (const char *str)
93 // The first case - see if it's a number:
95 if (isdigit (str [0]))
105 * Change the ownership of the file to the real userId of the calling
106 * program so we can access it.
107 *********************************************************************************
110 static void changeOwner (char *cmd, char *file)
112 uid_t uid = getuid () ;
113 uid_t gid = getgid () ;
115 if (chown (file, uid, gid) != 0)
117 if (errno == ENOENT) // Warn that it's not there
118 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
121 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
130 * Return true/false if the supplied module is loaded
131 *********************************************************************************
134 static int moduleLoaded (char *modName)
136 int len = strlen (modName) ;
138 FILE *fd = fopen ("/proc/modules", "r") ;
143 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
147 while (fgets (line, 80, fd) != NULL)
149 if (strncmp (line, modName, len) != 0)
164 * Load either the spi or i2c modules and change device ownerships, etc.
165 *********************************************************************************
168 static void _doLoadUsage (char *argv [])
170 fprintf (stderr, "Usage: %s load <spi/i2c> [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ;
174 static void doLoad (int argc, char *argv [])
176 char *module1, *module2 ;
178 char *file1, *file2 ;
179 char args1 [32], args2 [32] ;
182 _doLoadUsage (argv) ;
184 args1 [0] = args2 [0] = 0 ;
186 /**/ if (strcasecmp (argv [2], "spi") == 0)
189 module2 = "spi_bcm2708" ;
190 file1 = "/dev/spidev0.0" ;
191 file2 = "/dev/spidev0.1" ;
193 sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
195 _doLoadUsage (argv) ;
197 else if (strcasecmp (argv [2], "i2c") == 0)
199 module1 = "i2c_dev" ;
200 module2 = "i2c_bcm2708" ;
201 file1 = "/dev/i2c-0" ;
202 file2 = "/dev/i2c-1" ;
204 sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
206 _doLoadUsage (argv) ;
209 _doLoadUsage (argv) ;
211 if (!moduleLoaded (module1))
213 sprintf (cmd, "modprobe %s%s", module1, args1) ;
217 if (!moduleLoaded (module2))
219 sprintf (cmd, "modprobe %s%s", module2, args2) ;
223 if (!moduleLoaded (module2))
225 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
229 sleep (1) ; // To let things get settled
231 changeOwner (argv [0], file1) ;
232 changeOwner (argv [0], file2) ;
238 * Run the i2cdetect command with the right runes for this Pi revision
239 *********************************************************************************
242 static void doI2Cdetect (int argc, char *argv [])
244 int port = piBoardRev () == 1 ? 0 : 1 ;
246 struct stat statBuf ;
248 if (stat (I2CDETECT, &statBuf) < 0)
250 fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
254 if (!moduleLoaded ("i2c_dev"))
256 fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
260 sprintf (command, "%s -y %d", I2CDETECT, port) ;
261 if (system (command) < 0)
262 fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
269 * List all GPIO exports
270 *********************************************************************************
273 static void doExports (int argc, char *argv [])
280 for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
283 // Try to read the direction
285 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
286 if ((fd = open (fName, O_RDONLY)) == -1)
292 printf ("GPIO Pins exported:\n") ;
295 printf ("%4d: ", i) ;
297 if ((l = read (fd, buf, 16)) == 0)
298 sprintf (buf, "%s", "?") ;
301 if ((buf [strlen (buf) - 1]) == '\n')
302 buf [strlen (buf) - 1] = 0 ;
304 printf ("%-3s", buf) ;
308 // Try to Read the value
310 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
311 if ((fd = open (fName, O_RDONLY)) == -1)
313 printf ("No Value file (huh?)\n") ;
317 if ((l = read (fd, buf, 16)) == 0)
318 sprintf (buf, "%s", "?") ;
321 if ((buf [strlen (buf) - 1]) == '\n')
322 buf [strlen (buf) - 1] = 0 ;
324 printf (" %s", buf) ;
326 // Read any edge trigger file
328 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
329 if ((fd = open (fName, O_RDONLY)) == -1)
335 if ((l = read (fd, buf, 16)) == 0)
336 sprintf (buf, "%s", "?") ;
339 if ((buf [strlen (buf) - 1]) == '\n')
340 buf [strlen (buf) - 1] = 0 ;
342 printf (" %-8s\n", buf) ;
351 * gpio export pin mode
352 * This uses the /sys/class/gpio device interface.
353 *********************************************************************************
356 void doExport (int argc, char *argv [])
365 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
369 pin = atoi (argv [2]) ;
373 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
375 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
379 fprintf (fd, "%d\n", pin) ;
382 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
383 if ((fd = fopen (fName, "w")) == NULL)
385 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
389 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
390 fprintf (fd, "in\n") ;
391 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
392 fprintf (fd, "out\n") ;
395 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
401 // Change ownership so the current user can actually use it!
403 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
404 changeOwner (argv [0], fName) ;
406 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
407 changeOwner (argv [0], fName) ;
415 * Wait for Interrupt on a given pin.
416 * Slight cheat here - it's easier to actually use ISR now (which calls
417 * gpio to set the pin modes!) then we simply sleep, and expect the thread
418 * to exit the program. Crude but effective.
419 *********************************************************************************
422 static void wfi (void)
425 void doWfi (int argc, char *argv [])
431 fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
435 pin = atoi (argv [2]) ;
437 /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
438 else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
439 else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
442 fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
446 if (wiringPiISR (pin, mode, &wfi) < 0)
448 fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
461 * Easy access to changing the edge trigger on a GPIO pin
462 * This uses the /sys/class/gpio device interface.
463 *********************************************************************************
466 void doEdge (int argc, char *argv [])
475 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
479 pin = atoi (argv [2]) ;
482 // Export the pin and set direction to input
484 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
486 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
490 fprintf (fd, "%d\n", pin) ;
493 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
494 if ((fd = fopen (fName, "w")) == NULL)
496 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
500 fprintf (fd, "in\n") ;
503 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
504 if ((fd = fopen (fName, "w")) == NULL)
506 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
510 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
511 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
512 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
513 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
516 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
520 // Change ownership of the value and edge files, so the current user can actually use it!
522 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
523 changeOwner (argv [0], fName) ;
525 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
526 changeOwner (argv [0], fName) ;
535 * This uses the /sys/class/gpio device interface.
536 *********************************************************************************
539 void doUnexport (int argc, char *argv [])
546 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
550 pin = atoi (argv [2]) ;
552 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
554 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
558 fprintf (fd, "%d\n", pin) ;
566 * Un-Export all the GPIO pins.
567 * This uses the /sys/class/gpio device interface.
568 *********************************************************************************
571 void doUnexportall (char *progName)
576 for (pin = 0 ; pin < 63 ; ++pin)
578 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
580 fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
583 fprintf (fd, "%d\n", pin) ;
591 * Load readallExternal, we try to do this with an external device.
592 *********************************************************************************
595 static void doResetExternal (void)
599 for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
601 pinMode (pin, INPUT) ;
602 pullUpDnControl (pin, PUD_OFF) ;
609 * Reset the GPIO pins - as much as we can do
610 *********************************************************************************
613 static void doReset (char *progName)
615 int model, rev, mem, maker, overVolted ;
618 printf ("GPIO Reset is dangerous!\n") ;
619 printf (" - Do Not rely on this to do anything sensible!\n") ;
621 if (wiringPiNodes != NULL) // External
627 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
629 /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
631 else if (model == PI_MODEL_BP)
633 else if (model == PI_MODEL_CM)
635 printf (" - Don't know how to reset a comput module:\n") ;
636 printf (" Write a shell-script to reset the pins to the state you need.\n") ;
641 printf ("Oops - unable to determine board type... model: %d\n", model) ;
645 for (pin = 0 ; pin <= endPin ; ++pin)
647 if (wpiPinToGpio (pin) == -1)
650 digitalWrite (pin, LOW) ;
651 pinMode (pin, INPUT) ;
652 pullUpDnControl (pin, PUD_OFF) ;
659 * gpio mode pin mode ...
660 *********************************************************************************
663 void doMode (int argc, char *argv [])
670 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
674 pin = atoi (argv [2]) ;
678 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
679 else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
680 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
681 else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
682 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
683 else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ;
684 else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
685 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
686 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
687 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
688 else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
692 else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
693 else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
694 else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
695 else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
696 else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
697 else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
700 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
708 * gpio drive group value
709 *********************************************************************************
712 static void doPadDrive (int argc, char *argv [])
718 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
722 group = atoi (argv [2]) ;
723 val = atoi (argv [3]) ;
725 if ((group < 0) || (group > 2))
727 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
731 if ((val < 0) || (val > 7))
733 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
737 setPadDrive (group, val) ;
743 * Control USB Power - High (1.2A) or Low (600mA)
745 *********************************************************************************
748 static void doUsbP (int argc, char *argv [])
750 int model, rev, mem, maker, overVolted ;
754 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
758 // Make sure we're on a B+
760 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
762 if (model != PI_MODEL_BP)
764 fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ;
768 // Need to force BCM_GPIO mode:
770 wiringPiSetupGpio () ;
772 if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0))
774 digitalWrite (PI_USB_POWER_CONTROL, 1) ;
775 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
776 printf ("Switched to HIGH current USB (1.2A)\n") ;
780 if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0))
782 digitalWrite (PI_USB_POWER_CONTROL, 0) ;
783 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
784 printf ("Switched to LOW current USB (600mA)\n") ;
788 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
795 * gpio gbw channel value
796 * Gertboard Write - To the Analog output
797 *********************************************************************************
800 static void doGbw (int argc, char *argv [])
806 fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
810 channel = atoi (argv [2]) ;
811 value = atoi (argv [3]) ;
813 if ((channel < 0) || (channel > 1))
815 fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
819 if ((value < 0) || (value > 255))
821 fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
825 if (gertboardAnalogSetup (64) < 0)
827 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
831 analogWrite (64 + channel, value) ;
838 * From the analog input
839 *********************************************************************************
842 static void doGbr (int argc, char *argv [])
848 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
852 channel = atoi (argv [2]) ;
854 if ((channel < 0) || (channel > 1))
856 fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
860 if (gertboardAnalogSetup (64) < 0)
862 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
866 printf ("%d\n", analogRead (64 + channel)) ;
872 * gpio write pin value
873 *********************************************************************************
876 static void doWrite (int argc, char *argv [])
882 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
886 pin = atoi (argv [2]) ;
888 /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
890 else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
893 val = atoi (argv [3]) ;
896 digitalWrite (pin, LOW) ;
898 digitalWrite (pin, HIGH) ;
904 * gpio awrite pin value
905 *********************************************************************************
908 static void doAwrite (int argc, char *argv [])
914 fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
918 pin = atoi (argv [2]) ;
920 val = atoi (argv [3]) ;
922 analogWrite (pin, val) ;
929 *********************************************************************************
932 static void doWriteByte (int argc, char *argv [])
938 fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
942 val = (int)strtol (argv [2], NULL, 0) ;
944 digitalWriteByte (val) ;
950 * Read a pin and return the value
951 *********************************************************************************
954 void doRead (int argc, char *argv [])
960 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
964 pin = atoi (argv [2]) ;
965 val = digitalRead (pin) ;
967 printf ("%s\n", val == 0 ? "0" : "1") ;
973 * Read an analog pin and return the value
974 *********************************************************************************
977 void doAread (int argc, char *argv [])
981 fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
985 printf ("%d\n", analogRead (atoi (argv [2]))) ;
992 *********************************************************************************
995 void doToggle (int argc, char *argv [])
1001 fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
1005 pin = atoi (argv [2]) ;
1007 digitalWrite (pin, !digitalRead (pin)) ;
1013 * Output a tone in a PWM pin
1014 *********************************************************************************
1017 void doPwmTone (int argc, char *argv [])
1023 fprintf (stderr, "Usage: %s pwmTone <pin> <freq>\n", argv [0]) ;
1027 pin = atoi (argv [2]) ;
1028 freq = atoi (argv [3]) ;
1030 pwmToneWrite (pin, freq) ;
1036 * Output a clock on a pin
1037 *********************************************************************************
1040 void doClock (int argc, char *argv [])
1046 fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
1050 pin = atoi (argv [2]) ;
1052 freq = atoi (argv [3]) ;
1054 gpioClockSet (pin, freq) ;
1060 * Output a PWM value on a pin
1061 *********************************************************************************
1064 void doPwm (int argc, char *argv [])
1070 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
1074 pin = atoi (argv [2]) ;
1076 val = atoi (argv [3]) ;
1078 pwmWrite (pin, val) ;
1083 * doPwmMode: doPwmRange: doPwmClock:
1084 * Change the PWM mode, range and clock divider values
1085 *********************************************************************************
1088 static void doPwmMode (int mode)
1093 static void doPwmRange (int argc, char *argv [])
1095 unsigned int range ;
1099 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
1103 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
1107 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
1111 pwmSetRange (range) ;
1114 static void doPwmClock (int argc, char *argv [])
1116 unsigned int clock ;
1120 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
1124 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
1126 if ((clock < 1) || (clock > 4095))
1128 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
1132 pwmSetClock (clock) ;
1139 *********************************************************************************
1142 int main (int argc, char *argv [])
1145 int model, rev, mem, maker, overVolted ;
1147 if (getenv ("WIRINGPI_DEBUG") != NULL)
1149 printf ("gpio: wiringPi debug mode enabled\n") ;
1150 wiringPiDebug = TRUE ;
1155 fprintf (stderr, "%s\n", usage) ;
1161 if (strcasecmp (argv [1], "-h") == 0)
1163 printf ("%s: %s\n", argv [0], usage) ;
1167 // Sort of a special:
1169 if (strcmp (argv [1], "-R") == 0)
1171 printf ("%d\n", piBoardRev ()) ;
1175 // Version & Warranty
1177 if (strcmp (argv [1], "-V") == 0)
1179 printf ("%d\n", piBoardRev ()) ;
1183 if (strcmp (argv [1], "-v") == 0)
1185 printf ("gpio version: %s\n", VERSION) ;
1186 printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
1187 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
1188 printf ("For details type: %s -warranty\n", argv [0]) ;
1190 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
1191 if (model == PI_MODEL_UNKNOWN)
1193 printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ;
1194 printf (" projects@drogon.net\n") ;
1195 printf ("with a copy of your /proc/cpuinfo if possible\n") ;
1199 printf ("Raspberry Pi Details:\n") ;
1200 printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
1201 piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
1206 if (strcasecmp (argv [1], "-warranty") == 0)
1208 printf ("gpio version: %s\n", VERSION) ;
1209 printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
1211 printf (" This program is free software; you can redistribute it and/or modify\n") ;
1212 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
1213 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
1214 printf (" (at your option) any later version.\n") ;
1216 printf (" This program is distributed in the hope that it will be useful,\n") ;
1217 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
1218 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
1219 printf (" GNU Lesser General Public License for more details.\n") ;
1221 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
1222 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
1227 if (geteuid () != 0)
1229 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
1233 // Initial test for /sys/class/gpio operations:
1235 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
1236 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
1237 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
1238 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
1239 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
1241 // Check for load command:
1243 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
1245 // Gertboard commands
1247 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
1248 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
1250 // Check for -g argument
1252 /**/ if (strcasecmp (argv [1], "-g") == 0)
1254 wiringPiSetupGpio () ;
1256 for (i = 2 ; i < argc ; ++i)
1257 argv [i - 1] = argv [i] ;
1259 wpMode = WPI_MODE_GPIO ;
1262 // Check for -1 argument
1264 else if (strcasecmp (argv [1], "-1") == 0)
1266 wiringPiSetupPhys () ;
1268 for (i = 2 ; i < argc ; ++i)
1269 argv [i - 1] = argv [i] ;
1271 wpMode = WPI_MODE_PHYS ;
1274 // Check for -p argument for PiFace
1276 else if (strcasecmp (argv [1], "-p") == 0)
1280 for (i = 2 ; i < argc ; ++i)
1281 argv [i - 1] = argv [i] ;
1283 wpMode = WPI_MODE_PIFACE ;
1286 // Default to wiringPi mode
1291 wpMode = WPI_MODE_PINS ;
1294 // Check for -x argument to load in a new extension
1296 if (strcasecmp (argv [1], "-x") == 0)
1300 fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
1301 exit (EXIT_FAILURE) ;
1304 if (!doExtension (argv [0], argv [2])) // Prints its own error messages
1305 exit (EXIT_FAILURE) ;
1307 for (i = 3 ; i < argc ; ++i)
1308 argv [i - 2] = argv [i] ;
1314 fprintf (stderr, "%s: no command given\n", argv [0]) ;
1315 exit (EXIT_FAILURE) ;
1318 // Core wiringPi functions
1320 /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
1321 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
1322 else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
1323 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
1324 else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
1325 else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
1329 else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
1333 else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
1334 else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
1335 else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
1336 else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
1337 else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
1338 else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
1339 else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ;
1340 else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
1341 else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
1342 else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ;
1343 else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
1344 else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
1345 else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
1346 else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
1347 else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
1348 else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
1351 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
1352 exit (EXIT_FAILURE) ;