Linux下GIPO的操作

使用系统自带的GPIO

 首先,看看系统中有没有“/sys/class/gpio”这个文件夹。
如果没有请在编译内核 make menuconfig 的时候 加入
   Device Drivers-> GPIO Support ->/sys/class/gpio/… (sysfs interface)。


1. echo 9  > /sys/class/gpio/export
2. echo "out" > /sys/class/gpio/gpio9/direction
3. echo "1" > /sys/class/gpio/gpio9/value


1. echo 9  > /sys/class/gpio/export
2. echo "in" > /sys/class/gpio/gpio9/direction
3. cat  > /sys/class/gpio/gpio9/value

  echo "rising" > /sys/class/gpio/gpio9/edge
gpio_set_edge(gpio, "rising");  
edge      表示中断的触发方式,edge文件有如下四个值:"none", "rising", "falling","both"。
           none表示引脚为输入,不是中断引脚
           rising表示引脚为中断输入,上升沿触发
           falling表示引脚为中断输入,下降沿触发
           both表示引脚为中断输入,边沿触发
         这个文件节点只有在引脚被配置为输入引脚的时候才存在。 当值是none时可以通过如下方法将变为中断引脚
      echo "both" > edge;对于是both,falling还是rising依赖具体硬件的中断的触发方式。此方法即用户态gpio转换为中断引脚的方式
           

注意:GPIO9在设备树定义中 不可复用为其它功能




使用函数调用的 参考代码如下


#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64


/****************************************************************
 * gpio_export
 ****************************************************************/
int gpio_export(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];


    fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
    if (fd < 0) {
        perror("gpio/export");
        return fd;
    }


    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);
    close(fd);


    return 0;
}


/****************************************************************
 * gpio_unexport
 ****************************************************************/
int gpio_unexport(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];


    fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
    if (fd < 0) {
        perror("gpio/export");
        return fd;
    }


    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);
    close(fd);
    return 0;
}


/****************************************************************
 * gpio_set_dir
 ****************************************************************/
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR  "/gpio%d/direction", gpio);


    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        perror("gpio/direction");
        return fd;
    }


    if (out_flag)
        write(fd, "out", 4);
    else
        write(fd, "in", 3);


    close(fd);
    return 0;
}


/****************************************************************
 * gpio_set_value
 ****************************************************************/
int gpio_set_value(unsigned int gpio, unsigned int value)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);


    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        perror("gpio/set-value");
        return fd;
    }


    if (value)
        write(fd, "1", 2);
    else
        write(fd, "0", 2);


    close(fd);
    return 0;
}


/****************************************************************
 * gpio_get_value
 ****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
    int fd, len;
    char buf[MAX_BUF];
    char ch;


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);


    fd = open(buf, O_RDONLY);
    if (fd < 0) {
        perror("gpio/get-value");
        return fd;
    }


    read(fd, &ch, 1);


    if (ch != '0') {
        *value = 1;
    } else {
        *value = 0;
    }


    close(fd);
    return 0;
}




/****************************************************************
 * gpio_set_edge
 ****************************************************************/


int gpio_set_edge(unsigned int gpio, char *edge)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);


    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        perror("gpio/set-edge");
        return fd;
    }


    write(fd, edge, strlen(edge) + 1);
    close(fd);
    return 0;
}


/****************************************************************
 * gpio_fd_open
 ****************************************************************/


int gpio_fd_open(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);


    fd = open(buf, O_RDONLY | O_NONBLOCK );
    if (fd < 0) {
        perror("gpio/fd_open");
    }
    return fd;
}


/****************************************************************
 * gpio_fd_close
 ****************************************************************/


int gpio_fd_close(int fd)
{
    return close(fd);
}




/*
int main(int argc, char **argv, char **envp)
{
    struct pollfd fdset[2];
    int nfds = 2;
    int gpio_fd, timeout, rc;
    char *buf[MAX_BUF];
    unsigned int gpio;
    int len;






    if (argc < 2) {
        printf("Usage: gpio-int <gpio-pin>\n\n");
        printf("Waits for a change in the GPIO pin voltage level or input on stdin\n");
        exit(-1);
    }


    gpio = atoi(argv[1]);


    gpio_export(gpio);
    gpio_set_dir(gpio, 0);
    gpio_set_edge(gpio, "rising");
    gpio_fd = gpio_fd_open(gpio);


    timeout = POLL_TIMEOUT;


    while (1) {
        memset((void*)fdset, 0, sizeof(fdset));


        fdset[0].fd = STDIN_FILENO;
        fdset[0].events = POLLIN;


        fdset[1].fd = gpio_fd;
        fdset[1].events = POLLPRI;


        rc = poll(fdset, nfds, timeout);


        if (rc < 0) {
            printf("\npoll() failed!\n");
            return -1;
        }


        if (rc == 0) {
            printf(".");
        }


        if (fdset[1].revents & POLLPRI) {
            len = read(fdset[1].fd, buf, MAX_BUF);
            printf("\npoll() GPIO %d interrupt occurred\n", gpio);
        }


        if (fdset[0].revents & POLLIN) {
            (void)read(fdset[0].fd, buf, 1);
            printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]);
        }


        fflush(stdout);
    }


    gpio_fd_close(gpio_fd);
    return 0;
}  */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值