GPIO控制接口代码接口

可以通过以下接口控制gpio高低电平

static int sys_gpio_write(int pin, int value)
{
    static const char values_str[] = "01";
    char path[BUFFER_MAX] = {0};
    int fd;

    snprintf(path, BUFFER_MAX, "/sys/class/gpio/gpio%d/value", pin);
    fd = open(path, O_WRONLY);
    if (fd < 0) {
        printf("failed to open gpio value for writing!\n");
        return -1;
    }

    if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {
        printf("failed to write value!\n");
        return -1;
    }
    close(fd);
    return 0;
}

static int sys_gpio_export(int pin)
{
    char buffer[BUFFER_MAX] = {0};
    int len;
    int fd;

    fd = open("/sys/class/gpio/export", O_WRONLY);
    if (fd < 0) {
        printf("Failed to open export for writing!\n");
        return (-1);
    }

    len = snprintf(buffer, BUFFER_MAX, "%d", pin);

    if (write(fd, buffer, len) < 0) {
        printf("Fail to export gpio!\n");
        return -1;
    }

    close(fd);
    return 0;
}

static int sys_gpio_direction(int pin, int dir)
{
    static const char dir_str[] = "in\0out";
    char path[BUFFER_MAX] = {0};
    int fd;

    snprintf(path, BUFFER_MAX, "/sys/class/gpio/gpio%d/direction", pin);

    fd = open(path, O_WRONLY);
    if (fd < 0) {
        printf("failed to open gpio direction for writing!\n");
        return -1;
    }

    if (write(fd, &dir_str[dir == IN ? 0 : 3], dir == IN ? 2 : 3) < 0) {
        printf("failed to set direction!\n");
        return -1;
    }
    close(fd);
    return 0;
}

static int set_gpio(int pin, int val)
{
    int retval = 0;
    char buffer[BUFFER_MAX] = {0};

    retval = access("/sys/class/gpio", 0);
    if (-1 == retval) {
        printf("/sys/class/gpio file does not exist\n");
        return -1;
    }
    snprintf(buffer, BUFFER_MAX, "/sys/class/gpio/gpio%d", pin);
    retval = access(buffer, 0);
    if (-1 == retval) {
        sys_gpio_export(pin);
        sys_gpio_direction(pin, OUT);
    }
    sys_gpio_write(pin, val);

    return 1;
}

此操作示例通过命令实现对GPIO的读写操作
步骤 1. 在控制台使用echo命令将要操作的GPIO编号export:

echo N > /sys/class/gpio/export 
N为要操作的GPIO编号,该编号等于GPIO组号 * 8 + 组内偏移号,例如GPIO4_2编号为4 * 8 + 2 = 34。
export之后就会生成/sys/class/gpio/gpioN目录
例如:exportGPIO4_2:
echo 34 > /sys/class/gpio/export

步骤 2. 在控制台使用echo命令设置GPIO方向:

   对于输入:echo in > /sys/class/gpio/gpioN/direction
   对于输出:echo out > /sys/class/gpio/gpioN/direction
例如:设置GPIO4_2方向
−	对于输入:echo in > /sys/class/gpio/gpio34/direction
−	对于输出:echo out > /sys/class/gpio/gpio34/direction

步骤 3. 在控制台使用cat或echo命令查看GPIO输入值或设置GPIO输出值:

查看输入值:cat /sys/class/gpio/gpioN/value
输出低:echo 0 > /sys/class/gpio/gpioN/value
输出高:echo 1 > /sys/class/gpio/gpioN/value

步骤 4. 在控制台使用echo命令将操作的GPIO编号unexport:

echo N > /sys/class/gpio/unexport
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值