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/pwm/mode> ...\n"
50 " gpio [-p] <read/write/mode> ...\n"
51 " gpio export/edge/unexport/unexportall/exports ...\n"
52 " gpio drive <group> <value>\n"
53 " gpio pwm-bal/pwm-ms \n"
54 " gpio pwmr <range> \n"
55 " gpio pwmc <divider> \n"
56 " gpio load spi/i2c\n"
57 " gpio gbr <channel>\n"
58 " gpio gbw <channel> <value>" ; // No trailing newline needed here.
63 * Change the ownership of the file to the real userId of the calling
64 * program so we can access it.
65 *********************************************************************************
68 static void changeOwner (char *cmd, char *file)
70 uid_t uid = getuid () ;
71 uid_t gid = getgid () ;
73 if (chown (file, uid, gid) != 0)
75 if (errno == ENOENT) // Warn that it's not there
76 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
79 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
88 * Return true/false if the supplied module is loaded
89 *********************************************************************************
92 static int moduleLoaded (char *modName)
94 int len = strlen (modName) ;
96 FILE *fd = fopen ("/proc/modules", "r") ;
101 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
105 while (fgets (line, 80, fd) != NULL)
107 if (strncmp (line, modName, len) != 0)
122 * Load either the spi or i2c modules and change device ownerships, etc.
123 *********************************************************************************
126 static void _doLoadUsage (char *argv [])
128 fprintf (stderr, "Usage: %s load <spi/i2c>\n", argv [0]) ;
132 static void doLoad (int argc, char *argv [])
136 char *file1, *file2 ;
139 _doLoadUsage (argv) ;
141 /**/ if (strcasecmp (argv [2], "spi") == 0)
143 module = "spi_bcm2708" ;
144 file1 = "/dev/spidev0.0" ;
145 file2 = "/dev/spidev0.1" ;
147 else if (strcasecmp (argv [2], "i2c") == 0)
149 module = "i2c_bcm2708" ;
150 file1 = "/dev/i2c-0" ;
151 file2 = "/dev/i2c-1" ;
154 _doLoadUsage (argv) ;
156 if (!moduleLoaded (module))
158 sprintf (cmd, "modprobe %s", module) ;
162 if (!moduleLoaded (module))
164 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
168 sleep (1) ; // To let things get settled
170 changeOwner (argv [0], file1) ;
171 changeOwner (argv [0], file2) ;
178 * List all GPIO exports
179 *********************************************************************************
182 static void doExports (int argc, char *argv [])
189 // Rather crude, but who knows what others are up to...
191 for (first = 0, i = 0 ; i < 64 ; ++i)
194 // Try to read the direction
196 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
197 if ((fd = open (fName, O_RDONLY)) == -1)
203 printf ("GPIO Pins exported:\n") ;
206 printf ("%4d: ", i) ;
208 if ((l = read (fd, buf, 16)) == 0)
209 sprintf (buf, "%s", "?") ;
212 if ((buf [strlen (buf) - 1]) == '\n')
213 buf [strlen (buf) - 1] = 0 ;
215 printf ("%-3s", buf) ;
219 // Try to Read the value
221 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
222 if ((fd = open (fName, O_RDONLY)) == -1)
224 printf ("No Value file (huh?)\n") ;
228 if ((l = read (fd, buf, 16)) == 0)
229 sprintf (buf, "%s", "?") ;
232 if ((buf [strlen (buf) - 1]) == '\n')
233 buf [strlen (buf) - 1] = 0 ;
235 printf (" %s", buf) ;
237 // Read any edge trigger file
239 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
240 if ((fd = open (fName, O_RDONLY)) == -1)
246 if ((l = read (fd, buf, 16)) == 0)
247 sprintf (buf, "%s", "?") ;
250 if ((buf [strlen (buf) - 1]) == '\n')
251 buf [strlen (buf) - 1] = 0 ;
253 printf (" %-8s\n", buf) ;
262 * gpio export pin mode
263 * This uses the /sys/class/gpio device interface.
264 *********************************************************************************
267 void doExport (int argc, char *argv [])
276 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
280 pin = atoi (argv [2]) ;
284 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
286 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
290 fprintf (fd, "%d\n", pin) ;
293 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
294 if ((fd = fopen (fName, "w")) == NULL)
296 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
300 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
301 fprintf (fd, "in\n") ;
302 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
303 fprintf (fd, "out\n") ;
306 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
312 // Change ownership so the current user can actually use it!
314 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
315 changeOwner (argv [0], fName) ;
317 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
318 changeOwner (argv [0], fName) ;
326 * Easy access to changing the edge trigger on a GPIO pin
327 * This uses the /sys/class/gpio device interface.
328 *********************************************************************************
331 void doEdge (int argc, char *argv [])
340 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
344 pin = atoi (argv [2]) ;
347 // Export the pin and set direction to input
349 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
351 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
355 fprintf (fd, "%d\n", pin) ;
358 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
359 if ((fd = fopen (fName, "w")) == NULL)
361 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
365 fprintf (fd, "in\n") ;
368 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
369 if ((fd = fopen (fName, "w")) == NULL)
371 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
375 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
376 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
377 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
378 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
381 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
385 // Change ownership of the value and edge files, so the current user can actually use it!
387 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
388 changeOwner (argv [0], fName) ;
390 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
391 changeOwner (argv [0], fName) ;
400 * This uses the /sys/class/gpio device interface.
401 *********************************************************************************
404 void doUnexport (int argc, char *argv [])
411 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
415 pin = atoi (argv [2]) ;
417 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
419 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
423 fprintf (fd, "%d\n", pin) ;
431 * Un-Export all the GPIO pins.
432 * This uses the /sys/class/gpio device interface.
433 *********************************************************************************
436 void doUnexportall (int argc, char *argv [])
441 for (pin = 0 ; pin < 63 ; ++pin)
443 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
445 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
448 fprintf (fd, "%d\n", pin) ;
456 * gpio mode pin mode ...
457 *********************************************************************************
460 void doMode (int argc, char *argv [])
467 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
471 pin = atoi (argv [2]) ;
473 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
478 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
479 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
480 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
481 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
482 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
483 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
486 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
494 * gpio drive group value
495 *********************************************************************************
498 static void doPadDrive (int argc, char *argv [])
504 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
508 group = atoi (argv [2]) ;
509 val = atoi (argv [3]) ;
511 if ((group < 0) || (group > 2))
513 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
517 if ((val < 0) || (val > 7))
519 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
523 setPadDrive (group, val) ;
529 * gpio gbw channel value
530 *********************************************************************************
533 static void doGbw (int argc, char *argv [])
539 fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
543 channel = atoi (argv [2]) ;
544 value = atoi (argv [3]) ;
546 if ((channel < 0) || (channel > 1))
548 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
552 if ((value < 0) || (value > 1023))
554 fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
558 if (gertboardSPISetup () == -1)
560 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
564 gertboardAnalogWrite (channel, value) ;
571 *********************************************************************************
574 static void doGbr (int argc, char *argv [])
580 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
584 channel = atoi (argv [2]) ;
586 if ((channel < 0) || (channel > 1))
588 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
592 if (gertboardSPISetup () == -1)
594 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
598 printf ("%d\n",gertboardAnalogRead (channel)) ;
605 * gpio write pin value
606 *********************************************************************************
609 static void doWrite (int argc, char *argv [])
615 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
619 pin = atoi (argv [2]) ;
621 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
624 val = atoi (argv [3]) ;
627 digitalWrite (pin, LOW) ;
629 digitalWrite (pin, HIGH) ;
635 * Read a pin and return the value
636 *********************************************************************************
639 void doRead (int argc, char *argv [])
645 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
649 pin = atoi (argv [2]) ;
651 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
657 val = digitalRead (pin) ;
659 printf ("%s\n", val == 0 ? "0" : "1") ;
665 * Output a PWM value on a pin
666 *********************************************************************************
669 void doPwm (int argc, char *argv [])
675 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
679 pin = atoi (argv [2]) ;
681 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
684 val = atoi (argv [3]) ;
686 pwmWrite (pin, val) ;
691 * doPwmMode: doPwmRange: doPwmClock:
692 * Change the PWM mode, range and clock divider values
693 *********************************************************************************
696 static void doPwmMode (int mode)
701 static void doPwmRange (int argc, char *argv [])
707 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
711 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
715 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
719 pwmSetRange (range) ;
722 static void doPwmClock (int argc, char *argv [])
728 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
732 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
734 if ((clock < 1) || (clock > 4095))
736 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
740 pwmSetClock (clock) ;
747 *********************************************************************************
750 int main (int argc, char *argv [])
756 fprintf (stderr, "%s: %s\n", argv [0], usage) ;
760 if (strcasecmp (argv [1], "-h") == 0)
762 printf ("%s: %s\n", argv [0], usage) ;
766 if (strcasecmp (argv [1], "-v") == 0)
768 printf ("gpio version: %s\n", VERSION) ;
769 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
770 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
771 printf ("For details type: %s -warranty\n", argv [0]) ;
775 if (strcasecmp (argv [1], "-warranty") == 0)
777 printf ("gpio version: %s\n", VERSION) ;
778 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
780 printf (" This program is free software; you can redistribute it and/or modify\n") ;
781 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
782 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
783 printf (" (at your option) any later version.\n") ;
785 printf (" This program is distributed in the hope that it will be useful,\n") ;
786 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
787 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
788 printf (" GNU Lesser General Public License for more details.\n") ;
790 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
791 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
798 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
802 // Initial test for /sys/class/gpio operations:
804 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
805 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
806 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
807 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
808 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
810 // Check for drive or load commands:
812 if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
813 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
815 // Gertboard commands
817 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
818 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
820 // Check for -g argument
822 if (strcasecmp (argv [1], "-g") == 0)
824 if (wiringPiSetupGpio () == -1)
826 fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
830 for (i = 2 ; i < argc ; ++i)
831 argv [i - 1] = argv [i] ;
833 wpMode = WPI_MODE_GPIO ;
836 // Check for -p argument for PiFace
838 else if (strcasecmp (argv [1], "-p") == 0)
840 if (wiringPiSetupPiFaceForGpioProg () == -1)
842 fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
846 for (i = 2 ; i < argc ; ++i)
847 argv [i - 1] = argv [i] ;
849 wpMode = WPI_MODE_PIFACE ;
852 // Default to wiringPi mode
856 if (wiringPiSetup () == -1)
858 fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
861 wpMode = WPI_MODE_PINS ;
864 // Check for PWM operations
866 if (wpMode != WPI_MODE_PIFACE)
868 if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
869 if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
870 if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
871 if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; }
874 // Check for wiring commands
876 /**/ if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
877 else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ;
878 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
879 else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
882 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;