openwrt中控制gpio引脚

张开发
2026/4/16 19:49:48 15 分钟阅读

分享文章

openwrt中控制gpio引脚
在物联网产品开发中经常要控制一些led灯或者监听一些按键可以使用内核的gpio export功能导出需要的引脚。/** create by iversondeng168 */ #include stdlib.h #include stdio.h #include string.h #include unistd.h #include fcntl.h //sysfs #define GPIO_EXPORT /sys/class/gpio/export #define GPIO_PIN_VAL 54 #define GPIO_DIR /sys/class/gpio/gpio54/direction #define GPIO_DIR_VAL OUT #define GPIO_VAL /sys/class/gpio/gpio54/value #define GPIO_VAL_H 1 #define GPIO_VAL_L 0 int main(void) { int fd; //使能gpio export fd open(GPIO_EXPORT, O_WRONLY); if(fd 0) { return -1; } //向export中导出gpio 54引脚 write(fd, GPIO_PIN_VAL ,sizeof(GPIO_PIN_VAL)); close(fd); //配置gpio 54输入输出方向 fd open(GPIO_DIR, O_WRONLY); if(fd 0) { return -1; } write(fd, GPIO_DIR_VAL, sizeof(GPIO_DIR_VAL)); close(fd); //配置gpio 54输出高低电平 fd open(GPIO_VAL, O_RDWR); if(fd 0) { return -1; } write(fd, GPIO_VAL_H, sizeof(GPIO_VAL_H)); close(fd); return 0; }执行交叉编译/home/openwrt-master/staging_dir/toolchain-mips_74kc_gcc-5.3.0_musl-1.1.16/bin/mips-openwrt-linux-gcc gpio_export_test.c -o gpio_export_test

更多文章