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"
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 [])
138 char *file1, *file2 ;
141 _doLoadUsage (argv) ;
143 /**/ if (strcasecmp (argv [2], "spi") == 0)
145 module = "spi_bcm2708" ;
146 file1 = "/dev/spidev0.0" ;
147 file2 = "/dev/spidev0.1" ;
149 else if (strcasecmp (argv [2], "i2c") == 0)
151 module = "i2c_bcm2708" ;
152 file1 = "/dev/i2c-0" ;
153 file2 = "/dev/i2c-1" ;
156 _doLoadUsage (argv) ;
158 if (!moduleLoaded (module))
160 sprintf (cmd, "modprobe %s", module) ;
164 if (!moduleLoaded (module))
166 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
170 sleep (1) ; // To let things get settled
172 changeOwner (argv [0], file1) ;
173 changeOwner (argv [0], file2) ;
179 * Read all the GPIO pins
180 *********************************************************************************
183 static char *pinNames [] =
208 static void doReadall (void)
212 printf ("+----------+------+--------+-------+\n") ;
213 printf ("| wiringPi | GPIO | Name | Value |\n") ;
214 printf ("+----------+------+--------+-------+\n") ;
216 for (pin = 0 ; pin < NUM_PINS ; ++pin)
217 printf ("| %6d | %3d | %s | %s |\n",
218 pin, wpiPinToGpio (pin),
220 digitalRead (pin) == HIGH ? "High" : "Low ") ;
222 printf ("+----------+------+--------+-------+\n") ;
224 if (piBoardRev () == 1)
227 for (pin = 17 ; pin <= 20 ; ++pin)
228 printf ("| %6d | %3d | %s | %s |\n",
229 pin, wpiPinToGpio (pin),
231 digitalRead (pin) == HIGH ? "High" : "Low ") ;
233 printf ("+----------+------+--------+-------+\n") ;
239 * List all GPIO exports
240 *********************************************************************************
243 static void doExports (int argc, char *argv [])
250 // Rather crude, but who knows what others are up to...
252 for (first = 0, i = 0 ; i < 64 ; ++i)
255 // Try to read the direction
257 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
258 if ((fd = open (fName, O_RDONLY)) == -1)
264 printf ("GPIO Pins exported:\n") ;
267 printf ("%4d: ", i) ;
269 if ((l = read (fd, buf, 16)) == 0)
270 sprintf (buf, "%s", "?") ;
273 if ((buf [strlen (buf) - 1]) == '\n')
274 buf [strlen (buf) - 1] = 0 ;
276 printf ("%-3s", buf) ;
280 // Try to Read the value
282 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
283 if ((fd = open (fName, O_RDONLY)) == -1)
285 printf ("No Value file (huh?)\n") ;
289 if ((l = read (fd, buf, 16)) == 0)
290 sprintf (buf, "%s", "?") ;
293 if ((buf [strlen (buf) - 1]) == '\n')
294 buf [strlen (buf) - 1] = 0 ;
296 printf (" %s", buf) ;
298 // Read any edge trigger file
300 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
301 if ((fd = open (fName, O_RDONLY)) == -1)
307 if ((l = read (fd, buf, 16)) == 0)
308 sprintf (buf, "%s", "?") ;
311 if ((buf [strlen (buf) - 1]) == '\n')
312 buf [strlen (buf) - 1] = 0 ;
314 printf (" %-8s\n", buf) ;
323 * gpio export pin mode
324 * This uses the /sys/class/gpio device interface.
325 *********************************************************************************
328 void doExport (int argc, char *argv [])
337 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
341 pin = atoi (argv [2]) ;
345 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
347 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
351 fprintf (fd, "%d\n", pin) ;
354 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
355 if ((fd = fopen (fName, "w")) == NULL)
357 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
361 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
362 fprintf (fd, "in\n") ;
363 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
364 fprintf (fd, "out\n") ;
367 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
373 // Change ownership so the current user can actually use it!
375 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
376 changeOwner (argv [0], fName) ;
378 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
379 changeOwner (argv [0], fName) ;
387 * Easy access to changing the edge trigger on a GPIO pin
388 * This uses the /sys/class/gpio device interface.
389 *********************************************************************************
392 void doEdge (int argc, char *argv [])
401 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
405 pin = atoi (argv [2]) ;
408 // Export the pin and set direction to input
410 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
412 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
416 fprintf (fd, "%d\n", pin) ;
419 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
420 if ((fd = fopen (fName, "w")) == NULL)
422 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
426 fprintf (fd, "in\n") ;
429 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
430 if ((fd = fopen (fName, "w")) == NULL)
432 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
436 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
437 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
438 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
439 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
442 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
446 // Change ownership of the value and edge files, so the current user can actually use it!
448 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
449 changeOwner (argv [0], fName) ;
451 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
452 changeOwner (argv [0], fName) ;
461 * This uses the /sys/class/gpio device interface.
462 *********************************************************************************
465 void doUnexport (int argc, char *argv [])
472 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
476 pin = atoi (argv [2]) ;
478 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
480 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
484 fprintf (fd, "%d\n", pin) ;
492 * Un-Export all the GPIO pins.
493 * This uses the /sys/class/gpio device interface.
494 *********************************************************************************
497 void doUnexportall (int argc, char *argv [])
502 for (pin = 0 ; pin < 63 ; ++pin)
504 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
506 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
509 fprintf (fd, "%d\n", pin) ;
517 * gpio mode pin mode ...
518 *********************************************************************************
521 void doMode (int argc, char *argv [])
528 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
532 pin = atoi (argv [2]) ;
534 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
539 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
540 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
541 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
542 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
543 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
544 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
547 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
555 * gpio drive group value
556 *********************************************************************************
559 static void doPadDrive (int argc, char *argv [])
565 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
569 group = atoi (argv [2]) ;
570 val = atoi (argv [3]) ;
572 if ((group < 0) || (group > 2))
574 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
578 if ((val < 0) || (val > 7))
580 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
584 setPadDrive (group, val) ;
590 * gpio gbw channel value
591 *********************************************************************************
594 static void doGbw (int argc, char *argv [])
600 fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
604 channel = atoi (argv [2]) ;
605 value = atoi (argv [3]) ;
607 if ((channel < 0) || (channel > 1))
609 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
613 if ((value < 0) || (value > 1023))
615 fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
619 if (gertboardSPISetup () == -1)
621 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
625 gertboardAnalogWrite (channel, value) ;
632 *********************************************************************************
635 static void doGbr (int argc, char *argv [])
641 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
645 channel = atoi (argv [2]) ;
647 if ((channel < 0) || (channel > 1))
649 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
653 if (gertboardSPISetup () == -1)
655 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
659 printf ("%d\n",gertboardAnalogRead (channel)) ;
666 * gpio write pin value
667 *********************************************************************************
670 static void doWrite (int argc, char *argv [])
676 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
680 pin = atoi (argv [2]) ;
682 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
685 val = atoi (argv [3]) ;
688 digitalWrite (pin, LOW) ;
690 digitalWrite (pin, HIGH) ;
696 * Read a pin and return the value
697 *********************************************************************************
700 void doRead (int argc, char *argv [])
706 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
710 pin = atoi (argv [2]) ;
712 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
718 val = digitalRead (pin) ;
720 printf ("%s\n", val == 0 ? "0" : "1") ;
726 * Output a PWM value on a pin
727 *********************************************************************************
730 void doPwm (int argc, char *argv [])
736 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
740 pin = atoi (argv [2]) ;
742 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
745 val = atoi (argv [3]) ;
747 pwmWrite (pin, val) ;
752 * doPwmMode: doPwmRange: doPwmClock:
753 * Change the PWM mode, range and clock divider values
754 *********************************************************************************
757 static void doPwmMode (int mode)
762 static void doPwmRange (int argc, char *argv [])
768 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
772 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
776 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
780 pwmSetRange (range) ;
783 static void doPwmClock (int argc, char *argv [])
789 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
793 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
795 if ((clock < 1) || (clock > 4095))
797 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
801 pwmSetClock (clock) ;
808 *********************************************************************************
811 int main (int argc, char *argv [])
817 fprintf (stderr, "%s\n", usage) ;
821 if (strcasecmp (argv [1], "-h") == 0)
823 printf ("%s: %s\n", argv [0], usage) ;
827 if (strcasecmp (argv [1], "-v") == 0)
829 printf ("gpio version: %s\n", VERSION) ;
830 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
831 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
832 printf ("For details type: %s -warranty\n", argv [0]) ;
834 printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
838 if (strcasecmp (argv [1], "-warranty") == 0)
840 printf ("gpio version: %s\n", VERSION) ;
841 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
843 printf (" This program is free software; you can redistribute it and/or modify\n") ;
844 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
845 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
846 printf (" (at your option) any later version.\n") ;
848 printf (" This program is distributed in the hope that it will be useful,\n") ;
849 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
850 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
851 printf (" GNU Lesser General Public License for more details.\n") ;
853 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
854 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
861 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
865 // Initial test for /sys/class/gpio operations:
867 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
868 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
869 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
870 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
871 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
873 // Check for load command:
875 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
877 // Gertboard commands
879 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
880 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
882 // Check for -g argument
884 if (strcasecmp (argv [1], "-g") == 0)
886 if (wiringPiSetupGpio () == -1)
888 fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
892 for (i = 2 ; i < argc ; ++i)
893 argv [i - 1] = argv [i] ;
895 wpMode = WPI_MODE_GPIO ;
898 // Check for -p argument for PiFace
900 else if (strcasecmp (argv [1], "-p") == 0)
902 if (wiringPiSetupPiFaceForGpioProg () == -1)
904 fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
908 for (i = 2 ; i < argc ; ++i)
909 argv [i - 1] = argv [i] ;
911 wpMode = WPI_MODE_PIFACE ;
914 // Default to wiringPi mode
918 if (wiringPiSetup () == -1)
920 fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
923 wpMode = WPI_MODE_PINS ;
926 // Check for PWM or Pad Drive operations
928 if (wpMode != WPI_MODE_PIFACE)
930 if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
931 if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
932 if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
933 if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; }
934 if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
937 // Check for wiring commands
939 /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
940 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
941 else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ;
942 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
943 else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
946 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;