本文来源电子发烧友社区,作者:刘建华, 帖子地址:https://bbs.elecfans.com/jishu_2308743_1_1.html
【目的】学习使用GPIO外设。
1、学习资源:EASY EAI灵眸科技 | 让边缘AI落地更简单 (easy-eai.com)认直学习了gpio外设的使用。
2、在灵眸科技的示例文件夹下新建文件mypro1。在该文件夹下新建一个mypo1_gpio.c
3、代码如下:
#include
#include
#include
#ifndef CONSUMER
#define CONSUMER "Consumer"
#endif
int main(int argc, char **argv)
{
char *chipname = "gpiochip3";
unsigned int line_num = 11; // GPIO GPIO3_B3
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 failedn");
goto end;
}
line = gpiod_chip_get_line(chip, line_num);
if (!line) {
perror("Get line failedn");
goto close_chip;
}
ret = gpiod_line_request_output(line, CONSUMER, 0, 0);
if (ret < 0) {
perror("Request line as output failedn");
goto release_line;
}
/* Blink 20 times */
val = 0;
for (i = 20; i > 0; i--) {
ret = gpiod_line_set_value(line, val);
if (ret < 0) {
perror("Set line output failedn");
goto release_line;
}
printf("Output %u on line #%un", 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++")
# find host dir and set CMAKE_TOOLCHAIN_FILE before call project
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)
#--------------------------
# test-gpio
#--------------------------
add_executable(my_gpio mypro1_gpio.c) #-o 这里指定要编译的文件与输出的文件名
target_link_libraries(my_gpio pthread gpiod) #-l 这里指链接的文件名my_gpio
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灯也一秒交替亮灭。
附上开发板体验视频(详见作者原试用帖子)
全部0条评论
快来发表一下你的评论吧 !