您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

1234 行
28 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-2013 Gordon Henderson
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://projects.drogon.net/raspberry-pi/wiringpi/
  9. *
  10. * wiringPi is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * wiringPi is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <wiringPi.h>
  35. #include <gertboard.h>
  36. #include <piFace.h>
  37. #include "extensions.h"
  38. extern int wiringPiDebug ;
  39. #ifndef TRUE
  40. # define TRUE (1==1)
  41. # define FALSE (1==2)
  42. #endif
  43. #define VERSION "2.05"
  44. #define I2CDETECT "/usr/sbin/i2cdetect"
  45. static int wpMode ;
  46. char *usage = "Usage: gpio -v\n"
  47. " gpio -h\n"
  48. " gpio [-g|-1] [-x extension:params] ...\n"
  49. " gpio [-p] <read/write/wb> ...\n"
  50. " gpio <read/write/aread/awritewb/pwm/clock/mode> ...\n"
  51. " gpio readall/reset\n"
  52. " gpio unexportall/exports\n"
  53. " gpio export/edge/unexport ...\n"
  54. " gpio wfi <pin> <mode>\n"
  55. " gpio drive <group> <value>\n"
  56. " gpio pwm-bal/pwm-ms \n"
  57. " gpio pwmr <range> \n"
  58. " gpio pwmc <divider> \n"
  59. " gpio load spi/i2c\n"
  60. " gpio i2cd/i2cdetect\n"
  61. " gpio gbr <channel>\n"
  62. " gpio gbw <channel> <value>" ; // No trailing newline needed here.
  63. /*
  64. * changeOwner:
  65. * Change the ownership of the file to the real userId of the calling
  66. * program so we can access it.
  67. *********************************************************************************
  68. */
  69. static void changeOwner (char *cmd, char *file)
  70. {
  71. uid_t uid = getuid () ;
  72. uid_t gid = getgid () ;
  73. if (chown (file, uid, gid) != 0)
  74. {
  75. if (errno == ENOENT) // Warn that it's not there
  76. fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
  77. else
  78. {
  79. fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
  80. exit (1) ;
  81. }
  82. }
  83. }
  84. /*
  85. * moduleLoaded:
  86. * Return true/false if the supplied module is loaded
  87. *********************************************************************************
  88. */
  89. static int moduleLoaded (char *modName)
  90. {
  91. int len = strlen (modName) ;
  92. int found = FALSE ;
  93. FILE *fd = fopen ("/proc/modules", "r") ;
  94. char line [80] ;
  95. if (fd == NULL)
  96. {
  97. fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
  98. exit (1) ;
  99. }
  100. while (fgets (line, 80, fd) != NULL)
  101. {
  102. if (strncmp (line, modName, len) != 0)
  103. continue ;
  104. found = TRUE ;
  105. break ;
  106. }
  107. fclose (fd) ;
  108. return found ;
  109. }
  110. /*
  111. * doLoad:
  112. * Load either the spi or i2c modules and change device ownerships, etc.
  113. *********************************************************************************
  114. */
  115. static void _doLoadUsage (char *argv [])
  116. {
  117. fprintf (stderr, "Usage: %s load <spi/i2c> [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ;
  118. exit (1) ;
  119. }
  120. static void doLoad (int argc, char *argv [])
  121. {
  122. char *module1, *module2 ;
  123. char cmd [80] ;
  124. char *file1, *file2 ;
  125. char args1 [32], args2 [32] ;
  126. if (argc < 3)
  127. _doLoadUsage (argv) ;
  128. args1 [0] = args2 [0] = 0 ;
  129. /**/ if (strcasecmp (argv [2], "spi") == 0)
  130. {
  131. module1 = "spidev" ;
  132. module2 = "spi_bcm2708" ;
  133. file1 = "/dev/spidev0.0" ;
  134. file2 = "/dev/spidev0.1" ;
  135. if (argc == 4)
  136. sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
  137. else if (argc > 4)
  138. _doLoadUsage (argv) ;
  139. }
  140. else if (strcasecmp (argv [2], "i2c") == 0)
  141. {
  142. module1 = "i2c_dev" ;
  143. module2 = "i2c_bcm2708" ;
  144. file1 = "/dev/i2c-0" ;
  145. file2 = "/dev/i2c-1" ;
  146. if (argc == 4)
  147. sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
  148. else if (argc > 4)
  149. _doLoadUsage (argv) ;
  150. }
  151. else
  152. _doLoadUsage (argv) ;
  153. if (!moduleLoaded (module1))
  154. {
  155. sprintf (cmd, "modprobe %s%s", module1, args1) ;
  156. system (cmd) ;
  157. }
  158. if (!moduleLoaded (module2))
  159. {
  160. sprintf (cmd, "modprobe %s%s", module2, args2) ;
  161. system (cmd) ;
  162. }
  163. if (!moduleLoaded (module2))
  164. {
  165. fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
  166. exit (1) ;
  167. }
  168. sleep (1) ; // To let things get settled
  169. changeOwner (argv [0], file1) ;
  170. changeOwner (argv [0], file2) ;
  171. }
  172. /*
  173. * doI2Cdetect:
  174. * Run the i2cdetect command with the right runes for this Pi revision
  175. *********************************************************************************
  176. */
  177. static void doI2Cdetect (int argc, char *argv [])
  178. {
  179. int port = piBoardRev () == 1 ? 0 : 1 ;
  180. char command [128] ;
  181. struct stat statBuf ;
  182. if (stat (I2CDETECT, &statBuf) < 0)
  183. {
  184. fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
  185. return ;
  186. }
  187. if (!moduleLoaded ("i2c_dev"))
  188. {
  189. fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
  190. return ;
  191. }
  192. sprintf (command, "%s -y %d", I2CDETECT, port) ;
  193. if (system (command) < 0)
  194. fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
  195. }
  196. /*
  197. * doReadall:
  198. * Read all the GPIO pins
  199. *********************************************************************************
  200. */
  201. static char *pinNames [] =
  202. {
  203. "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
  204. "SDA ", "SCL ",
  205. "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ",
  206. "TxD ", "RxD ",
  207. "GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
  208. } ;
  209. static char *alts [] =
  210. {
  211. "IN ", "OUT ", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
  212. } ;
  213. static int wpiToPhys [64] =
  214. {
  215. 11, 12, 13, 15, 16, 18, 22, 7, // 0...7
  216. 3, 5, // 8...9
  217. 24, 26, 19, 21, 23, // 10..14
  218. 8, 10, // 15..16
  219. 3, 4, 5, 6, // 17..20
  220. 0,0,0,0,0,0,0,0,0,0,0, // 20..31
  221. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47
  222. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63
  223. } ;
  224. static void doReadall (void)
  225. {
  226. int pin ;
  227. printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
  228. printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ;
  229. printf ("+----------+------+------+--------+------+-------+\n") ;
  230. for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
  231. {
  232. if (wpiPinToGpio (pin) == -1)
  233. continue ;
  234. printf ("| %6d | %3d | %3d | %s | %s | %s |\n",
  235. pin, wpiPinToGpio (pin), wpiToPhys [pin],
  236. pinNames [pin],
  237. alts [getAlt (pin)],
  238. digitalRead (pin) == HIGH ? "High" : "Low ") ;
  239. }
  240. printf ("+----------+------+------+--------+------+-------+\n") ;
  241. }
  242. /*
  243. * doExports:
  244. * List all GPIO exports
  245. *********************************************************************************
  246. */
  247. static void doExports (int argc, char *argv [])
  248. {
  249. int fd ;
  250. int i, l, first ;
  251. char fName [128] ;
  252. char buf [16] ;
  253. for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
  254. {
  255. // Try to read the direction
  256. sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
  257. if ((fd = open (fName, O_RDONLY)) == -1)
  258. continue ;
  259. if (first == 0)
  260. {
  261. ++first ;
  262. printf ("GPIO Pins exported:\n") ;
  263. }
  264. printf ("%4d: ", i) ;
  265. if ((l = read (fd, buf, 16)) == 0)
  266. sprintf (buf, "%s", "?") ;
  267. buf [l] = 0 ;
  268. if ((buf [strlen (buf) - 1]) == '\n')
  269. buf [strlen (buf) - 1] = 0 ;
  270. printf ("%-3s", buf) ;
  271. close (fd) ;
  272. // Try to Read the value
  273. sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
  274. if ((fd = open (fName, O_RDONLY)) == -1)
  275. {
  276. printf ("No Value file (huh?)\n") ;
  277. continue ;
  278. }
  279. if ((l = read (fd, buf, 16)) == 0)
  280. sprintf (buf, "%s", "?") ;
  281. buf [l] = 0 ;
  282. if ((buf [strlen (buf) - 1]) == '\n')
  283. buf [strlen (buf) - 1] = 0 ;
  284. printf (" %s", buf) ;
  285. // Read any edge trigger file
  286. sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
  287. if ((fd = open (fName, O_RDONLY)) == -1)
  288. {
  289. printf ("\n") ;
  290. continue ;
  291. }
  292. if ((l = read (fd, buf, 16)) == 0)
  293. sprintf (buf, "%s", "?") ;
  294. buf [l] = 0 ;
  295. if ((buf [strlen (buf) - 1]) == '\n')
  296. buf [strlen (buf) - 1] = 0 ;
  297. printf (" %-8s\n", buf) ;
  298. close (fd) ;
  299. }
  300. }
  301. /*
  302. * doExport:
  303. * gpio export pin mode
  304. * This uses the /sys/class/gpio device interface.
  305. *********************************************************************************
  306. */
  307. void doExport (int argc, char *argv [])
  308. {
  309. FILE *fd ;
  310. int pin ;
  311. char *mode ;
  312. char fName [128] ;
  313. if (argc != 4)
  314. {
  315. fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
  316. exit (1) ;
  317. }
  318. pin = atoi (argv [2]) ;
  319. mode = argv [3] ;
  320. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  321. {
  322. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  323. exit (1) ;
  324. }
  325. fprintf (fd, "%d\n", pin) ;
  326. fclose (fd) ;
  327. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
  328. if ((fd = fopen (fName, "w")) == NULL)
  329. {
  330. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  331. exit (1) ;
  332. }
  333. /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
  334. fprintf (fd, "in\n") ;
  335. else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
  336. fprintf (fd, "out\n") ;
  337. else
  338. {
  339. fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
  340. exit (1) ;
  341. }
  342. fclose (fd) ;
  343. // Change ownership so the current user can actually use it!
  344. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  345. changeOwner (argv [0], fName) ;
  346. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  347. changeOwner (argv [0], fName) ;
  348. }
  349. /*
  350. * doWfi:
  351. * gpio wfi pin mode
  352. * Wait for Interrupt on a given pin.
  353. * Slight cheat here - it's easier to actually use ISR now (which calls
  354. * gpio to set the pin modes!) then we simply sleep, and expect the thread
  355. * to exit the program. Crude but effective.
  356. *********************************************************************************
  357. */
  358. static void wfi (void)
  359. { exit (0) ; }
  360. void doWfi (int argc, char *argv [])
  361. {
  362. int pin, mode ;
  363. if (argc != 4)
  364. {
  365. fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
  366. exit (1) ;
  367. }
  368. pin = atoi (argv [2]) ;
  369. /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
  370. else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
  371. else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
  372. else
  373. {
  374. fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
  375. exit (1) ;
  376. }
  377. if (wiringPiISR (pin, mode, &wfi) < 0)
  378. {
  379. fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
  380. exit (1) ;
  381. }
  382. for (;;)
  383. delay (9999) ;
  384. }
  385. /*
  386. * doEdge:
  387. * gpio edge pin mode
  388. * Easy access to changing the edge trigger on a GPIO pin
  389. * This uses the /sys/class/gpio device interface.
  390. *********************************************************************************
  391. */
  392. void doEdge (int argc, char *argv [])
  393. {
  394. FILE *fd ;
  395. int pin ;
  396. char *mode ;
  397. char fName [128] ;
  398. if (argc != 4)
  399. {
  400. fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
  401. exit (1) ;
  402. }
  403. pin = atoi (argv [2]) ;
  404. mode = argv [3] ;
  405. // Export the pin and set direction to input
  406. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  407. {
  408. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  409. exit (1) ;
  410. }
  411. fprintf (fd, "%d\n", pin) ;
  412. fclose (fd) ;
  413. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
  414. if ((fd = fopen (fName, "w")) == NULL)
  415. {
  416. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  417. exit (1) ;
  418. }
  419. fprintf (fd, "in\n") ;
  420. fclose (fd) ;
  421. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  422. if ((fd = fopen (fName, "w")) == NULL)
  423. {
  424. fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  425. exit (1) ;
  426. }
  427. /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
  428. else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
  429. else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
  430. else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
  431. else
  432. {
  433. fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
  434. exit (1) ;
  435. }
  436. // Change ownership of the value and edge files, so the current user can actually use it!
  437. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  438. changeOwner (argv [0], fName) ;
  439. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  440. changeOwner (argv [0], fName) ;
  441. fclose (fd) ;
  442. }
  443. /*
  444. * doUnexport:
  445. * gpio unexport pin
  446. * This uses the /sys/class/gpio device interface.
  447. *********************************************************************************
  448. */
  449. void doUnexport (int argc, char *argv [])
  450. {
  451. FILE *fd ;
  452. int pin ;
  453. if (argc != 3)
  454. {
  455. fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
  456. exit (1) ;
  457. }
  458. pin = atoi (argv [2]) ;
  459. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  460. {
  461. fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
  462. exit (1) ;
  463. }
  464. fprintf (fd, "%d\n", pin) ;
  465. fclose (fd) ;
  466. }
  467. /*
  468. * doUnexportAll:
  469. * gpio unexportall
  470. * Un-Export all the GPIO pins.
  471. * This uses the /sys/class/gpio device interface.
  472. *********************************************************************************
  473. */
  474. void doUnexportall (char *progName)
  475. {
  476. FILE *fd ;
  477. int pin ;
  478. for (pin = 0 ; pin < 63 ; ++pin)
  479. {
  480. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  481. {
  482. fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
  483. exit (1) ;
  484. }
  485. fprintf (fd, "%d\n", pin) ;
  486. fclose (fd) ;
  487. }
  488. }
  489. /*
  490. * doReset:
  491. * Reset the GPIO pins - as much as we can do
  492. *********************************************************************************
  493. */
  494. static void doReset (char *progName)
  495. {
  496. int pin ;
  497. doUnexportall (progName) ;
  498. for (pin = 0 ; pin < 64 ; ++pin)
  499. {
  500. if (wpiPinToGpio (pin) == -1)
  501. continue ;
  502. digitalWrite (pin, LOW) ;
  503. pinMode (pin, INPUT) ;
  504. pullUpDnControl (pin, PUD_OFF) ;
  505. }
  506. }
  507. /*
  508. * doMode:
  509. * gpio mode pin mode ...
  510. *********************************************************************************
  511. */
  512. void doMode (int argc, char *argv [])
  513. {
  514. int pin ;
  515. char *mode ;
  516. if (argc != 4)
  517. {
  518. fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
  519. exit (1) ;
  520. }
  521. pin = atoi (argv [2]) ;
  522. mode = argv [3] ;
  523. /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
  524. else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
  525. else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
  526. else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
  527. else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
  528. else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
  529. else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
  530. else
  531. {
  532. fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
  533. exit (1) ;
  534. }
  535. }
  536. /*
  537. * doPadDrive:
  538. * gpio drive group value
  539. *********************************************************************************
  540. */
  541. static void doPadDrive (int argc, char *argv [])
  542. {
  543. int group, val ;
  544. if (argc != 4)
  545. {
  546. fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
  547. exit (1) ;
  548. }
  549. group = atoi (argv [2]) ;
  550. val = atoi (argv [3]) ;
  551. if ((group < 0) || (group > 2))
  552. {
  553. fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
  554. exit (1) ;
  555. }
  556. if ((val < 0) || (val > 7))
  557. {
  558. fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
  559. exit (1) ;
  560. }
  561. setPadDrive (group, val) ;
  562. }
  563. /*
  564. * doGbw:
  565. * gpio gbw channel value
  566. * Gertboard Write - To the Analog output
  567. *********************************************************************************
  568. */
  569. static void doGbw (int argc, char *argv [])
  570. {
  571. int channel, value ;
  572. if (argc != 4)
  573. {
  574. fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
  575. exit (1) ;
  576. }
  577. channel = atoi (argv [2]) ;
  578. value = atoi (argv [3]) ;
  579. if ((channel < 0) || (channel > 1))
  580. {
  581. fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
  582. exit (1) ;
  583. }
  584. if ((value < 0) || (value > 1023))
  585. {
  586. fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
  587. exit (1) ;
  588. }
  589. if (gertboardAnalogSetup (64) < 0)
  590. {
  591. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  592. exit (1) ;
  593. }
  594. analogWrite (64 + channel, value) ;
  595. }
  596. /*
  597. * doGbr:
  598. * gpio gbr channel
  599. * From the analog input
  600. *********************************************************************************
  601. */
  602. static void doGbr (int argc, char *argv [])
  603. {
  604. int channel ;
  605. if (argc != 3)
  606. {
  607. fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
  608. exit (1) ;
  609. }
  610. channel = atoi (argv [2]) ;
  611. if ((channel < 0) || (channel > 1))
  612. {
  613. fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
  614. exit (1) ;
  615. }
  616. if (gertboardAnalogSetup (64) < 0)
  617. {
  618. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  619. exit (1) ;
  620. }
  621. printf ("%d\n", analogRead (64 + channel)) ;
  622. }
  623. /*
  624. * doWrite:
  625. * gpio write pin value
  626. *********************************************************************************
  627. */
  628. static void doWrite (int argc, char *argv [])
  629. {
  630. int pin, val ;
  631. if (argc != 4)
  632. {
  633. fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
  634. exit (1) ;
  635. }
  636. pin = atoi (argv [2]) ;
  637. /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
  638. val = 1 ;
  639. else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
  640. val = 0 ;
  641. else
  642. val = atoi (argv [3]) ;
  643. /**/ if (val == 0)
  644. digitalWrite (pin, LOW) ;
  645. else
  646. digitalWrite (pin, HIGH) ;
  647. }
  648. /*
  649. * doAwriterite:
  650. * gpio awrite pin value
  651. *********************************************************************************
  652. */
  653. static void doAwrite (int argc, char *argv [])
  654. {
  655. int pin, val ;
  656. if (argc != 4)
  657. {
  658. fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
  659. exit (1) ;
  660. }
  661. pin = atoi (argv [2]) ;
  662. val = atoi (argv [3]) ;
  663. analogWrite (pin, val) ;
  664. }
  665. /*
  666. * doWriteByte:
  667. * gpio write value
  668. *********************************************************************************
  669. */
  670. static void doWriteByte (int argc, char *argv [])
  671. {
  672. int val ;
  673. if (argc != 3)
  674. {
  675. fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
  676. exit (1) ;
  677. }
  678. val = (int)strtol (argv [2], NULL, 0) ;
  679. digitalWriteByte (val) ;
  680. }
  681. /*
  682. * doRead:
  683. * Read a pin and return the value
  684. *********************************************************************************
  685. */
  686. void doRead (int argc, char *argv [])
  687. {
  688. int pin, val ;
  689. if (argc != 3)
  690. {
  691. fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
  692. exit (1) ;
  693. }
  694. pin = atoi (argv [2]) ;
  695. val = digitalRead (pin) ;
  696. printf ("%s\n", val == 0 ? "0" : "1") ;
  697. }
  698. /*
  699. * doAread:
  700. * Read an analog pin and return the value
  701. *********************************************************************************
  702. */
  703. void doAread (int argc, char *argv [])
  704. {
  705. if (argc != 3)
  706. {
  707. fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
  708. exit (1) ;
  709. }
  710. printf ("%d\n", analogRead (atoi (argv [2]))) ;
  711. }
  712. /*
  713. * doToggle:
  714. * Toggle an IO pin
  715. *********************************************************************************
  716. */
  717. void doToggle (int argc, char *argv [])
  718. {
  719. int pin ;
  720. if (argc != 3)
  721. {
  722. fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
  723. exit (1) ;
  724. }
  725. pin = atoi (argv [2]) ;
  726. digitalWrite (pin, !digitalRead (pin)) ;
  727. }
  728. /*
  729. * doClock:
  730. * Output a clock on a pin
  731. *********************************************************************************
  732. */
  733. void doClock (int argc, char *argv [])
  734. {
  735. int pin, freq ;
  736. if (argc != 4)
  737. {
  738. fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
  739. exit (1) ;
  740. }
  741. pin = atoi (argv [2]) ;
  742. freq = atoi (argv [3]) ;
  743. gpioClockSet (pin, freq) ;
  744. }
  745. /*
  746. * doPwm:
  747. * Output a PWM value on a pin
  748. *********************************************************************************
  749. */
  750. void doPwm (int argc, char *argv [])
  751. {
  752. int pin, val ;
  753. if (argc != 4)
  754. {
  755. fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
  756. exit (1) ;
  757. }
  758. pin = atoi (argv [2]) ;
  759. val = atoi (argv [3]) ;
  760. pwmWrite (pin, val) ;
  761. }
  762. /*
  763. * doPwmMode: doPwmRange: doPwmClock:
  764. * Change the PWM mode, range and clock divider values
  765. *********************************************************************************
  766. */
  767. static void doPwmMode (int mode)
  768. {
  769. pwmSetMode (mode) ;
  770. }
  771. static void doPwmRange (int argc, char *argv [])
  772. {
  773. unsigned int range ;
  774. if (argc != 3)
  775. {
  776. fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
  777. exit (1) ;
  778. }
  779. range = (unsigned int)strtoul (argv [2], NULL, 10) ;
  780. if (range == 0)
  781. {
  782. fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
  783. exit (1) ;
  784. }
  785. pwmSetRange (range) ;
  786. }
  787. static void doPwmClock (int argc, char *argv [])
  788. {
  789. unsigned int clock ;
  790. if (argc != 3)
  791. {
  792. fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
  793. exit (1) ;
  794. }
  795. clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
  796. if ((clock < 1) || (clock > 4095))
  797. {
  798. fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
  799. exit (1) ;
  800. }
  801. pwmSetClock (clock) ;
  802. }
  803. /*
  804. * main:
  805. * Start here
  806. *********************************************************************************
  807. */
  808. int main (int argc, char *argv [])
  809. {
  810. int i ;
  811. if (getenv ("WIRINGPI_DEBUG") != NULL)
  812. {
  813. printf ("gpio: wiringPi debug mode enabled\n") ;
  814. wiringPiDebug = TRUE ;
  815. }
  816. if (argc == 1)
  817. {
  818. fprintf (stderr, "%s\n", usage) ;
  819. return 1 ;
  820. }
  821. // Help
  822. if (strcasecmp (argv [1], "-h") == 0)
  823. {
  824. printf ("%s: %s\n", argv [0], usage) ;
  825. return 0 ;
  826. }
  827. // Sort of a special:
  828. if (strcmp (argv [1], "-R") == 0)
  829. {
  830. printf ("%d\n", piBoardRev ()) ;
  831. return 0 ;
  832. }
  833. // Version & Warranty
  834. if (strcmp (argv [1], "-V") == 0)
  835. {
  836. printf ("%d\n", piBoardRev ()) ;
  837. return 0 ;
  838. }
  839. if (strcmp (argv [1], "-v") == 0)
  840. {
  841. printf ("gpio version: %s\n", VERSION) ;
  842. printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
  843. printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
  844. printf ("For details type: %s -warranty\n", argv [0]) ;
  845. printf ("\n") ;
  846. printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
  847. return 0 ;
  848. }
  849. if (strcasecmp (argv [1], "-warranty") == 0)
  850. {
  851. printf ("gpio version: %s\n", VERSION) ;
  852. printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
  853. printf ("\n") ;
  854. printf (" This program is free software; you can redistribute it and/or modify\n") ;
  855. printf (" it under the terms of the GNU Leser General Public License as published\n") ;
  856. printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
  857. printf (" (at your option) any later version.\n") ;
  858. printf ("\n") ;
  859. printf (" This program is distributed in the hope that it will be useful,\n") ;
  860. printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
  861. printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
  862. printf (" GNU Lesser General Public License for more details.\n") ;
  863. printf ("\n") ;
  864. printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
  865. printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
  866. printf ("\n") ;
  867. return 0 ;
  868. }
  869. if (geteuid () != 0)
  870. {
  871. fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
  872. return 1 ;
  873. }
  874. // Initial test for /sys/class/gpio operations:
  875. /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
  876. else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
  877. else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
  878. else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
  879. else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
  880. // Check for load command:
  881. if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
  882. // Gertboard commands
  883. if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
  884. if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
  885. // Check for -g argument
  886. /**/ if (strcasecmp (argv [1], "-g") == 0)
  887. {
  888. wiringPiSetupGpio () ;
  889. for (i = 2 ; i < argc ; ++i)
  890. argv [i - 1] = argv [i] ;
  891. --argc ;
  892. wpMode = WPI_MODE_GPIO ;
  893. }
  894. // Check for -1 argument
  895. else if (strcasecmp (argv [1], "-1") == 0)
  896. {
  897. wiringPiSetupPhys () ;
  898. for (i = 2 ; i < argc ; ++i)
  899. argv [i - 1] = argv [i] ;
  900. --argc ;
  901. wpMode = WPI_MODE_PHYS ;
  902. }
  903. // Check for -p argument for PiFace
  904. else if (strcasecmp (argv [1], "-p") == 0)
  905. {
  906. piFaceSetup (200) ;
  907. for (i = 2 ; i < argc ; ++i)
  908. argv [i - 1] = argv [i] ;
  909. --argc ;
  910. wpMode = WPI_MODE_PIFACE ;
  911. }
  912. // Default to wiringPi mode
  913. else
  914. {
  915. wiringPiSetup () ;
  916. wpMode = WPI_MODE_PINS ;
  917. }
  918. // Check for -x argument to load in a new extension
  919. if (strcasecmp (argv [1], "-x") == 0)
  920. {
  921. if (argc < 3)
  922. {
  923. fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
  924. exit (EXIT_FAILURE) ;
  925. }
  926. if (!doExtension (argv [0], argv [2])) // Prints its own error messages
  927. exit (EXIT_FAILURE) ;
  928. for (i = 3 ; i < argc ; ++i)
  929. argv [i - 2] = argv [i] ;
  930. argc -= 2 ;
  931. }
  932. if (argc <= 1)
  933. {
  934. fprintf (stderr, "%s: no command given\n", argv [0]) ;
  935. exit (EXIT_FAILURE) ;
  936. }
  937. // Core wiringPi functions
  938. /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
  939. else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
  940. else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
  941. else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
  942. else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
  943. else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
  944. // GPIO Nicies
  945. else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
  946. // Pi Specifics
  947. else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
  948. else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
  949. else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
  950. else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
  951. else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
  952. else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
  953. else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
  954. else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
  955. else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
  956. else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
  957. else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
  958. else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
  959. else
  960. {
  961. fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
  962. exit (EXIT_FAILURE) ;
  963. }
  964. return 0 ;
  965. }