Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

1004 lignes
23 KiB

  1. /*
  2. * gpio.c:
  3. * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
  4. * Pi's GPIO.
  5. * Copyright (c) 2012 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 <string.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <sys/types.h>
  31. #include <fcntl.h>
  32. #include <wiringPi.h>
  33. #include <gertboard.h>
  34. extern int wiringPiDebug ;
  35. #ifndef TRUE
  36. # define TRUE (1==1)
  37. # define FALSE (1==2)
  38. #endif
  39. #define VERSION "1.6"
  40. static int wpMode ;
  41. char *usage = "Usage: gpio -v\n"
  42. " gpio -h\n"
  43. " gpio [-g] <read/write/wb/pwm/mode> ...\n"
  44. " gpio [-p] <read/write/wb> ...\n"
  45. " gpio readall\n"
  46. " gpio unexportall/exports ...\n"
  47. " gpio export/edge/unexport ...\n"
  48. " gpio drive <group> <value>\n"
  49. " gpio pwm-bal/pwm-ms \n"
  50. " gpio pwmr <range> \n"
  51. " gpio pwmc <divider> \n"
  52. " gpio load spi/i2c\n"
  53. " gpio gbr <channel>\n"
  54. " gpio gbw <channel> <value>" ; // No trailing newline needed here.
  55. /*
  56. * changeOwner:
  57. * Change the ownership of the file to the real userId of the calling
  58. * program so we can access it.
  59. *********************************************************************************
  60. */
  61. static void changeOwner (char *cmd, char *file)
  62. {
  63. uid_t uid = getuid () ;
  64. uid_t gid = getgid () ;
  65. if (chown (file, uid, gid) != 0)
  66. {
  67. if (errno == ENOENT) // Warn that it's not there
  68. fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
  69. else
  70. {
  71. fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
  72. exit (1) ;
  73. }
  74. }
  75. }
  76. /*
  77. * moduleLoaded:
  78. * Return true/false if the supplied module is loaded
  79. *********************************************************************************
  80. */
  81. static int moduleLoaded (char *modName)
  82. {
  83. int len = strlen (modName) ;
  84. int found = FALSE ;
  85. FILE *fd = fopen ("/proc/modules", "r") ;
  86. char line [80] ;
  87. if (fd == NULL)
  88. {
  89. fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
  90. exit (1) ;
  91. }
  92. while (fgets (line, 80, fd) != NULL)
  93. {
  94. if (strncmp (line, modName, len) != 0)
  95. continue ;
  96. found = TRUE ;
  97. break ;
  98. }
  99. fclose (fd) ;
  100. return found ;
  101. }
  102. /*
  103. * doLoad:
  104. * Load either the spi or i2c modules and change device ownerships, etc.
  105. *********************************************************************************
  106. */
  107. static void _doLoadUsage (char *argv [])
  108. {
  109. fprintf (stderr, "Usage: %s load <spi/i2c> [bufferSize in KB for spi]\n", argv [0]) ;
  110. exit (1) ;
  111. }
  112. static void doLoad (int argc, char *argv [])
  113. {
  114. char *module1, *module2 ;
  115. char cmd [80] ;
  116. char *file1, *file2 ;
  117. char spiBuf [32] ;
  118. if (argc < 3)
  119. _doLoadUsage (argv) ;
  120. spiBuf [0] = 0 ;
  121. /**/ if (strcasecmp (argv [2], "spi") == 0)
  122. {
  123. module1 = "spidev" ;
  124. module2 = "spi_bcm2708" ;
  125. file1 = "/dev/spidev0.0" ;
  126. file2 = "/dev/spidev0.1" ;
  127. if (argc == 4)
  128. sprintf (spiBuf, " bufsize=%d", atoi (argv [3]) * 1024) ;
  129. else if (argc > 4)
  130. _doLoadUsage (argv) ;
  131. }
  132. else if (strcasecmp (argv [2], "i2c") == 0)
  133. {
  134. module1 = "i2c_dev" ;
  135. module2 = "i2c_bcm2708" ;
  136. file1 = "/dev/i2c-0" ;
  137. file2 = "/dev/i2c-1" ;
  138. }
  139. else
  140. _doLoadUsage (argv) ;
  141. if (!moduleLoaded (module1))
  142. {
  143. sprintf (cmd, "modprobe %s%s", module1, spiBuf) ;
  144. system (cmd) ;
  145. }
  146. if (!moduleLoaded (module2))
  147. {
  148. sprintf (cmd, "modprobe %s", module2) ;
  149. system (cmd) ;
  150. }
  151. if (!moduleLoaded (module2))
  152. {
  153. fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
  154. exit (1) ;
  155. }
  156. sleep (1) ; // To let things get settled
  157. changeOwner (argv [0], file1) ;
  158. changeOwner (argv [0], file2) ;
  159. }
  160. /*
  161. * doReadall:
  162. * Read all the GPIO pins
  163. *********************************************************************************
  164. */
  165. static char *pinNames [] =
  166. {
  167. "GPIO 0",
  168. "GPIO 1",
  169. "GPIO 2",
  170. "GPIO 3",
  171. "GPIO 4",
  172. "GPIO 5",
  173. "GPIO 6",
  174. "GPIO 7",
  175. "SDA ",
  176. "SCL ",
  177. "CE0 ",
  178. "CE1 ",
  179. "MOSI ",
  180. "MISO ",
  181. "SCLK ",
  182. "TxD ",
  183. "RxD ",
  184. "GPIO 8",
  185. "GPIO 9",
  186. "GPIO10",
  187. "GPIO11",
  188. } ;
  189. static void doReadall (void)
  190. {
  191. int pin ;
  192. printf ("+----------+------+--------+-------+\n") ;
  193. printf ("| wiringPi | GPIO | Name | Value |\n") ;
  194. printf ("+----------+------+--------+-------+\n") ;
  195. for (pin = 0 ; pin < NUM_PINS ; ++pin)
  196. printf ("| %6d | %3d | %s | %s |\n",
  197. pin, wpiPinToGpio (pin),
  198. pinNames [pin],
  199. digitalRead (pin) == HIGH ? "High" : "Low ") ;
  200. printf ("+----------+------+--------+-------+\n") ;
  201. if (piBoardRev () == 1)
  202. return ;
  203. for (pin = 17 ; pin <= 20 ; ++pin)
  204. printf ("| %6d | %3d | %s | %s |\n",
  205. pin, wpiPinToGpio (pin),
  206. pinNames [pin],
  207. digitalRead (pin) == HIGH ? "High" : "Low ") ;
  208. printf ("+----------+------+--------+-------+\n") ;
  209. }
  210. /*
  211. * doExports:
  212. * List all GPIO exports
  213. *********************************************************************************
  214. */
  215. static void doExports (int argc, char *argv [])
  216. {
  217. int fd ;
  218. int i, l, first ;
  219. char fName [128] ;
  220. char buf [16] ;
  221. // Rather crude, but who knows what others are up to...
  222. for (first = 0, i = 0 ; i < 64 ; ++i)
  223. {
  224. // Try to read the direction
  225. sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
  226. if ((fd = open (fName, O_RDONLY)) == -1)
  227. continue ;
  228. if (first == 0)
  229. {
  230. ++first ;
  231. printf ("GPIO Pins exported:\n") ;
  232. }
  233. printf ("%4d: ", i) ;
  234. if ((l = read (fd, buf, 16)) == 0)
  235. sprintf (buf, "%s", "?") ;
  236. buf [l] = 0 ;
  237. if ((buf [strlen (buf) - 1]) == '\n')
  238. buf [strlen (buf) - 1] = 0 ;
  239. printf ("%-3s", buf) ;
  240. close (fd) ;
  241. // Try to Read the value
  242. sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
  243. if ((fd = open (fName, O_RDONLY)) == -1)
  244. {
  245. printf ("No Value file (huh?)\n") ;
  246. continue ;
  247. }
  248. if ((l = read (fd, buf, 16)) == 0)
  249. sprintf (buf, "%s", "?") ;
  250. buf [l] = 0 ;
  251. if ((buf [strlen (buf) - 1]) == '\n')
  252. buf [strlen (buf) - 1] = 0 ;
  253. printf (" %s", buf) ;
  254. // Read any edge trigger file
  255. sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
  256. if ((fd = open (fName, O_RDONLY)) == -1)
  257. {
  258. printf ("\n") ;
  259. continue ;
  260. }
  261. if ((l = read (fd, buf, 16)) == 0)
  262. sprintf (buf, "%s", "?") ;
  263. buf [l] = 0 ;
  264. if ((buf [strlen (buf) - 1]) == '\n')
  265. buf [strlen (buf) - 1] = 0 ;
  266. printf (" %-8s\n", buf) ;
  267. close (fd) ;
  268. }
  269. }
  270. /*
  271. * doExport:
  272. * gpio export pin mode
  273. * This uses the /sys/class/gpio device interface.
  274. *********************************************************************************
  275. */
  276. void doExport (int argc, char *argv [])
  277. {
  278. FILE *fd ;
  279. int pin ;
  280. char *mode ;
  281. char fName [128] ;
  282. if (argc != 4)
  283. {
  284. fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
  285. exit (1) ;
  286. }
  287. pin = atoi (argv [2]) ;
  288. mode = argv [3] ;
  289. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  290. {
  291. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  292. exit (1) ;
  293. }
  294. fprintf (fd, "%d\n", pin) ;
  295. fclose (fd) ;
  296. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
  297. if ((fd = fopen (fName, "w")) == NULL)
  298. {
  299. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  300. exit (1) ;
  301. }
  302. /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
  303. fprintf (fd, "in\n") ;
  304. else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
  305. fprintf (fd, "out\n") ;
  306. else
  307. {
  308. fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
  309. exit (1) ;
  310. }
  311. fclose (fd) ;
  312. // Change ownership so the current user can actually use it!
  313. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  314. changeOwner (argv [0], fName) ;
  315. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  316. changeOwner (argv [0], fName) ;
  317. }
  318. /*
  319. * doEdge:
  320. * gpio edge pin mode
  321. * Easy access to changing the edge trigger on a GPIO pin
  322. * This uses the /sys/class/gpio device interface.
  323. *********************************************************************************
  324. */
  325. void doEdge (int argc, char *argv [])
  326. {
  327. FILE *fd ;
  328. int pin ;
  329. char *mode ;
  330. char fName [128] ;
  331. if (argc != 4)
  332. {
  333. fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
  334. exit (1) ;
  335. }
  336. pin = atoi (argv [2]) ;
  337. mode = argv [3] ;
  338. // Export the pin and set direction to input
  339. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  340. {
  341. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  342. exit (1) ;
  343. }
  344. fprintf (fd, "%d\n", pin) ;
  345. fclose (fd) ;
  346. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
  347. if ((fd = fopen (fName, "w")) == NULL)
  348. {
  349. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  350. exit (1) ;
  351. }
  352. fprintf (fd, "in\n") ;
  353. fclose (fd) ;
  354. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  355. if ((fd = fopen (fName, "w")) == NULL)
  356. {
  357. fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  358. exit (1) ;
  359. }
  360. /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
  361. else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
  362. else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
  363. else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
  364. else
  365. {
  366. fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
  367. exit (1) ;
  368. }
  369. // Change ownership of the value and edge files, so the current user can actually use it!
  370. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  371. changeOwner (argv [0], fName) ;
  372. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  373. changeOwner (argv [0], fName) ;
  374. fclose (fd) ;
  375. }
  376. /*
  377. * doUnexport:
  378. * gpio unexport pin
  379. * This uses the /sys/class/gpio device interface.
  380. *********************************************************************************
  381. */
  382. void doUnexport (int argc, char *argv [])
  383. {
  384. FILE *fd ;
  385. int pin ;
  386. if (argc != 3)
  387. {
  388. fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
  389. exit (1) ;
  390. }
  391. pin = atoi (argv [2]) ;
  392. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  393. {
  394. fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
  395. exit (1) ;
  396. }
  397. fprintf (fd, "%d\n", pin) ;
  398. fclose (fd) ;
  399. }
  400. /*
  401. * doUnexportAll:
  402. * gpio unexportall
  403. * Un-Export all the GPIO pins.
  404. * This uses the /sys/class/gpio device interface.
  405. *********************************************************************************
  406. */
  407. void doUnexportall (int argc, char *argv [])
  408. {
  409. FILE *fd ;
  410. int pin ;
  411. for (pin = 0 ; pin < 63 ; ++pin)
  412. {
  413. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  414. {
  415. fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
  416. exit (1) ;
  417. }
  418. fprintf (fd, "%d\n", pin) ;
  419. fclose (fd) ;
  420. }
  421. }
  422. /*
  423. * doMode:
  424. * gpio mode pin mode ...
  425. *********************************************************************************
  426. */
  427. void doMode (int argc, char *argv [])
  428. {
  429. int pin ;
  430. char *mode ;
  431. if (argc != 4)
  432. {
  433. fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
  434. exit (1) ;
  435. }
  436. pin = atoi (argv [2]) ;
  437. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  438. return ;
  439. mode = argv [3] ;
  440. /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
  441. else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
  442. else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
  443. else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
  444. else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
  445. else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
  446. else
  447. {
  448. fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
  449. exit (1) ;
  450. }
  451. }
  452. /*
  453. * doPadDrive:
  454. * gpio drive group value
  455. *********************************************************************************
  456. */
  457. static void doPadDrive (int argc, char *argv [])
  458. {
  459. int group, val ;
  460. if (argc != 4)
  461. {
  462. fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
  463. exit (1) ;
  464. }
  465. group = atoi (argv [2]) ;
  466. val = atoi (argv [3]) ;
  467. if ((group < 0) || (group > 2))
  468. {
  469. fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
  470. exit (1) ;
  471. }
  472. if ((val < 0) || (val > 7))
  473. {
  474. fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
  475. exit (1) ;
  476. }
  477. setPadDrive (group, val) ;
  478. }
  479. /*
  480. * doGbw:
  481. * gpio gbw channel value
  482. * Gertboard Write - To the Analog output
  483. *********************************************************************************
  484. */
  485. static void doGbw (int argc, char *argv [])
  486. {
  487. int channel, value ;
  488. if (argc != 4)
  489. {
  490. fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
  491. exit (1) ;
  492. }
  493. channel = atoi (argv [2]) ;
  494. value = atoi (argv [3]) ;
  495. if ((channel < 0) || (channel > 1))
  496. {
  497. fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
  498. exit (1) ;
  499. }
  500. if ((value < 0) || (value > 1023))
  501. {
  502. fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
  503. exit (1) ;
  504. }
  505. if (gertboardSPISetup () == -1)
  506. {
  507. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  508. exit (1) ;
  509. }
  510. gertboardAnalogWrite (channel, value) ;
  511. }
  512. /*
  513. * doGbr:
  514. * gpio gbr channel
  515. * From the analog input
  516. *********************************************************************************
  517. */
  518. static void doGbr (int argc, char *argv [])
  519. {
  520. int channel ;
  521. if (argc != 3)
  522. {
  523. fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
  524. exit (1) ;
  525. }
  526. channel = atoi (argv [2]) ;
  527. if ((channel < 0) || (channel > 1))
  528. {
  529. fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
  530. exit (1) ;
  531. }
  532. if (gertboardSPISetup () == -1)
  533. {
  534. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  535. exit (1) ;
  536. }
  537. printf ("%d\n",gertboardAnalogRead (channel)) ;
  538. }
  539. /*
  540. * doWrite:
  541. * gpio write pin value
  542. *********************************************************************************
  543. */
  544. static void doWrite (int argc, char *argv [])
  545. {
  546. int pin, val ;
  547. if (argc != 4)
  548. {
  549. fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
  550. exit (1) ;
  551. }
  552. pin = atoi (argv [2]) ;
  553. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  554. return ;
  555. /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
  556. val = 1 ;
  557. else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
  558. val = 0 ;
  559. else
  560. val = atoi (argv [3]) ;
  561. /**/ if (val == 0)
  562. digitalWrite (pin, LOW) ;
  563. else
  564. digitalWrite (pin, HIGH) ;
  565. }
  566. /*
  567. * doWriteByte:
  568. * gpio write value
  569. *********************************************************************************
  570. */
  571. static void doWriteByte (int argc, char *argv [])
  572. {
  573. int val ;
  574. if (argc != 3)
  575. {
  576. fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
  577. exit (1) ;
  578. }
  579. val = (int)strtol (argv [2], NULL, 0) ;
  580. digitalWriteByte (val) ;
  581. }
  582. /*
  583. * doRead:
  584. * Read a pin and return the value
  585. *********************************************************************************
  586. */
  587. void doRead (int argc, char *argv [])
  588. {
  589. int pin, val ;
  590. if (argc != 3)
  591. {
  592. fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
  593. exit (1) ;
  594. }
  595. pin = atoi (argv [2]) ;
  596. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  597. {
  598. printf ("0\n") ;
  599. return ;
  600. }
  601. val = digitalRead (pin) ;
  602. printf ("%s\n", val == 0 ? "0" : "1") ;
  603. }
  604. /*
  605. * doPwm:
  606. * Output a PWM value on a pin
  607. *********************************************************************************
  608. */
  609. void doPwm (int argc, char *argv [])
  610. {
  611. int pin, val ;
  612. if (argc != 4)
  613. {
  614. fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
  615. exit (1) ;
  616. }
  617. pin = atoi (argv [2]) ;
  618. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  619. return ;
  620. val = atoi (argv [3]) ;
  621. pwmWrite (pin, val) ;
  622. }
  623. /*
  624. * doPwmMode: doPwmRange: doPwmClock:
  625. * Change the PWM mode, range and clock divider values
  626. *********************************************************************************
  627. */
  628. static void doPwmMode (int mode)
  629. {
  630. pwmSetMode (mode) ;
  631. }
  632. static void doPwmRange (int argc, char *argv [])
  633. {
  634. unsigned int range ;
  635. if (argc != 3)
  636. {
  637. fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
  638. exit (1) ;
  639. }
  640. range = (unsigned int)strtoul (argv [2], NULL, 10) ;
  641. if (range == 0)
  642. {
  643. fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
  644. exit (1) ;
  645. }
  646. pwmSetRange (range) ;
  647. }
  648. static void doPwmClock (int argc, char *argv [])
  649. {
  650. unsigned int clock ;
  651. if (argc != 3)
  652. {
  653. fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
  654. exit (1) ;
  655. }
  656. clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
  657. if ((clock < 1) || (clock > 4095))
  658. {
  659. fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
  660. exit (1) ;
  661. }
  662. pwmSetClock (clock) ;
  663. }
  664. /*
  665. * main:
  666. * Start here
  667. *********************************************************************************
  668. */
  669. int main (int argc, char *argv [])
  670. {
  671. int i ;
  672. if (getenv ("WIRINGPI_DEBUG") != NULL)
  673. {
  674. printf ("gpio: wiringPi debug mode enabled\n") ;
  675. wiringPiDebug = TRUE ;
  676. }
  677. if (argc == 1)
  678. {
  679. fprintf (stderr, "%s\n", usage) ;
  680. return 1 ;
  681. }
  682. if (strcasecmp (argv [1], "-h") == 0)
  683. {
  684. printf ("%s: %s\n", argv [0], usage) ;
  685. return 0 ;
  686. }
  687. if (strcasecmp (argv [1], "-v") == 0)
  688. {
  689. printf ("gpio version: %s\n", VERSION) ;
  690. printf ("Copyright (c) 2012 Gordon Henderson\n") ;
  691. printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
  692. printf ("For details type: %s -warranty\n", argv [0]) ;
  693. printf ("\n") ;
  694. printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
  695. return 0 ;
  696. }
  697. if (strcasecmp (argv [1], "-warranty") == 0)
  698. {
  699. printf ("gpio version: %s\n", VERSION) ;
  700. printf ("Copyright (c) 2012 Gordon Henderson\n") ;
  701. printf ("\n") ;
  702. printf (" This program is free software; you can redistribute it and/or modify\n") ;
  703. printf (" it under the terms of the GNU Leser General Public License as published\n") ;
  704. printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
  705. printf (" (at your option) any later version.\n") ;
  706. printf ("\n") ;
  707. printf (" This program is distributed in the hope that it will be useful,\n") ;
  708. printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
  709. printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
  710. printf (" GNU Lesser General Public License for more details.\n") ;
  711. printf ("\n") ;
  712. printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
  713. printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
  714. printf ("\n") ;
  715. return 0 ;
  716. }
  717. if (geteuid () != 0)
  718. {
  719. fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
  720. return 1 ;
  721. }
  722. // Initial test for /sys/class/gpio operations:
  723. /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
  724. else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
  725. else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
  726. else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
  727. else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
  728. // Check for load command:
  729. if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
  730. // Gertboard commands
  731. if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
  732. if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
  733. // Check for -g argument
  734. if (strcasecmp (argv [1], "-g") == 0)
  735. {
  736. if (wiringPiSetupGpio () == -1)
  737. {
  738. fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
  739. exit (1) ;
  740. }
  741. for (i = 2 ; i < argc ; ++i)
  742. argv [i - 1] = argv [i] ;
  743. --argc ;
  744. wpMode = WPI_MODE_GPIO ;
  745. }
  746. // Check for -p argument for PiFace
  747. else if (strcasecmp (argv [1], "-p") == 0)
  748. {
  749. if (wiringPiSetupPiFaceForGpioProg () == -1)
  750. {
  751. fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
  752. exit (1) ;
  753. }
  754. for (i = 2 ; i < argc ; ++i)
  755. argv [i - 1] = argv [i] ;
  756. --argc ;
  757. wpMode = WPI_MODE_PIFACE ;
  758. }
  759. // Default to wiringPi mode
  760. else
  761. {
  762. if (wiringPiSetup () == -1)
  763. {
  764. fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
  765. exit (1) ;
  766. }
  767. wpMode = WPI_MODE_PINS ;
  768. }
  769. // Check for PWM or Pad Drive operations
  770. if (wpMode != WPI_MODE_PIFACE)
  771. {
  772. if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
  773. if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
  774. if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
  775. if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; }
  776. if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
  777. }
  778. // Check for wiring commands
  779. /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
  780. else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
  781. else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ;
  782. else if (strcasecmp (argv [1], "wb") == 0) doWriteByte (argc, argv) ;
  783. else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
  784. else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
  785. else
  786. {
  787. fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
  788. exit (1) ;
  789. }
  790. return 0 ;
  791. }