commGPIO.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "commGPIO.h"
  2. int gpio_export(int pin)
  3. {
  4. char buffer[64];
  5. int len;
  6. int fd;
  7. fd = open("/sys/class/gpio/export", O_WRONLY);
  8. if (fd < 0) {
  9. MSG("Failed to open export for writing!");
  10. return(-1);
  11. }
  12. len = snprintf(buffer, sizeof(buffer), "%d", pin);
  13. if (write(fd, buffer, len) < 0) {
  14. MSG("Failed to export gpio!");
  15. return -1;
  16. }
  17. close(fd);
  18. return 0;
  19. }
  20. int gpio_unexport(int pin)
  21. {
  22. char buffer[64];
  23. int len;
  24. int fd;
  25. fd = open("/sys/class/gpio/unexport", O_WRONLY);
  26. if (fd < 0) {
  27. MSG("Failed to open unexport for writing!");
  28. return -1;
  29. }
  30. len = snprintf(buffer, sizeof(buffer), "%d", pin);
  31. if (write(fd, buffer, len) < 0) {
  32. MSG("Failed to unexport gpio!");
  33. return -1;
  34. }
  35. close(fd);
  36. return 0;
  37. }
  38. //dir: 0-->IN, 1-->OUT
  39. int gpio_direction(int pin, int dir)
  40. {
  41. static const char dir_str[] = "in\0out";
  42. char path[64];
  43. int fd;
  44. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
  45. fd = open(path, O_WRONLY);
  46. if (fd < 0) {
  47. MSG("Failed to open gpio direction for writing!");
  48. return -1;
  49. }
  50. if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0) {
  51. MSG("Failed to set direction!");
  52. return -1;
  53. }
  54. close(fd);
  55. return 0;
  56. }
  57. //value: 0-->LOW, 1-->HIGH
  58. int gpio_write(int pin, int value)
  59. {
  60. static const char values_str[] = "01";
  61. char path[64];
  62. int fd;
  63. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
  64. fd = open(path, O_WRONLY);
  65. if (fd < 0) {
  66. MSG("Failed to open gpio value for writing!");
  67. return -1;
  68. }
  69. if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {
  70. MSG("Failed to write value!");
  71. return -1;
  72. }
  73. close(fd);
  74. return 0;
  75. }
  76. int gpio_read(int pin)
  77. {
  78. char path[64];
  79. char value_str[3];
  80. int fd;
  81. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
  82. fd = open(path, O_RDONLY);
  83. if (fd < 0) {
  84. MSG("Failed to open gpio value for reading!");
  85. return -1;
  86. }
  87. if (read(fd, value_str, 3) < 0) {
  88. MSG("Failed to read value!");
  89. return -1;
  90. }
  91. close(fd);
  92. return (atoi(value_str));
  93. }
  94. // none表示引脚为输入,不是中断引脚
  95. // rising表示引脚为中断输入,上升沿触发
  96. // falling表示引脚为中断输入,下降沿触发
  97. // both表示引脚为中断输入,边沿触发
  98. // 0-->none, 1-->rising, 2-->falling, 3-->both
  99. int gpio_edge(int pin, int edge)
  100. {
  101. const char dir_str[] = "none\0rising\0falling\0both";
  102. int ptr;
  103. char path[64];
  104. int fd;
  105. switch(edge){
  106. case 0:
  107. ptr = 0;
  108. break;
  109. case 1:
  110. ptr = 5;
  111. break;
  112. case 2:
  113. ptr = 12;
  114. break;
  115. case 3:
  116. ptr = 20;
  117. break;
  118. default:
  119. ptr = 0;
  120. break;
  121. }
  122. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
  123. fd = open(path, O_WRONLY);
  124. if (fd < 0) {
  125. MSG("Failed to open gpio edge for writing!");
  126. return -1;
  127. }
  128. if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0) {
  129. MSG("Failed to set edge!");
  130. return -1;
  131. }
  132. close(fd);
  133. return 0;
  134. }
  135. //实现中断检测函数
  136. //GPIO1_17
  137. int main_test()
  138. {
  139. int gpio_fd, ret;
  140. struct pollfd fds[1];
  141. char buff[10];
  142. unsigned char cnt = 0;
  143. //LED引脚初始化
  144. gpio_export(115);
  145. gpio_direction(115, 1);
  146. gpio_write(115, 0);
  147. //按键引脚初始化
  148. gpio_export(49);
  149. gpio_direction(49, 0);
  150. gpio_edge(49,1);
  151. gpio_fd = open("/sys/class/gpio/gpio49/value",O_RDONLY);
  152. if(gpio_fd < 0){
  153. MSG("Failed to open value!");
  154. return -1;
  155. }
  156. fds[0].fd = gpio_fd;
  157. fds[0].events = POLLPRI;
  158. ret = read(gpio_fd,buff,10);
  159. if( ret == -1 )
  160. MSG("read");
  161. while(1){
  162. ret = poll(fds,1,0);
  163. if( ret == -1 )
  164. MSG("poll\n");
  165. if( fds[0].revents & POLLPRI){
  166. ret = lseek(gpio_fd,0,SEEK_SET);
  167. if( ret == -1 )
  168. MSG("lseek");
  169. ret = read(gpio_fd,buff,10);
  170. if( ret == -1 )
  171. MSG("read");
  172. gpio_write(115, cnt++%2);
  173. }
  174. usleep(100000);
  175. }
  176. return 0;
  177. }