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>
45 extern int wiringPiDebug ;
47 // External functions I can't be bothered creating a separate .h file for:
49 extern void doReadall (void) ;
50 extern void doPins (void) ;
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") ;
441 else if ((strcasecmp (mode, "high") == 0) || (strcasecmp (mode, "up") == 0))
442 fprintf (fd, "high\n") ;
443 else if ((strcasecmp (mode, "low") == 0) || (strcasecmp (mode, "down") == 0))
444 fprintf (fd, "low\n") ;
447 fprintf (stderr, "%s: Invalid mode: %s. Should be in, out, high or low\n", argv [1], mode) ;
453 // Change ownership so the current user can actually use it
455 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
456 changeOwner (argv [0], fName) ;
458 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
459 changeOwner (argv [0], fName) ;
467 * Wait for Interrupt on a given pin.
468 * Slight cheat here - it's easier to actually use ISR now (which calls
469 * gpio to set the pin modes!) then we simply sleep, and expect the thread
470 * to exit the program. Crude but effective.
471 *********************************************************************************
474 static void wfi (void)
477 void doWfi (int argc, char *argv [])
483 fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
487 pin = atoi (argv [2]) ;
489 /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
490 else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
491 else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
494 fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
498 if (wiringPiISR (pin, mode, &wfi) < 0)
500 fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
513 * Easy access to changing the edge trigger on a GPIO pin
514 * This uses the /sys/class/gpio device interface.
515 *********************************************************************************
518 void doEdge (int argc, char *argv [])
527 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
531 pin = atoi (argv [2]) ;
534 // Export the pin and set direction to input
536 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
538 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
542 fprintf (fd, "%d\n", pin) ;
545 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
546 if ((fd = fopen (fName, "w")) == NULL)
548 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
552 fprintf (fd, "in\n") ;
555 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
556 if ((fd = fopen (fName, "w")) == NULL)
558 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
562 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
563 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
564 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
565 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
568 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
572 // Change ownership of the value and edge files, so the current user can actually use it!
574 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
575 changeOwner (argv [0], fName) ;
577 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
578 changeOwner (argv [0], fName) ;
587 * This uses the /sys/class/gpio device interface.
588 *********************************************************************************
591 void doUnexport (int argc, char *argv [])
598 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
602 pin = atoi (argv [2]) ;
604 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
606 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
610 fprintf (fd, "%d\n", pin) ;
618 * Un-Export all the GPIO pins.
619 * This uses the /sys/class/gpio device interface.
620 *********************************************************************************
623 void doUnexportall (char *progName)
628 for (pin = 0 ; pin < 63 ; ++pin)
630 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
632 fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
635 fprintf (fd, "%d\n", pin) ;
643 * Reset the GPIO pins - as much as we can do
644 *********************************************************************************
647 static void doReset (char *progName)
649 printf ("GPIO Reset is dangerous and has been removed from the gpio command.\n") ;
650 printf (" - Please write a shell-script to reset the GPIO pins into the state\n") ;
651 printf (" that you need them in for your applications.\n") ;
657 * gpio mode pin mode ...
658 *********************************************************************************
661 void doMode (int argc, char *argv [])
668 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
672 pin = atoi (argv [2]) ;
676 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
677 else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
678 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
679 else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
680 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
681 else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ;
682 else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
683 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
684 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
685 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
686 else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
687 else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
688 else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
689 else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
690 else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
691 else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
692 else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
695 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
703 * gpio drive group value
704 *********************************************************************************
707 static void doPadDrive (int argc, char *argv [])
713 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
717 group = atoi (argv [2]) ;
718 val = atoi (argv [3]) ;
720 if ((group < 0) || (group > 2))
722 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
726 if ((val < 0) || (val > 7))
728 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
732 setPadDrive (group, val) ;
738 * Control USB Power - High (1.2A) or Low (600mA)
740 *********************************************************************************
743 static void doUsbP (int argc, char *argv [])
745 int model, rev, mem, maker, overVolted ;
749 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
753 // Make sure we're on a B+
755 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
757 if (model != PI_MODEL_BP)
759 fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ;
763 // Need to force BCM_GPIO mode:
765 wiringPiSetupGpio () ;
767 if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0))
769 digitalWrite (PI_USB_POWER_CONTROL, 1) ;
770 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
771 printf ("Switched to HIGH current USB (1.2A)\n") ;
775 if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0))
777 digitalWrite (PI_USB_POWER_CONTROL, 0) ;
778 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
779 printf ("Switched to LOW current USB (600mA)\n") ;
783 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
790 * gpio gbw channel value
791 * Gertboard Write - To the Analog output
792 *********************************************************************************
795 static void doGbw (int argc, char *argv [])
801 fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
805 channel = atoi (argv [2]) ;
806 value = atoi (argv [3]) ;
808 if ((channel < 0) || (channel > 1))
810 fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
814 if ((value < 0) || (value > 255))
816 fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
820 if (gertboardAnalogSetup (64) < 0)
822 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
826 analogWrite (64 + channel, value) ;
833 * From the analog input
834 *********************************************************************************
837 static void doGbr (int argc, char *argv [])
843 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
847 channel = atoi (argv [2]) ;
849 if ((channel < 0) || (channel > 1))
851 fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
855 if (gertboardAnalogSetup (64) < 0)
857 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
861 printf ("%d\n", analogRead (64 + channel)) ;
867 * gpio write pin value
868 *********************************************************************************
871 static void doWrite (int argc, char *argv [])
877 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
881 pin = atoi (argv [2]) ;
883 /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
885 else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
888 val = atoi (argv [3]) ;
891 digitalWrite (pin, LOW) ;
893 digitalWrite (pin, HIGH) ;
899 * gpio awrite pin value
900 *********************************************************************************
903 static void doAwrite (int argc, char *argv [])
909 fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
913 pin = atoi (argv [2]) ;
915 val = atoi (argv [3]) ;
917 analogWrite (pin, val) ;
924 *********************************************************************************
927 static void doWriteByte (int argc, char *argv [])
933 fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
937 val = (int)strtol (argv [2], NULL, 0) ;
939 digitalWriteByte (val) ;
945 * Read a pin and return the value
946 *********************************************************************************
949 void doRead (int argc, char *argv [])
955 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
959 pin = atoi (argv [2]) ;
960 val = digitalRead (pin) ;
962 printf ("%s\n", val == 0 ? "0" : "1") ;
968 * Read an analog pin and return the value
969 *********************************************************************************
972 void doAread (int argc, char *argv [])
976 fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
980 printf ("%d\n", analogRead (atoi (argv [2]))) ;
987 *********************************************************************************
990 void doToggle (int argc, char *argv [])
996 fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
1000 pin = atoi (argv [2]) ;
1002 digitalWrite (pin, !digitalRead (pin)) ;
1008 * Output a tone in a PWM pin
1009 *********************************************************************************
1012 void doPwmTone (int argc, char *argv [])
1018 fprintf (stderr, "Usage: %s pwmTone <pin> <freq>\n", argv [0]) ;
1022 pin = atoi (argv [2]) ;
1023 freq = atoi (argv [3]) ;
1025 pwmToneWrite (pin, freq) ;
1031 * Output a clock on a pin
1032 *********************************************************************************
1035 void doClock (int argc, char *argv [])
1041 fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
1045 pin = atoi (argv [2]) ;
1047 freq = atoi (argv [3]) ;
1049 gpioClockSet (pin, freq) ;
1055 * Output a PWM value on a pin
1056 *********************************************************************************
1059 void doPwm (int argc, char *argv [])
1065 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
1069 pin = atoi (argv [2]) ;
1071 val = atoi (argv [3]) ;
1073 pwmWrite (pin, val) ;
1078 * doPwmMode: doPwmRange: doPwmClock:
1079 * Change the PWM mode, range and clock divider values
1080 *********************************************************************************
1083 static void doPwmMode (int mode)
1088 static void doPwmRange (int argc, char *argv [])
1090 unsigned int range ;
1094 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
1098 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
1102 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
1106 pwmSetRange (range) ;
1109 static void doPwmClock (int argc, char *argv [])
1111 unsigned int clock ;
1115 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
1119 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
1121 if ((clock < 1) || (clock > 4095))
1123 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
1127 pwmSetClock (clock) ;
1134 *********************************************************************************
1137 int main (int argc, char *argv [])
1140 int model, rev, mem, maker, overVolted ;
1142 if (getenv ("WIRINGPI_DEBUG") != NULL)
1144 printf ("gpio: wiringPi debug mode enabled\n") ;
1145 wiringPiDebug = TRUE ;
1150 fprintf (stderr, "%s\n", usage) ;
1156 if (strcasecmp (argv [1], "-h") == 0)
1158 printf ("%s: %s\n", argv [0], usage) ;
1162 // Sort of a special:
1164 if (strcmp (argv [1], "-R") == 0)
1166 printf ("%d\n", piBoardRev ()) ;
1170 // Version & Warranty
1172 if (strcmp (argv [1], "-V") == 0)
1174 printf ("%d\n", piBoardRev ()) ;
1178 if (strcmp (argv [1], "-v") == 0)
1180 printf ("gpio version: %s\n", VERSION) ;
1181 printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
1182 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
1183 printf ("For details type: %s -warranty\n", argv [0]) ;
1185 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
1186 if (model == PI_MODEL_UNKNOWN)
1188 printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ;
1189 printf (" projects@drogon.net\n") ;
1190 printf ("with a copy of your /proc/cpuinfo if possible\n") ;
1194 printf ("Raspberry Pi Details:\n") ;
1195 printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
1196 piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
1198 // Quick check for /dev/gpiomem
1200 if ((i = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0)
1201 printf (" This Raspberry Pi supports user-level GPIO access via /dev/gpiomem.\n") ;
1204 printf (" You need to run your programs as root for GPIO access\n") ;
1205 printf (" (Old /dev/mem method - consider upgrading)\n") ;
1212 if (strcasecmp (argv [1], "-warranty") == 0)
1214 printf ("gpio version: %s\n", VERSION) ;
1215 printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
1217 printf (" This program is free software; you can redistribute it and/or modify\n") ;
1218 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
1219 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
1220 printf (" (at your option) any later version.\n") ;
1222 printf (" This program is distributed in the hope that it will be useful,\n") ;
1223 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
1224 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
1225 printf (" GNU Lesser General Public License for more details.\n") ;
1227 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
1228 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
1233 if (geteuid () != 0)
1235 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
1239 // Initial test for /sys/class/gpio operations:
1241 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
1242 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
1243 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
1244 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
1245 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
1247 // Check for load command:
1249 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
1250 if (strcasecmp (argv [1], "unload" ) == 0) { doUnLoad (argc, argv) ; return 0 ; }
1252 // Gertboard commands
1254 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
1255 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
1257 // Check for -g argument
1259 /**/ if (strcasecmp (argv [1], "-g") == 0)
1261 wiringPiSetupGpio () ;
1263 for (i = 2 ; i < argc ; ++i)
1264 argv [i - 1] = argv [i] ;
1266 wpMode = WPI_MODE_GPIO ;
1269 // Check for -1 argument
1271 else if (strcasecmp (argv [1], "-1") == 0)
1273 wiringPiSetupPhys () ;
1275 for (i = 2 ; i < argc ; ++i)
1276 argv [i - 1] = argv [i] ;
1278 wpMode = WPI_MODE_PHYS ;
1281 // Check for -p argument for PiFace
1283 else if (strcasecmp (argv [1], "-p") == 0)
1287 for (i = 2 ; i < argc ; ++i)
1288 argv [i - 1] = argv [i] ;
1290 wpMode = WPI_MODE_PIFACE ;
1293 // Default to wiringPi mode
1298 wpMode = WPI_MODE_PINS ;
1301 // Check for -x argument to load in a new extension
1303 if (strcasecmp (argv [1], "-x") == 0)
1307 fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
1308 exit (EXIT_FAILURE) ;
1311 if (!loadWPiExtension (argv [0], argv [2], TRUE)) // Prints its own error messages
1312 exit (EXIT_FAILURE) ;
1314 for (i = 3 ; i < argc ; ++i)
1315 argv [i - 2] = argv [i] ;
1321 fprintf (stderr, "%s: no command given\n", argv [0]) ;
1322 exit (EXIT_FAILURE) ;
1325 // Core wiringPi functions
1327 /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
1328 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
1329 else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
1330 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
1331 else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
1332 else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
1336 else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
1340 else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
1341 else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
1342 else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
1343 else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
1344 else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
1345 else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
1346 else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ;
1347 else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
1348 else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
1349 else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ;
1350 else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
1351 else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
1352 else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
1353 else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
1354 else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
1355 else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
1358 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
1359 exit (EXIT_FAILURE) ;