b.liu | 1c74d69 | 2024-08-14 17:43:59 +0800 | [diff] [blame] | 1 | #include "mbtk_type.h" |
| 2 | #include "lynq-gpio.h" |
| 3 | #include "unistd.h" |
| 4 | #include "fcntl.h" |
| 5 | #include "mbtk_log.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | static int gpio_export(int gpio) |
| 9 | { |
| 10 | int index=0; |
| 11 | int file=-1; |
| 12 | int result =-1; |
| 13 | char pin_index_buffer[5]= {0}; |
| 14 | |
| 15 | char buffer[50]; |
| 16 | memset(buffer,0,50); |
| 17 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 18 | if(access(buffer , F_OK) == 0) |
| 19 | { |
| 20 | LOGD("%d has export.", gpio); |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | file = open("/sys/class/gpio/export",O_WRONLY); |
| 25 | if(file == -1) |
| 26 | { |
| 27 | LOGE("Open gpio export file fail."); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | memset(pin_index_buffer,0,5); |
| 32 | sprintf(pin_index_buffer,"%d", gpio); |
| 33 | result = write(file,pin_index_buffer,strlen(pin_index_buffer)); |
| 34 | if(result < 0) |
| 35 | { |
| 36 | LOGE("Gpio[%d] export fail. err = %d", gpio, errno); |
| 37 | close(file); |
| 38 | return -1; |
| 39 | } |
| 40 | close(file); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int gpio_unexport(int gpio) |
| 46 | { |
| 47 | int index=0; |
| 48 | int file=-1; |
| 49 | int result =-1; |
| 50 | char pin_index_buffer[5]= {0}; |
| 51 | char buffer[50]; |
| 52 | memset(buffer,0,50); |
| 53 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 54 | if(access(buffer , F_OK) == -1) |
| 55 | { |
| 56 | LOGD("%d not export.", gpio); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | file = open("/sys/class/gpio/unexport",O_WRONLY); |
| 61 | if(file == -1) |
| 62 | { |
| 63 | LOGE("Open gpio unexport file fail."); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | memset(pin_index_buffer,0,5); |
| 68 | sprintf(pin_index_buffer,"%d", gpio); |
| 69 | result=write(file,pin_index_buffer,strlen(pin_index_buffer)); |
| 70 | if(result < 0) |
| 71 | { |
| 72 | close(file); |
| 73 | LOGE("Gpio[%d] unexport fail.", gpio); |
| 74 | return -1; |
| 75 | } |
| 76 | close(file); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int gpio_direct_get(int gpio, char *value, int value_size) |
| 82 | { |
| 83 | char buffer[50]= {0}; |
| 84 | int file =-1; |
| 85 | int result =-1; |
| 86 | |
| 87 | memset(buffer,0,50); |
| 88 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 89 | file = open(buffer, O_RDONLY); |
| 90 | if(file == -1) |
| 91 | { |
| 92 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | memset(value, 0x0, value_size); |
| 97 | result = read(file,value,value_size); |
| 98 | if(result <= 0) |
| 99 | { |
| 100 | LOGE("Get gpio[%d] direct fail.", gpio); |
| 101 | close(file); |
| 102 | return -1; |
| 103 | } |
| 104 | close(file); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static int gpio_direct_set(int gpio, char *value) |
| 111 | { |
| 112 | char buffer[50]= {0}; |
| 113 | int file =-1; |
| 114 | int result =-1; |
| 115 | |
| 116 | memset(buffer,0,50); |
| 117 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 118 | file = open(buffer, O_WRONLY); |
| 119 | if(file == -1) |
| 120 | { |
| 121 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | result = write(file,value,strlen(value)); |
| 126 | if(result != strlen(value)) |
| 127 | { |
| 128 | LOGE("Set gpio[%d] direct fail.", gpio); |
| 129 | close(file); |
| 130 | return -1; |
| 131 | } |
| 132 | close(file); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int gpio_value_get(int gpio) |
| 138 | { |
| 139 | char buffer[50]; |
| 140 | char path[10]; |
| 141 | int file =-1; |
| 142 | int result =-1; |
| 143 | int value; |
| 144 | |
| 145 | memset(path,0,50); |
| 146 | memset(buffer,0,10); |
| 147 | sprintf(path,"/sys/class/gpio/gpio%d/value", gpio); |
| 148 | file = open(path,O_RDONLY); |
| 149 | if(file == -1) |
| 150 | { |
| 151 | LOGE("Open gpio[%d] fail.", gpio); |
| 152 | return -1; |
| 153 | } |
| 154 | result = read(file,buffer,5); |
| 155 | if(result <= 0) |
| 156 | { |
| 157 | LOGE("Get gpio[%d] value fail", gpio); |
| 158 | close(file); |
| 159 | return -1; |
| 160 | } |
| 161 | close(file); |
| 162 | value = atoi(buffer); |
| 163 | return value; |
| 164 | } |
| 165 | |
| 166 | static int gpio_value_set(int gpio, int value) |
| 167 | { |
| 168 | char buffer[50]= {0}; |
| 169 | int file =-1; |
| 170 | int result =-1; |
| 171 | |
| 172 | memset(buffer,0,50); |
| 173 | sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio); |
| 174 | file = open(buffer,O_WRONLY); |
| 175 | if(file == -1) |
| 176 | { |
| 177 | LOGE("Open gpio[%d] value fail.", gpio); |
| 178 | return -1; |
| 179 | } |
| 180 | if(value == 0) { |
| 181 | result = write(file,"0",1); |
| 182 | } else { |
| 183 | result = write(file,"1",1); |
| 184 | } |
| 185 | if(result != 1) |
| 186 | { |
| 187 | LOGE("Set gpio[%d] value fail err =%d.", gpio, errno); |
| 188 | close(file); |
| 189 | return -1; |
| 190 | } |
| 191 | close(file); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | int lynq_gpio_init(int gpio, int direction, int value, int pullsel) |
| 198 | { |
| 199 | //UNUSED(gpio); |
| 200 | //UNUSED(direction); |
| 201 | //UNUSED(value); |
| 202 | UNUSED(pullsel); |
| 203 | |
| 204 | if (direction != 1 && direction != 0) |
| 205 | { |
| 206 | LOGE("[lynq_gpio_init] direction fail."); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | if (value != 1 && value != 0) |
| 211 | { |
| 212 | LOGE("[lynq_gpio_init] value fail."); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | if(gpio_export(gpio)) |
| 217 | { |
| 218 | LOGE("[lynq_gpio_init]gpio_export fail."); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | if(gpio_direct_set(gpio, direction == 0 ? "in" : "out")) |
| 223 | { |
| 224 | LOGE("[lynq_gpio_init]gpio_direct_set fail."); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | if(direction == 1 && (gpio_value_set(gpio, value) != 0)) |
| 229 | { |
| 230 | LOGE("[lynq_gpio_init]gpio_value_set fail."); |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int lynq_gpio_deinit(int gpio) |
| 239 | { |
| 240 | UNUSED(gpio); |
| 241 | |
| 242 | if(gpio_unexport(gpio)) |
| 243 | { |
| 244 | LOGE("[lynq_gpio_deinit]gpio_unexport fail."); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | int lynq_gpio_direction_set(int gpio, int direction) |
| 252 | { |
| 253 | //UNUSED(gpio); |
| 254 | //UNUSED(direction); |
| 255 | |
| 256 | if(gpio_direct_set(gpio, direction == 0 ? "in" : "out")) |
| 257 | { |
| 258 | LOGE("[lynq_gpio_direction_set]gpio_direct_set fail."); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | int lynq_gpio_value_set(int gpio, int value) |
| 267 | { |
| 268 | //UNUSED(gpio); |
| 269 | //UNUSED(value); |
| 270 | |
| 271 | if(gpio_value_set(gpio, value)) |
| 272 | { |
| 273 | LOGE("[lynq_gpio_value_set]gpio_value_set fail."); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | int lynq_gpio_value_get(int gpio) |
| 281 | { |
| 282 | //UNUSED(gpio); |
| 283 | int ret = -1; |
| 284 | |
| 285 | ret = gpio_value_get(gpio); |
| 286 | if (ret == -1) |
| 287 | { |
| 288 | LOGE("[lynq_gpio_value_get]gpio_value_get fail."); |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | int lynq_gpio_pullsel_set(int gpio, int pullsel) |
| 296 | { |
| 297 | //UNUSED(gpio); |
| 298 | //UNUSED(pullsel); |
| 299 | int ret = -1; |
| 300 | int value_t; |
| 301 | |
| 302 | if (pullsel == 1) |
| 303 | value_t = 0; |
| 304 | else if (pullsel == 2) |
| 305 | value_t = 1; |
| 306 | else |
| 307 | { |
| 308 | LOGE("[lynq_gpio_pullsel_set] value_t fail."); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | ret = gpio_value_set(gpio, value_t); |
| 313 | if(ret == -1) |
| 314 | { |
| 315 | LOGE("[lynq_gpio_pullsel_set]gpio_value_set() fail."); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | int lynq_gpio_pullsel_get(int gpio) |
| 324 | { |
| 325 | //UNUSED(gpio); |
| 326 | int ret = -1; |
| 327 | |
| 328 | ret = gpio_value_get(gpio); |
| 329 | if (ret == -1) |
| 330 | { |
| 331 | LOGE("[lynq_gpio_pullsel_get]gpio_value_get() fail."); |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | return ret + 1; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |