Linux下操作GPIO

背景

嵌入式Linux下需要经常操作GPIO管脚,其中一种方式是使用/sys/文件系统下内核暴露出来的gpio文件。

代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

int set_gpio_val(int gpio, int val)
{
    int fd = -1;
    char buf[64] = {0};

    /* export */
    fd = open("/sys/class/gpio/export", O_WRONLY);
    if (fd < 0) {
        printf("/sys/class/gpio/export open error\n");
        return -1;
    }
    sprintf(buf, "%d", gpio);
    write(fd, buf, 3);
    close(fd);

    /* set direction: in or out */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        printf("open direction\n");
        return -1;
    }
    write(fd, "out", 3);
    close(fd);

    /* set value: 1 or 0 */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        printf("set value error\n");
        return -1;
    }
    sprintf(buf, "%d", val ? 1 : 0);
    write(fd, buf, 1);
    close(fd);

    /* unexport */
    fd = open("/sys/class/gpio/unexport", O_WRONLY);
    if (fd < 0) {
        printf("unexport error\n");
        return -1;
    }
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%d", gpio);
    write(fd, buf, 3);

    return 0;
}
                                                                         
int main()
{
	/* lora aux: 205 + 232 */
	set_gpio_val(437, 0);

	return 0;
}
参考

https://blog.csdn.net/donglicaiju76152/article/details/49962631

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以使用一系列的命令来操作GPIO(通用输入输出)引脚。首先,需要通过echo命令将要操作GPIO引脚导出,使用如下命令:echo N > /sys/class/gpio/export ,其中N表示GPIO的编号。 接下来,可以通过设置direction文件来定义GPIO的输入输出方向。例如,要将GPIO设置为输出,可以使用如下命令:echo out > /sys/class/gpio/gpioN/direction ,其中N为GPIO的编号。 一旦GPIO被设置为输出,可以使用echo命令将特定的值写入value文件,以控制GPIO的电平状态。例如,要将GPIO置高,可以使用如下命令:echo 1 > /sys/class/gpio/gpioN/value。同样地,要将GPIO置低,可以使用如下命令:echo 0 > /sys/class/gpio/gpioN/value。 最后,当不再需要操作某个GPIO时,可以使用echo命令将其取消导出,使用如下命令:echo N > /sys/class/gpio/unexport ,其中N表示GPIO的编号。 需要注意的是,GPIO的具体编号和可用性可能会根据所使用的硬件平台和系统配置而有所不同。因此,在实际操作中,需要根据具体情况来替换命令中的GPIO编号。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [linux操作GPIO命令](https://blog.csdn.net/great_Jiang/article/details/125652656)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Linux下用文件IO的方式操作GPIO(/sys/class/gpio)](https://blog.csdn.net/luckydarcy/article/details/53061901)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值