gpio.c 33 KB

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