sysfs_gpio.c (4138B)
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 # 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 following conditions: 18 # 19 # The above copyright notice and this permission notice shall be included 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. IN NO EVENT SHALL THE 25 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 # THE SOFTWARE. 29 # 30 ******************************************************************************/ 31 #include "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 40 int SYSFS_GPIO_Export(int Pin) 41 { 42 char buffer[NUM_MAXBUF]; 43 int len; 44 int fd; 45 46 fd = open("/sys/class/gpio/export", O_WRONLY); 47 if (fd < 0) { 48 SYSFS_GPIO_Debug( "Export Failed: Pin%d\n", Pin); 49 return -1; 50 } 51 52 len = snprintf(buffer, NUM_MAXBUF, "%d", Pin); 53 write(fd, buffer, len); 54 55 SYSFS_GPIO_Debug( "Export: Pin%d\r\n", Pin); 56 57 close(fd); 58 return 0; 59 } 60 61 int SYSFS_GPIO_Unexport(int Pin) 62 { 63 char buffer[NUM_MAXBUF]; 64 int len; 65 int fd; 66 67 fd = open("/sys/class/gpio/unexport", O_WRONLY); 68 if (fd < 0) { 69 SYSFS_GPIO_Debug( "unexport Failed: Pin%d\n", Pin); 70 return -1; 71 } 72 73 len = snprintf(buffer, NUM_MAXBUF, "%d", Pin); 74 write(fd, buffer, len); 75 76 SYSFS_GPIO_Debug( "Unexport: Pin%d\r\n", Pin); 77 78 close(fd); 79 return 0; 80 } 81 82 int SYSFS_GPIO_Direction(int Pin, int Dir) 83 { 84 const char dir_str[] = "in\0out"; 85 char path[DIR_MAXSIZ]; 86 int fd; 87 88 snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/direction", Pin); 89 fd = open(path, O_WRONLY); 90 if (fd < 0) { 91 SYSFS_GPIO_Debug( "Set Direction failed: Pin%d\n", Pin); 92 return -1; 93 } 94 95 if (write(fd, &dir_str[Dir == IN ? 0 : 3], Dir == IN ? 2 : 3) < 0) { 96 SYSFS_GPIO_Debug("failed to set direction!\r\n"); 97 return -1; 98 } 99 100 if(Dir == IN){ 101 SYSFS_GPIO_Debug("Pin%d:intput\r\n", Pin); 102 }else{ 103 SYSFS_GPIO_Debug("Pin%d:Output\r\n", Pin); 104 } 105 106 close(fd); 107 return 0; 108 } 109 110 int SYSFS_GPIO_Read(int Pin) 111 { 112 char path[DIR_MAXSIZ]; 113 char value_str[3]; 114 int fd; 115 116 snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin); 117 fd = open(path, O_RDONLY); 118 if (fd < 0) { 119 SYSFS_GPIO_Debug( "Read failed Pin%d\n", Pin); 120 return -1; 121 } 122 123 if (read(fd, value_str, 3) < 0) { 124 SYSFS_GPIO_Debug( "failed to read value!\n"); 125 return -1; 126 } 127 128 close(fd); 129 return(atoi(value_str)); 130 } 131 132 int SYSFS_GPIO_Write(int Pin, int value) 133 { 134 const char s_values_str[] = "01"; 135 char path[DIR_MAXSIZ]; 136 int fd; 137 138 snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin); 139 fd = open(path, O_WRONLY); 140 if (fd < 0) { 141 SYSFS_GPIO_Debug( "Write failed : Pin%d,value = %d\n", Pin, value); 142 return -1; 143 } 144 145 if (write(fd, &s_values_str[value == LOW ? 0 : 1], 1) < 0) { 146 SYSFS_GPIO_Debug( "failed to write value!\n"); 147 return -1; 148 } 149 150 close(fd); 151 return 0; 152 }