You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1671 regels
40 KiB

  1. /*
  2. * gpio.c:
  3. * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
  4. * Pi's GPIO.
  5. * Copyright (c) 2012-2024 Gordon Henderson and contributors
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://github.com/WiringPi/WiringPi/
  9. *
  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.
  14. *
  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.
  19. *
  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. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <wiringPi.h>
  35. #include <wpiExtensions.h>
  36. #include <gertboard.h>
  37. #include <piFace.h>
  38. #include "../version.h"
  39. extern int wiringPiDebug ;
  40. int gpioDebug ;
  41. // External functions I can't be bothered creating a separate .h file for:
  42. extern void doReadall (void) ;
  43. extern void doAllReadall (void) ;
  44. extern void doQmode (int argc, char *argv []) ;
  45. #ifndef TRUE
  46. # define TRUE (1==1)
  47. # define FALSE (1==2)
  48. #endif
  49. #define PI_USB_POWER_CONTROL 38
  50. #define I2CDETECT "i2cdetect"
  51. #define MODPROBE "modprobe"
  52. #define RMMOD "rmmod"
  53. int wpMode ;
  54. char *usage = "Usage: gpio -v\n"
  55. " gpio -h\n"
  56. " gpio [-g|-1] ...\n"
  57. " gpio [-d] ...\n"
  58. " [-x extension:params] [[ -x ...]] ...\n"
  59. " gpio [-p] <read/write/wb> ...\n"
  60. " gpio <mode/read/write/aread/awritewb/pwm/pwmTone/clock> ...\n"
  61. " gpio <toggle/blink> <pin>\n"
  62. " gpio readall\n"
  63. " gpio unexportall/exports\n"
  64. " gpio export/edge/unexport ...\n"
  65. " gpio wfi <pin> <mode>\n"
  66. " gpio drive <group> <value>\n"
  67. " gpio pwm-bal/pwm-ms \n"
  68. " gpio pwmr <range> \n"
  69. " gpio pwmc <divider> \n"
  70. " gpio load spi/i2c\n"
  71. " gpio unload spi/i2c\n"
  72. " gpio i2cd/i2cdetect\n"
  73. " gpio rbx/rbd\n"
  74. " gpio wb <value>\n"
  75. " gpio usbp high/low\n"
  76. " gpio gbr <channel>\n"
  77. " gpio gbw <channel> <value>" ; // No trailing newline needed here.
  78. int GPIOToSysFS_ExitonFail (const int pin, const char* name) {
  79. int pinFS = GPIOToSysFS(pin);
  80. if (pinFS<0) {
  81. fprintf (stderr, "%s: invalid sysfs pin of bcm pin %d\n", name, pin) ;
  82. exit (1) ;
  83. }
  84. return pinFS;
  85. }
  86. #ifdef NOT_FOR_NOW
  87. /*
  88. * decodePin:
  89. * Decode a pin "number" which can actually be a pin name to represent
  90. * one of the Pi's on-board pins.
  91. *********************************************************************************
  92. */
  93. static int decodePin (const char *str)
  94. {
  95. // The first case - see if it's a number:
  96. if (isdigit (str [0]))
  97. return atoi (str) ;
  98. return 0 ;
  99. }
  100. #endif
  101. /*
  102. * findExecutable:
  103. * Code to locate the path to the given executable. We have a fixed list
  104. * of locations to try which completely overrides any $PATH environment.
  105. * This may be detrimental, however it avoids the reliance on $PATH
  106. * which may be a security issue when this program is run a set-uid-root.
  107. *********************************************************************************
  108. */
  109. static const char *searchPath [] =
  110. {
  111. "/sbin",
  112. "/usr/sbin",
  113. "/bin",
  114. "/usr/bin",
  115. NULL,
  116. } ;
  117. static char *findExecutable (const char *progName)
  118. {
  119. static char *path = NULL ;
  120. int len = strlen (progName) ;
  121. int i = 0 ;
  122. struct stat statBuf ;
  123. for (i = 0 ; searchPath [i] != NULL ; ++i)
  124. {
  125. path = malloc (strlen (searchPath [i]) + len + 2) ;
  126. sprintf (path, "%s/%s", searchPath [i], progName) ;
  127. if (stat (path, &statBuf) == 0)
  128. return path ;
  129. free (path) ;
  130. }
  131. return NULL ;
  132. }
  133. /*
  134. * changeOwner:
  135. * Change the ownership of the file to the real userId of the calling
  136. * program so we can access it.
  137. *********************************************************************************
  138. */
  139. static void changeOwner (char *cmd, char *file)
  140. {
  141. uid_t uid = getuid () ;
  142. uid_t gid = getgid () ;
  143. if (chown (file, uid, gid) != 0)
  144. {
  145. // Removed (ignoring) the check for not existing as I'm fed-up with morons telling me that
  146. // the warning message is an error.
  147. if (errno != ENOENT)
  148. fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
  149. }
  150. }
  151. /*
  152. * moduleLoaded:
  153. * Return true/false if the supplied module is loaded
  154. *********************************************************************************
  155. */
  156. static int moduleLoaded (char *modName)
  157. {
  158. int len = strlen (modName) ;
  159. int found = FALSE ;
  160. FILE *fd = fopen ("/proc/modules", "r") ;
  161. char line [80] ;
  162. if (fd == NULL)
  163. {
  164. fprintf (stderr, "gpio: Unable to check /proc/modules: %s\n", strerror (errno)) ;
  165. exit (1) ;
  166. }
  167. while (fgets (line, 80, fd) != NULL)
  168. {
  169. if (strncmp (line, modName, len) != 0)
  170. continue ;
  171. found = TRUE ;
  172. break ;
  173. }
  174. fclose (fd) ;
  175. return found ;
  176. }
  177. /*
  178. * doLoad:
  179. * Load either the spi or i2c modules and change device ownerships, etc.
  180. *********************************************************************************
  181. */
  182. static void checkDevTree (char *argv [])
  183. {
  184. struct stat statBuf ;
  185. if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
  186. {
  187. fprintf (stderr,
  188. "%s: Unable to load/unload modules as this Pi has the device tree enabled.\n"
  189. " You need to run the raspi-config program (as root) and select the\n"
  190. " modules (SPI or I2C) that you wish to load/unload there and reboot.\n", argv [0]) ;
  191. exit (1) ;
  192. }
  193. }
  194. static void _doLoadUsage (char *argv [])
  195. {
  196. fprintf (stderr, "Usage: %s load <spi/i2c> [I2C baudrate in Kb/sec]\n", argv [0]) ;
  197. exit (1) ;
  198. }
  199. static void doLoad (int argc, char *argv [])
  200. {
  201. char *module1, *module2 ;
  202. char cmd [80] ;
  203. char *file1, *file2 ;
  204. char args1 [32], args2 [32] ;
  205. checkDevTree (argv) ;
  206. if (argc < 3)
  207. _doLoadUsage (argv) ;
  208. args1 [0] = args2 [0] = 0 ;
  209. /**/ if (strcasecmp (argv [2], "spi") == 0)
  210. {
  211. module1 = "spidev" ;
  212. module2 = "spi_bcm2708" ;
  213. file1 = "/dev/spidev0.0" ;
  214. file2 = "/dev/spidev0.1" ;
  215. if (argc == 4)
  216. {
  217. fprintf (stderr, "%s: Unable to set the buffer size now. Load aborted. Please see the man page.\n", argv [0]) ;
  218. exit (1) ;
  219. }
  220. else if (argc > 4)
  221. _doLoadUsage (argv) ;
  222. }
  223. else if (strcasecmp (argv [2], "i2c") == 0)
  224. {
  225. module1 = "i2c_dev" ;
  226. module2 = "i2c_bcm2708" ;
  227. file1 = "/dev/i2c-0" ;
  228. file2 = "/dev/i2c-1" ;
  229. if (argc == 4)
  230. sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
  231. else if (argc > 4)
  232. _doLoadUsage (argv) ;
  233. }
  234. else
  235. _doLoadUsage (argv) ;
  236. if (findExecutable ("modprobe") == NULL)
  237. printf ("No found\n") ;
  238. if (!moduleLoaded (module1))
  239. {
  240. sprintf (cmd, "%s %s%s", findExecutable (MODPROBE), module1, args1) ;
  241. int ret = system(cmd);
  242. if (ret == -1) {
  243. perror("Error executing command");
  244. } else if (WIFEXITED(ret)) {
  245. int exit_status = WEXITSTATUS(ret);
  246. if (exit_status != 0) {
  247. fprintf(stderr, "Command failed with exit status %d\n", exit_status);
  248. }
  249. } else {
  250. fprintf(stderr, "Command terminated by signal\n");
  251. }
  252. }
  253. if (!moduleLoaded (module2))
  254. {
  255. sprintf (cmd, "%s %s%s", findExecutable (MODPROBE), module2, args2) ;
  256. int ret = system(cmd);
  257. if (ret == -1) {
  258. perror("Error executing command");
  259. } else if (WIFEXITED(ret)) {
  260. int exit_status = WEXITSTATUS(ret);
  261. if (exit_status != 0) {
  262. fprintf(stderr, "Command failed with exit status %d\n", exit_status);
  263. }
  264. } else {
  265. fprintf(stderr, "Command terminated by signal\n");
  266. }
  267. }
  268. if (!moduleLoaded (module2))
  269. {
  270. fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
  271. exit (1) ;
  272. }
  273. sleep (1) ; // To let things get settled
  274. changeOwner (argv [0], file1) ;
  275. changeOwner (argv [0], file2) ;
  276. }
  277. /*
  278. * doUnLoad:
  279. * Un-Load either the spi or i2c modules and change device ownerships, etc.
  280. *********************************************************************************
  281. */
  282. static void _doUnLoadUsage (char *argv [])
  283. {
  284. fprintf (stderr, "Usage: %s unload <spi/i2c>\n", argv [0]) ;
  285. exit (1) ;
  286. }
  287. static void doUnLoad (int argc, char *argv [])
  288. {
  289. char *module1, *module2 ;
  290. char cmd [80] ;
  291. checkDevTree (argv) ;
  292. if (argc != 3)
  293. _doUnLoadUsage (argv) ;
  294. /**/ if (strcasecmp (argv [2], "spi") == 0)
  295. {
  296. module1 = "spidev" ;
  297. module2 = "spi_bcm2708" ;
  298. }
  299. else if (strcasecmp (argv [2], "i2c") == 0)
  300. {
  301. module1 = "i2c_dev" ;
  302. module2 = "i2c_bcm2708" ;
  303. }
  304. else
  305. _doUnLoadUsage (argv) ;
  306. if (moduleLoaded (module1))
  307. {
  308. sprintf (cmd, "%s %s", findExecutable (RMMOD), module1) ;
  309. int ret = system(cmd);
  310. if (ret == -1) {
  311. perror("Error executing command");
  312. } else if (WIFEXITED(ret)) {
  313. int exit_status = WEXITSTATUS(ret);
  314. if (exit_status != 0) {
  315. fprintf(stderr, "Command failed with exit status %d\n", exit_status);
  316. }
  317. } else {
  318. fprintf(stderr, "Command terminated by signal\n");
  319. }
  320. }
  321. if (moduleLoaded (module2))
  322. {
  323. sprintf (cmd, "%s %s", findExecutable (RMMOD), module2) ;
  324. int ret = system(cmd);
  325. if (ret == -1) {
  326. perror("Error executing command");
  327. } else if (WIFEXITED(ret)) {
  328. int exit_status = WEXITSTATUS(ret);
  329. if (exit_status != 0) {
  330. fprintf(stderr, "Command failed with exit status %d\n", exit_status);
  331. }
  332. } else {
  333. fprintf(stderr, "Command terminated by signal\n");
  334. }
  335. }
  336. }
  337. /*
  338. * doI2Cdetect:
  339. * Run the i2cdetect command with the right runes for this Pi revision
  340. *********************************************************************************
  341. */
  342. static void doI2Cdetect (UNU int argc, char *argv [])
  343. {
  344. int port = piGpioLayout () == GPIO_LAYOUT_PI1_REV1 ? 0 : 1 ;
  345. char *c, *command ;
  346. if ((c = findExecutable (I2CDETECT)) == NULL)
  347. {
  348. fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
  349. return ;
  350. }
  351. if (!moduleLoaded ("i2c_dev"))
  352. {
  353. fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
  354. return ;
  355. }
  356. command = malloc (strlen (c) + 16) ;
  357. sprintf (command, "%s -y %d", c, port) ;
  358. if (system (command) < 0)
  359. fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
  360. }
  361. /*
  362. * doExports:
  363. * List all GPIO exports
  364. *********************************************************************************
  365. */
  366. static void doExports (UNU int argc, UNU char *argv [])
  367. {
  368. int fd ;
  369. int pin, l, first ;
  370. char fName [128] ;
  371. char buf [16] ;
  372. for (first = 0, pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
  373. {
  374. // Try to read the direction
  375. int pinFS = GPIOToSysFS(pin);
  376. if (pinFS<0) {
  377. continue;
  378. }
  379. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pinFS) ;
  380. if ((fd = open (fName, O_RDONLY)) == -1)
  381. continue ;
  382. if (first == 0)
  383. {
  384. ++first ;
  385. printf ("GPIO Pins exported:\n") ;
  386. }
  387. if(pinFS==pin) {
  388. printf ("%4d: ", pin) ;
  389. } else {
  390. printf ("%4d (%4d): ", pin, pinFS) ;
  391. }
  392. if ((l = read (fd, buf, 16)) == 0)
  393. sprintf (buf, "%s", "?") ;
  394. buf [l] = 0 ;
  395. if ((buf [strlen (buf) - 1]) == '\n')
  396. buf [strlen (buf) - 1] = 0 ;
  397. printf ("%-3s", buf) ;
  398. close (fd) ;
  399. // Try to Read the value
  400. sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
  401. if ((fd = open (fName, O_RDONLY)) == -1)
  402. {
  403. printf ("No Value file (huh?)\n") ;
  404. continue ;
  405. }
  406. if ((l = read (fd, buf, 16)) == 0)
  407. sprintf (buf, "%s", "?") ;
  408. buf [l] = 0 ;
  409. if ((buf [strlen (buf) - 1]) == '\n')
  410. buf [strlen (buf) - 1] = 0 ;
  411. printf (" %s", buf) ;
  412. // Read any edge trigger file
  413. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
  414. if ((fd = open (fName, O_RDONLY)) == -1)
  415. {
  416. printf ("\n") ;
  417. continue ;
  418. }
  419. if ((l = read (fd, buf, 16)) == 0)
  420. sprintf (buf, "%s", "?") ;
  421. buf [l] = 0 ;
  422. if ((buf [strlen (buf) - 1]) == '\n')
  423. buf [strlen (buf) - 1] = 0 ;
  424. printf (" %-8s\n", buf) ;
  425. close (fd) ;
  426. }
  427. }
  428. /*
  429. * doExport:
  430. * gpio export pin mode
  431. * This uses the /sys/class/gpio device interface.
  432. *********************************************************************************
  433. */
  434. void doExport (int argc, char *argv [])
  435. {
  436. FILE *fd ;
  437. int pin ;
  438. char *mode ;
  439. char fName [128] ;
  440. if (argc != 4)
  441. {
  442. fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
  443. exit (1) ;
  444. }
  445. pin = atoi (argv [2]) ;
  446. int pinFS = GPIOToSysFS_ExitonFail(pin, argv [0]);
  447. mode = argv [3] ;
  448. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  449. {
  450. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  451. exit (1) ;
  452. }
  453. fprintf (fd, "%d\n", pinFS) ;
  454. fclose (fd) ;
  455. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pinFS) ;
  456. if ((fd = fopen (fName, "w")) == NULL)
  457. {
  458. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  459. exit (1) ;
  460. }
  461. /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
  462. fprintf (fd, "in\n") ;
  463. else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
  464. fprintf (fd, "out\n") ;
  465. else if ((strcasecmp (mode, "high") == 0) || (strcasecmp (mode, "up") == 0))
  466. fprintf (fd, "high\n") ;
  467. else if ((strcasecmp (mode, "low") == 0) || (strcasecmp (mode, "down") == 0))
  468. fprintf (fd, "low\n") ;
  469. else
  470. {
  471. fprintf (stderr, "%s: Invalid mode: %s. Should be in, out, high or low\n", argv [1], mode) ;
  472. exit (1) ;
  473. }
  474. fclose (fd) ;
  475. // Change ownership so the current user can actually use it
  476. sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
  477. changeOwner (argv [0], fName) ;
  478. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
  479. changeOwner (argv [0], fName) ;
  480. }
  481. /*
  482. * doWfi:
  483. * gpio wfi pin mode
  484. * Wait for Interrupt on a given pin.
  485. * Slight cheat here - it's easier to actually use ISR now (which calls
  486. * gpio to set the pin modes!) then we simply sleep, and expect the thread
  487. * to exit the program. Crude but effective.
  488. *********************************************************************************
  489. */
  490. static volatile int iterations ;
  491. static volatile int globalCounter ;
  492. void printgpioflush(const char* text) {
  493. if (gpioDebug) {
  494. printf("%s", text);
  495. fflush(stdout);
  496. }
  497. }
  498. void printgpio(const char* text) {
  499. if (gpioDebug) {
  500. printf("%s", text);
  501. }
  502. }
  503. static void wfi (void) {
  504. globalCounter++;
  505. if(globalCounter>=iterations) {
  506. printgpio("finished\n");
  507. exit (0) ;
  508. } else {
  509. printgpioflush("I");
  510. }
  511. }
  512. void doWfi (int argc, char *argv [])
  513. {
  514. int pin, mode;
  515. int timeoutSec = 2147483647;
  516. iterations = 1;
  517. globalCounter = 0;
  518. if (argc != 4 && argc != 5 && argc != 6)
  519. {
  520. fprintf (stderr, "Usage: %s wfi pin mode [interations] [timeout sec.]\n", argv [0]) ;
  521. exit (1) ;
  522. }
  523. pin = atoi (argv [2]) ;
  524. /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
  525. else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
  526. else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
  527. else
  528. {
  529. fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
  530. exit (1) ;
  531. }
  532. if (argc>=5) {
  533. iterations = atoi(argv [4]);
  534. }
  535. if (argc>=6) {
  536. timeoutSec = atoi(argv [5]);
  537. }
  538. if (wiringPiISR (pin, mode, &wfi) < 0)
  539. {
  540. fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
  541. exit (1) ;
  542. }
  543. printgpio("wait for interrupt function call\n");
  544. for (int Sec=0; Sec<timeoutSec; ++Sec) {
  545. printgpioflush(".");
  546. delay (999);
  547. }
  548. printgpio("\nstopping wait for interrupt\n");
  549. wiringPiISRStop (pin);
  550. }
  551. /*
  552. * doEdge:
  553. * gpio edge pin mode
  554. * Easy access to changing the edge trigger on a GPIO pin
  555. * This uses the /sys/class/gpio device interface.
  556. *********************************************************************************
  557. */
  558. void doEdge (int argc, char *argv [])
  559. {
  560. FILE *fd ;
  561. int pin ;
  562. char *mode ;
  563. char fName [128] ;
  564. if (argc != 4)
  565. {
  566. fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
  567. exit (1) ;
  568. }
  569. pin = atoi (argv [2]) ;
  570. int pinFS = GPIOToSysFS_ExitonFail(pin, argv [0]);
  571. mode = argv [3] ;
  572. // Export the pin and set direction to input
  573. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  574. {
  575. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  576. exit (1) ;
  577. }
  578. fprintf (fd, "%d\n", pinFS) ;
  579. fclose (fd) ;
  580. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pinFS) ;
  581. if ((fd = fopen (fName, "w")) == NULL)
  582. {
  583. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  584. exit (1) ;
  585. }
  586. fprintf (fd, "in\n") ;
  587. fclose (fd) ;
  588. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
  589. if ((fd = fopen (fName, "w")) == NULL)
  590. {
  591. fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  592. exit (1) ;
  593. }
  594. /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
  595. else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
  596. else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
  597. else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
  598. else
  599. {
  600. fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
  601. exit (1) ;
  602. }
  603. // Change ownership of the value and edge files, so the current user can actually use it!
  604. sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
  605. changeOwner (argv [0], fName) ;
  606. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
  607. changeOwner (argv [0], fName) ;
  608. fclose (fd) ;
  609. }
  610. /*
  611. * doUnexport:
  612. * gpio unexport pin
  613. * This uses the /sys/class/gpio device interface.
  614. *********************************************************************************
  615. */
  616. void doUnexport (int argc, char *argv [])
  617. {
  618. FILE *fd ;
  619. int pin ;
  620. if (argc != 3)
  621. {
  622. fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
  623. exit (1) ;
  624. }
  625. pin = atoi (argv [2]) ;
  626. int pinFS = GPIOToSysFS_ExitonFail(pin, argv [0]);
  627. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  628. {
  629. fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
  630. exit (1) ;
  631. }
  632. fprintf (fd, "%d\n", pinFS) ;
  633. fclose (fd) ;
  634. }
  635. /*
  636. * doUnexportAll:
  637. * gpio unexportall
  638. * Un-Export all the GPIO pins.
  639. * This uses the /sys/class/gpio device interface.
  640. *********************************************************************************
  641. */
  642. void doUnexportall (char *progName)
  643. {
  644. FILE *fd ;
  645. int pin ;
  646. for (pin = 0 ; pin < 63 ; ++pin)
  647. {
  648. int pinFS = GPIOToSysFS(pin);
  649. if (pinFS>=0) {
  650. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  651. {
  652. fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
  653. exit (1) ;
  654. }
  655. fprintf (fd, "%d\n", pinFS) ;
  656. fclose (fd) ;
  657. }
  658. }
  659. }
  660. /*
  661. * doReset:
  662. * Reset the GPIO pins - as much as we can do
  663. *********************************************************************************
  664. */
  665. static void doReset (UNU char *progName)
  666. {
  667. printf ("GPIO Reset is dangerous and has been removed from the gpio command.\n") ;
  668. printf (" - Please write a shell-script to reset the GPIO pins into the state\n") ;
  669. printf (" that you need them in for your applications.\n") ;
  670. }
  671. /*
  672. * doMode:
  673. * gpio mode pin mode ...
  674. *********************************************************************************
  675. */
  676. void doMode (int argc, char *argv [])
  677. {
  678. int pin ;
  679. char *mode ;
  680. if (argc != 4)
  681. {
  682. fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
  683. exit (1) ;
  684. }
  685. pin = atoi (argv [2]) ;
  686. mode = argv [3] ;
  687. /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
  688. else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
  689. else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
  690. else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
  691. else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
  692. else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ;
  693. else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
  694. else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
  695. else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
  696. else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
  697. else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
  698. else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
  699. else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
  700. else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
  701. else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
  702. else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
  703. else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
  704. else
  705. {
  706. fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
  707. exit (1) ;
  708. }
  709. }
  710. /*
  711. * doPadDrive:
  712. * gpio drive group value
  713. *********************************************************************************
  714. */
  715. static void doPadDrivePin (int argc, char *argv [])
  716. {
  717. if (argc != 4) {
  718. fprintf (stderr, "Usage: %s drivepin pin value\n", argv [0]) ;
  719. exit (1) ;
  720. }
  721. int pin = atoi (argv [2]) ;
  722. int val = atoi (argv [3]) ;
  723. if ((pin < 0) || (pin > 27)) {
  724. fprintf (stderr, "%s: drive pin not 0-27: %d\n", argv [0], pin) ;
  725. exit (1) ;
  726. }
  727. if ((val < 0) || (val > 3)) {
  728. fprintf (stderr, "%s: drive value not 0-3: %d\n", argv [0], val) ;
  729. exit (1) ;
  730. }
  731. setPadDrivePin (pin, val) ;
  732. }
  733. static void doPadDrive (int argc, char *argv [])
  734. {
  735. int group, val ;
  736. if (argc != 4)
  737. {
  738. fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
  739. exit (1) ;
  740. }
  741. group = atoi (argv [2]) ;
  742. val = atoi (argv [3]) ;
  743. if ((group < -1) || (group > 2)) //-1 hidden feature for read and print values
  744. {
  745. fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
  746. exit (1) ;
  747. }
  748. if ((val < 0) || (val > 7))
  749. {
  750. fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
  751. exit (1) ;
  752. }
  753. setPadDrive (group, val) ;
  754. }
  755. /*
  756. * doUsbP:
  757. * Control USB Power - High (1.2A) or Low (600mA)
  758. * gpio usbp high/low
  759. *********************************************************************************
  760. */
  761. static void doUsbP (int argc, char *argv [])
  762. {
  763. int model, rev, mem, maker, overVolted ;
  764. if (argc != 3)
  765. {
  766. fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
  767. exit (1) ;
  768. }
  769. // Make sure we're on a B+
  770. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  771. if (!((model == PI_MODEL_BP) || (model == PI_MODEL_2)))
  772. {
  773. fprintf (stderr, "USB power contol is applicable to B+ and v2 boards only.\n") ;
  774. exit (1) ;
  775. }
  776. // Make sure we start in BCM_GPIO mode
  777. wiringPiSetupGpio () ;
  778. if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0))
  779. {
  780. digitalWrite (PI_USB_POWER_CONTROL, 1) ;
  781. pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
  782. printf ("Switched to HIGH current USB (1.2A)\n") ;
  783. return ;
  784. }
  785. if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0))
  786. {
  787. digitalWrite (PI_USB_POWER_CONTROL, 0) ;
  788. pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
  789. printf ("Switched to LOW current USB (600mA)\n") ;
  790. return ;
  791. }
  792. fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
  793. exit (1) ;
  794. }
  795. /*
  796. * doGbw:
  797. * gpio gbw channel value
  798. * Gertboard Write - To the Analog output
  799. *********************************************************************************
  800. */
  801. static void doGbw (int argc, char *argv [])
  802. {
  803. int channel, value ;
  804. if (argc != 4)
  805. {
  806. fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
  807. exit (1) ;
  808. }
  809. channel = atoi (argv [2]) ;
  810. value = atoi (argv [3]) ;
  811. if ((channel < 0) || (channel > 1))
  812. {
  813. fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
  814. exit (1) ;
  815. }
  816. if ((value < 0) || (value > 255))
  817. {
  818. fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
  819. exit (1) ;
  820. }
  821. if (gertboardAnalogSetup (64) < 0)
  822. {
  823. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  824. exit (1) ;
  825. }
  826. analogWrite (64 + channel, value) ;
  827. }
  828. /*
  829. * doGbr:
  830. * gpio gbr channel
  831. * From the analog input
  832. *********************************************************************************
  833. */
  834. static void doGbr (int argc, char *argv [])
  835. {
  836. int channel ;
  837. if (argc != 3)
  838. {
  839. fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
  840. exit (1) ;
  841. }
  842. channel = atoi (argv [2]) ;
  843. if ((channel < 0) || (channel > 1))
  844. {
  845. fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
  846. exit (1) ;
  847. }
  848. if (gertboardAnalogSetup (64) < 0)
  849. {
  850. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  851. exit (1) ;
  852. }
  853. printf ("%d\n", analogRead (64 + channel)) ;
  854. }
  855. /*
  856. * doWrite:
  857. * gpio write pin value
  858. *********************************************************************************
  859. */
  860. static void doWrite (int argc, char *argv [])
  861. {
  862. int pin, val ;
  863. if (argc != 4)
  864. {
  865. fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
  866. exit (1) ;
  867. }
  868. pin = atoi (argv [2]) ;
  869. /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
  870. val = 1 ;
  871. else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
  872. val = 0 ;
  873. else
  874. val = atoi (argv [3]) ;
  875. /**/ if (val == 0)
  876. digitalWrite (pin, LOW) ;
  877. else
  878. digitalWrite (pin, HIGH) ;
  879. }
  880. /*
  881. * doAwriterite:
  882. * gpio awrite pin value
  883. *********************************************************************************
  884. */
  885. static void doAwrite (int argc, char *argv [])
  886. {
  887. int pin, val ;
  888. if (argc != 4)
  889. {
  890. fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
  891. exit (1) ;
  892. }
  893. pin = atoi (argv [2]) ;
  894. val = atoi (argv [3]) ;
  895. analogWrite (pin, val) ;
  896. }
  897. /*
  898. * doWriteByte:
  899. * gpio wb value
  900. *********************************************************************************
  901. */
  902. static void doWriteByte (int argc, char *argv [])
  903. {
  904. int val ;
  905. if (argc != 3)
  906. {
  907. fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
  908. exit (1) ;
  909. }
  910. val = (int)strtol (argv [2], NULL, 0) ;
  911. digitalWriteByte (val) ;
  912. }
  913. /*
  914. * doReadByte:
  915. * gpio rbx|rbd value
  916. *********************************************************************************
  917. */
  918. static void doReadByte (int argc, char *argv [], int printHex)
  919. {
  920. int val ;
  921. if (argc != 2)
  922. {
  923. fprintf (stderr, "Usage: %s rbx|rbd\n", argv [0]) ;
  924. exit (1) ;
  925. }
  926. val = digitalReadByte () ;
  927. if (printHex)
  928. printf ("%02X\n", val) ;
  929. else
  930. printf ("%d\n", val) ;
  931. }
  932. /*
  933. * doRead:
  934. * Read a pin and return the value
  935. *********************************************************************************
  936. */
  937. void doRead (int argc, char *argv [])
  938. {
  939. int pin, val ;
  940. if (argc != 3)
  941. {
  942. fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
  943. exit (1) ;
  944. }
  945. pin = atoi (argv [2]) ;
  946. val = digitalRead (pin) ;
  947. printf ("%s\n", val == 0 ? "0" : "1") ;
  948. }
  949. /*
  950. * doAread:
  951. * Read an analog pin and return the value
  952. *********************************************************************************
  953. */
  954. void doAread (int argc, char *argv [])
  955. {
  956. if (argc != 3)
  957. {
  958. fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
  959. exit (1) ;
  960. }
  961. printf ("%d\n", analogRead (atoi (argv [2]))) ;
  962. }
  963. /*
  964. * doToggle:
  965. * Toggle an IO pin
  966. *********************************************************************************
  967. */
  968. void doToggle (int argc, char *argv [])
  969. {
  970. int pin ;
  971. if (argc != 3)
  972. {
  973. fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
  974. exit (1) ;
  975. }
  976. pin = atoi (argv [2]) ;
  977. digitalWrite (pin, !digitalRead (pin)) ;
  978. }
  979. /*
  980. * doBlink:
  981. * Blink an IO pin
  982. *********************************************************************************
  983. */
  984. void doBlink (int argc, char *argv [])
  985. {
  986. int pin ;
  987. if (argc != 3)
  988. {
  989. fprintf (stderr, "Usage: %s blink pin\n", argv [0]) ;
  990. exit (1) ;
  991. }
  992. pin = atoi (argv [2]) ;
  993. pinMode (pin, OUTPUT) ;
  994. for (;;)
  995. {
  996. digitalWrite (pin, !digitalRead (pin)) ;
  997. delay (500) ;
  998. }
  999. }
  1000. /*
  1001. * doPwmTone:
  1002. * Output a tone in a PWM pin
  1003. *********************************************************************************
  1004. */
  1005. void doPwmTone (int argc, char *argv [])
  1006. {
  1007. int pin, freq ;
  1008. if (argc != 4)
  1009. {
  1010. fprintf (stderr, "Usage: %s pwmTone <pin> <freq>\n", argv [0]) ;
  1011. exit (1) ;
  1012. }
  1013. pin = atoi (argv [2]) ;
  1014. freq = atoi (argv [3]) ;
  1015. pwmToneWrite (pin, freq) ;
  1016. }
  1017. /*
  1018. * doClock:
  1019. * Output a clock on a pin
  1020. *********************************************************************************
  1021. */
  1022. void doClock (int argc, char *argv [])
  1023. {
  1024. int pin, freq ;
  1025. if (argc != 4)
  1026. {
  1027. fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
  1028. exit (1) ;
  1029. }
  1030. pin = atoi (argv [2]) ;
  1031. freq = atoi (argv [3]) ;
  1032. gpioClockSet (pin, freq) ;
  1033. }
  1034. /*
  1035. * doPwm:
  1036. * Output a PWM value on a pin
  1037. *********************************************************************************
  1038. */
  1039. void doPwm (int argc, char *argv [])
  1040. {
  1041. int pin, val ;
  1042. if (argc != 4)
  1043. {
  1044. fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
  1045. exit (1) ;
  1046. }
  1047. pin = atoi (argv [2]) ;
  1048. val = atoi (argv [3]) ;
  1049. pwmWrite (pin, val) ;
  1050. }
  1051. /*
  1052. * doPwmMode: doPwmRange: doPwmClock:
  1053. * Change the PWM mode, range and clock divider values
  1054. *********************************************************************************
  1055. */
  1056. static void doPwmMode (int mode)
  1057. {
  1058. pwmSetMode (mode) ;
  1059. }
  1060. static void doPwmRange (int argc, char *argv [])
  1061. {
  1062. unsigned int range ;
  1063. if (argc != 3)
  1064. {
  1065. fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
  1066. exit (1) ;
  1067. }
  1068. range = (unsigned int)strtoul (argv [2], NULL, 10) ;
  1069. if (range == 0)
  1070. {
  1071. fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
  1072. exit (1) ;
  1073. }
  1074. pwmSetRange (range) ;
  1075. }
  1076. static void doPwmClock (int argc, char *argv [])
  1077. {
  1078. unsigned int clock ;
  1079. if (argc != 3)
  1080. {
  1081. fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
  1082. exit (1) ;
  1083. }
  1084. clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
  1085. if ((clock < 1) || (clock > 4095))
  1086. {
  1087. fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
  1088. exit (1) ;
  1089. }
  1090. pwmSetClock (clock) ;
  1091. }
  1092. /*
  1093. * doVersion:
  1094. * Handle the ever more complicated version command and print out
  1095. * some usefull information.
  1096. *********************************************************************************
  1097. */
  1098. static void doVersion (char *argv [])
  1099. {
  1100. int model, rev, mem, maker, warranty ;
  1101. struct stat statBuf ;
  1102. char name [80] ;
  1103. FILE *fd ;
  1104. int vMaj, vMin ;
  1105. wiringPiVersion (&vMaj, &vMin) ;
  1106. printf ("gpio version: %d.%d\n", vMaj, vMin) ;
  1107. printf ("Copyright (c) 2012-2024 Gordon Henderson and contributors\n") ;
  1108. printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
  1109. printf ("For details type: %s -warranty\n", argv [0]) ;
  1110. printf ("\n") ;
  1111. piBoardId (&model, &rev, &mem, &maker, &warranty) ;
  1112. printf ("Raspberry Pi Details:\n") ;
  1113. printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
  1114. piModelNames [model], piRevisionNames [rev], piMemorySize [mem], piMakerNames [maker], warranty ? "[Out of Warranty]" : "") ;
  1115. // Check for device tree
  1116. if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
  1117. printf (" * Device tree is enabled.\n") ;
  1118. if (stat ("/proc/device-tree/model", &statBuf) == 0) // Output Kernel idea of board type
  1119. {
  1120. if ((fd = fopen ("/proc/device-tree/model", "r")) != NULL)
  1121. {
  1122. if (fgets(name, sizeof(name), fd) == NULL) {
  1123. // Handle error or end of file condition
  1124. perror("Error reading /proc/device-tree/model");
  1125. }
  1126. fclose (fd) ;
  1127. printf (" *--> %s\n", name) ;
  1128. }
  1129. }
  1130. if (wiringPiUserLevelAccess()) // User level GPIO is GO
  1131. printf (" * This Raspberry Pi supports user-level GPIO access.\n") ;
  1132. else
  1133. printf (" * Root or sudo required for GPIO access.\n") ;
  1134. }
  1135. /*
  1136. * main:
  1137. * Start here
  1138. *********************************************************************************
  1139. */
  1140. int main (int argc, char *argv [])
  1141. {
  1142. int i ;
  1143. if (getenv ("WIRINGPI_DEBUG") != NULL)
  1144. {
  1145. printf ("gpio: wiringPi debug mode enabled\n") ;
  1146. wiringPiDebug = TRUE ;
  1147. }
  1148. if (getenv ("GPIO_DEBUG") != NULL)
  1149. {
  1150. printf ("gpio: gpio debug mode enabled\n") ;
  1151. gpioDebug = TRUE ;
  1152. }
  1153. if (argc == 1)
  1154. {
  1155. fprintf (stderr,
  1156. "%s: At your service!\n"
  1157. " Type: gpio -h for full details and\n"
  1158. " gpio readall for a quick printout of your connector details\n", argv [0]) ;
  1159. exit (EXIT_FAILURE) ;
  1160. }
  1161. // Help
  1162. if (strcasecmp (argv [1], "-h") == 0)
  1163. {
  1164. printf ("%s: %s\n", argv [0], usage) ;
  1165. exit (EXIT_SUCCESS) ;
  1166. }
  1167. // Version & Warranty
  1168. // Wish I could remember why I have both -R and -V ...
  1169. if ((strcmp (argv [1], "-R") == 0) || (strcmp (argv [1], "-V") == 0))
  1170. {
  1171. printf ("%d\n", piGpioLayout ()) ;
  1172. exit (EXIT_SUCCESS) ;
  1173. }
  1174. // Version and information
  1175. if (strcmp (argv [1], "-v") == 0)
  1176. {
  1177. doVersion (argv) ;
  1178. exit (EXIT_SUCCESS) ;
  1179. }
  1180. if (strcasecmp (argv [1], "-warranty") == 0)
  1181. {
  1182. printf ("gpio version: %s\n", VERSION) ;
  1183. printf ("Copyright (c) 2012-2024 Gordon Henderson and contributors\n") ;
  1184. printf ("\n") ;
  1185. printf (" This program is free software; you can redistribute it and/or modify\n") ;
  1186. printf (" it under the terms of the GNU Leser General Public License as published\n") ;
  1187. printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
  1188. printf (" (at your option) any later version.\n") ;
  1189. printf ("\n") ;
  1190. printf (" This program is distributed in the hope that it will be useful,\n") ;
  1191. printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
  1192. printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
  1193. printf (" GNU Lesser General Public License for more details.\n") ;
  1194. printf ("\n") ;
  1195. printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
  1196. printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
  1197. printf ("\n") ;
  1198. exit (EXIT_SUCCESS) ;
  1199. }
  1200. if (geteuid () != 0)
  1201. {
  1202. fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
  1203. exit (EXIT_FAILURE) ;
  1204. }
  1205. // Initial test for /sys/class/gpio operations:
  1206. /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
  1207. else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
  1208. else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
  1209. else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
  1210. else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
  1211. // Check for load command:
  1212. if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
  1213. if (strcasecmp (argv [1], "unload" ) == 0) { doUnLoad (argc, argv) ; return 0 ; }
  1214. // Check for usb power command
  1215. if (strcasecmp (argv [1], "usbp" ) == 0) { doUsbP (argc, argv) ; return 0 ; }
  1216. // Gertboard commands
  1217. if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
  1218. if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
  1219. // Check for allreadall command, force Gpio mode
  1220. if (strcasecmp (argv [1], "allreadall") == 0)
  1221. {
  1222. wiringPiSetupGpio () ;
  1223. doAllReadall () ;
  1224. return 0 ;
  1225. }
  1226. // Check for -g argument
  1227. /**/ if (strcasecmp (argv [1], "-g") == 0)
  1228. {
  1229. wiringPiSetupGpio () ;
  1230. for (i = 2 ; i < argc ; ++i)
  1231. argv [i - 1] = argv [i] ;
  1232. --argc ;
  1233. wpMode = WPI_MODE_GPIO ;
  1234. }
  1235. // Check for -1 argument
  1236. else if (strcasecmp (argv [1], "-1") == 0)
  1237. {
  1238. wiringPiSetupPhys () ;
  1239. for (i = 2 ; i < argc ; ++i)
  1240. argv [i - 1] = argv [i] ;
  1241. --argc ;
  1242. wpMode = WPI_MODE_PHYS ;
  1243. }
  1244. // Check for -p argument for PiFace
  1245. else if (strcasecmp (argv [1], "-p") == 0)
  1246. {
  1247. piFaceSetup (200) ;
  1248. for (i = 2 ; i < argc ; ++i)
  1249. argv [i - 1] = argv [i] ;
  1250. --argc ;
  1251. wpMode = WPI_MODE_PIFACE ;
  1252. }
  1253. // Check for -z argument so we don't actually initialise wiringPi
  1254. else if (strcasecmp (argv [1], "-z") == 0)
  1255. {
  1256. for (i = 2 ; i < argc ; ++i)
  1257. argv [i - 1] = argv [i] ;
  1258. --argc ;
  1259. wpMode = WPI_MODE_UNINITIALISED ;
  1260. }
  1261. // Default to wiringPi mode
  1262. else
  1263. {
  1264. wiringPiSetup () ;
  1265. wpMode = WPI_MODE_PINS ;
  1266. }
  1267. // Check for -x argument to load in a new extension
  1268. // -x extension:base:args
  1269. // Can load many modules, but unless daemon mode we can only send one
  1270. // command at a time.
  1271. while (strcasecmp (argv [1], "-x") == 0)
  1272. {
  1273. if (argc < 3)
  1274. {
  1275. fprintf (stderr, "%s: -x missing extension command.\n", argv [0]) ;
  1276. exit (EXIT_FAILURE) ;
  1277. }
  1278. if (!loadWPiExtension (argv [0], argv [2], TRUE))
  1279. {
  1280. fprintf (stderr, "%s: Extension load failed: %s\n", argv [0], strerror (errno)) ;
  1281. exit (EXIT_FAILURE) ;
  1282. }
  1283. // Shift args down by 2
  1284. for (i = 3 ; i < argc ; ++i)
  1285. argv [i - 2] = argv [i] ;
  1286. argc -= 2 ;
  1287. }
  1288. if (argc <= 1)
  1289. {
  1290. fprintf (stderr, "%s: no command given\n", argv [0]) ;
  1291. exit (EXIT_FAILURE) ;
  1292. }
  1293. // Core wiringPi functions
  1294. /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
  1295. else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
  1296. else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
  1297. else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
  1298. else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
  1299. else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
  1300. // GPIO Nicies
  1301. else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
  1302. else if (strcasecmp (argv [1], "blink" ) == 0) doBlink (argc, argv) ;
  1303. // Pi Specifics
  1304. else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
  1305. else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
  1306. else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
  1307. else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
  1308. else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
  1309. else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
  1310. else if (strcasecmp (argv [1], "drivepin" ) == 0) doPadDrivePin(argc, argv) ;
  1311. else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
  1312. else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
  1313. else if (strcasecmp (argv [1], "pins" ) == 0) doReadall () ;
  1314. else if (strcasecmp (argv [1], "qmode" ) == 0) doQmode (argc, argv) ;
  1315. else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
  1316. else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
  1317. else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
  1318. else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
  1319. else if (strcasecmp (argv [1], "rbx" ) == 0) doReadByte (argc, argv, TRUE) ;
  1320. else if (strcasecmp (argv [1], "rbd" ) == 0) doReadByte (argc, argv, FALSE) ;
  1321. else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
  1322. else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
  1323. else
  1324. {
  1325. fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
  1326. exit (EXIT_FAILURE) ;
  1327. }
  1328. return 0 ;
  1329. }