gpio-utils.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Copyright (c) 2019 FunKey
  2. * All rights reserved.
  3. *
  4. */
  5. #include "gpio-utils.h"
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. /****************************************************************
  12. * gpio_export
  13. ****************************************************************/
  14. int gpio_export(unsigned int gpio)
  15. {
  16. int fd, len;
  17. char buf[MAX_BUF];
  18. fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
  19. if (fd < 0) {
  20. perror("gpio/export");
  21. return fd;
  22. }
  23. len = snprintf(buf, sizeof(buf), "%d", gpio);
  24. write(fd, buf, len);
  25. close(fd);
  26. return 0;
  27. }
  28. /****************************************************************
  29. * gpio_unexport
  30. ****************************************************************/
  31. int gpio_unexport(unsigned int gpio)
  32. {
  33. int fd, len;
  34. char buf[MAX_BUF];
  35. fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
  36. if (fd < 0) {
  37. perror("gpio/export");
  38. return fd;
  39. }
  40. len = snprintf(buf, sizeof(buf), "%d", gpio);
  41. write(fd, buf, len);
  42. close(fd);
  43. return 0;
  44. }
  45. /****************************************************************
  46. * gpio_set_dir
  47. ****************************************************************/
  48. int gpio_set_dir(unsigned int gpio, const char* dir)
  49. {
  50. int fd, len;
  51. char buf[MAX_BUF];
  52. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
  53. fd = open(buf, O_WRONLY);
  54. if (fd < 0) {
  55. perror("gpio/direction");
  56. return fd;
  57. }
  58. write(fd, dir, sizeof(dir)+1);
  59. close(fd);
  60. return 0;
  61. }
  62. /****************************************************************
  63. * gpio_set_value
  64. ****************************************************************/
  65. int gpio_set_value(unsigned int gpio, unsigned int value)
  66. {
  67. int fd, len;
  68. char buf[MAX_BUF];
  69. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  70. fd = open(buf, O_WRONLY);
  71. if (fd < 0) {
  72. perror("gpio/set-value");
  73. return fd;
  74. }
  75. if (value)
  76. write(fd, "1", 2);
  77. else
  78. write(fd, "0", 2);
  79. close(fd);
  80. return 0;
  81. }
  82. /****************************************************************
  83. * gpio_get_value
  84. ****************************************************************/
  85. int gpio_get_value(unsigned int gpio, unsigned int *value)
  86. {
  87. int fd, len;
  88. char buf[MAX_BUF];
  89. char ch;
  90. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  91. fd = open(buf, O_RDONLY);
  92. if (fd < 0) {
  93. perror("gpio/get-value");
  94. return fd;
  95. }
  96. read(fd, &ch, 1);
  97. if (ch != '0') {
  98. *value = 1;
  99. } else {
  100. *value = 0;
  101. }
  102. close(fd);
  103. return 0;
  104. }
  105. /****************************************************************
  106. * gpio_set_edge
  107. ****************************************************************/
  108. int gpio_set_edge(unsigned int gpio, const char *edge)
  109. {
  110. int fd, len;
  111. char buf[MAX_BUF];
  112. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
  113. fd = open(buf, O_WRONLY);
  114. if (fd < 0) {
  115. perror("gpio/set-edge");
  116. return fd;
  117. }
  118. write(fd, edge, strlen(edge) + 1);
  119. close(fd);
  120. return 0;
  121. }
  122. /****************************************************************
  123. * gpio_fd_open
  124. ****************************************************************/
  125. int gpio_fd_open(unsigned int gpio, unsigned int dir)
  126. {
  127. int fd, len;
  128. char buf[MAX_BUF];
  129. len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  130. fd = open(buf, dir | O_NONBLOCK );
  131. if (fd < 0) {
  132. perror("gpio/fd_open");
  133. }
  134. return fd;
  135. }
  136. /****************************************************************
  137. * gpio_fd_close
  138. ****************************************************************/
  139. int gpio_fd_close(int fd)
  140. {
  141. return close(fd);
  142. }
  143. /****************************************************************
  144. * ain_get_value (from Mike McDonald)
  145. * https://groups.google.com/forum/#!topic/beagleboard-ece497/SLJ5nQQ_GoU
  146. ****************************************************************/
  147. int ain_get_value(unsigned int ain, unsigned int *value)
  148. {
  149. int fd, len, bytesRead;
  150. char buf[MAX_BUF];
  151. char adc_in[ADC_BUF];
  152. len = snprintf(buf, sizeof(buf), SYSFS_AIN_DIR "/AIN%d", ain);
  153. fd = open(buf, O_RDONLY);
  154. if (fd < 0) {
  155. perror(buf);
  156. return fd;
  157. }
  158. // Read from the
  159. bytesRead = read(fd, adc_in, ADC_BUF);
  160. // Turn the buffer value (a string) into an integer
  161. if (bytesRead != -1) {
  162. *value = atoi(adc_in);
  163. adc_in[bytesRead] = (int)NULL;
  164. lseek(fd, 0, SEEK_SET);
  165. }
  166. // Sleep for a little to ensure that we get good ADC values
  167. usleep(1000);
  168. close(fd);
  169. return bytesRead;
  170. }