3 * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
5 * Copyright (c) 2012-2015 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 ***********************************************************************
34 #include <sys/types.h>
38 #include <wpiExtensions.h>
40 #include <gertboard.h>
45 extern int wiringPiDebug ;
47 // External functions I can't be bothered creating a separate .h file for:
49 extern void doReadall (void) ;
50 extern void doPins (void) ;
57 #define PI_USB_POWER_CONTROL 38
58 #define I2CDETECT "/usr/sbin/i2cdetect"
62 char *usage = "Usage: gpio -v\n"
64 " gpio [-g|-1] [-x extension:params] ...\n"
65 " gpio [-p] <read/write/wb> ...\n"
66 " gpio <read/write/aread/awritewb/pwm/clock/mode> ...\n"
67 " gpio readall/reset\n"
68 " gpio unexportall/exports\n"
69 " gpio export/edge/unexport ...\n"
70 " gpio wfi <pin> <mode>\n"
71 " gpio drive <group> <value>\n"
72 " gpio pwm-bal/pwm-ms \n"
73 " gpio pwmr <range> \n"
74 " gpio pwmc <divider> \n"
75 " gpio load spi/i2c\n"
76 " gpio unload spi/i2c\n"
77 " gpio i2cd/i2cdetect\n"
78 " gpio usbp high/low\n"
79 " gpio gbr <channel>\n"
80 " gpio gbw <channel> <value>" ; // No trailing newline needed here.
86 * Decode a pin "number" which can actually be a pin name to represent
87 * one of the Pi's on-board pins.
88 *********************************************************************************
91 static int decodePin (const char *str)
94 // The first case - see if it's a number:
96 if (isdigit (str [0]))
106 * Change the ownership of the file to the real userId of the calling
107 * program so we can access it.
108 *********************************************************************************
111 static void changeOwner (char *cmd, char *file)
113 uid_t uid = getuid () ;
114 uid_t gid = getgid () ;
116 if (chown (file, uid, gid) != 0)
118 if (errno == ENOENT) // Warn that it's not there
119 fprintf (stderr, "%s: Warning (not an error, do not report): File not present: %s\n", cmd, file) ;
121 fprintf (stderr, "%s: Warning (not an error): Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
128 * Return true/false if the supplied module is loaded
129 *********************************************************************************
132 static int moduleLoaded (char *modName)
134 int len = strlen (modName) ;
136 FILE *fd = fopen ("/proc/modules", "r") ;
141 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
145 while (fgets (line, 80, fd) != NULL)
147 if (strncmp (line, modName, len) != 0)
162 * Load either the spi or i2c modules and change device ownerships, etc.
163 *********************************************************************************
166 static void checkDevTree (char *argv [])
168 struct stat statBuf ;
170 if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
173 "%s: Unable to load/unload modules as this Pi has the device tree enabled.\n"
174 " You need to run the raspi-config program (as root) and select the\n"
175 " modules (SPI or I2C) that you wish to load/unload there and reboot.\n"
176 " There is more information here:\n"
177 " https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314\n", argv [0]) ;
182 static void _doLoadUsage (char *argv [])
184 fprintf (stderr, "Usage: %s load <spi/i2c> [I2C baudrate in Kb/sec]\n", argv [0]) ;
188 static void doLoad (int argc, char *argv [])
190 char *module1, *module2 ;
192 char *file1, *file2 ;
193 char args1 [32], args2 [32] ;
195 checkDevTree (argv) ;
198 _doLoadUsage (argv) ;
200 args1 [0] = args2 [0] = 0 ;
202 /**/ if (strcasecmp (argv [2], "spi") == 0)
205 module2 = "spi_bcm2708" ;
206 file1 = "/dev/spidev0.0" ;
207 file2 = "/dev/spidev0.1" ;
210 fprintf (stderr, "%s: Unable to set the buffer size now. Load aborted. Please see the man page.\n", argv [0]) ;
214 _doLoadUsage (argv) ;
216 else if (strcasecmp (argv [2], "i2c") == 0)
218 module1 = "i2c_dev" ;
219 module2 = "i2c_bcm2708" ;
220 file1 = "/dev/i2c-0" ;
221 file2 = "/dev/i2c-1" ;
223 sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
225 _doLoadUsage (argv) ;
228 _doLoadUsage (argv) ;
230 if (!moduleLoaded (module1))
232 sprintf (cmd, "/sbin/modprobe %s%s", module1, args1) ;
236 if (!moduleLoaded (module2))
238 sprintf (cmd, "/sbin/modprobe %s%s", module2, args2) ;
242 if (!moduleLoaded (module2))
244 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
248 sleep (1) ; // To let things get settled
250 changeOwner (argv [0], file1) ;
251 changeOwner (argv [0], file2) ;
257 * Un-Load either the spi or i2c modules and change device ownerships, etc.
258 *********************************************************************************
261 static void _doUnLoadUsage (char *argv [])
263 fprintf (stderr, "Usage: %s unload <spi/i2c>\n", argv [0]) ;
267 static void doUnLoad (int argc, char *argv [])
269 char *module1, *module2 ;
272 checkDevTree (argv) ;
275 _doUnLoadUsage (argv) ;
277 /**/ if (strcasecmp (argv [2], "spi") == 0)
280 module2 = "spi_bcm2708" ;
282 else if (strcasecmp (argv [2], "i2c") == 0)
284 module1 = "i2c_dev" ;
285 module2 = "i2c_bcm2708" ;
288 _doUnLoadUsage (argv) ;
290 if (moduleLoaded (module1))
292 sprintf (cmd, "/sbin/rmmod %s", module1) ;
296 if (moduleLoaded (module2))
298 sprintf (cmd, "/sbin/rmmod %s", module2) ;
306 * Run the i2cdetect command with the right runes for this Pi revision
307 *********************************************************************************
310 static void doI2Cdetect (int argc, char *argv [])
312 int port = piBoardRev () == 1 ? 0 : 1 ;
314 struct stat statBuf ;
316 if (stat (I2CDETECT, &statBuf) < 0)
318 fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
322 if (!moduleLoaded ("i2c_dev"))
324 fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
328 sprintf (command, "%s -y %d", I2CDETECT, port) ;
329 if (system (command) < 0)
330 fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
337 * List all GPIO exports
338 *********************************************************************************
341 static void doExports (int argc, char *argv [])
348 for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
351 // Try to read the direction
353 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
354 if ((fd = open (fName, O_RDONLY)) == -1)
360 printf ("GPIO Pins exported:\n") ;
363 printf ("%4d: ", i) ;
365 if ((l = read (fd, buf, 16)) == 0)
366 sprintf (buf, "%s", "?") ;
369 if ((buf [strlen (buf) - 1]) == '\n')
370 buf [strlen (buf) - 1] = 0 ;
372 printf ("%-3s", buf) ;
376 // Try to Read the value
378 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
379 if ((fd = open (fName, O_RDONLY)) == -1)
381 printf ("No Value file (huh?)\n") ;
385 if ((l = read (fd, buf, 16)) == 0)
386 sprintf (buf, "%s", "?") ;
389 if ((buf [strlen (buf) - 1]) == '\n')
390 buf [strlen (buf) - 1] = 0 ;
392 printf (" %s", buf) ;
394 // Read any edge trigger file
396 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
397 if ((fd = open (fName, O_RDONLY)) == -1)
403 if ((l = read (fd, buf, 16)) == 0)
404 sprintf (buf, "%s", "?") ;
407 if ((buf [strlen (buf) - 1]) == '\n')
408 buf [strlen (buf) - 1] = 0 ;
410 printf (" %-8s\n", buf) ;
419 * gpio export pin mode
420 * This uses the /sys/class/gpio device interface.
421 *********************************************************************************
424 void doExport (int argc, char *argv [])
433 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
437 pin = atoi (argv [2]) ;
441 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
443 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
447 fprintf (fd, "%d\n", pin) ;
450 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
451 if ((fd = fopen (fName, "w")) == NULL)
453 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
457 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
458 fprintf (fd, "in\n") ;
459 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
460 fprintf (fd, "out\n") ;
461 else if ((strcasecmp (mode, "high") == 0) || (strcasecmp (mode, "up") == 0))
462 fprintf (fd, "high\n") ;
463 else if ((strcasecmp (mode, "low") == 0) || (strcasecmp (mode, "down") == 0))
464 fprintf (fd, "low\n") ;
467 fprintf (stderr, "%s: Invalid mode: %s. Should be in, out, high or low\n", argv [1], mode) ;
473 // Change ownership so the current user can actually use it
475 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
476 changeOwner (argv [0], fName) ;
478 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
479 changeOwner (argv [0], fName) ;
487 * Wait for Interrupt on a given pin.
488 * Slight cheat here - it's easier to actually use ISR now (which calls
489 * gpio to set the pin modes!) then we simply sleep, and expect the thread
490 * to exit the program. Crude but effective.
491 *********************************************************************************
494 static void wfi (void)
497 void doWfi (int argc, char *argv [])
503 fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
507 pin = atoi (argv [2]) ;
509 /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
510 else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
511 else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
514 fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
518 if (wiringPiISR (pin, mode, &wfi) < 0)
520 fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
533 * Easy access to changing the edge trigger on a GPIO pin
534 * This uses the /sys/class/gpio device interface.
535 *********************************************************************************
538 void doEdge (int argc, char *argv [])
547 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
551 pin = atoi (argv [2]) ;
554 // Export the pin and set direction to input
556 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
558 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
562 fprintf (fd, "%d\n", pin) ;
565 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
566 if ((fd = fopen (fName, "w")) == NULL)
568 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
572 fprintf (fd, "in\n") ;
575 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
576 if ((fd = fopen (fName, "w")) == NULL)
578 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
582 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
583 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
584 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
585 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
588 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
592 // Change ownership of the value and edge files, so the current user can actually use it!
594 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
595 changeOwner (argv [0], fName) ;
597 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
598 changeOwner (argv [0], fName) ;
607 * This uses the /sys/class/gpio device interface.
608 *********************************************************************************
611 void doUnexport (int argc, char *argv [])
618 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
622 pin = atoi (argv [2]) ;
624 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
626 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
630 fprintf (fd, "%d\n", pin) ;
638 * Un-Export all the GPIO pins.
639 * This uses the /sys/class/gpio device interface.
640 *********************************************************************************
643 void doUnexportall (char *progName)
648 for (pin = 0 ; pin < 63 ; ++pin)
650 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
652 fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
655 fprintf (fd, "%d\n", pin) ;
663 * Reset the GPIO pins - as much as we can do
664 *********************************************************************************
667 static void doReset (char *progName)
669 printf ("GPIO Reset is dangerous and has been removed from the gpio command.\n") ;
670 printf (" - Please write a shell-script to reset the GPIO pins into the state\n") ;
671 printf (" that you need them in for your applications.\n") ;
677 * gpio mode pin mode ...
678 *********************************************************************************
681 void doMode (int argc, char *argv [])
688 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
692 pin = atoi (argv [2]) ;
696 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
697 else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
698 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
699 else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
700 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
701 else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ;
702 else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
703 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
704 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
705 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
706 else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
707 else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
708 else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
709 else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
710 else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
711 else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
712 else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
715 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
723 * gpio drive group value
724 *********************************************************************************
727 static void doPadDrive (int argc, char *argv [])
733 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
737 group = atoi (argv [2]) ;
738 val = atoi (argv [3]) ;
740 if ((group < 0) || (group > 2))
742 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
746 if ((val < 0) || (val > 7))
748 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
752 setPadDrive (group, val) ;
758 * Control USB Power - High (1.2A) or Low (600mA)
760 *********************************************************************************
763 static void doUsbP (int argc, char *argv [])
765 int model, rev, mem, maker, overVolted ;
769 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
773 // Make sure we're on a B+
775 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
777 if (model != PI_MODEL_BP)
779 fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ;
783 // Need to force BCM_GPIO mode:
785 wiringPiSetupGpio () ;
787 if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0))
789 digitalWrite (PI_USB_POWER_CONTROL, 1) ;
790 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
791 printf ("Switched to HIGH current USB (1.2A)\n") ;
795 if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0))
797 digitalWrite (PI_USB_POWER_CONTROL, 0) ;
798 pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
799 printf ("Switched to LOW current USB (600mA)\n") ;
803 fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
810 * gpio gbw channel value
811 * Gertboard Write - To the Analog output
812 *********************************************************************************
815 static void doGbw (int argc, char *argv [])
821 fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
825 channel = atoi (argv [2]) ;
826 value = atoi (argv [3]) ;
828 if ((channel < 0) || (channel > 1))
830 fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
834 if ((value < 0) || (value > 255))
836 fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
840 if (gertboardAnalogSetup (64) < 0)
842 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
846 analogWrite (64 + channel, value) ;
853 * From the analog input
854 *********************************************************************************
857 static void doGbr (int argc, char *argv [])
863 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
867 channel = atoi (argv [2]) ;
869 if ((channel < 0) || (channel > 1))
871 fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
875 if (gertboardAnalogSetup (64) < 0)
877 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
881 printf ("%d\n", analogRead (64 + channel)) ;
887 * gpio write pin value
888 *********************************************************************************
891 static void doWrite (int argc, char *argv [])
897 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
901 pin = atoi (argv [2]) ;
903 /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
905 else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
908 val = atoi (argv [3]) ;
911 digitalWrite (pin, LOW) ;
913 digitalWrite (pin, HIGH) ;
919 * gpio awrite pin value
920 *********************************************************************************
923 static void doAwrite (int argc, char *argv [])
929 fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
933 pin = atoi (argv [2]) ;
935 val = atoi (argv [3]) ;
937 analogWrite (pin, val) ;
944 *********************************************************************************
947 static void doWriteByte (int argc, char *argv [])
953 fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
957 val = (int)strtol (argv [2], NULL, 0) ;
959 digitalWriteByte (val) ;
965 * Read a pin and return the value
966 *********************************************************************************
969 void doRead (int argc, char *argv [])
975 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
979 pin = atoi (argv [2]) ;
980 val = digitalRead (pin) ;
982 printf ("%s\n", val == 0 ? "0" : "1") ;
988 * Read an analog pin and return the value
989 *********************************************************************************
992 void doAread (int argc, char *argv [])
996 fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
1000 printf ("%d\n", analogRead (atoi (argv [2]))) ;
1007 *********************************************************************************
1010 void doToggle (int argc, char *argv [])
1016 fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
1020 pin = atoi (argv [2]) ;
1022 digitalWrite (pin, !digitalRead (pin)) ;
1028 * Output a tone in a PWM pin
1029 *********************************************************************************
1032 void doPwmTone (int argc, char *argv [])
1038 fprintf (stderr, "Usage: %s pwmTone <pin> <freq>\n", argv [0]) ;
1042 pin = atoi (argv [2]) ;
1043 freq = atoi (argv [3]) ;
1045 pwmToneWrite (pin, freq) ;
1051 * Output a clock on a pin
1052 *********************************************************************************
1055 void doClock (int argc, char *argv [])
1061 fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
1065 pin = atoi (argv [2]) ;
1067 freq = atoi (argv [3]) ;
1069 gpioClockSet (pin, freq) ;
1075 * Output a PWM value on a pin
1076 *********************************************************************************
1079 void doPwm (int argc, char *argv [])
1085 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
1089 pin = atoi (argv [2]) ;
1091 val = atoi (argv [3]) ;
1093 pwmWrite (pin, val) ;
1098 * doPwmMode: doPwmRange: doPwmClock:
1099 * Change the PWM mode, range and clock divider values
1100 *********************************************************************************
1103 static void doPwmMode (int mode)
1108 static void doPwmRange (int argc, char *argv [])
1110 unsigned int range ;
1114 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
1118 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
1122 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
1126 pwmSetRange (range) ;
1129 static void doPwmClock (int argc, char *argv [])
1131 unsigned int clock ;
1135 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
1139 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
1141 if ((clock < 1) || (clock > 4095))
1143 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
1147 pwmSetClock (clock) ;
1154 *********************************************************************************
1157 int main (int argc, char *argv [])
1160 int model, rev, mem, maker, overVolted ;
1161 struct stat statBuf ;
1164 if (getenv ("WIRINGPI_DEBUG") != NULL)
1166 printf ("gpio: wiringPi debug mode enabled\n") ;
1167 wiringPiDebug = TRUE ;
1172 fprintf (stderr, "%s\n", usage) ;
1178 if (strcasecmp (argv [1], "-h") == 0)
1180 printf ("%s: %s\n", argv [0], usage) ;
1184 // Sort of a special:
1186 if (strcmp (argv [1], "-R") == 0)
1188 printf ("%d\n", piBoardRev ()) ;
1192 // Version & Warranty
1194 if (strcmp (argv [1], "-V") == 0)
1196 printf ("%d\n", piBoardRev ()) ;
1200 if (strcmp (argv [1], "-v") == 0)
1202 printf ("gpio version: %s\n", VERSION) ;
1203 printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
1204 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
1205 printf ("For details type: %s -warranty\n", argv [0]) ;
1207 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
1208 if (model == PI_MODEL_UNKNOWN)
1210 printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ;
1211 printf (" projects@drogon.net\n") ;
1212 printf ("with a copy of your /proc/cpuinfo if possible\n") ;
1216 printf ("Raspberry Pi Details:\n") ;
1217 printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
1218 piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
1220 // Check for device tree
1222 if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
1223 printf (" Device tree is enabled.\n") ;
1225 if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO
1226 printf (" This Raspberry Pi supports user-level GPIO access.\n") ;
1228 printf (" * Root or sudo required for GPIO access.\n") ;
1234 if (strcasecmp (argv [1], "-warranty") == 0)
1236 printf ("gpio version: %s\n", VERSION) ;
1237 printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
1239 printf (" This program is free software; you can redistribute it and/or modify\n") ;
1240 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
1241 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
1242 printf (" (at your option) any later version.\n") ;
1244 printf (" This program is distributed in the hope that it will be useful,\n") ;
1245 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
1246 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
1247 printf (" GNU Lesser General Public License for more details.\n") ;
1249 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
1250 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
1255 if (geteuid () != 0)
1257 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
1261 // Initial test for /sys/class/gpio operations:
1263 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
1264 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
1265 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
1266 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
1267 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
1269 // Check for load command:
1271 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
1272 if (strcasecmp (argv [1], "unload" ) == 0) { doUnLoad (argc, argv) ; return 0 ; }
1274 // Gertboard commands
1276 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
1277 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
1279 // Check for -g argument
1281 /**/ if (strcasecmp (argv [1], "-g") == 0)
1283 wiringPiSetupGpio () ;
1285 for (i = 2 ; i < argc ; ++i)
1286 argv [i - 1] = argv [i] ;
1288 wpMode = WPI_MODE_GPIO ;
1291 // Check for -1 argument
1293 else if (strcasecmp (argv [1], "-1") == 0)
1295 wiringPiSetupPhys () ;
1297 for (i = 2 ; i < argc ; ++i)
1298 argv [i - 1] = argv [i] ;
1300 wpMode = WPI_MODE_PHYS ;
1303 // Check for -p argument for PiFace
1305 else if (strcasecmp (argv [1], "-p") == 0)
1309 for (i = 2 ; i < argc ; ++i)
1310 argv [i - 1] = argv [i] ;
1312 wpMode = WPI_MODE_PIFACE ;
1315 // Default to wiringPi mode
1320 wpMode = WPI_MODE_PINS ;
1323 // Check for -x argument to load in a new extension
1325 if (strcasecmp (argv [1], "-x") == 0)
1329 fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
1330 exit (EXIT_FAILURE) ;
1333 if (!loadWPiExtension (argv [0], argv [2], TRUE)) // Prints its own error messages
1334 exit (EXIT_FAILURE) ;
1336 for (i = 3 ; i < argc ; ++i)
1337 argv [i - 2] = argv [i] ;
1343 fprintf (stderr, "%s: no command given\n", argv [0]) ;
1344 exit (EXIT_FAILURE) ;
1347 // Core wiringPi functions
1349 /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
1350 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
1351 else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
1352 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
1353 else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
1354 else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
1358 else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
1362 else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
1363 else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
1364 else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
1365 else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
1366 else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
1367 else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
1368 else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ;
1369 else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
1370 else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
1371 else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ;
1372 else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
1373 else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
1374 else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
1375 else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
1376 else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
1377 else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
1380 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
1381 exit (EXIT_FAILURE) ;