本文來源電子發(fā)燒友社區(qū),作者:劉建華, 帖子地址:https://bbs.elecfans.com/jishu_2308743_1_1.html
【目的】學(xué)習(xí)使用GPIO外設(shè)。
1、學(xué)習(xí)資源:EASY EAI靈眸科技 | 讓邊緣AI落地更簡單 (easy-eai.com)認直學(xué)習(xí)了gpio外設(shè)的使用。
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、編譯,執(zhí)行./build.sh,生成了my_gpio。
5、用file檢查一下my_gpio,是arm的程序。
6、用adb push到開發(fā)板:
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、用杜綁線將開發(fā)板P1的15腳與LED燈的正級相連,LED燈的負極接開發(fā)板的地。
8、adb登錄到開發(fā)板,進入userdata/Demo目錄,執(zhí)行./my_gpio:
看到終端輸入,同時LED燈也一秒交替亮滅。
附上開發(fā)板體驗視頻(詳見作者原試用帖子)
-
nano
+關(guān)注
關(guān)注
1文章
155瀏覽量
30618 -
靈眸
+關(guān)注
關(guān)注
0文章
19瀏覽量
3195 -
開發(fā)板試用
+關(guān)注
關(guān)注
3文章
301瀏覽量
2100
發(fā)布評論請先 登錄
相關(guān)推薦
評論