gpio-utils.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright (c) 2011, RidgeRun
  2. * All rights reserved.
  3. *
  4. From https://www.ridgerun.com/developer/wiki/index.php/Gpio-int-test.c
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the RidgeRun.
  16. * 4. Neither the name of the RidgeRun nor the
  17. * names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "gpio-utils.h"
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <string.h>
  37. /****************************************************************
  38. * gpio_export
  39. ****************************************************************/
  40. int gpio_export(unsigned int gpio)
  41. {
  42. int fd, len;
  43. char buf[MAX_BUF];
  44. fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
  45. if (fd < 0) {
  46. perror("gpio/export");
  47. return fd;
  48. }
  49. len = snprintf(buf, sizeof(buf), "%d", gpio);
  50. write(fd, buf, len);
  51. close(fd);
  52. return 0;
  53. }
  54. /****************************************************************
  55. * gpio_unexport
  56. ****************************************************************/
  57. int gpio_unexport(unsigned int gpio)
  58. {
  59. int fd, len;
  60. char buf[MAX_BUF];
  61. fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
  62. if (fd < 0) {
  63. perror("gpio/export");
  64. return fd;
  65. }
  66. len = snprintf(buf, sizeof(buf), "%d", gpio);
  67. write(fd, buf, len);
  68. close(fd);
  69. return 0;
  70. }
  71. /****************************************************************
  72. * gpio_set_dir
  73. ****************************************************************/
  74. int gpio_set_dir(unsigned int gpio, const char* dir)
  75. {
  76. int fd, len;
  77. char buf[MAX_BUF];
  78. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
  79. fd = open(buf, O_WRONLY);
  80. if (fd < 0) {
  81. perror("gpio/direction");
  82. return fd;
  83. }
  84. write(fd, dir, sizeof(dir)+1);
  85. close(fd);
  86. return 0;
  87. }
  88. /****************************************************************
  89. * gpio_set_value
  90. ****************************************************************/
  91. int gpio_set_value(unsigned int gpio, unsigned int value)
  92. {
  93. int fd, len;
  94. char buf[MAX_BUF];
  95. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  96. fd = open(buf, O_WRONLY);
  97. if (fd < 0) {
  98. perror("gpio/set-value");
  99. return fd;
  100. }
  101. if (value)
  102. write(fd, "1", 2);
  103. else
  104. write(fd, "0", 2);
  105. close(fd);
  106. return 0;
  107. }
  108. /****************************************************************
  109. * gpio_get_value
  110. ****************************************************************/
  111. int gpio_get_value(unsigned int gpio, unsigned int *value)
  112. {
  113. int fd, len;
  114. char buf[MAX_BUF];
  115. char ch;
  116. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  117. fd = open(buf, O_RDONLY);
  118. if (fd < 0) {
  119. perror("gpio/get-value");
  120. return fd;
  121. }
  122. read(fd, &ch, 1);
  123. if (ch != '0') {
  124. *value = 1;
  125. } else {
  126. *value = 0;
  127. }
  128. close(fd);
  129. return 0;
  130. }
  131. /****************************************************************
  132. * gpio_set_edge
  133. ****************************************************************/
  134. int gpio_set_edge(unsigned int gpio, const char *edge)
  135. {
  136. int fd, len;
  137. char buf[MAX_BUF];
  138. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
  139. fd = open(buf, O_WRONLY);
  140. if (fd < 0) {
  141. perror("gpio/set-edge");
  142. return fd;
  143. }
  144. write(fd, edge, strlen(edge) + 1);
  145. close(fd);
  146. return 0;
  147. }
  148. /****************************************************************
  149. * gpio_fd_open
  150. ****************************************************************/
  151. int gpio_fd_open(unsigned int gpio, unsigned int dir)
  152. {
  153. int fd, len;
  154. char buf[MAX_BUF];
  155. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  156. fd = open(buf, dir | O_NONBLOCK );
  157. if (fd < 0) {
  158. perror("gpio/fd_open");
  159. }
  160. return fd;
  161. }
  162. /****************************************************************
  163. * gpio_fd_close
  164. ****************************************************************/
  165. int gpio_fd_close(int fd)
  166. {
  167. return close(fd);
  168. }
  169. /****************************************************************
  170. * ain_get_value (from Mike McDonald)
  171. * https://groups.google.com/forum/#!topic/beagleboard-ece497/SLJ5nQQ_GoU
  172. ****************************************************************/
  173. int ain_get_value(unsigned int ain, unsigned int *value)
  174. {
  175. int fd, len, bytesRead;
  176. char buf[MAX_BUF];
  177. char adc_in[ADC_BUF];
  178. len = snprintf(buf, sizeof(buf), SYSFS_AIN_DIR "/AIN%d", ain);
  179. fd = open(buf, O_RDONLY);
  180. if (fd < 0) {
  181. perror(buf);
  182. return fd;
  183. }
  184. // Read from the
  185. bytesRead = read(fd, adc_in, ADC_BUF);
  186. // Turn the buffer value (a string) into an integer
  187. if (bytesRead != -1) {
  188. *value = atoi(adc_in);
  189. adc_in[bytesRead] = (int)NULL;
  190. lseek(fd, 0, SEEK_SET);
  191. }
  192. // Sleep for a little to ensure that we get good ADC values
  193. usleep(1000);
  194. close(fd);
  195. return bytesRead;
  196. }