ARM40-A5D27应用程序——GPIO输入

ARM40-A5D27应用程序——GPIO输入

 Linux内核4.8版本后,提供了新的GPIO字符驱动接口gpiod;若内核版本不支持gpiod的方式,则需要使用GPIO sysfs interface接口,见《ARM40-A5应用程序——GPIO输出高低电平》的示例。
 本文测试板为ARM40-A5D27.

一、在shell中测试

# gpiofind PC9
gpiochip0 73

//get PC9 value:
# gpioget gpiochip0 73
1

二、获取GPIO的值

 文件名为 test_gpio_input.c,代码见本文附录(1)。
 测试DI1~DI12等12个gpio的状态。

三、交叉编译

arm-linux-gnueabihf-gcc -o test_gpio_input test_gpio_input.c

四、执行程序

 将交叉编译得到的 test_gpio_input 文件拷贝到ARM40-A5D27板中,执行程序:

./test_gpio_input
di-input is: 0 0 1 1 1 1 1 1 1 1 1 1

 用示波器可观察对应IO上的电平是否与程序一致。

参考文章:

https://cybertale.github.io/2018/08/15/ELCE-2017-新的用户空间GPIO接口.html
https://microchipdeveloper.com/32mpu:apps-gpio
http://wiki.i2som.com/pages/viewpage.action?pageId=29655687
http://bluequiet.blogspot.com/2019/09/gpio-gpiohandlerequest-gpiochip.html
http://www.dydata.cc/Admin/ShowFile/%60Linux%60kernel%60linux-5%7C0%60tools%60gpio%60gpio-event-mon%7Cc
https://www.cnx-software.com/2017/11/03/learn-more-about-linuxs-new-gpio-user-space-subsystem-libgpiod/
https://blog.adafruit.com/2018/11/26/sysfs-is-dead-long-live-libgpiod-libgpiod-for-linux-circuitpython/
https://cregit.linuxsources.org/code/4.8/tools/gpio/gpio-hammer.c.html
http://tomoyo.osdn.jp/cgi-bin/lxr/source/tools/gpio/lsgpio.c?v=linux-4.7.10
https://framagit.org/cpb/ioctl-access-to-
gpio/commit/bbde7780f1ae12fba3836c21062baea4b0546e85
荟聚计划:共商 共建 共享 Grant

附:

(1)test_gpio_input.c代码:

// test for gpio input
// 128 gpio in gpiochip0
// 0  ~ 31  PA0 -> PA31
// 32 ~ 63  PB0 -> PB31
// 33 ~ 95  PC0 -> PC31
// 96 ~ 127 PD0 -> PD31

#define DI_LINES        12
#define DI1             73      //PC9
#define DI2             74      //PC10
#define DI3             75      //PC11
#define DI4             76      //PC12
#define DI5             77      //PC13
#define DI6             78      //PC14
#define DI7             79      //PC15
#define DI8             80      //PC16
#define DI9             81      //PC17
#define DI10            82      //PC18
#define DI11            85      //PC21
#define DI12            86      //PC22

#define DEV_GPIO  "/dev/gpiochip0"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <linux/gpio.h>
#include <sys/ioctl.h>

int main(int argc, char *argv[])
{
        int fd;
        int ret;

        struct gpiohandle_request req;
        struct gpiohandle_data data;

        /* open gpio */
        fd = open(DEV_GPIO, 0);
        if (fd < 0) {
                printf("ERROR: open %s ret=%d\n", DEV_GPIO, fd);
                return -1;
        }

        req.lineoffsets[0] = DI1;
        req.lineoffsets[1] = DI2;
        req.lineoffsets[2] = DI3;
        req.lineoffsets[3] = DI4;
        req.lineoffsets[4] = DI5;
        req.lineoffsets[5] = DI6;
        req.lineoffsets[6] = DI7;
        req.lineoffsets[7] = DI8;
        req.lineoffsets[8] = DI9;
        req.lineoffsets[9] = DI10;
        req.lineoffsets[10] = DI11;
        req.lineoffsets[11] = DI12;
        req.lines = DI_LINES;
        req.flags = GPIOHANDLE_REQUEST_INPUT;           // Request lines as input
        strcpy(req.consumer_label, "di-input");
        ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
        if (ret < 0) {
                printf("Failed to issue GPIO_GET_LINEHANDLE_IOCTL(%d)\n", ret);
                close(fd);
                return -1;
        }
        ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
        if (ret < 0) {
                printf("Failed to issue GPIOHANDLE_GET_LINE_VALUES_IOCTL(%d)\n", ret);
                close(fd);
                return -1;
        }
        printf("di-input is: %d %d %d %d %d %d %d %d %d %d %d %d\n",
                data.values[0],data.values[1],data.values[2],data.values[3],data.values[4],data.values[5],
                data.values[6],data.values[7],data.values[8],data.values[9],data.values[10],data.values[11]);

        close(fd);
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值