Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

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