Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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