b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <stdlib.h>
|
| 3 | #include <unistd.h>
|
| 4 | #include <fcntl.h>
|
| 5 | #include <errno.h>
|
| 6 | #include <stdint.h>
|
| 7 | #include <string.h>
|
| 8 | #include <dlfcn.h>
|
| 9 |
|
| 10 | #include "gsw_gpio.h"
|
| 11 |
|
| 12 | #ifndef LOG_ERR_LEVEL
|
| 13 | #define LOG_ERR_LEVEL 3 /* error conditions */
|
| 14 | #endif
|
| 15 | #ifndef LOG_WARN_LEVEL
|
| 16 | #define LOG_WARN_LEVEL 4 /* warning conditions */
|
| 17 | #endif
|
| 18 | #ifndef LOG_INFO_LEVEL
|
| 19 | #define LOG_INFO_LEVEL 6 /* informational */
|
| 20 | #endif
|
| 21 | #ifndef LOG_DEBUG_LEVEL
|
| 22 | #define LOG_DEBUG_LEVEL 7 /* debug-level messages */
|
| 23 | #endif
|
| 24 | #ifndef LOG_VERBOSE_LEVEL
|
| 25 | #define LOG_VERBOSE_LEVEL 8
|
| 26 | #endif
|
| 27 |
|
| 28 | #define LOGV(fmt, args ...) \
|
| 29 | do{ \
|
| 30 | char *file_ptr_1001 = __FILE__; \
|
| 31 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 32 | char line_1001[10] = {0}; \
|
| 33 | sprintf(line_1001, "%d", __LINE__); \
|
| 34 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 35 | if(*ptr_1001 == '/') \
|
| 36 | break; \
|
| 37 | ptr_1001--; \
|
| 38 | } \
|
| 39 | fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
|
| 40 | } while(0)
|
| 41 |
|
| 42 | #define LOGI(fmt, args...) \
|
| 43 | do{ \
|
| 44 | char *file_ptr_1001 = __FILE__; \
|
| 45 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 46 | char line_1001[10] = {0}; \
|
| 47 | sprintf(line_1001, "%d", __LINE__); \
|
| 48 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 49 | if(*ptr_1001 == '/') \
|
| 50 | break; \
|
| 51 | ptr_1001--; \
|
| 52 | } \
|
| 53 | fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
|
| 54 | } while(0)
|
| 55 |
|
| 56 | #define LOGD(fmt, args...) \
|
| 57 | do{ \
|
| 58 | char *file_ptr_1001 = __FILE__; \
|
| 59 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 60 | char line_1001[10] = {0}; \
|
| 61 | sprintf(line_1001, "%d", __LINE__); \
|
| 62 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 63 | if(*ptr_1001 == '/') \
|
| 64 | break; \
|
| 65 | ptr_1001--; \
|
| 66 | } \
|
| 67 | fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
|
| 68 | } while(0)
|
| 69 |
|
| 70 | #define LOGW(fmt, args...) \
|
| 71 | do{ \
|
| 72 | char *file_ptr_1001 = __FILE__; \
|
| 73 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 74 | char line_1001[10] = {0}; \
|
| 75 | sprintf(line_1001, "%d", __LINE__); \
|
| 76 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 77 | if(*ptr_1001 == '/') \
|
| 78 | break; \
|
| 79 | ptr_1001--; \
|
| 80 | } \
|
| 81 | fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
|
| 82 | } while(0)
|
| 83 |
|
| 84 | #define LOGE(fmt, args...) \
|
| 85 | do{ \
|
| 86 | char *file_ptr_1001 = __FILE__; \
|
| 87 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 88 | char line_1001[10] = {0}; \
|
| 89 | sprintf(line_1001, "%d", __LINE__); \
|
| 90 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 91 | if(*ptr_1001 == '/') \
|
| 92 | break; \
|
| 93 | ptr_1001--; \
|
| 94 | } \
|
| 95 | fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
|
| 96 | } while(0)
|
| 97 |
|
| 98 |
|
| 99 | #define GSW_HAL_SUCCESS 0
|
| 100 | #define GSW_HAL_FAIL -1 //表示失败(通用性)
|
| 101 | #define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
|
| 102 |
|
| 103 | #define wakeout 117
|
| 104 |
|
| 105 | typedef void (*mbtk_log)(int level, const char *format,...);
|
| 106 | static mbtk_log fun_ptr_log = NULL;
|
| 107 | void *dlHandle_gpio = NULL;
|
| 108 | char *lynqLib_gpio = "/lib/libmbtk_lib.so";
|
| 109 |
|
| 110 | int gpio_check(int gpio)
|
| 111 | {
|
| 112 | char buffer[128];
|
| 113 | memset(buffer,0,128);
|
| 114 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
|
| 115 | return access(buffer , F_OK);
|
| 116 | }
|
| 117 |
|
| 118 | static int handle()
|
| 119 | {
|
| 120 | if(dlHandle_gpio == NULL || fun_ptr_log == NULL)
|
| 121 | {
|
| 122 | dlHandle_gpio = dlopen(lynqLib_gpio, RTLD_NOW);
|
| 123 | fun_ptr_log = (mbtk_log)dlsym(dlHandle_gpio, "mbtk_log");
|
| 124 | if(fun_ptr_log == NULL || dlHandle_gpio == NULL)
|
| 125 | {
|
| 126 | return GSW_HAL_FAIL;
|
| 127 | }
|
| 128 | }
|
| 129 | return GSW_HAL_SUCCESS;
|
| 130 | }
|
| 131 |
|
| 132 | static int gpio_export(int gpio)
|
| 133 | {
|
| 134 | int file=-1;
|
| 135 | int result =-1;
|
| 136 | char pin_index_buffer[5]= {0};
|
| 137 |
|
| 138 | if (gpio_check(gpio) == 0)
|
| 139 | {
|
| 140 | LOGE("export has been add\n");
|
| 141 | return 0;
|
| 142 | }
|
| 143 |
|
| 144 | file = open("/sys/class/gpio/export",O_WRONLY);
|
| 145 | if(file == -1)
|
| 146 | {
|
| 147 | LOGE("Open gpio export file fail.");
|
| 148 | return -1;
|
| 149 | }
|
| 150 |
|
| 151 | memset(pin_index_buffer,0,5);
|
| 152 | sprintf(pin_index_buffer,"%d", gpio);
|
| 153 | result = write(file,pin_index_buffer,strlen(pin_index_buffer));
|
| 154 | if(result < 0)
|
| 155 | {
|
| 156 | LOGE("Gpio[%d] export fail.", gpio);
|
| 157 | close(file);
|
| 158 | return -1;
|
| 159 | }
|
| 160 | close(file);
|
| 161 |
|
| 162 | return 0;
|
| 163 | }
|
| 164 |
|
| 165 | static int gpio_unexport(int gpio)
|
| 166 | {
|
| 167 | int file=-1;
|
| 168 | int result =-1;
|
| 169 | char pin_index_buffer[5]= {0};
|
| 170 | if (gpio_check(gpio) == -1)
|
| 171 | {
|
| 172 | LOGE("export has been not add\n");
|
| 173 | return -1;
|
| 174 | }
|
| 175 |
|
| 176 | file = open("/sys/class/gpio/unexport",O_WRONLY);
|
| 177 | if(file == -1)
|
| 178 | {
|
| 179 | LOGE("Open gpio unexport file fail.");
|
| 180 | return -1;
|
| 181 | }
|
| 182 |
|
| 183 | memset(pin_index_buffer,0,5);
|
| 184 | sprintf(pin_index_buffer,"%d", gpio);
|
| 185 | result=write(file,pin_index_buffer,strlen(pin_index_buffer));
|
| 186 | if(result < 0)
|
| 187 | {
|
| 188 | close(file);
|
| 189 | LOGE("Gpio[%d] unexport fail.", gpio);
|
| 190 | return -1;
|
| 191 | }
|
| 192 | close(file);
|
| 193 |
|
| 194 | return 0;
|
| 195 | }
|
| 196 |
|
| 197 | int32_t gsw_gpio_SetDirection(uint32_t gpioNum, gsw_hal_pinDirection dir)
|
| 198 | {
|
| 199 | char buffer[128]= {0};
|
| 200 | int file =-1;
|
| 201 | int result =-1;
|
| 202 | char direction[10] = {0};
|
| 203 | memset(buffer,0,128);
|
| 204 | if (handle())
|
| 205 | return GSW_HAL_FAIL;
|
| 206 |
|
| 207 | if (gpio_check(gpioNum) == -1)
|
| 208 | {
|
| 209 | LOGD("in dir export has been not add\n");
|
| 210 | gpio_export(gpioNum);
|
| 211 | }
|
| 212 |
|
| 213 |
|
| 214 | sprintf(buffer,"/sys/class/gpio/gpio%u/direction", gpioNum);
|
| 215 | file = open(buffer, O_WRONLY);
|
| 216 | if(file == -1)
|
| 217 | {
|
| 218 | LOGE("Open gpio[%d] direct fail.", gpioNum);
|
| 219 | return -1;
|
| 220 | }
|
| 221 | if (dir == GSW_HAL_PINDIR_IN)
|
| 222 | sprintf(direction,"in");
|
| 223 | else if (dir == GSW_HAL_PINDIR_OUT)
|
| 224 | sprintf(direction,"out");
|
| 225 |
|
| 226 | result = write(file,direction,strlen(direction));
|
| 227 | if(result != strlen(direction))
|
| 228 | {
|
| 229 | LOGE("Set gpio[%d] direct fail.", gpioNum);
|
| 230 | close(file);
|
| 231 | gpio_unexport(gpioNum);
|
| 232 | return GSW_HAL_FAIL;
|
| 233 | }
|
| 234 | close(file);
|
| 235 | gpio_unexport(gpioNum);
|
| 236 | return GSW_HAL_SUCCESS;
|
| 237 | }
|
| 238 |
|
| 239 | gsw_hal_pinLevel gsw_gpio_GetLevel(uint32_t gpioNum)
|
| 240 | {
|
| 241 | char buffer[50];
|
| 242 | char path[128];
|
| 243 | int file =-1;
|
| 244 | int result =-1;
|
| 245 | memset(path,0,128);
|
| 246 | memset(buffer,0,50);
|
| 247 | if (handle())
|
| 248 | return GSW_HAL_FAIL;
|
| 249 |
|
| 250 | if (gpio_check(gpioNum) == -1)
|
| 251 | {
|
| 252 | LOGD("in get value export has been not add\n");
|
| 253 | gpio_export(gpioNum);
|
| 254 | }
|
| 255 | sprintf(path,"/sys/class/gpio/gpio%u/value", gpioNum);
|
| 256 | file = open(path,O_RDONLY);
|
| 257 | if(file == -1)
|
| 258 | {
|
| 259 | LOGE("Open gpio[%d] fail.", gpioNum);
|
| 260 | return -1;
|
| 261 | }
|
| 262 | result = read(file,buffer,50);
|
| 263 | if(result <= 0)
|
| 264 | {
|
| 265 | LOGE("Get gpio[%d] value fail", gpioNum);
|
| 266 | close(file);
|
| 267 | gpio_unexport(gpioNum);
|
| 268 | return GSW_HAL_FAIL;
|
| 269 | }
|
| 270 | close(file);
|
| 271 | gpio_unexport(gpioNum);
|
| 272 | return atoi(buffer);
|
| 273 | }
|
| 274 |
|
| 275 | int32_t gsw_gpio_SetLevel(uint32_t gpioNum, gsw_hal_pinLevel level)
|
| 276 | {
|
| 277 | char buffer[128]= {0};
|
| 278 | int file =-1;
|
| 279 | int result =-1;
|
| 280 | memset(buffer,0,50);
|
| 281 | if (handle())
|
| 282 | return GSW_HAL_FAIL;
|
| 283 |
|
| 284 | if (gpio_check(gpioNum) == -1)
|
| 285 | {
|
| 286 | LOGD("in set value export has been not add\n");
|
| 287 | gpio_export(gpioNum);
|
| 288 | }
|
| 289 |
|
| 290 | sprintf(buffer,"/sys/class/gpio/gpio%u/value", gpioNum);
|
| 291 | file = open(buffer,O_WRONLY);
|
| 292 | if(file == -1)
|
| 293 | {
|
| 294 | LOGE("Open gpio[%d] value fail.", gpioNum);
|
| 295 | return -1;
|
| 296 | }
|
| 297 | if(level == GSW_HAL_LEVEL_LOW) {
|
| 298 | result = write(file,"0",1);
|
| 299 | } else {
|
| 300 | result = write(file,"1",1);
|
| 301 | }
|
| 302 | if(result != 1)
|
| 303 | {
|
| 304 | LOGE("Set gpio[%d] value fail err =%d.", gpioNum, errno);
|
| 305 | close(file);
|
| 306 | gpio_unexport(gpioNum);
|
| 307 | return GSW_HAL_FAIL;
|
| 308 | }
|
| 309 | close(file);
|
| 310 | gpio_unexport(gpioNum);
|
| 311 | return GSW_HAL_SUCCESS;
|
| 312 | }
|
| 313 |
|
| 314 |
|
| 315 | int32_t gsw_gpio_Init(uint32_t gpioNum, gsw_hal_pinDirection dir, gsw_hal_pinLevel level, gsw_hal_pinPullSel pullsel)
|
| 316 | {
|
| 317 | if (handle())
|
| 318 | return GSW_HAL_FAIL;
|
| 319 |
|
| 320 | if (dir != 1 && dir != 0)
|
| 321 | {
|
| 322 | LOGE("[lynq_gpio_init] direction fail.");
|
| 323 | return -1;
|
| 324 | }
|
| 325 |
|
| 326 | if (level != 1 && level != 0)
|
| 327 | {
|
| 328 | LOGE("[lynq_gpio_init] value fail.");
|
| 329 | return GSW_HAL_FAIL;
|
| 330 | }
|
| 331 |
|
| 332 | if(gpio_export(gpioNum))
|
| 333 | {
|
| 334 | LOGE("[lynq_gpio_init]gpio_export fail.");
|
| 335 | return GSW_HAL_FAIL;
|
| 336 | }
|
| 337 |
|
| 338 | if(gsw_gpio_SetDirection(gpioNum, dir))
|
| 339 | {
|
| 340 | LOGE("[lynq_gpio_init]gpio_direct_set fail.");
|
| 341 | return GSW_HAL_FAIL;
|
| 342 | }
|
| 343 |
|
| 344 | if(dir == 1 && (gsw_gpio_SetLevel(gpioNum, level) != 0))
|
| 345 | {
|
| 346 | LOGE("[lynq_gpio_init]gpio_value_set fail.");
|
| 347 | return GSW_HAL_FAIL;
|
| 348 | }
|
| 349 | return GSW_HAL_SUCCESS;
|
| 350 | }
|
| 351 | gsw_hal_usbStatus gsw_hwpin_is_usb_connected(void)
|
| 352 | {
|
| 353 | FILE *fp;
|
| 354 | char cmd[128] = {0};
|
| 355 | char tmp_rsp[20] = {0};
|
| 356 | //char *CONNECTED = "configured";
|
| 357 | char *DISCONNECTED = "not attached";
|
| 358 | if (handle())
|
| 359 | return GSW_HAL_FAIL;
|
| 360 |
|
| 361 | sprintf(cmd,"cat /sys/devices/platform/soc/d4200000.axi/c0000000.usb/udc/c0000000.usb/state");
|
| 362 | fp=popen(cmd, "r");
|
| 363 | if (fgets(tmp_rsp,sizeof(tmp_rsp),fp) == NULL)
|
| 364 | LOGE("gsw_hwpin_is_usb_connected fail.");
|
| 365 | pclose(fp);
|
| 366 | if (strncmp(tmp_rsp,DISCONNECTED,strlen(DISCONNECTED)) == 0)
|
| 367 | return GSW_HAL_USB_DISCONNECTED;
|
| 368 | else
|
| 369 | return GSW_HAL_USB_CONNECTED;
|
| 370 | }
|
| 371 |
|
| 372 | int32_t gsw_setWakeupLevel(gsw_hal_pinLevel level)
|
| 373 | {
|
| 374 | if (gsw_gpio_SetLevel(wakeout, level) == GSW_HAL_FAIL)
|
| 375 | {
|
| 376 | return GSW_HAL_FAIL;
|
| 377 | }
|
| 378 | return GSW_HAL_SUCCESS;
|
| 379 | }
|