No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

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