Linux/Android系统开发 控制GPIO管脚输出高低电平源码

2019-04-14 18:16发布

adb指令实现: adb root adb remount adb shell echo -wdout 5 0 > /sys/class/misc/mtgpio/pin echo -wdout 5 1 > /sys/class/misc/mtgpio/pin C程序实现: 此方法适用于linux内核的系统,测试平台MTK8735。 输入参数:port ---> GPIO端口,如设置MTK的GPIO1端口时port=1   value ---> 1为高电平,0为低电平 返回值:小于等于0时设置失败;大于0时设置成功。 int setGpio(int port,int value) { int result = 0; int gpio_fd = 0; char H_buf[128] = {0}; char L_buf[128] = {0}; gpio_fd = open("/sys/class/misc/mtgpio/pin", O_RDWR); if(gpio_fd <= 0){ printf("open gpio port failed"); return 0; } sprintf(H_buf,"-wdout %d 1",port);//设置GPIO端口port为高电平 sprintf(L_buf,"-wdout %d 0",port);//设置GPIO端口port为低电平 if(value== 1) { result = write(gpio_fd, H_buf, sizeof(H_buf)); } if(value== 0) else { result = write(gpio_fd, L_buf, sizeof(L_buf)); }else{ printf("gpio value is err");   } if(result <= 0){ printf("write gpio port failed"); }else{ printf("write gpio port success"); } close(gpio_fd); gpio_fd = -1; return result; }