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" |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 11 | #include "mbtk_utils.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 12 | #include "ql/ql_gpio.h" |
| 13 | |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 14 | typedef struct { |
| 15 | int pin; |
| 16 | int gpio; |
| 17 | } pin_gpio_mmap_s; |
| 18 | |
| 19 | static pin_gpio_mmap_s pin_gpio_mmap[] = { |
| 20 | {PINNAME_GPIO1, -1}, /*PIN-1*/ |
| 21 | {PINNAME_GPIO2, -1}, /*PIN-2*/ |
| 22 | {PINNAME_GPIO3, -1}, /*PIN-3*/ |
| 23 | {PINNAME_GPIO4, -1}, /*PIN-4*/ |
| 24 | {PINNAME_GPIO5, -1}, /*PIN-5*/ |
| 25 | {PINNAME_NET_STATUS, -1}, /*PIN-6*/ |
| 26 | {PINNAME_DBG_RXD, -1}, /*PIN-11*/ |
| 27 | {PINNAME_DBG_TXD, -1}, /*PIN-12*/ |
| 28 | {PINNAME_USIM_PRESENCE, 19}, /*PIN-13*/ |
| 29 | {PINNAME_SD_INT_DET, -1}, /*PIN-23*/ |
| 30 | {PINNAME_PCM_IN, 28}, /*PIN-24*/ |
| 31 | {PINNAME_PCM_OUT, 27}, /*PIN-25*/ |
| 32 | {PINNAME_PCM_SYNC, -1}, /*PIN-26*/ |
| 33 | {PINNAME_PCM_CLK, -1}, /*PIN-27*/ |
| 34 | {PINNAME_SDC2_DATA3, -1}, /*PIN-28*/ |
| 35 | {PINNAME_SDC2_DATA2, -1}, /*PIN-29*/ |
| 36 | {PINNAME_SDC2_DATA1, -1}, /*PIN-30*/ |
| 37 | {PINNAME_SDC2_DATA0, -1}, /*PIN-31*/ |
| 38 | {PINNAME_SDC2_CLK, -1}, /*PIN-32*/ |
| 39 | {PINNAME_SDC2_CMD, -1}, /*PIN-33*/ |
| 40 | {PINNAME_SPI_CS_N, -1}, /*PIN-37*/ |
| 41 | {PINNAME_SPI_MOSI, -1}, /*PIN-38*/ |
| 42 | {PINNAME_SPI_MISO, -1}, /*PIN-39*/ |
| 43 | {PINNAME_SPI_CLK, -1}, /*PIN-40*/ |
| 44 | {PINNAME_I2C_SCL, -1}, /*PIN-41*/ |
| 45 | {PINNAME_I2C_SDA, -1}, /*PIN-42*/ |
| 46 | {PINNAME_GPIO6, -1}, /*PIN-62*/ |
| 47 | {PINNAME_DCD, -1}, /*PIN-63*/ |
| 48 | {PINNAME_DTR, -1}, /*PIN-66*/ |
| 49 | {PINNAME_MAIN_CTS, -1}, /*PIN-64*/ |
| 50 | {PINNAME_MAIN_RTS, -1}, /*PIN-65*/ |
| 51 | {PINNAME_MAIN_TXD, -1}, /*PIN-67*/ |
| 52 | {PINNAME_MAIN_RXD, -1}, /*PIN-68*/ |
| 53 | {PINNAME_RMII_RXD1, -1}, /*PIN-73*/ |
| 54 | {PINNAME_RMII_RXCL, -1}, /*PIN-74*/ |
| 55 | {PINNAME_RMII_CLK, 3}, /*PIN-75*/ |
| 56 | {PINNAME_RMII_RXD0, 1}, /*PIN-76*/ |
| 57 | {PINNAME_RMII_TXD0, -1}, /*PIN-77*/ |
| 58 | {PINNAME_RMII_TXD1, -1}, /*PIN-78*/ |
| 59 | {PINNAME_RMII_RXD2, -1}, /*PIN-79*/ |
| 60 | {PINNAME_RMII_TXD2, -1}, /*PIN-80*/ |
| 61 | {PINNAME_RMII_TX_CTRL, -1}, /*PIN-81*/ |
| 62 | {PINNAME_RMII_RXD3, -1}, /*PIN-82*/ |
| 63 | {PINNAME_RMII_TXCL, -1}, /*PIN-83*/ |
| 64 | {PINNAME_RMII_TXD3, -1}, /*PIN-84*/ |
| 65 | {PINNAME_WLAN_SLP_CLK, -1}, /*PIN-118*/ |
| 66 | {PINNAME_RMII_RST, 20}, /*PIN-119*/ |
| 67 | {PINNAME_RMII_INT, -1}, /*PIN-120*/ |
| 68 | {PINNAME_RMII_MDIO, 17}, /*PIN-121*/ |
| 69 | {PINNAME_RMII_MDC, 16}, /*PIN-122*/ |
| 70 | {PINNAME_WLAN_PER_EN, 24}, /*PIN-127*/ |
| 71 | {PINNAME_WLAN_WAKE, 21}, /*PIN-135*/ |
| 72 | {PINNAME_WLAN_EN, 22}, /*PIN-136*/ |
| 73 | {PINNAME_GPIO8, -1}, /*PIN-139*/ |
| 74 | }; |
| 75 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 76 | static int gpio_export(int gpio) |
| 77 | { |
| 78 | int index=0; |
| 79 | int file=-1; |
| 80 | int result =-1; |
| 81 | char pin_index_buffer[5]= {0}; |
| 82 | |
| 83 | char buffer[50]; |
| 84 | memset(buffer,0,50); |
| 85 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 86 | if(access(buffer , F_OK) == 0) |
| 87 | { |
| 88 | LOGD("%d has export.", gpio); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | file = open("/sys/class/gpio/export",O_WRONLY); |
| 93 | if(file == -1) |
| 94 | { |
| 95 | LOGE("Open gpio export file fail."); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | memset(pin_index_buffer,0,5); |
| 100 | sprintf(pin_index_buffer,"%d", gpio); |
| 101 | result = write(file,pin_index_buffer,strlen(pin_index_buffer)); |
| 102 | if(result < 0) |
| 103 | { |
| 104 | LOGE("Gpio[%d] export fail.", gpio); |
| 105 | close(file); |
| 106 | return -1; |
| 107 | } |
| 108 | close(file); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int gpio_unexport(int gpio) |
| 114 | { |
| 115 | int index=0; |
| 116 | int file=-1; |
| 117 | int result =-1; |
| 118 | char pin_index_buffer[5]= {0}; |
| 119 | char buffer[50]; |
| 120 | memset(buffer,0,50); |
| 121 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 122 | if(access(buffer , F_OK) == -1) |
| 123 | { |
| 124 | LOGD("%d not export.", gpio); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | file = open("/sys/class/gpio/unexport",O_WRONLY); |
| 129 | if(file == -1) |
| 130 | { |
| 131 | LOGE("Open gpio unexport file fail."); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | memset(pin_index_buffer,0,5); |
| 136 | sprintf(pin_index_buffer,"%d", gpio); |
| 137 | result=write(file,pin_index_buffer,strlen(pin_index_buffer)); |
| 138 | if(result < 0) |
| 139 | { |
| 140 | close(file); |
| 141 | LOGE("Gpio[%d] unexport fail.", gpio); |
| 142 | return -1; |
| 143 | } |
| 144 | close(file); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int gpio_direct_get(int gpio, char *value, int value_size) |
| 150 | { |
| 151 | char buffer[50]= {0}; |
| 152 | int file =-1; |
| 153 | int result =-1; |
| 154 | |
| 155 | memset(buffer,0,50); |
| 156 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 157 | file = open(buffer, O_RDONLY); |
| 158 | if(file == -1) |
| 159 | { |
| 160 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | memset(value, 0x0, value_size); |
| 165 | result = read(file,value,value_size); |
| 166 | if(result <= 0) |
| 167 | { |
| 168 | LOGE("Get gpio[%d] direct fail.", gpio); |
| 169 | close(file); |
| 170 | return -1; |
| 171 | } |
| 172 | close(file); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | static int gpio_direct_set(int gpio, char *value) |
| 179 | { |
| 180 | char buffer[50]= {0}; |
| 181 | int file =-1; |
| 182 | int result =-1; |
| 183 | |
| 184 | memset(buffer,0,50); |
| 185 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio); |
| 186 | file = open(buffer, O_WRONLY); |
| 187 | if(file == -1) |
| 188 | { |
| 189 | LOGE("Open gpio[%d] direct fail.", gpio); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | result = write(file,value,strlen(value)); |
| 194 | if(result != strlen(value)) |
| 195 | { |
| 196 | LOGE("Set gpio[%d] direct fail.", gpio); |
| 197 | close(file); |
| 198 | return -1; |
| 199 | } |
| 200 | close(file); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int gpio_value_get(int gpio) |
| 206 | { |
| 207 | char buffer[50]; |
| 208 | char path[10]; |
| 209 | int file =-1; |
| 210 | int result =-1; |
| 211 | int value; |
| 212 | |
| 213 | memset(path,0,50); |
| 214 | memset(buffer,0,10); |
| 215 | sprintf(path,"/sys/class/gpio/gpio%d/value", gpio); |
| 216 | file = open(path,O_RDONLY); |
| 217 | if(file == -1) |
| 218 | { |
| 219 | LOGE("Open gpio[%d] fail.", gpio); |
| 220 | return -1; |
| 221 | } |
| 222 | result = read(file,buffer,5); |
| 223 | if(result <= 0) |
| 224 | { |
| 225 | LOGE("Get gpio[%d] value fail", gpio); |
| 226 | close(file); |
| 227 | return -1; |
| 228 | } |
| 229 | close(file); |
| 230 | value = atoi(buffer); |
| 231 | return value; |
| 232 | } |
| 233 | |
| 234 | static int gpio_value_set(int gpio, int value) |
| 235 | { |
| 236 | char buffer[50]= {0}; |
| 237 | int file =-1; |
| 238 | int result =-1; |
| 239 | |
| 240 | memset(buffer,0,50); |
| 241 | sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio); |
| 242 | file = open(buffer,O_WRONLY); |
| 243 | if(file == -1) |
| 244 | { |
| 245 | LOGE("Open gpio[%d] value fail.", gpio); |
| 246 | return -1; |
| 247 | } |
| 248 | if(value == 0) { |
| 249 | result = write(file,"0",1); |
| 250 | } else { |
| 251 | result = write(file,"1",1); |
| 252 | } |
| 253 | if(result != 1) |
| 254 | { |
| 255 | LOGE("Set gpio[%d] value fail.", gpio); |
| 256 | close(file); |
| 257 | return -1; |
| 258 | } |
| 259 | close(file); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 264 | static int pin_2_gpio(Enum_PinName pin_name) |
| 265 | { |
| 266 | #if 0 |
| 267 | switch(pin_name){ |
| 268 | case PINNAME_USIM_PRESENCE: /*PIN-13*/ |
| 269 | return 19; |
| 270 | case PINNAME_PCM_IN: /*PIN-24*/ |
| 271 | return 28; |
| 272 | case PINNAME_PCM_OUT: /*PIN-25*/ |
| 273 | return 27; |
| 274 | case PINNAME_RMII_CLK: /*PIN-75*/ |
| 275 | return 3; |
| 276 | case PINNAME_RMII_RXD0: /*PIN-76*/ |
| 277 | return 1; |
| 278 | case PINNAME_RMII_RST: /*PIN-119*/ |
| 279 | return 20; |
| 280 | case PINNAME_RMII_MDIO: /*PIN-121*/ |
| 281 | return 17; |
| 282 | case PINNAME_RMII_MDC: /*PIN-122*/ |
| 283 | return 16; |
| 284 | case PINNAME_WLAN_PER_EN: /*PIN-127*/ |
| 285 | return 24; |
| 286 | case PINNAME_WLAN_WAKE: /*PIN-135*/ |
| 287 | return 21; |
| 288 | case PINNAME_WLAN_EN: /*PIN-136*/ |
| 289 | return 22; |
| 290 | |
| 291 | // Unknown PIN. |
| 292 | case PINNAME_GPIO1: /*PIN-1*/ |
| 293 | case PINNAME_GPIO2: /*PIN-2*/ |
| 294 | case PINNAME_GPIO3: /*PIN-3*/ |
| 295 | case PINNAME_GPIO4: /*PIN-4*/ |
| 296 | case PINNAME_GPIO5: /*PIN-5*/ |
| 297 | case PINNAME_NET_STATUS: /*PIN-6*/ |
| 298 | case PINNAME_DBG_RXD: /*PIN-11*/ |
| 299 | case PINNAME_DBG_TXD: /*PIN-12*/ |
| 300 | case PINNAME_SD_INT_DET: /*PIN-23*/ |
| 301 | case PINNAME_PCM_SYNC: /*PIN-26*/ |
| 302 | case PINNAME_PCM_CLK: /*PIN-27*/ |
| 303 | case PINNAME_SDC2_DATA3: /*PIN-28*/ |
| 304 | case PINNAME_SDC2_DATA2: /*PIN-29*/ |
| 305 | case PINNAME_SDC2_DATA1: /*PIN-30*/ |
| 306 | case PINNAME_SDC2_DATA0: /*PIN-31*/ |
| 307 | case PINNAME_SDC2_CLK: /*PIN-32*/ |
| 308 | case PINNAME_SDC2_CMD: /*PIN-33*/ |
| 309 | case PINNAME_SPI_CS_N: /*PIN-37*/ |
| 310 | case PINNAME_SPI_MOSI: /*PIN-38*/ |
| 311 | case PINNAME_SPI_MISO: /*PIN-39*/ |
| 312 | case PINNAME_SPI_CLK: /*PIN-40*/ |
| 313 | case PINNAME_I2C_SCL: /*PIN-41*/ |
| 314 | case PINNAME_I2C_SDA: /*PIN-42*/ |
| 315 | case PINNAME_GPIO6: /*PIN-62*/ |
| 316 | case PINNAME_DCD: /*PIN-63*/ |
| 317 | case PINNAME_DTR: /*PIN-66*/ |
| 318 | case PINNAME_MAIN_CTS: /*PIN-64*/ |
| 319 | case PINNAME_MAIN_RTS: /*PIN-65*/ |
| 320 | case PINNAME_MAIN_TXD: /*PIN-67*/ |
| 321 | case PINNAME_MAIN_RXD: /*PIN-68*/ |
| 322 | case PINNAME_RMII_RXD1: /*PIN-73*/ |
| 323 | case PINNAME_RMII_RXCL: /*PIN-74*/ |
| 324 | case PINNAME_RMII_TXD0: /*PIN-77*/ |
| 325 | case PINNAME_RMII_TXD1: /*PIN-78*/ |
| 326 | case PINNAME_RMII_RXD2: /*PIN-79*/ |
| 327 | case PINNAME_RMII_TXD2: /*PIN-80*/ |
| 328 | case PINNAME_RMII_TX_CTRL: /*PIN-81*/ |
| 329 | case PINNAME_RMII_RXD3: /*PIN-82*/ |
| 330 | case PINNAME_RMII_TXCL: /*PIN-83*/ |
| 331 | case PINNAME_RMII_TXD3: /*PIN-84*/ |
| 332 | case PINNAME_WLAN_SLP_CLK: /*PIN-118*/ |
| 333 | case PINNAME_RMII_INT: /*PIN-120*/ |
| 334 | case PINNAME_GPIO8: /*PIN-139*/ |
| 335 | default: |
| 336 | LOGE("Unknown PIN : %d", pin_name); |
| 337 | return -1; |
| 338 | } |
| 339 | #else |
| 340 | int i = 0; |
| 341 | while(i < ARRAY_SIZE(pin_gpio_mmap)) { |
| 342 | if(pin_name == pin_gpio_mmap[i].pin) { |
| 343 | return pin_gpio_mmap[i].gpio; |
| 344 | } |
| 345 | i++; |
| 346 | } |
| 347 | |
| 348 | LOGE("No found PIN : %d", pin_name); |
| 349 | return -1; |
| 350 | #endif |
| 351 | } |
| 352 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 353 | |
| 354 | /***************************************************************** |
| 355 | * Function: Ql_GPIO_Init |
| 356 | * |
| 357 | * Description: |
| 358 | * This function enables the GPIO function of the specified pin, |
| 359 | * and initialize the configurations, including direction, |
| 360 | * level and pull selection. |
| 361 | * |
| 362 | * Parameters: |
| 363 | * pin_name: |
| 364 | * Pin name, one value of Enum_PinName. |
| 365 | * dir: |
| 366 | * The initial direction of GPIO, one value of Enum_PinDirection. |
| 367 | * level: |
| 368 | * The initial level of GPIO, one value of Enum_PinLevel. |
| 369 | * pull_sel: |
| 370 | * Pull selection, one value of Enum_PinPullSel. |
| 371 | * Return: |
| 372 | * RES_OK, this function succeeds. |
| 373 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 374 | * RES_IO_ERR, the function failed |
| 375 | * other place. For example this GPIO has been using as EINT. |
| 376 | *****************************************************************/ |
| 377 | int Ql_GPIO_Init(Enum_PinName pin_name, |
| 378 | Enum_PinDirection dir, |
| 379 | Enum_PinLevel level, |
| 380 | Enum_PinPullSel pull_sel |
| 381 | ) |
| 382 | { |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 383 | int gpio = pin_2_gpio(pin_name); |
| 384 | if(gpio == -1) { |
| 385 | return RES_IO_ERROR; |
| 386 | } |
| 387 | LOGD("PIN-%d => GPIO-%d", pin_name, gpio); |
| 388 | |
| 389 | if(gpio_export(gpio)) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 390 | { |
| 391 | LOGE("gpio_export() fail."); |
| 392 | return RES_IO_ERROR; |
| 393 | } |
| 394 | |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 395 | if(gpio_direct_set(gpio, dir == PINDIRECTION_IN ? "in" : "out")) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 396 | { |
| 397 | LOGE("gpio_direct_set() fail."); |
| 398 | return RES_IO_ERROR; |
| 399 | } |
| 400 | |
xl.meng | 4f0e29f | 2023-10-11 16:34:42 +0800 | [diff] [blame^] | 401 | if(dir == PINDIRECTION_OUT){ |
| 402 | if(gpio_value_set(gpio, level)) |
| 403 | { |
| 404 | LOGE("gpio_value_set() fail."); |
| 405 | return RES_IO_ERROR; |
| 406 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 407 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 408 | // No support pull mode now. |
| 409 | |
| 410 | return RES_OK; |
| 411 | } |
| 412 | |
| 413 | /***************************************************************** |
| 414 | * Function: Ql_GPIO_Base_Init |
| 415 | * |
| 416 | * Description: |
| 417 | * This function enables the GPIO function of the specified pin. |
| 418 | * |
| 419 | * Parameters: |
| 420 | * pin_name: |
| 421 | * Pin name, one value of Enum_PinName. |
| 422 | * |
| 423 | * Return: |
| 424 | * RES_OK, this function succeeds. |
| 425 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 426 | * RES_IO_ERR, the function failed |
| 427 | *****************************************************************/ |
| 428 | int Ql_GPIO_Base_Init(Enum_PinName pin_name ); |
| 429 | |
| 430 | /***************************************************************** |
| 431 | * Function: Ql_GPIO_SetLevel |
| 432 | * |
| 433 | * Description: |
| 434 | * This function sets the level of the specified GPIO. |
| 435 | * |
| 436 | * Parameters: |
| 437 | * pin_name: |
| 438 | * Pin name, one value of Enum_PinName. |
| 439 | * level: |
| 440 | * The initial level of GPIO, one value of Enum_PinLevel. |
| 441 | * Return: |
| 442 | * RES_OK, this function succeeds. |
| 443 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 444 | * RES_IO_ERR, the function failed |
| 445 | * other place. For example this GPIO has been using as EINT. |
| 446 | *****************************************************************/ |
| 447 | int Ql_GPIO_SetLevel(Enum_PinName pin_name, Enum_PinLevel level) |
| 448 | { |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 449 | int gpio = pin_2_gpio(pin_name); |
| 450 | if(gpio == -1) { |
| 451 | return RES_IO_ERROR; |
| 452 | } |
| 453 | LOGD("PIN-%d => GPIO-%d", pin_name, gpio); |
| 454 | |
| 455 | if(gpio_value_set(gpio, level)) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 456 | LOGE("gpio_value_set() fail."); |
| 457 | return RES_IO_ERROR; |
| 458 | } else { |
| 459 | return RES_OK; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /***************************************************************** |
| 464 | * Function: Ql_GPIO_GetLevel |
| 465 | * |
| 466 | * Description: |
| 467 | * This function gets the level of the specified GPIO. |
| 468 | * |
| 469 | * Parameters: |
| 470 | * pin_name: |
| 471 | * Pin name, one value of Enum_PinName. |
| 472 | * Return: |
| 473 | * The level value of the specified GPIO, which is |
| 474 | * nonnegative integer. |
| 475 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 476 | *****************************************************************/ |
| 477 | int Ql_GPIO_GetLevel(Enum_PinName pin_name) |
| 478 | { |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 479 | int gpio = pin_2_gpio(pin_name); |
| 480 | if(gpio == -1) { |
| 481 | return RES_IO_ERROR; |
| 482 | } |
| 483 | LOGD("PIN-%d => GPIO-%d", pin_name, gpio); |
| 484 | |
| 485 | return gpio_value_get(gpio); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /***************************************************************** |
| 489 | * Function: Ql_GPIO_SetDirection |
| 490 | * |
| 491 | * Description: |
| 492 | * This function sets the direction of the specified GPIO. |
| 493 | * |
| 494 | * Parameters: |
| 495 | * pin_name: |
| 496 | * Pin name, one value of Enum_PinName. |
| 497 | * dir: |
| 498 | * The initial direction of GPIO, one value of Enum_PinDirection. |
| 499 | * Return: |
| 500 | * RES_OK, this function succeeds. |
| 501 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 502 | * RES_IO_ERR, the function failed |
| 503 | * other place. For example this GPIO has been using as EINT. |
| 504 | *****************************************************************/ |
| 505 | int Ql_GPIO_SetDirection(Enum_PinName pin_name, Enum_PinDirection dir) |
| 506 | { |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 507 | int gpio = pin_2_gpio(pin_name); |
| 508 | if(gpio == -1) { |
| 509 | return RES_IO_ERROR; |
| 510 | } |
| 511 | LOGD("PIN-%d => GPIO-%d", pin_name, gpio); |
| 512 | |
| 513 | if(gpio_direct_set(gpio, dir == PINDIRECTION_IN ? "in" : "out")) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 514 | LOGE("gpio_direct_set() fail."); |
| 515 | return RES_IO_ERROR; |
| 516 | } else { |
| 517 | return RES_OK; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /***************************************************************** |
| 522 | * Function: Ql_GPIO_GetDirection |
| 523 | * |
| 524 | * Description: |
| 525 | * This function gets the direction of the specified GPIO. |
| 526 | * |
| 527 | * Parameters: |
| 528 | * pin_name: |
| 529 | * Pin name, one value of Enum_PinName. |
| 530 | * Return: |
| 531 | * 0 INPUT |
| 532 | * 1 OUTPUT |
| 533 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 534 | * other place. For example this GPIO has been using as EINT. |
| 535 | *****************************************************************/ |
| 536 | int Ql_GPIO_GetDirection(Enum_PinName pin_name) |
| 537 | { |
| 538 | char buff[10]; |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 539 | int gpio = pin_2_gpio(pin_name); |
| 540 | if(gpio == -1) { |
| 541 | return RES_IO_ERROR; |
| 542 | } |
| 543 | LOGD("PIN-%d => GPIO-%d", pin_name, gpio); |
| 544 | |
| 545 | if(gpio_direct_get(gpio, buff, 10)) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 546 | LOGE("gpio_direct_get() fail."); |
| 547 | return RES_IO_NOT_SUPPORT; |
| 548 | } else { |
xl.meng | 4f0e29f | 2023-10-11 16:34:42 +0800 | [diff] [blame^] | 549 | if(strncmp(buff, "in",2) == 0) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 550 | return PINDIRECTION_IN; |
xl.meng | 4f0e29f | 2023-10-11 16:34:42 +0800 | [diff] [blame^] | 551 | } else if(strncmp(buff, "out",3) == 0) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 552 | return PINDIRECTION_OUT; |
| 553 | } else { |
| 554 | return RES_IO_NOT_SUPPORT; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /***************************************************************** |
| 560 | * Function: Ql_GPIO_SetPullSelection |
| 561 | * |
| 562 | * Description: |
| 563 | * This function sets the pull selection of the specified GPIO. |
| 564 | * |
| 565 | * Parameters: |
| 566 | * pin_name: |
| 567 | * Pin name, one value of Enum_PinName. |
| 568 | * Enum_PinPullSel: |
| 569 | * Pull selection, one value of Enum_PinPullSel. |
| 570 | * Return: |
| 571 | * RES_OK, this function succeeds. |
| 572 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 573 | * RES_IO_ERR, the function failed |
| 574 | * other place. For example this GPIO has been using as EINT. |
| 575 | *****************************************************************/ |
| 576 | int Ql_GPIO_SetPullSelection(Enum_PinName pin_name, Enum_PinPullSel pull_sel); |
| 577 | |
| 578 | /***************************************************************** |
| 579 | * Function: ql_gpio_get_pull_selection |
| 580 | * |
| 581 | * Description: |
| 582 | * This function gets the pull selection of the specified GPIO. |
| 583 | * |
| 584 | * Parameters: |
| 585 | * pin_name: |
| 586 | * Pin name, one value of Enum_PinName. |
| 587 | * Return: |
| 588 | * 0<<13 no pull |
| 589 | * 5<<13 pull down |
| 590 | * 6<<13 pull up |
| 591 | *****************************************************************/ |
| 592 | int Ql_GPIO_GetPullSelection(Enum_PinName pin_name); |
| 593 | |
| 594 | |
| 595 | /***************************************************************** |
| 596 | * Function: Ql_GPIO_Uninit |
| 597 | * |
| 598 | * Description: |
| 599 | * This function releases the specified GPIO that was |
| 600 | * initialized by calling Ql_GPIO_Init() previously. |
| 601 | * After releasing, the GPIO can be used for other purpose. |
| 602 | * Parameters: |
| 603 | * pin_name: |
| 604 | * Pin name, one value of Enum_PinName. |
| 605 | * Return: |
| 606 | * RES_OK, this function succeeds. |
| 607 | * RES_IO_NOT_SUPPORT, the input GPIO is invalid. |
| 608 | * RES_IO_ERR, the function failed |
| 609 | * other place. For example this GPIO has been using as EINT. |
| 610 | *****************************************************************/ |
| 611 | int Ql_GPIO_Uninit(Enum_PinName pin_name) |
| 612 | { |
b.liu | b5c98e8 | 2023-09-21 15:10:53 +0800 | [diff] [blame] | 613 | int gpio = pin_2_gpio(pin_name); |
| 614 | if(gpio == -1) { |
| 615 | return RES_IO_ERROR; |
| 616 | } |
| 617 | LOGD("PIN-%d => GPIO-%d", pin_name, gpio); |
| 618 | |
| 619 | if(gpio_unexport(gpio)) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 620 | { |
| 621 | LOGE("gpio_unexport() fail."); |
| 622 | return RES_IO_ERROR; |
| 623 | } |
| 624 | |
| 625 | return RES_OK; |
| 626 | } |
| 627 | |
| 628 | //------------------------------------------------------------------------------ |
| 629 | /* |
| 630 | * Function: Ql_EINT_Enable |
| 631 | * |
| 632 | * Description: |
| 633 | * Set the interrupt sense mode, and enable interrupt. |
| 634 | * |
| 635 | * Parameters: |
| 636 | * eint_pin_name: |
| 637 | * EINT pin name, one value of Enum_PinName that has |
| 638 | * the interrupt function. |
| 639 | * |
| 640 | * eint_type: |
| 641 | * Interrupt type, level-triggered or edge-triggered. |
| 642 | * Now, only edge-triggered interrupt is supported. |
| 643 | * |
| 644 | * eint_callback: |
| 645 | * call back function |
| 646 | * |
| 647 | * Return: |
| 648 | * RES_OK, this function succeeds. |
| 649 | * else failed to execute the function. |
| 650 | */ |
| 651 | //------------------------------------------------------------------------------ |
| 652 | int Ql_EINT_Enable(Enum_PinName eint_pin_name, Enum_EintType eint_type, Ql_EINT_Callback eint_callback); |
| 653 | |
| 654 | |
| 655 | //------------------------------------------------------------------------------ |
| 656 | /* |
| 657 | * Function: Ql_EINT_Disable |
| 658 | * |
| 659 | * Description: |
| 660 | * Disable the interrupt sense. |
| 661 | * |
| 662 | * Parameters: |
| 663 | * eint_pin_name: |
| 664 | * EINT pin name, one value of Enum_PinName that has |
| 665 | * the interrupt function. |
| 666 | * |
| 667 | * Return: |
| 668 | * RES_OK, this function succeeds. |
| 669 | * else failed to execute the function. |
| 670 | */ |
| 671 | //------------------------------------------------------------------------------ |
| 672 | int Ql_EINT_Disable(Enum_PinName eint_pin_name); |
| 673 | |