Linux设备io方式控制IO

头文件包含:

#include "gpioset.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <poll.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <sys/epoll.h>
#include <QMutex> 

接口函数:

static QMutex gpio_mutex
void gpio_ctrl(unsigned int gpio_number, const char *direction,unsigned int value)
{
    QMutexLocker ioLock( &gpio_mutex );
    gpio_export(gpio_number);
    gpio_set_dir(gpio_number, direction);
    gpio_set_value(gpio_number, value);
}

gpio导出函数:

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

    fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
    if (fd < 0) {
         printf ("\nFailed export GPIO-%d\n", gpio);
        return fd;
    }

    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);
    close(fd);
   // printf ("\nSucessfully export GPIO-%d\n", gpio);
    return 0;
}

gpio关闭导出:

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

    fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
    if (fd < 0) {
         printf ("\nFailed unexport GPIO-%d\n", gpio);
        return fd;
    }

    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);
    close(fd);
   // printf ("\nSucessfully unexport GPIO-%d\n", gpio);
    return 0;
}

设置gpio方向,输入或输出:

int gpio_set_dir(unsigned int gpio, const char *dir)
{
    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) {
         printf ("\nFailed set GPIO-%d direction\n", gpio);
        return fd;
    }

    write(fd, dir, strlen(dir)+1);
    close(fd);
    //printf ("\nSucessfully set GPIO-%d direction\n", gpio);
    return 0;
}

设置gpio的值,0或1:

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) {
        printf ("\nFailed set GPIO-%d value\n", gpio);
        return fd;
    }

    if (value!=0)
        {
        int i = write(fd, "1", 2);
       // printf ("\nGPIO-%d value set high\n", gpio);
        }
    else
        {
        write(fd, "0", 2);
       // printf ("\nGPIO-%d value set low\n", gpio);
        }

    close(fd);
    //printf ("\nSucessfully set GPIO-%d value\n", gpio);
    return 0;
}

获取gpio的值,0或1:

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) {
        //printf ("\nFailed get GPIO-%d value\n", gpio);
        return fd;
    }

    read(fd, &ch, 1);

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

    close(fd);
    //printf ("\nSucessfully get GPIO-%d value\n", gpio);
    return 0;
}

设置gpio边沿,上升沿或下降沿:

int gpio_set_edge(unsigned int gpio, const 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) {
        //printf ("\nFailed set GPIO-%d edge\n", gpio);
        return fd;
    }

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

打开gpio:

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

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

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

关闭文件描述符:

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

使用:

#define SYSFS_GPIO_DIR "/sys/class/gpio"
enum PIN_DIRECTION{
     INPUT_PIN=0,
     OUTPUT_PIN=1
};
enum PIN_VALUE{
     LOW=0,
     HIGH=1
};



gpio_ctrl(134,"out", HIGH);
//134为gpio编号,out输出方式,输出1。

 


版权声明:本文为baidu_33879812原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。