RPI_sysfs_gpio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*****************************************************************************
  2. * | File : SYSFS_GPIO.c
  3. * | Author : Waveshare team
  4. * | Function : Drive SYSFS_ GPIO
  5. * | Info : Read and write /sys/class/gpio
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-04
  9. * | Info : Basic version
  10. *
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # SYSFS_GPIO_IN the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the folSYSFS_GPIO_LOWing conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included SYSFS_GPIO_IN
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. SYSFS_GPIO_IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER SYSFS_GPIO_IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # SYSFS_GPIO_OUT OF OR SYSFS_GPIO_IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS SYSFS_GPIO_IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "RPI_sysfs_gpio.h"
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. int SYSFS_GPIO_Export(int Pin)
  40. {
  41. char buffer[NUM_MAXBUF];
  42. int len;
  43. int fd;
  44. fd = open("/sys/class/gpio/export", O_WRONLY);
  45. if (fd < 0) {
  46. SYSFS_GPIO_Debug( "Export Failed: Pin%d\n", Pin);
  47. return -1;
  48. }
  49. len = snprintf(buffer, NUM_MAXBUF, "%d", Pin);
  50. write(fd, buffer, len);
  51. SYSFS_GPIO_Debug( "Export: Pin%d\r\n", Pin);
  52. close(fd);
  53. return 0;
  54. }
  55. int SYSFS_GPIO_Unexport(int Pin)
  56. {
  57. char buffer[NUM_MAXBUF];
  58. int len;
  59. int fd;
  60. fd = open("/sys/class/gpio/unexport", O_WRONLY);
  61. if (fd < 0) {
  62. SYSFS_GPIO_Debug( "unexport Failed: Pin%d\n", Pin);
  63. return -1;
  64. }
  65. len = snprintf(buffer, NUM_MAXBUF, "%d", Pin);
  66. write(fd, buffer, len);
  67. SYSFS_GPIO_Debug( "Unexport: Pin%d\r\n", Pin);
  68. close(fd);
  69. return 0;
  70. }
  71. int SYSFS_GPIO_Direction(int Pin, int Dir)
  72. {
  73. const char dir_str[] = "in\0out";
  74. char path[DIR_MAXSIZ];
  75. int fd;
  76. snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/direction", Pin);
  77. fd = open(path, O_WRONLY);
  78. if (fd < 0) {
  79. SYSFS_GPIO_Debug( "Set Direction failed: Pin%d\n", Pin);
  80. return -1;
  81. }
  82. if (write(fd, &dir_str[Dir == SYSFS_GPIO_IN ? 0 : 3], Dir == SYSFS_GPIO_IN ? 2 : 3) < 0) {
  83. SYSFS_GPIO_Debug("failed to set direction!\r\n");
  84. return -1;
  85. }
  86. if(Dir == SYSFS_GPIO_IN){
  87. SYSFS_GPIO_Debug("Pin%d:intput\r\n", Pin);
  88. }else{
  89. SYSFS_GPIO_Debug("Pin%d:Output\r\n", Pin);
  90. }
  91. close(fd);
  92. return 0;
  93. }
  94. int SYSFS_GPIO_Read(int Pin)
  95. {
  96. char path[DIR_MAXSIZ];
  97. char value_str[3];
  98. int fd;
  99. snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin);
  100. fd = open(path, O_RDONLY);
  101. if (fd < 0) {
  102. SYSFS_GPIO_Debug( "Read failed Pin%d\n", Pin);
  103. return -1;
  104. }
  105. if (read(fd, value_str, 3) < 0) {
  106. SYSFS_GPIO_Debug( "failed to read value!\n");
  107. return -1;
  108. }
  109. close(fd);
  110. return(atoi(value_str));
  111. }
  112. int SYSFS_GPIO_Write(int Pin, int value)
  113. {
  114. const char s_values_str[] = "01";
  115. char path[DIR_MAXSIZ];
  116. int fd;
  117. snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin);
  118. fd = open(path, O_WRONLY);
  119. if (fd < 0) {
  120. SYSFS_GPIO_Debug( "Write failed : Pin%d,value = %d\n", Pin, value);
  121. return -1;
  122. }
  123. if (write(fd, &s_values_str[value == SYSFS_GPIO_LOW ? 0 : 1], 1) < 0) {
  124. SYSFS_GPIO_Debug( "failed to write value!\n");
  125. return -1;
  126. }
  127. close(fd);
  128. return 0;
  129. }