RK3128开发环境操作CPU的GPIO,初始化方法如下:
int gpio_init() { int ret = 0; ret = system("cd /sys/class/gpio && echo 117 > export"); printf("ret = %d \n",ret); ret = system("cd /sys/class/gpio/gpio117 && echo out > direction"); printf("ret = %d \n",ret); ret = system("cd /sys/class/gpio/gpio117 && echo 1 > value"); printf("ret = %d \n",ret); ret = system("cd /sys/class/gpio && echo 123 > export"); printf("gpio_init ret = %d \n",ret); ret = system("cd /sys/class/gpio/gpio123 && echo in > direction"); printf("gpio_init ret = %d \n",ret); return 0; }
得到GPIO电平的方法如下:
int get_val_gpio(int gpio) { int ret = -1; char path[256]; char sys_line[256]; char buf_ps[1024]; FILE * ptr; sprintf(path, "%s%d","/sys/class/gpio/gpio",gpio); printf("\npath:%s\n",path); sprintf(sys_line, "cd %s && cat value",path); if((ptr=popen(sys_line, "r"))!=NULL) { while(fgets(buf_ps, 1024, ptr)!=NULL) { if(strlen(buf_ps)>1024) break; } pclose(ptr); ptr = NULL; printf("res[%d]:%sm >>> %s\n",(int)strlen(buf_ps),sys_line,buf_ps); } else { gpio_init(); printf("popen %s error\n", sys_line); return -1; } ret = atoi(&buf_ps[0]); return ret; }
控制GPIO 的方法
ret = system("cd /sys/class/gpio/gpio117 && echo 0 > value");//拉低 ret = system("cd /sys/class/gpio/gpio117 && echo 1 > value");//拉高