gpio操作


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/poll.h>

#define err_out(fmt, ...)                                                      \
    do                                                                         \
    {                                                                          \
        printf(fmt, ##__VA_ARGS__);                                            \
        printf("<%s>  \"%s()\" %d error\n", __FILE__, __FUNCTION__, __LINE__); \
    } while (0)

#define open_gpio_file(path, flag, fd)              \
    do                                              \
    {                                               \
        fd = open(path, flag);                      \
        if (fd < 0)                                 \
        {                                           \
            err_out("\"%s\" open failed \n", path); \
            return (-1);                            \
        }                                           \
    } while (0);

/**
 * @brief : gpio 导出
 * @param : pin
 * @retval: 0 成功; -1失败
 */
static int gpio_export(const int pin)
{
    int fd, len;
    char buffer[64];
    char path[64];

    sprintf(&path[0], "/sys/class/gpio/gpio%d", pin);
    /* 文件不存在时,导出gpio*/
    if (access(path, F_OK) != 0)
    {
        memset(path, 0, 64);
        sprintf(&path[0], "/sys/class/gpio/export");
        open_gpio_file(path, O_WRONLY, fd);
        len = snprintf(buffer, sizeof(buffer), "%d", pin);

        if (write(fd, buffer, len) < 0)
        {
            err_out("write failed to export gpio!\n");
            return -1;
        }
        close(fd);
    }
    return 0;
}
/**
 * @brief : gpio 卸载
 * @param : pin
 * @retval: 0 成功; -1失败
 */
static int gpio_unexport(const int pin)
{
    int fd, len;
    char buffer[64];
    char path[64];

    sprintf(&path[0], "/sys/class/gpio/gpio%d", pin);
    /* 文件存在时,卸载gpio*/
    if (access(path, F_OK) == 0)
    {
        memset(path, 0, 64);
        sprintf(&path[0], "/sys/class/gpio/unexport");
        open_gpio_file(path, O_WRONLY, fd);
        len = snprintf(buffer, sizeof(buffer), "%d", pin);
        if (write(fd, buffer, len) < 0)
        {
            err_out("write failed to unexport gpio!\n");
            return -1;
        }
        close(fd);
    }
    return 0;
}

/**
 * @brief : gpio 方向控制
 * @param : dir 方向(in/out)
 * @retval: 0 成功; -1失败
 */
static int gpio_direction(const int pin, const char *dir)
{
    int fd;
    char path[64];
    int w_len = ((dir[0] == 'i') && (dir[1] == 'n')) ? 2 : 3;

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
    open_gpio_file(path, O_WRONLY, fd);
    if (write(fd, dir, w_len) < 0)
    {
        err_out("Failed to set direction!\n");
        return -1;
    }

    close(fd);
    return 0;
}

/**
 * @brief : gpio 写
 * @param : 0 / 1
 * @retval: 0 成功; -1失败
 */
static int gpio_write(const int pin, const int value)
{
    int fd;
    char path[64];
    const char *val[] = {"0", "1"};

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
    open_gpio_file(path, O_WRONLY, fd);

    if (write(fd, val[value], 1) < 0)
    {
        err_out("Failed to write value!\n");
        return -1;
    }

    close(fd);
    return 0;
}

/**
 * @brief : gpio 读
 * @param : 读取的引脚值
 * @retval: 返回引脚的值 0 / 1
 */
static int gpio_read(const int pin)
{
    int fd;
    char path[64];
    char value_str[3];

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
    open_gpio_file(path, O_RDONLY, fd);
    if (read(fd, value_str, 3) < 0)
    {
        err_out("Failed to read value!\n");
        return -1;
    }

    close(fd);
    return (atoi(value_str));
}

/**
 * @brief : gpio 中断控制
 * @param : 0 none 表示引脚为输入,不是中断引脚
 *   @arg : 1 rising 表示引脚为中断输入,上升沿触发
 *   @arg : 2 falling 表示引脚为中断输入,下降沿触发
 *   @arg : 3 both 表示引脚为中断输入,边沿触发
 * @retval:
 */
static int gpio_edge(const int pin, int edge)
{
    int fd;
    char path[64];
    const char *dir_str[] = {"none", "rising", "falling", "both"};

    if (edge > 3)
    {
        err_out("Failed edge parameter error\n");
        return -1;
    }

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
    open_gpio_file(path, O_WRONLY, fd);

    if (write(fd, dir_str[edge], strlen(dir_str[edge])) < 0)
    {
        err_out("Failed to set edge!\n");
        return -1;
    }

    close(fd);
    return 0;
}

int main()
{
    char buff[10], res;
    int led_fd, key_fd;
    struct pollfd fds;

    /* 按键led指示灯*/
    gpio_export(55);
    gpio_direction(55, "out");
    gpio_write(55, 0);

    /* 按键引脚初始化*/
    gpio_export(70);
    gpio_direction(70, "in");
    gpio_edge(70, 2);
    open_gpio_file("/sys/class/gpio/gpio70/value", O_RDONLY, key_fd);

    /* poll 操作的文件符号*/
    fds.fd = key_fd;

    /* 经测试io中断产生后,正常会发送POLLPRI 这个中断,循环中必须等待这个事件*/
    fds.events = POLLPRI | POLLERR;

    /* 真实发生的事件*/
    fds.revents = 0;
    while (1)
    {
        int ret = poll(&fds, 1, -1);
        if (!ret)
        {
            err_out("poll time out\n");
        }
        else
        {
            if (fds.revents & POLLPRI)
            {
                gpio_write(55, gpio_read(55) ? 0 : 1);

                res = lseek(fds.fd, 0, SEEK_SET); /* 读取按键值*/
                if (res == -1)
                {
                    printf("lseek failed!\n");
                    return -1;
                }
                res = read(fds.fd, buff, sizeof(buff));
                if (res == -1)
                {
                    perror("read failed!\n");
                    return -1;
                }
                printf("fds.revents %d key value %s \n", fds.revents, buff);
            }
        }
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c2h5oh2012

有用记得给点打赏yo

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值