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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值