3 * Set-UID command-line interface to the Raspberry Pi's GPIO
4 * Copyright (c) 2012 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
21 ***********************************************************************
32 #include <sys/types.h>
44 char *usage = "Usage: gpio -v\n"
45 " gpio [-g] <read/write/pwm/mode> ...\n"
46 " gpio [-p] <read/write/mode> ...\n"
47 " gpio export/edge/unexport/unexportall/exports ...\n"
48 " gpio drive <group> <value>\n"
49 " gpio pwm-bal/pwm-ms \n"
50 " gpio pwmr <range> \n"
51 " gpio load spi/i2c" ;
57 * Change the ownership of the file to the real userId of the calling
58 * program so we can access it.
59 *********************************************************************************
62 static void changeOwner (char *cmd, char *file)
64 uid_t uid = getuid () ;
65 uid_t gid = getgid () ;
67 if (chown (file, uid, gid) != 0)
69 if (errno == ENOENT) // Warn that it's not there
70 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
73 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
82 * Return true/false if the supplied module is loaded
83 *********************************************************************************
86 static int moduleLoaded (char *modName)
88 int len = strlen (modName) ;
90 FILE *fd = fopen ("/proc/modules", "r") ;
95 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
99 while (fgets (line, 80, fd) != NULL)
101 if (strncmp (line, modName, len) != 0)
116 * Load either the spi or i2c modules and change device ownerships, etc.
117 *********************************************************************************
120 static void _doLoadUsage (char *argv [])
122 fprintf (stderr, "Usage: %s load <spi/i2c>\n", argv [0]) ;
126 static void doLoad (int argc, char *argv [])
130 char *file1, *file2 ;
133 _doLoadUsage (argv) ;
135 /**/ if (strcasecmp (argv [2], "spi") == 0)
137 module = "spi_bcm2708" ;
138 file1 = "/dev/spidev0.0" ;
139 file2 = "/dev/spidev0.1" ;
141 else if (strcasecmp (argv [2], "i2c") == 0)
143 module = "i2c_bcm2708" ;
144 file1 = "/dev/i2c-0" ;
145 file2 = "/dev/i2c-1" ;
148 _doLoadUsage (argv) ;
150 if (!moduleLoaded (module))
152 sprintf (cmd, "modprobe %s", module) ;
156 if (!moduleLoaded (module))
158 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
162 sleep (1) ; // To let things get settled
164 changeOwner (argv [0], file1) ;
165 changeOwner (argv [0], file2) ;
172 * List all GPIO exports
173 *********************************************************************************
176 void doExports (void)
183 // Rather crude, but who knows what others are up to...
185 for (first = 0, i = 0 ; i < 64 ; ++i)
188 // Try to read the direction
190 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
191 if ((fd = open (fName, O_RDONLY)) == -1)
197 printf ("GPIO Pins exported:\n") ;
200 printf ("%4d: ", i) ;
202 if ((l = read (fd, buf, 16)) == 0)
203 sprintf (buf, "%s", "?") ;
206 if ((buf [strlen (buf) - 1]) == '\n')
207 buf [strlen (buf) - 1] = 0 ;
209 printf ("%-3s", buf) ;
213 // Try to Read the value
215 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
216 if ((fd = open (fName, O_RDONLY)) == -1)
218 printf ("No Value file (huh?)\n") ;
222 if ((l = read (fd, buf, 16)) == 0)
223 sprintf (buf, "%s", "?") ;
226 if ((buf [strlen (buf) - 1]) == '\n')
227 buf [strlen (buf) - 1] = 0 ;
229 printf (" %s", buf) ;
231 // Read any edge trigger file
233 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
234 if ((fd = open (fName, O_RDONLY)) == -1)
240 if ((l = read (fd, buf, 16)) == 0)
241 sprintf (buf, "%s", "?") ;
244 if ((buf [strlen (buf) - 1]) == '\n')
245 buf [strlen (buf) - 1] = 0 ;
247 printf (" %-8s\n", buf) ;
256 * gpio export pin mode
257 * This uses the /sys/class/gpio device interface.
258 *********************************************************************************
261 void doExport (int argc, char *argv [])
270 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
274 pin = atoi (argv [2]) ;
278 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
280 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
284 fprintf (fd, "%d\n", pin) ;
287 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
288 if ((fd = fopen (fName, "w")) == NULL)
290 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
294 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
295 fprintf (fd, "in\n") ;
296 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
297 fprintf (fd, "out\n") ;
300 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
306 // Change ownership so the current user can actually use it!
308 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
309 changeOwner (argv [0], fName) ;
311 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
312 changeOwner (argv [0], fName) ;
320 * Easy access to changing the edge trigger on a GPIO pin
321 * This uses the /sys/class/gpio device interface.
322 *********************************************************************************
325 void doEdge (int argc, char *argv [])
336 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
340 pin = atoi (argv [2]) ;
344 // Export the pin and set direction to input
346 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
348 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
352 fprintf (fd, "%d\n", pin) ;
355 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
356 if ((fd = fopen (fName, "w")) == NULL)
358 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
362 fprintf (fd, "in\n") ;
365 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
366 if ((fd = fopen (fName, "w")) == NULL)
368 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
372 /**/ if (strcasecmp (mode, "none") == 0)
373 fprintf (fd, "none\n") ;
374 else if (strcasecmp (mode, "rising") == 0)
375 fprintf (fd, "rising\n") ;
376 else if (strcasecmp (mode, "falling") == 0)
377 fprintf (fd, "falling\n") ;
378 else if (strcasecmp (mode, "both") == 0)
379 fprintf (fd, "both\n") ;
382 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
386 // Change ownership so the current user can actually use it!
391 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
392 if (chown (fName, uid, gid) != 0)
394 fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
398 // Also change ownership of the edge file
400 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
401 if (chown (fName, uid, gid) != 0)
403 fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
414 * This uses the /sys/class/gpio device interface.
415 *********************************************************************************
418 void doUnexport (int argc, char *argv [])
425 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
429 pin = atoi (argv [2]) ;
431 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
433 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
437 fprintf (fd, "%d\n", pin) ;
445 * Un-Export all the GPIO pins.
446 * This uses the /sys/class/gpio device interface.
447 *********************************************************************************
450 void doUnexportall (int argc, char *argv [])
455 for (pin = 0 ; pin < 63 ; ++pin)
457 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
459 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
462 fprintf (fd, "%d\n", pin) ;
470 * gpio mode pin mode ...
471 *********************************************************************************
474 void doMode (int argc, char *argv [])
481 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
485 pin = atoi (argv [2]) ;
487 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
492 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
493 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
494 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
495 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
496 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
497 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
500 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
508 * gpio drive group value
509 *********************************************************************************
512 void doPadDrive (int argc, char *argv [])
518 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
522 group = atoi (argv [2]) ;
523 val = atoi (argv [3]) ;
525 if ((group < 0) || (group > 2))
527 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
531 if ((val < 0) || (val > 7))
533 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
537 setPadDrive (group, val) ;
543 * gpio write pin value
544 *********************************************************************************
547 void doWrite (int argc, char *argv [])
553 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
557 pin = atoi (argv [2]) ;
559 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
562 val = atoi (argv [3]) ;
565 digitalWrite (pin, LOW) ;
567 digitalWrite (pin, HIGH) ;
573 * Read a pin and return the value
574 *********************************************************************************
577 void doRead (int argc, char *argv [])
583 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
587 pin = atoi (argv [2]) ;
589 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
595 val = digitalRead (pin) ;
597 printf ("%s\n", val == 0 ? "0" : "1") ;
603 * Output a PWM value on a pin
604 *********************************************************************************
607 void doPwm (int argc, char *argv [])
613 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
617 pin = atoi (argv [2]) ;
619 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
622 val = atoi (argv [3]) ;
624 pwmWrite (pin, val) ;
629 * doPwmMode: doPwmRange:
630 * Change the PWM mode and Range values
631 *********************************************************************************
634 static void doPwmMode (int mode)
639 static void doPwmRange (int argc, char *argv [])
645 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
649 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
653 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
657 pwmSetRange (range) ;
664 *********************************************************************************
667 int main (int argc, char *argv [])
673 fprintf (stderr, "%s: %s\n", argv [0], usage) ;
677 if (strcasecmp (argv [1], "-v") == 0)
679 printf ("gpio version: %s\n", VERSION) ;
680 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
681 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
682 printf ("For details type: %s -warranty\n", argv [0]) ;
686 if (strcasecmp (argv [1], "-warranty") == 0)
688 printf ("gpio version: %s\n", VERSION) ;
689 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
691 printf (" This program is free software; you can redistribute it and/or modify\n") ;
692 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
693 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
694 printf (" (at your option) any later version.\n") ;
696 printf (" This program is distributed in the hope that it will be useful,\n") ;
697 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
698 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
699 printf (" GNU Lesser General Public License for more details.\n") ;
701 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
702 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
709 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
713 // Initial test for /sys/class/gpio operations:
715 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports () ; return 0 ; }
716 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
717 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
718 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
719 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
720 else if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
722 // Check for -g argument
724 if (strcasecmp (argv [1], "-g") == 0)
726 if (wiringPiSetupGpio () == -1)
728 fprintf (stderr, "%s: Unable to initialise GPIO in GPIO mode.\n", argv [0]) ;
732 for (i = 2 ; i < argc ; ++i)
733 argv [i - 1] = argv [i] ;
735 wpMode = WPI_MODE_GPIO ;
738 // Check for -p argument for PiFace
740 else if (strcasecmp (argv [1], "-p") == 0)
742 if (wiringPiSetupPiFaceForGpioProg () == -1)
744 fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
748 for (i = 2 ; i < argc ; ++i)
749 argv [i - 1] = argv [i] ;
751 wpMode = WPI_MODE_PIFACE ;
754 // Default to wiringPi mode
758 if (wiringPiSetup () == -1)
760 fprintf (stderr, "%s: Unable to initialise GPIO in wiringPi mode\n", argv [0]) ;
763 wpMode = WPI_MODE_PINS ;
766 // Check for PWM operations
768 if (wpMode != WPI_MODE_PIFACE)
770 if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
771 if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
772 if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
775 // Check for wiring commands
777 /**/ if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
778 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
779 else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
780 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
781 else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
784 fprintf (stderr, "%s: Unknown command: %s. (read/write/pwm/mode/drive expected)\n", argv [0], argv [1]) ;