3 * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
5 * Copyright (c) 2012 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 ***********************************************************************
32 #include <sys/types.h>
36 #include <gertboard.h>
47 char *usage = "Usage: gpio -v\n"
49 " gpio [-g] <read/write/wb/pwm/mode> ...\n"
50 " gpio [-p] <read/write/wb> ...\n"
52 " gpio unexportall/exports ...\n"
53 " gpio export/edge/unexport ...\n"
54 " gpio drive <group> <value>\n"
55 " gpio pwm-bal/pwm-ms \n"
56 " gpio pwmr <range> \n"
57 " gpio pwmc <divider> \n"
58 " gpio load spi/i2c\n"
59 " gpio gbr <channel>\n"
60 " gpio gbw <channel> <value>" ; // No trailing newline needed here.
65 * Change the ownership of the file to the real userId of the calling
66 * program so we can access it.
67 *********************************************************************************
70 static void changeOwner (char *cmd, char *file)
72 uid_t uid = getuid () ;
73 uid_t gid = getgid () ;
75 if (chown (file, uid, gid) != 0)
77 if (errno == ENOENT) // Warn that it's not there
78 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
81 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
90 * Return true/false if the supplied module is loaded
91 *********************************************************************************
94 static int moduleLoaded (char *modName)
96 int len = strlen (modName) ;
98 FILE *fd = fopen ("/proc/modules", "r") ;
103 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
107 while (fgets (line, 80, fd) != NULL)
109 if (strncmp (line, modName, len) != 0)
124 * Load either the spi or i2c modules and change device ownerships, etc.
125 *********************************************************************************
128 static void _doLoadUsage (char *argv [])
130 fprintf (stderr, "Usage: %s load <spi/i2c>\n", argv [0]) ;
134 static void doLoad (int argc, char *argv [])
136 char *module1, *module2 ;
138 char *file1, *file2 ;
141 _doLoadUsage (argv) ;
143 /**/ if (strcasecmp (argv [2], "spi") == 0)
146 module2 = "spi_bcm2708" ;
147 file1 = "/dev/spidev0.0" ;
148 file2 = "/dev/spidev0.1" ;
150 else if (strcasecmp (argv [2], "i2c") == 0)
152 module1 = "i2c_dev" ;
153 module2 = "i2c_bcm2708" ;
154 file1 = "/dev/i2c-0" ;
155 file2 = "/dev/i2c-1" ;
158 _doLoadUsage (argv) ;
160 if (!moduleLoaded (module1))
162 sprintf (cmd, "modprobe %s", module1) ;
166 if (!moduleLoaded (module2))
168 sprintf (cmd, "modprobe %s", module2) ;
172 if (!moduleLoaded (module2))
174 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
178 sleep (1) ; // To let things get settled
180 changeOwner (argv [0], file1) ;
181 changeOwner (argv [0], file2) ;
187 * Read all the GPIO pins
188 *********************************************************************************
191 static char *pinNames [] =
216 static void doReadall (void)
220 printf ("+----------+------+--------+-------+\n") ;
221 printf ("| wiringPi | GPIO | Name | Value |\n") ;
222 printf ("+----------+------+--------+-------+\n") ;
224 for (pin = 0 ; pin < NUM_PINS ; ++pin)
225 printf ("| %6d | %3d | %s | %s |\n",
226 pin, wpiPinToGpio (pin),
228 digitalRead (pin) == HIGH ? "High" : "Low ") ;
230 printf ("+----------+------+--------+-------+\n") ;
232 if (piBoardRev () == 1)
235 for (pin = 17 ; pin <= 20 ; ++pin)
236 printf ("| %6d | %3d | %s | %s |\n",
237 pin, wpiPinToGpio (pin),
239 digitalRead (pin) == HIGH ? "High" : "Low ") ;
241 printf ("+----------+------+--------+-------+\n") ;
247 * List all GPIO exports
248 *********************************************************************************
251 static void doExports (int argc, char *argv [])
258 // Rather crude, but who knows what others are up to...
260 for (first = 0, i = 0 ; i < 64 ; ++i)
263 // Try to read the direction
265 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
266 if ((fd = open (fName, O_RDONLY)) == -1)
272 printf ("GPIO Pins exported:\n") ;
275 printf ("%4d: ", i) ;
277 if ((l = read (fd, buf, 16)) == 0)
278 sprintf (buf, "%s", "?") ;
281 if ((buf [strlen (buf) - 1]) == '\n')
282 buf [strlen (buf) - 1] = 0 ;
284 printf ("%-3s", buf) ;
288 // Try to Read the value
290 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
291 if ((fd = open (fName, O_RDONLY)) == -1)
293 printf ("No Value file (huh?)\n") ;
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 (" %s", buf) ;
306 // Read any edge trigger file
308 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
309 if ((fd = open (fName, O_RDONLY)) == -1)
315 if ((l = read (fd, buf, 16)) == 0)
316 sprintf (buf, "%s", "?") ;
319 if ((buf [strlen (buf) - 1]) == '\n')
320 buf [strlen (buf) - 1] = 0 ;
322 printf (" %-8s\n", buf) ;
331 * gpio export pin mode
332 * This uses the /sys/class/gpio device interface.
333 *********************************************************************************
336 void doExport (int argc, char *argv [])
345 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
349 pin = atoi (argv [2]) ;
353 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
355 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
359 fprintf (fd, "%d\n", pin) ;
362 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
363 if ((fd = fopen (fName, "w")) == NULL)
365 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
369 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
370 fprintf (fd, "in\n") ;
371 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
372 fprintf (fd, "out\n") ;
375 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
381 // Change ownership so the current user can actually use it!
383 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
384 changeOwner (argv [0], fName) ;
386 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
387 changeOwner (argv [0], fName) ;
395 * Easy access to changing the edge trigger on a GPIO pin
396 * This uses the /sys/class/gpio device interface.
397 *********************************************************************************
400 void doEdge (int argc, char *argv [])
409 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
413 pin = atoi (argv [2]) ;
416 // Export the pin and set direction to input
418 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
420 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
424 fprintf (fd, "%d\n", pin) ;
427 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
428 if ((fd = fopen (fName, "w")) == NULL)
430 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
434 fprintf (fd, "in\n") ;
437 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
438 if ((fd = fopen (fName, "w")) == NULL)
440 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
444 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
445 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
446 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
447 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
450 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
454 // Change ownership of the value and edge files, so the current user can actually use it!
456 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
457 changeOwner (argv [0], fName) ;
459 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
460 changeOwner (argv [0], fName) ;
469 * This uses the /sys/class/gpio device interface.
470 *********************************************************************************
473 void doUnexport (int argc, char *argv [])
480 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
484 pin = atoi (argv [2]) ;
486 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
488 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
492 fprintf (fd, "%d\n", pin) ;
500 * Un-Export all the GPIO pins.
501 * This uses the /sys/class/gpio device interface.
502 *********************************************************************************
505 void doUnexportall (int argc, char *argv [])
510 for (pin = 0 ; pin < 63 ; ++pin)
512 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
514 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
517 fprintf (fd, "%d\n", pin) ;
525 * gpio mode pin mode ...
526 *********************************************************************************
529 void doMode (int argc, char *argv [])
536 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
540 pin = atoi (argv [2]) ;
542 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
547 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
548 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
549 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
550 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
551 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
552 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
555 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
563 * gpio drive group value
564 *********************************************************************************
567 static void doPadDrive (int argc, char *argv [])
573 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
577 group = atoi (argv [2]) ;
578 val = atoi (argv [3]) ;
580 if ((group < 0) || (group > 2))
582 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
586 if ((val < 0) || (val > 7))
588 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
592 setPadDrive (group, val) ;
598 * gpio gbw channel value
599 * Gertboard Write - To the Analog output
600 *********************************************************************************
603 static void doGbw (int argc, char *argv [])
609 fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
613 channel = atoi (argv [2]) ;
614 value = atoi (argv [3]) ;
616 if ((channel < 0) || (channel > 1))
618 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
622 if ((value < 0) || (value > 1023))
624 fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
628 if (gertboardSPISetup () == -1)
630 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
634 gertboardAnalogWrite (channel, value) ;
641 * From the analog input
642 *********************************************************************************
645 static void doGbr (int argc, char *argv [])
651 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
655 channel = atoi (argv [2]) ;
657 if ((channel < 0) || (channel > 1))
659 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
663 if (gertboardSPISetup () == -1)
665 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
669 printf ("%d\n",gertboardAnalogRead (channel)) ;
676 * gpio write pin value
677 *********************************************************************************
680 static void doWrite (int argc, char *argv [])
686 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
690 pin = atoi (argv [2]) ;
692 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
695 /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
697 else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
700 val = atoi (argv [3]) ;
703 digitalWrite (pin, LOW) ;
705 digitalWrite (pin, HIGH) ;
711 *********************************************************************************
714 static void doWriteByte (int argc, char *argv [])
720 fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
724 val = (int)strtol (argv [2], NULL, 0) ;
726 digitalWriteByte (val) ;
732 * Read a pin and return the value
733 *********************************************************************************
736 void doRead (int argc, char *argv [])
742 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
746 pin = atoi (argv [2]) ;
748 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
754 val = digitalRead (pin) ;
756 printf ("%s\n", val == 0 ? "0" : "1") ;
762 * Output a PWM value on a pin
763 *********************************************************************************
766 void doPwm (int argc, char *argv [])
772 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
776 pin = atoi (argv [2]) ;
778 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
781 val = atoi (argv [3]) ;
783 pwmWrite (pin, val) ;
788 * doPwmMode: doPwmRange: doPwmClock:
789 * Change the PWM mode, range and clock divider values
790 *********************************************************************************
793 static void doPwmMode (int mode)
798 static void doPwmRange (int argc, char *argv [])
804 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
808 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
812 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
816 pwmSetRange (range) ;
819 static void doPwmClock (int argc, char *argv [])
825 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
829 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
831 if ((clock < 1) || (clock > 4095))
833 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
837 pwmSetClock (clock) ;
844 *********************************************************************************
847 int main (int argc, char *argv [])
853 fprintf (stderr, "%s\n", usage) ;
857 if (strcasecmp (argv [1], "-h") == 0)
859 printf ("%s: %s\n", argv [0], usage) ;
863 if (strcasecmp (argv [1], "-v") == 0)
865 printf ("gpio version: %s\n", VERSION) ;
866 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
867 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
868 printf ("For details type: %s -warranty\n", argv [0]) ;
870 printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
874 if (strcasecmp (argv [1], "-warranty") == 0)
876 printf ("gpio version: %s\n", VERSION) ;
877 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
879 printf (" This program is free software; you can redistribute it and/or modify\n") ;
880 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
881 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
882 printf (" (at your option) any later version.\n") ;
884 printf (" This program is distributed in the hope that it will be useful,\n") ;
885 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
886 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
887 printf (" GNU Lesser General Public License for more details.\n") ;
889 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
890 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
897 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
901 // Initial test for /sys/class/gpio operations:
903 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
904 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
905 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
906 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
907 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
909 // Check for load command:
911 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
913 // Gertboard commands
915 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
916 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
918 // Check for -g argument
920 if (strcasecmp (argv [1], "-g") == 0)
922 if (wiringPiSetupGpio () == -1)
924 fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
928 for (i = 2 ; i < argc ; ++i)
929 argv [i - 1] = argv [i] ;
931 wpMode = WPI_MODE_GPIO ;
934 // Check for -p argument for PiFace
936 else if (strcasecmp (argv [1], "-p") == 0)
938 if (wiringPiSetupPiFaceForGpioProg () == -1)
940 fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
944 for (i = 2 ; i < argc ; ++i)
945 argv [i - 1] = argv [i] ;
947 wpMode = WPI_MODE_PIFACE ;
950 // Default to wiringPi mode
954 if (wiringPiSetup () == -1)
956 fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
959 wpMode = WPI_MODE_PINS ;
962 // Check for PWM or Pad Drive operations
964 if (wpMode != WPI_MODE_PIFACE)
966 if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
967 if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
968 if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
969 if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; }
970 if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
973 // Check for wiring commands
975 /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
976 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
977 else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ;
978 else if (strcasecmp (argv [1], "wb") == 0) doWriteByte (argc, argv) ;
979 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
980 else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
983 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;