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