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.

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