在线时间897 小时
UID3067179
注册时间2014-10-23
NXP金币2776
TA的每日心情 | 开心 11 小时前 |
---|
签到天数: 1510 天 连续签到: 12 天 [LV.Master]伴坛终老
版主
  
- 积分
- 8758
- 最后登录
- 2025-8-31
|
本帖最后由 TLLED 于 2025-6-25 22:00 编辑
一、下载库
下载地址:https://github.com/brgl/libgpiod/tree/v1.6.x
二、安装库
2.1、安装编译需要的软件包
- hui@hui-virtual-machine:~$ sudo apt-get update
- hui@hui-virtual-machine:~$ sudo apt-get upgrade
- hui@hui-virtual-machine:~$ sudo apt install pkg-config
- hui@hui-virtual-machine:~$ sudo apt-get install autoconf automake libtool
- hui@hui-virtual-machine:~$ sudo apt-get install autoconf-archive
- hui@hui-virtual-machine:~$ sudo apt-get install m4
复制代码
2.2、安装
2.2.1、安装命令:
- hui@hui-virtual-machine:~/libgpiod$ ./autogen.sh --enable-tools=yes --prefix=${PWD}/build --host=aarch64-poky-linux
复制代码
安装报错1:
执行:
- hui@hui-virtual-machine:~/libgpiod$ autoreconf -vi
复制代码
2.2.3、继续安装
执行:
- hui@hui-virtual-machine:~/libgpiod$ ./configure --enable-tools=yes --prefix=${PWD}/build --host=aarch64-poky-linux
复制代码
安装报错2:
修改confgure 文件
- hui@hui-virtual-machine:~/libgpiod$ vi configure
复制代码 屏蔽掉 AX_CXX_COMPILE_STDCXX_17(ext, mandatory)
正常后,编译通过
2.2.4、继续安装
执行命令:
- hui@hui-virtual-machine:~/libgpiod$ make
- hui@hui-virtual-machine:~/libgpiod$ make install
复制代码
安装完成后,会在build文件下生成库文件
后面编译使用此编译后的库文件。
三、硬件部分
3.1、LED硬件部分
3.2、使用到开发板的端口
3.3、手册中端口的定义
四、程序部分
4.1、gpio_tesc.c
- #include<stdio.h>
- #include<unistd.h>
- #include<gpiod.h>
- #define PIN_IO2_12 12 //D9 -- LD4
- #define PIN_IO2_21 21 //D8 -- LD3
- #define PIN_IO2_20 20 //D7 -- LD2
- #define PIN_IO2_19 19 //D6 -- LD1
- #define PIN_IO2_18 18 //D5 -- K4
- int main()
- {
- uint8_t ds=1;
- uint8_t dd=0;
- struct gpiod_chip *gchip;
- struct gpiod_line_info *glinein, *glineout;
- struct gpiod_line_settings *gline_settings_in, *gline_settings_out;
- struct gpiod_line_config *gline_config_in, *gline_config_out;
- struct gpiod_request_config *gline_request_config_in, *gline_request_config_out;
- struct gpiod_line_request *gline_request_in, *gline_request_out;
- int offset_in[1] = {PIN_IO2_18};
- int offset_out[3] = {PIN_IO2_21,PIN_IO2_20,PIN_IO2_19};
- int value;
- if ((gchip=gpiod_chip_open("/dev/gpiochip0")) == NULL)
- {
- perror("gpiod_chip_open");
- return 1;
- }
- gline_settings_out = gpiod_line_settings_new();
- if (gpiod_line_settings_set_direction(gline_settings_out, GPIOD_LINE_DIRECTION_OUTPUT) != 0)
- {
- perror("gpiod_line_settings_set_direction");
- }
- gline_config_out = gpiod_line_config_new();
- gpiod_line_config_add_line_settings(gline_config_out, offset_out, 3, gline_settings_out);
- gline_request_config_out = gpiod_request_config_new();
- gline_request_out = gpiod_chip_request_lines(gchip, gline_request_config_out, gline_config_out);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_12, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_21, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_20, 1);
- for (int i = 0; i < 100; i++)
- {
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_21, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_20, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_19, 0);
- usleep(200000);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_21, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_20, 0);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_19, 1);
- usleep(200000);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_21, 0);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_20, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_19, 1);
- usleep(200000);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_21, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_20, 1);
- gpiod_line_request_set_value(gline_request_out, PIN_IO2_19, 1);
- usleep(200000);
- printf("gpio out test!\n");
- }
- gpiod_chip_close(gchip);
- return 0;
- }
复制代码
4.2、编译程序
执行编译命令:
- hui@hui-virtual-machine:~/gpio_test$ $CC gpio_test.c -o gpio_test -I /home/hui/libgpiod/build/include -L /home/hui/libgpiod/build/lib -lgpiod
复制代码
编译完成后,生成的文件
4.3、复制文件到开发板
五、程序运行
开发板系统中已经有libgpiod库,只复制应用程序运行。
运行命令:./gpio_test
点亮LED灯
|
|