【目的】学习使用GPIO外设。
1、学习资源:EASY EAI灵眸科技 | 让边缘AI落地更简单 (easy-eai.com)认直学习了gpio外设的使用。
2、在灵眸科技的示例文件夹下新建文件mypro1。在该文件夹下新建一个mypo1_gpio.c
3、代码如下:
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#ifndef CONSUMER
#define CONSUMER "Consumer"
#endif
int main(int argc, char **argv)
{
char *chipname = "gpiochip3";
unsigned int line_num = 11;
unsigned int val;
struct gpiod_chip *chip;
struct gpiod_line *line;
int i, ret;
chip = gpiod_chip_open_by_name(chipname);
if (!chip) {
perror("Open chip failed\n");
goto end;
}
line = gpiod_chip_get_line(chip, line_num);
if (!line) {
perror("Get line failed\n");
goto close_chip;
}
ret = gpiod_line_request_output(line, CONSUMER, 0, 0);
if (ret < 0) {
perror("Request line as output failed\n");
goto release_line;
}
val = 0;
for (i = 20; i > 0; i--) {
ret = gpiod_line_set_value(line, val);
if (ret < 0) {
perror("Set line output failed\n");
goto release_line;
}
printf("Output %u on line #%u\n", val, line_num);
sleep(1);
val = !val;
}
release_line:
gpiod_line_release(line);
close_chip:
gpiod_chip_close(chip);
end:
return 0;
}
3、在其的demo下面拷贝build.sh到文件夹下。再拷贝一个makefile.txt到此文件下,修改makefile.txt如下:
cmake_minimum_required(VERSION 2.8.4)
STRING(REGEX REPLACE ".*/(.*)" "\1" CURRENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR} )
MESSAGE("current project: " ${CURRENT_FOLDER})
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_C_COMPILER "arm-linux-gnueabihf-gcc")
set(CMAKE_CXX_COMPILER "arm-linux-gnueabihf-g++")
execute_process(COMMAND which ${CMAKE_C_COMPILER}
OUTPUT_VARIABLE gcc_full_path)
get_filename_component(gcc_full_dir ${gcc_full_path} DIRECTORY)
set(HOST_DIR "${gcc_full_dir}/..")
set(CMAKE_TOOLCHAIN_FILE "${HOST_DIR}/share/buildroot/toolchainfile.cmake")
project(${CURRENT_FOLDER})
add_definitions(-Wall)
set(toolkit_root ${CMAKE_CURRENT_SOURCE_DIR}/../easyeai-api)
add_executable(my_gpio mypro1_gpio.c)
target_link_libraries(my_gpio pthread gpiod)
4、编译,执行./build.sh,生成了my_gpio。
5、用file检查一下my_gpio,是arm的程序。
6、用adb push到开发板:
developer@EASY-EAI-Develop:~/EASY-EAI-Toolkit-C-Demo/mypro1/build$ adb push my_gpio /userdata/Demo/my_gpio
my_gpio: 1 file pushed. 0.4 MB/s (12308 bytes in 0.026s)
7、用杜绑线将开发板P1的15脚与LED灯的正级相连,LED灯的负极接开发板的地。
8、adb登录到开发板,进入userdata/Demo目录,执行./my_gpio:
看到终端输入,同时LED灯也一秒交替亮灭。