b.liu | 98a8ebf | 2024-06-14 15:53:25 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <errno.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <string.h> |
| 8 | #include <fcntl.h> |
| 9 | |
| 10 | #include "mbtk_log.h" |
| 11 | #include "mbtk_utils.h" |
| 12 | #include "mbtk_gpio.h" |
| 13 | |
| 14 | |
r.xiao | 12cca6d | 2024-11-03 23:30:27 -0800 | [diff] [blame^] | 15 | int mbtk_gpio_export(int gpio) |
b.liu | 98a8ebf | 2024-06-14 15:53:25 +0800 | [diff] [blame] | 16 | { |
| 17 | int fd = -1; |
| 18 | char buffer[50]; |
| 19 | |
| 20 | memset(buffer, 0, sizeof(buffer)); |
| 21 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 22 | if(access(buffer , F_OK) == 0) |
| 23 | { |
| 24 | LOGD("%d has export.", gpio); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | fd = open("/sys/class/gpio/export", O_WRONLY); |
| 29 | if(fd < 0) |
| 30 | { |
| 31 | LOGE("Open gpio export file fail."); |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | memset(buffer, 0, sizeof(buffer)); |
| 36 | sprintf(buffer , "%d", gpio); |
| 37 | if(write(fd, buffer, strlen(buffer)) <= 0) |
| 38 | { |
| 39 | LOGE("Gpio[%d] export fail.", gpio); |
| 40 | close(fd); |
| 41 | return -1; |
| 42 | } |
| 43 | close(fd); |
| 44 | return 0; |
| 45 | } |
| 46 | |
r.xiao | 12cca6d | 2024-11-03 23:30:27 -0800 | [diff] [blame^] | 47 | int mbtk_gpio_unexport(int gpio) |
b.liu | 98a8ebf | 2024-06-14 15:53:25 +0800 | [diff] [blame] | 48 | { |
| 49 | int fd = -1; |
| 50 | char buffer[50]; |
| 51 | |
| 52 | memset(buffer, 0, sizeof(buffer)); |
| 53 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 54 | if(access(buffer , F_OK)) |
| 55 | { |
| 56 | LOGD("%d not export.", gpio); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | fd = open("/sys/class/gpio/unexport", O_WRONLY); |
| 61 | if(fd < 0) |
| 62 | { |
| 63 | LOGE("Open gpio unexport file fail."); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | memset(buffer, 0, sizeof(buffer)); |
| 68 | sprintf(buffer , "%d", gpio); |
| 69 | if(write(fd, buffer, strlen(buffer)) <= 0) |
| 70 | { |
| 71 | LOGE("Gpio[%d] unexport fail.", gpio); |
| 72 | close(fd); |
| 73 | return -1; |
| 74 | } |
| 75 | close(fd); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | mbtk_gpio_direct_t mbtk_gpio_direct_get(int gpio) |
| 80 | { |
| 81 | char buffer[50]; |
| 82 | int fd = -1; |
| 83 | |
| 84 | memset(buffer, 0, sizeof(buffer)); |
| 85 | sprintf(buffer, "/sys/class/gpio/gpio%d/direction", gpio); |
| 86 | fd = open(buffer, O_RDONLY); |
| 87 | if(fd < 0) |
| 88 | { |
| 89 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 90 | return MBTK_GPIO_DIRECT_UNKNOWN; |
| 91 | } |
| 92 | |
| 93 | memset(buffer, 0x0, sizeof(buffer)); |
| 94 | if(read(fd, buffer, sizeof(buffer)) <= 0) |
| 95 | { |
| 96 | LOGE("Get gpio[%d] direct fail.", gpio); |
| 97 | close(fd); |
| 98 | return MBTK_GPIO_DIRECT_UNKNOWN; |
| 99 | } |
| 100 | close(fd); |
| 101 | |
| 102 | if(strcmp(buffer, "out") == 0) { |
| 103 | return MBTK_GPIO_DIRECT_OUT; |
| 104 | } else if(strcmp(buffer, "in") == 0) { |
| 105 | return MBTK_GPIO_DIRECT_IN; |
| 106 | } else { |
| 107 | LOGE("direct : %s", buffer); |
| 108 | return MBTK_GPIO_DIRECT_UNKNOWN; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int mbtk_gpio_direct_set(int gpio, mbtk_gpio_direct_t dir) |
| 113 | { |
| 114 | char buffer[50]; |
| 115 | int fd = -1; |
| 116 | int ret = 0; |
| 117 | |
r.xiao | 12cca6d | 2024-11-03 23:30:27 -0800 | [diff] [blame^] | 118 | if(mbtk_gpio_export(gpio)) { |
b.liu | 98a8ebf | 2024-06-14 15:53:25 +0800 | [diff] [blame] | 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | memset(buffer, 0, sizeof(buffer)); |
| 123 | sprintf(buffer, "/sys/class/gpio/gpio%d/direction", gpio); |
| 124 | fd = open(buffer, O_WRONLY); |
| 125 | if(fd < 0) |
| 126 | { |
| 127 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | if(MBTK_GPIO_DIRECT_OUT == dir) { |
| 132 | if(write(fd, "out", 3) != 3) { |
| 133 | ret = -1; |
| 134 | LOGE("Write out fail:%d", errno); |
| 135 | } |
| 136 | } else if(MBTK_GPIO_DIRECT_IN == dir) { |
| 137 | if(write(fd, "in", 2) != 2) { |
| 138 | ret = -1; |
| 139 | LOGE("Write in fail:%d", errno); |
| 140 | } |
| 141 | } else { |
| 142 | LOGE("Unknown direct : %d", dir); |
| 143 | ret = -1; |
| 144 | } |
| 145 | |
| 146 | close(fd); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | int mbtk_gpio_value_get(int gpio) |
| 151 | { |
| 152 | char buffer[50]; |
| 153 | int fd =-1; |
| 154 | |
| 155 | memset(buffer, 0, sizeof(buffer)); |
| 156 | sprintf(buffer, "/sys/class/gpio/gpio%d/value", gpio); |
| 157 | fd = open(buffer, O_RDONLY); |
| 158 | if(fd == -1) |
| 159 | { |
| 160 | LOGE("Open gpio[%d] fail.", gpio); |
| 161 | return -1; |
| 162 | } |
| 163 | |
r.xiao | 12cca6d | 2024-11-03 23:30:27 -0800 | [diff] [blame^] | 164 | memset(buffer, 0, sizeof(buffer)); |
b.liu | 98a8ebf | 2024-06-14 15:53:25 +0800 | [diff] [blame] | 165 | if(read(fd, buffer, sizeof(buffer)) <= 0) |
| 166 | { |
| 167 | LOGE("Get gpio[%d] value fail", gpio); |
| 168 | close(fd); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | close(fd); |
| 173 | return atoi(buffer); |
| 174 | } |
| 175 | |
| 176 | int mbtk_gpio_value_set(int gpio, mbtk_gpio_direct_t dir, int value) |
| 177 | { |
| 178 | char buffer[50]; |
| 179 | int fd = -1; |
| 180 | int ret =-1; |
| 181 | |
r.xiao | 12cca6d | 2024-11-03 23:30:27 -0800 | [diff] [blame^] | 182 | if(mbtk_gpio_export(gpio)) { |
b.liu | 98a8ebf | 2024-06-14 15:53:25 +0800 | [diff] [blame] | 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | if(mbtk_gpio_direct_set(gpio, dir)) { |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | memset(buffer, 0, sizeof(buffer)); |
| 191 | sprintf(buffer, "/sys/class/gpio/gpio%d/value", gpio); |
| 192 | fd = open(buffer, O_WRONLY); |
| 193 | if(fd == -1) |
| 194 | { |
| 195 | LOGE("Open gpio[%d] value fail.", gpio); |
| 196 | return -1; |
| 197 | } |
| 198 | if(value == 0) { |
| 199 | ret = write(fd, "0", 1); |
| 200 | } else { |
| 201 | ret = write(fd, "1", 1); |
| 202 | } |
| 203 | |
| 204 | close(fd); |
| 205 | if(ret != 1) |
| 206 | { |
| 207 | LOGE("Set gpio[%d] value fail.", gpio); |
| 208 | return -1; |
| 209 | } else { |
| 210 | return 0; |
| 211 | } |
| 212 | } |
| 213 | |
r.xiao | 12cca6d | 2024-11-03 23:30:27 -0800 | [diff] [blame^] | 214 | int mbtk_gpio_value_set_2(int gpio, int value) |
r.xiao | a149363 | 2024-10-25 02:19:35 -0700 | [diff] [blame] | 215 | { |
| 216 | char buffer[50]= {0}; |
| 217 | int file =-1; |
| 218 | int result =-1; |
| 219 | |
| 220 | memset(buffer,0,50); |
| 221 | sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio); |
| 222 | file = open(buffer,O_WRONLY); |
| 223 | if(file == -1) |
| 224 | { |
| 225 | LOGE("Open gpio[%d] value fail.", gpio); |
| 226 | return -1; |
| 227 | } |
| 228 | if(value == 0) { |
| 229 | result = write(file,"0",1); |
| 230 | } else { |
| 231 | result = write(file,"1",1); |
| 232 | } |
| 233 | if(result != 1) |
| 234 | { |
| 235 | LOGE("Set gpio[%d] value fail err =%d.", gpio, errno); |
| 236 | close(file); |
| 237 | return -1; |
| 238 | } |
| 239 | close(file); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | |