liubin | 281ac46 | 2023-07-19 14:22:54 +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 "ql/ql_gpio.h" |
| 12 | |
| 13 | static int gpio_export(int gpio) |
| 14 | { |
| 15 | int index=0; |
| 16 | int file=-1; |
| 17 | int result =-1; |
| 18 | char pin_index_buffer[5]= {0}; |
| 19 | |
| 20 | char buffer[50]; |
| 21 | memset(buffer,0,50); |
| 22 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 23 | if(access(buffer , F_OK) == 0) |
| 24 | { |
| 25 | LOGD("%d has export.", gpio); |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | file = open("/sys/class/gpio/export",O_WRONLY); |
| 30 | if(file == -1) |
| 31 | { |
| 32 | LOGE("Open gpio export file fail."); |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | memset(pin_index_buffer,0,5); |
| 37 | sprintf(pin_index_buffer,"%d", gpio); |
| 38 | result = write(file,pin_index_buffer,strlen(pin_index_buffer)); |
| 39 | if(result < 0) |
| 40 | { |
| 41 | LOGE("Gpio[%d] export fail.", gpio); |
| 42 | close(file); |
| 43 | return -1; |
| 44 | } |
| 45 | close(file); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int gpio_unexport(int gpio) |
| 51 | { |
| 52 | int index=0; |
| 53 | int file=-1; |
| 54 | int result =-1; |
| 55 | char pin_index_buffer[5]= {0}; |
| 56 | char buffer[50]; |
| 57 | memset(buffer,0,50); |
| 58 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 59 | if(access(buffer , F_OK) == -1) |
| 60 | { |
| 61 | LOGD("%d not export.", gpio); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | file = open("/sys/class/gpio/unexport",O_WRONLY); |
| 66 | if(file == -1) |
| 67 | { |
| 68 | LOGE("Open gpio unexport file fail."); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | memset(pin_index_buffer,0,5); |
| 73 | sprintf(pin_index_buffer,"%d", gpio); |
| 74 | result=write(file,pin_index_buffer,strlen(pin_index_buffer)); |
| 75 | if(result < 0) |
| 76 | { |
| 77 | close(file); |
| 78 | LOGE("Gpio[%d] unexport fail.", gpio); |
| 79 | return -1; |
| 80 | } |
| 81 | close(file); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int gpio_direct_get(int gpio, char *value, int value_size) |
| 87 | { |
| 88 | char buffer[50]= {0}; |
| 89 | int file =-1; |
| 90 | int result =-1; |
| 91 | |
| 92 | memset(buffer,0,50); |
| 93 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 94 | file = open(buffer, O_RDONLY); |
| 95 | if(file == -1) |
| 96 | { |
| 97 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | memset(value, 0x0, value_size); |
| 102 | result = read(file,value,value_size); |
| 103 | if(result <= 0) |
| 104 | { |
| 105 | LOGE("Get gpio[%d] direct fail.", gpio); |
| 106 | close(file); |
| 107 | return -1; |
| 108 | } |
| 109 | close(file); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static int gpio_direct_set(int gpio, char *value) |
| 116 | { |
| 117 | char buffer[50]= {0}; |
| 118 | int file =-1; |
| 119 | int result =-1; |
| 120 | |
| 121 | memset(buffer,0,50); |
| 122 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 123 | file = open(buffer, O_WRONLY); |
| 124 | if(file == -1) |
| 125 | { |
| 126 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | result = write(file,value,strlen(value)); |
| 131 | if(result != strlen(value)) |
| 132 | { |
| 133 | LOGE("Set gpio[%d] direct fail.", gpio); |
| 134 | close(file); |
| 135 | return -1; |
| 136 | } |
| 137 | close(file); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int gpio_value_get(int gpio) |
| 143 | { |
| 144 | char buffer[50]; |
| 145 | char path[10]; |
| 146 | int file =-1; |
| 147 | int result =-1; |
| 148 | int value; |
| 149 | |
| 150 | memset(path,0,50); |
| 151 | memset(buffer,0,10); |
| 152 | sprintf(path,"/sys/class/gpio/gpio%d/value", gpio); |
| 153 | file = open(path,O_RDONLY); |
| 154 | if(file == -1) |
| 155 | { |
| 156 | LOGE("Open gpio[%d] fail.", gpio); |
| 157 | return -1; |
| 158 | } |
| 159 | result = read(file,buffer,5); |
| 160 | if(result <= 0) |
| 161 | { |
| 162 | LOGE("Get gpio[%d] value fail", gpio); |
| 163 | close(file); |
| 164 | return -1; |
| 165 | } |
| 166 | close(file); |
| 167 | value = atoi(buffer); |
| 168 | return value; |
| 169 | } |
| 170 | |
| 171 | static int gpio_value_set(int gpio, int value) |
| 172 | { |
| 173 | char buffer[50]= {0}; |
| 174 | int file =-1; |
| 175 | int result =-1; |
| 176 | |
| 177 | memset(buffer,0,50); |
| 178 | sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio); |
| 179 | file = open(buffer,O_WRONLY); |
| 180 | if(file == -1) |
| 181 | { |
| 182 | LOGE("Open gpio[%d] value fail.", gpio); |
| 183 | return -1; |
| 184 | } |
| 185 | if(value == 0) { |
| 186 | result = write(file,"0",1); |
| 187 | } else { |
| 188 | result = write(file,"1",1); |
| 189 | } |
| 190 | if(result != 1) |
| 191 | { |
| 192 | LOGE("Set gpio[%d] value fail.", gpio); |
| 193 | close(file); |
| 194 | return -1; |
| 195 | } |
| 196 | close(file); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /***************************************************************** |
| 203 | * Function: Ql_GPIO_Init |
| 204 | * |
| 205 | * Description: |
| 206 | * This function enables the GPIO function of the specified pin, |
| 207 | * and initialize the configurations, including direction, |
| 208 | * level and pull selection. |
| 209 | * |
| 210 | * Parameters: |
| 211 | * pin_name: |
| 212 | * Pin name, one value of Enum_PinName. |
| 213 | * dir: |
| 214 | * The initial direction of GPIO, one value of Enum_PinDirection. |
| 215 | * level: |
| 216 | * The initial level of GPIO, one value of Enum_PinLevel. |
| 217 | * pull_sel: |
| 218 | * Pull selection, one value of Enum_PinPullSel. |
| 219 | * Return: |
| 220 | * RES_OK, this function succeeds. |
| 221 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 222 | * RES_IO_ERR, the function failed |
| 223 | * other place. For example this GPIO has been using as EINT. |
| 224 | *****************************************************************/ |
| 225 | int Ql_GPIO_Init(Enum_PinName pin_name, |
| 226 | Enum_PinDirection dir, |
| 227 | Enum_PinLevel level, |
| 228 | Enum_PinPullSel pull_sel |
| 229 | ) |
| 230 | { |
| 231 | if(gpio_export(pin_name)) |
| 232 | { |
| 233 | LOGE("gpio_export() fail."); |
| 234 | return RES_IO_ERROR; |
| 235 | } |
| 236 | |
| 237 | if(gpio_direct_set(pin_name, dir == PINDIRECTION_IN ? "in" : "out")) |
| 238 | { |
| 239 | LOGE("gpio_direct_set() fail."); |
| 240 | return RES_IO_ERROR; |
| 241 | } |
| 242 | |
| 243 | if(gpio_value_set(pin_name, level)) |
| 244 | { |
| 245 | LOGE("gpio_value_set() fail."); |
| 246 | return RES_IO_ERROR; |
| 247 | } |
| 248 | |
| 249 | // No support pull mode now. |
| 250 | |
| 251 | return RES_OK; |
| 252 | } |
| 253 | |
| 254 | /***************************************************************** |
| 255 | * Function: Ql_GPIO_Base_Init |
| 256 | * |
| 257 | * Description: |
| 258 | * This function enables the GPIO function of the specified pin. |
| 259 | * |
| 260 | * Parameters: |
| 261 | * pin_name: |
| 262 | * Pin name, one value of Enum_PinName. |
| 263 | * |
| 264 | * Return: |
| 265 | * RES_OK, this function succeeds. |
| 266 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 267 | * RES_IO_ERR, the function failed |
| 268 | *****************************************************************/ |
| 269 | int Ql_GPIO_Base_Init(Enum_PinName pin_name ); |
| 270 | |
| 271 | /***************************************************************** |
| 272 | * Function: Ql_GPIO_SetLevel |
| 273 | * |
| 274 | * Description: |
| 275 | * This function sets the level of the specified GPIO. |
| 276 | * |
| 277 | * Parameters: |
| 278 | * pin_name: |
| 279 | * Pin name, one value of Enum_PinName. |
| 280 | * level: |
| 281 | * The initial level of GPIO, one value of Enum_PinLevel. |
| 282 | * Return: |
| 283 | * RES_OK, this function succeeds. |
| 284 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 285 | * RES_IO_ERR, the function failed |
| 286 | * other place. For example this GPIO has been using as EINT. |
| 287 | *****************************************************************/ |
| 288 | int Ql_GPIO_SetLevel(Enum_PinName pin_name, Enum_PinLevel level) |
| 289 | { |
| 290 | if(gpio_value_set(pin_name, level)) { |
| 291 | LOGE("gpio_value_set() fail."); |
| 292 | return RES_IO_ERROR; |
| 293 | } else { |
| 294 | return RES_OK; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /***************************************************************** |
| 299 | * Function: Ql_GPIO_GetLevel |
| 300 | * |
| 301 | * Description: |
| 302 | * This function gets the level of the specified GPIO. |
| 303 | * |
| 304 | * Parameters: |
| 305 | * pin_name: |
| 306 | * Pin name, one value of Enum_PinName. |
| 307 | * Return: |
| 308 | * The level value of the specified GPIO, which is |
| 309 | * nonnegative integer. |
| 310 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 311 | *****************************************************************/ |
| 312 | int Ql_GPIO_GetLevel(Enum_PinName pin_name) |
| 313 | { |
| 314 | return gpio_value_get(pin_name); |
| 315 | } |
| 316 | |
| 317 | /***************************************************************** |
| 318 | * Function: Ql_GPIO_SetDirection |
| 319 | * |
| 320 | * Description: |
| 321 | * This function sets the direction of the specified GPIO. |
| 322 | * |
| 323 | * Parameters: |
| 324 | * pin_name: |
| 325 | * Pin name, one value of Enum_PinName. |
| 326 | * dir: |
| 327 | * The initial direction of GPIO, one value of Enum_PinDirection. |
| 328 | * Return: |
| 329 | * RES_OK, this function succeeds. |
| 330 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 331 | * RES_IO_ERR, the function failed |
| 332 | * other place. For example this GPIO has been using as EINT. |
| 333 | *****************************************************************/ |
| 334 | int Ql_GPIO_SetDirection(Enum_PinName pin_name, Enum_PinDirection dir) |
| 335 | { |
| 336 | if(gpio_direct_set(pin_name, dir == PINDIRECTION_IN ? "in" : "out")) { |
| 337 | LOGE("gpio_direct_set() fail."); |
| 338 | return RES_IO_ERROR; |
| 339 | } else { |
| 340 | return RES_OK; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /***************************************************************** |
| 345 | * Function: Ql_GPIO_GetDirection |
| 346 | * |
| 347 | * Description: |
| 348 | * This function gets the direction of the specified GPIO. |
| 349 | * |
| 350 | * Parameters: |
| 351 | * pin_name: |
| 352 | * Pin name, one value of Enum_PinName. |
| 353 | * Return: |
| 354 | * 0 INPUT |
| 355 | * 1 OUTPUT |
| 356 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 357 | * other place. For example this GPIO has been using as EINT. |
| 358 | *****************************************************************/ |
| 359 | int Ql_GPIO_GetDirection(Enum_PinName pin_name) |
| 360 | { |
| 361 | char buff[10]; |
| 362 | if(gpio_direct_get(pin_name, buff, 10)) { |
| 363 | LOGE("gpio_direct_get() fail."); |
| 364 | return RES_IO_NOT_SUPPORT; |
| 365 | } else { |
| 366 | if(strcmp(buff, "in") == 0) { |
| 367 | return PINDIRECTION_IN; |
| 368 | } else if(strcmp(buff, "out") == 0) { |
| 369 | return PINDIRECTION_OUT; |
| 370 | } else { |
| 371 | return RES_IO_NOT_SUPPORT; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /***************************************************************** |
| 377 | * Function: Ql_GPIO_SetPullSelection |
| 378 | * |
| 379 | * Description: |
| 380 | * This function sets the pull selection of the specified GPIO. |
| 381 | * |
| 382 | * Parameters: |
| 383 | * pin_name: |
| 384 | * Pin name, one value of Enum_PinName. |
| 385 | * Enum_PinPullSel: |
| 386 | * Pull selection, one value of Enum_PinPullSel. |
| 387 | * Return: |
| 388 | * RES_OK, this function succeeds. |
| 389 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 390 | * RES_IO_ERR, the function failed |
| 391 | * other place. For example this GPIO has been using as EINT. |
| 392 | *****************************************************************/ |
| 393 | int Ql_GPIO_SetPullSelection(Enum_PinName pin_name, Enum_PinPullSel pull_sel); |
| 394 | |
| 395 | /***************************************************************** |
| 396 | * Function: ql_gpio_get_pull_selection |
| 397 | * |
| 398 | * Description: |
| 399 | * This function gets the pull selection of the specified GPIO. |
| 400 | * |
| 401 | * Parameters: |
| 402 | * pin_name: |
| 403 | * Pin name, one value of Enum_PinName. |
| 404 | * Return: |
| 405 | * 0<<13 no pull |
| 406 | * 5<<13 pull down |
| 407 | * 6<<13 pull up |
| 408 | *****************************************************************/ |
| 409 | int Ql_GPIO_GetPullSelection(Enum_PinName pin_name); |
| 410 | |
| 411 | |
| 412 | /***************************************************************** |
| 413 | * Function: Ql_GPIO_Uninit |
| 414 | * |
| 415 | * Description: |
| 416 | * This function releases the specified GPIO that was |
| 417 | * initialized by calling Ql_GPIO_Init() previously. |
| 418 | * After releasing, the GPIO can be used for other purpose. |
| 419 | * Parameters: |
| 420 | * pin_name: |
| 421 | * Pin name, one value of Enum_PinName. |
| 422 | * Return: |
| 423 | * RES_OK, this function succeeds. |
| 424 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 425 | * RES_IO_ERR, the function failed |
| 426 | * other place. For example this GPIO has been using as EINT. |
| 427 | *****************************************************************/ |
| 428 | int Ql_GPIO_Uninit(Enum_PinName pin_name) |
| 429 | { |
| 430 | if(gpio_unexport(pin_name)) |
| 431 | { |
| 432 | LOGE("gpio_unexport() fail."); |
| 433 | return RES_IO_ERROR; |
| 434 | } |
| 435 | |
| 436 | return RES_OK; |
| 437 | } |
| 438 | |
| 439 | //------------------------------------------------------------------------------ |
| 440 | /* |
| 441 | * Function: Ql_EINT_Enable |
| 442 | * |
| 443 | * Description: |
| 444 | * Set the interrupt sense mode, and enable interrupt. |
| 445 | * |
| 446 | * Parameters: |
| 447 | * eint_pin_name: |
| 448 | * EINT pin name, one value of Enum_PinName that has |
| 449 | * the interrupt function. |
| 450 | * |
| 451 | * eint_type: |
| 452 | * Interrupt type, level-triggered or edge-triggered. |
| 453 | * Now, only edge-triggered interrupt is supported. |
| 454 | * |
| 455 | * eint_callback: |
| 456 | * call back function |
| 457 | * |
| 458 | * Return: |
| 459 | * RES_OK, this function succeeds. |
| 460 | * else failed to execute the function. |
| 461 | */ |
| 462 | //------------------------------------------------------------------------------ |
| 463 | int Ql_EINT_Enable(Enum_PinName eint_pin_name, Enum_EintType eint_type, Ql_EINT_Callback eint_callback); |
| 464 | |
| 465 | |
| 466 | //------------------------------------------------------------------------------ |
| 467 | /* |
| 468 | * Function: Ql_EINT_Disable |
| 469 | * |
| 470 | * Description: |
| 471 | * Disable the interrupt sense. |
| 472 | * |
| 473 | * Parameters: |
| 474 | * eint_pin_name: |
| 475 | * EINT pin name, one value of Enum_PinName that has |
| 476 | * the interrupt function. |
| 477 | * |
| 478 | * Return: |
| 479 | * RES_OK, this function succeeds. |
| 480 | * else failed to execute the function. |
| 481 | */ |
| 482 | //------------------------------------------------------------------------------ |
| 483 | int Ql_EINT_Disable(Enum_PinName eint_pin_name); |
| 484 | |