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 load spi/i2c" ;
55 * Change the ownership of the file to the real userId of the calling
56 * program so we can access it.
57 *********************************************************************************
60 static void changeOwner (char *cmd, char *file)
62 uid_t uid = getuid () ;
63 uid_t gid = getgid () ;
65 if (chown (file, uid, gid) != 0)
67 if (errno == ENOENT) // Warn that it's not there
68 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
71 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
80 * Return true/false if the supplied module is loaded
81 *********************************************************************************
84 static int moduleLoaded (char *modName)
86 int len = strlen (modName) ;
88 FILE *fd = fopen ("/proc/modules", "r") ;
93 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
97 while (fgets (line, 80, fd) != NULL)
99 if (strncmp (line, modName, len) != 0)
114 * Load either the spi or i2c modules and change device ownerships, etc.
115 *********************************************************************************
118 static void _doLoadUsage (char *argv [])
120 fprintf (stderr, "Usage: %s load <spi/i2c>\n", argv [0]) ;
124 static void doLoad (int argc, char *argv [])
128 char *file1, *file2 ;
131 _doLoadUsage (argv) ;
133 /**/ if (strcasecmp (argv [2], "spi") == 0)
135 module = "spi_bcm2708" ;
136 file1 = "/dev/spidev0.0" ;
137 file2 = "/dev/spidev0.1" ;
139 else if (strcasecmp (argv [2], "i2c") == 0)
141 module = "i2c_bcm2708" ;
142 file1 = "/dev/i2c-0" ;
143 file2 = "/dev/i2c-1" ;
146 _doLoadUsage (argv) ;
148 if (!moduleLoaded (module))
150 sprintf (cmd, "modprobe %s", module) ;
154 if (!moduleLoaded (module))
156 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
160 sleep (1) ; // To let things get settled
162 changeOwner (argv [0], file1) ;
163 changeOwner (argv [0], file2) ;
170 * List all GPIO exports
171 *********************************************************************************
174 void doExports (void)
181 // Rather crude, but who knows what others are up to...
183 for (first = 0, i = 0 ; i < 64 ; ++i)
186 // Try to read the direction
188 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
189 if ((fd = open (fName, O_RDONLY)) == -1)
195 printf ("GPIO Pins exported:\n") ;
198 printf ("%4d: ", i) ;
200 if ((l = read (fd, buf, 16)) == 0)
201 sprintf (buf, "%s", "?") ;
204 if ((buf [strlen (buf) - 1]) == '\n')
205 buf [strlen (buf) - 1] = 0 ;
207 printf ("%-3s", buf) ;
211 // Try to Read the value
213 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
214 if ((fd = open (fName, O_RDONLY)) == -1)
216 printf ("No Value file (huh?)\n") ;
220 if ((l = read (fd, buf, 16)) == 0)
221 sprintf (buf, "%s", "?") ;
224 if ((buf [strlen (buf) - 1]) == '\n')
225 buf [strlen (buf) - 1] = 0 ;
227 printf (" %s", buf) ;
229 // Read any edge trigger file
231 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
232 if ((fd = open (fName, O_RDONLY)) == -1)
238 if ((l = read (fd, buf, 16)) == 0)
239 sprintf (buf, "%s", "?") ;
242 if ((buf [strlen (buf) - 1]) == '\n')
243 buf [strlen (buf) - 1] = 0 ;
245 printf (" %-8s\n", buf) ;
254 * gpio export pin mode
255 * This uses the /sys/class/gpio device interface.
256 *********************************************************************************
259 void doExport (int argc, char *argv [])
268 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
272 pin = atoi (argv [2]) ;
276 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
278 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
282 fprintf (fd, "%d\n", pin) ;
285 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
286 if ((fd = fopen (fName, "w")) == NULL)
288 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
292 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
293 fprintf (fd, "in\n") ;
294 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
295 fprintf (fd, "out\n") ;
298 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
304 // Change ownership so the current user can actually use it!
306 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
307 changeOwner (argv [0], fName) ;
309 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
310 changeOwner (argv [0], fName) ;
318 * Easy access to changing the edge trigger on a GPIO pin
319 * This uses the /sys/class/gpio device interface.
320 *********************************************************************************
323 void doEdge (int argc, char *argv [])
334 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
338 pin = atoi (argv [2]) ;
342 // Export the pin and set direction to input
344 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
346 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
350 fprintf (fd, "%d\n", pin) ;
353 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
354 if ((fd = fopen (fName, "w")) == NULL)
356 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
360 fprintf (fd, "in\n") ;
363 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
364 if ((fd = fopen (fName, "w")) == NULL)
366 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
370 /**/ if (strcasecmp (mode, "none") == 0)
371 fprintf (fd, "none\n") ;
372 else if (strcasecmp (mode, "rising") == 0)
373 fprintf (fd, "rising\n") ;
374 else if (strcasecmp (mode, "falling") == 0)
375 fprintf (fd, "falling\n") ;
376 else if (strcasecmp (mode, "both") == 0)
377 fprintf (fd, "both\n") ;
380 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
384 // Change ownership so the current user can actually use it!
389 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
390 if (chown (fName, uid, gid) != 0)
392 fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
396 // Also change ownership of the edge file
398 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
399 if (chown (fName, uid, gid) != 0)
401 fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
412 * This uses the /sys/class/gpio device interface.
413 *********************************************************************************
416 void doUnexport (int argc, char *argv [])
423 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
427 pin = atoi (argv [2]) ;
429 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
431 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
435 fprintf (fd, "%d\n", pin) ;
443 * Un-Export all the GPIO pins.
444 * This uses the /sys/class/gpio device interface.
445 *********************************************************************************
448 void doUnexportall (int argc, char *argv [])
453 for (pin = 0 ; pin < 63 ; ++pin)
455 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
457 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
460 fprintf (fd, "%d\n", pin) ;
468 * gpio mode pin mode ...
469 *********************************************************************************
472 void doMode (int argc, char *argv [])
479 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
483 pin = atoi (argv [2]) ;
485 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
490 /**/ if (strcasecmp (mode, "in") == 0)
491 pinMode (pin, INPUT) ;
492 else if (strcasecmp (mode, "out") == 0)
493 pinMode (pin, OUTPUT) ;
494 else if (strcasecmp (mode, "pwm") == 0)
495 pinMode (pin, PWM_OUTPUT) ;
496 else if (strcasecmp (mode, "up") == 0)
497 pullUpDnControl (pin, PUD_UP) ;
498 else if (strcasecmp (mode, "down") == 0)
499 pullUpDnControl (pin, PUD_DOWN) ;
500 else if (strcasecmp (mode, "tri") == 0)
501 pullUpDnControl (pin, PUD_OFF) ;
504 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
512 * gpio drive group value
513 *********************************************************************************
516 void doPadDrive (int argc, char *argv [])
522 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
526 group = atoi (argv [2]) ;
527 val = atoi (argv [3]) ;
529 if ((group < 0) || (group > 2))
531 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
535 if ((val < 0) || (val > 7))
537 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
541 setPadDrive (group, val) ;
547 * gpio write pin value
548 *********************************************************************************
551 void doWrite (int argc, char *argv [])
557 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
561 pin = atoi (argv [2]) ;
563 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
566 val = atoi (argv [3]) ;
569 digitalWrite (pin, LOW) ;
571 digitalWrite (pin, HIGH) ;
577 * Read a pin and return the value
578 *********************************************************************************
581 void doRead (int argc, char *argv [])
587 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
591 pin = atoi (argv [2]) ;
593 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
599 val = digitalRead (pin) ;
601 printf ("%s\n", val == 0 ? "0" : "1") ;
607 * Output a PWM value on a pin
608 *********************************************************************************
611 void doPwm (int argc, char *argv [])
617 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
621 pin = atoi (argv [2]) ;
623 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
626 val = atoi (argv [3]) ;
628 pwmWrite (pin, val) ;
635 *********************************************************************************
638 int main (int argc, char *argv [])
644 fprintf (stderr, "%s: %s\n", argv [0], usage) ;
648 if (strcasecmp (argv [1], "-v") == 0)
650 printf ("gpio version: %s\n", VERSION) ;
651 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
652 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
653 printf ("For details type: %s -warranty\n", argv [0]) ;
657 if (strcasecmp (argv [1], "-warranty") == 0)
659 printf ("gpio version: %s\n", VERSION) ;
660 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
662 printf (" This program is free software; you can redistribute it and/or modify\n") ;
663 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
664 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
665 printf (" (at your option) any later version.\n") ;
667 printf (" This program is distributed in the hope that it will be useful,\n") ;
668 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
669 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
670 printf (" GNU Lesser General Public License for more details.\n") ;
672 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
673 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
680 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
684 // Initial test for /sys/class/gpio operations:
686 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports () ; return 0 ; }
687 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
688 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
689 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
690 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
691 else if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
693 // Check for -g argument
695 if (strcasecmp (argv [1], "-g") == 0)
697 if (wiringPiSetupGpio () == -1)
699 fprintf (stderr, "%s: Unable to initialise GPIO in GPIO mode.\n", argv [0]) ;
703 for (i = 2 ; i < argc ; ++i)
704 argv [i - 1] = argv [i] ;
706 wpMode = WPI_MODE_GPIO ;
709 // Check for -p argument for PiFace
711 else if (strcasecmp (argv [1], "-p") == 0)
713 if (wiringPiSetupPiFaceForGpioProg () == -1)
715 fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
719 for (i = 2 ; i < argc ; ++i)
720 argv [i - 1] = argv [i] ;
722 wpMode = WPI_MODE_PIFACE ;
725 // Default to wiringPi mode
729 if (wiringPiSetup () == -1)
731 fprintf (stderr, "%s: Unable to initialise GPIO in wiringPi mode\n", argv [0]) ;
734 wpMode = WPI_MODE_PINS ;
737 // Check for wiring commands
739 /**/ if (strcasecmp (argv [1], "write" ) == 0)
740 doWrite (argc, argv) ;
741 else if (strcasecmp (argv [1], "read" ) == 0)
742 doRead (argc, argv) ;
743 else if (strcasecmp (argv [1], "mode" ) == 0)
744 doMode (argc, argv) ;
745 else if (strcasecmp (argv [1], "pwm" ) == 0)
747 else if (strcasecmp (argv [1], "drive" ) == 0)
748 doPadDrive (argc, argv) ;
751 fprintf (stderr, "%s: Unknown command: %s. (read/write/pwm/mode/drive expected)\n", argv [0], argv [1]) ;