GPIO應(yīng)用程序編寫
作者@chhjnavy
查找合適的GPIO
在這里我們選取 GPIOH14(注意目前開發(fā)使用這個(gè)pin 作為觸摸屏的pin腳,需要將觸摸屏connect斷開) ,因?yàn)榭梢酝ㄟ^排插使用杜邦線將其引出,用于連接別的設(shè)備。
計(jì)算GPIO IO號
在 Linux 系統(tǒng)中,GPIO 通常由 Pinctrl 系統(tǒng)進(jìn)行管理。Linux 定義了 Pinctrl 框架,統(tǒng)一了各大 SoC 廠商的 Pin 管理方式,避免了各大廠商自行實(shí)現(xiàn)自己的 Pin 管理系統(tǒng),是一個(gè)非常有用的功能。
每個(gè)gpio 都對應(yīng)一個(gè)IO 號:
- PH14: 7 * 32 + 14 = 238
- PH13: 7 * 32 + 13 = 237
- PH12: 7 * 32 + 12 = 236
- PH11: 7 * 32 + 11 = 235
導(dǎo)出GPIO節(jié)點(diǎn)
通過終端操作手動導(dǎo)出:
echo 238 > /sys/class/gpio/export
查看導(dǎo)出的gpio節(jié)點(diǎn)
cd /sys/class/gpio
可以看到gpio238
如果通過應(yīng)用程序?qū)С觯琧ode 如下:
#include#include #include #include #include #include #include #include #define GPIOH14 238 #define XGPIO_HIGH 1 #define XGPIO_LOW 0 /**************************************************************** * Constants ****************************************************************/ #define SYSFS_GPIO_DIR "/sys/class/gpio" #define POLL_TIMEOUT (3 * 1000) /* 3 seconds */ #define MAX_BUF 64 /**************************************************************** * gpio_export ****************************************************************/ int gpio_export(unsigned int gpio) { int fd, len; char buf[MAX_BUF]; fd = open(SYSFS_GPIO_DIR"/export", O_WRONLY); printf("open device ==================fd = %d ", fd); if (fd < 0) { ? ? ? ?printf("gpio/export "); ? ? ? ?return fd; ? ?} ? ?len = snprintf(buf, sizeof(buf), "%d", gpio); ? ?write(fd, buf, len); ? ?close(fd); ? ?return 0; }
根據(jù)IO 號導(dǎo)出gpio 節(jié)點(diǎn)是很重要的一個(gè)環(huán)節(jié),接下來就可以通過gpio 節(jié)點(diǎn),對gpio 進(jìn)行操作。
設(shè)置高低電平
#include#include #include #include #include #include #include #include #include "gpioAPIs.h" /**************************************************************** * Constants ****************************************************************/ #define SYSFS_GPIO_DIR "/sys/class/gpio" #define POLL_TIMEOUT (3 * 1000) /* 3 seconds */ #define MAX_BUF 64 /**************************************************************** * gpio_export ****************************************************************/ int gpio_export(unsigned int gpio) { int fd, len; char buf[MAX_BUF]; fd = open(SYSFS_GPIO_DIR"/export", O_WRONLY); printf("open device ==================fd = %d ", fd); if (fd < 0) { ? ? ? ?printf("gpio/export "); ? ? ? ?return fd; ? ?} ? ?len = snprintf(buf, sizeof(buf), "%d", gpio); ? ?write(fd, buf, len); ? ?close(fd); ? ?return 0; } /**************************************************************** * gpio_unexport ****************************************************************/ int gpio_unexport(unsigned int gpio) { ? ?int fd, len; ? ?char buf[MAX_BUF]; ? ?fd = open(SYSFS_GPIO_DIR"/unexport", O_WRONLY); ? ?if (fd < 0) { ? ? ? ?printf("gpio/export "); ? ? ? ?return fd; ? ?} ? ?len = snprintf(buf, sizeof(buf), "%d", gpio); ? ?write(fd, buf, len); ? ?close(fd); ? ?return 0; } /**************************************************************** * gpio_set_dir ****************************************************************/ int gpio_set_dir(unsigned int gpio, unsigned int out_flag) { ? ?int fd, len; ? ?char buf[MAX_BUF]; ? ?len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/direction", gpio); ? ?fd = open(buf, O_WRONLY); ? ?if (fd < 0) { ? ? ? ?printf("gpio/direction "); ? ? ? ?return fd; ? ?} ? ?if (out_flag) ? ? ? ?write(fd, "out", 4); ? ?else ? ? ? ?write(fd, "in", 3); ? ?close(fd); ? ?return 0; } /**************************************************************** * gpio_set_value ****************************************************************/ int gpio_set_value(unsigned int gpio, unsigned int value) { ? ?int fd, len; ? ?char buf[MAX_BUF]; ? ?len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/value", gpio); ? ?fd = open(buf, O_RDWR ); ? ?if (fd < 0) { ? ? ? ?printf("gpio/set-value "); ? ? ? ?return fd; ? ?} ? ?if (value) ? ? ? ?write(fd, "1", 2); ? ?else ? ? ? ?write(fd, "0", 2); ? ?close(fd); ? ?return 0; } /**************************************************************** * gpio_get_value ****************************************************************/ int gpio_get_value(unsigned int gpio, unsigned int *value) { ? ?int fd, len; ? ?char buf[MAX_BUF]; ? ?char ch; ? ?len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/value", gpio); ? ?fd = open(buf, O_RDWR ); ? ?if (fd < 0) { ? ? ? ?printf("gpio/get-value "); ? ? ? ?return fd; ? ?} ? ?read(fd, &ch, 1); ? ?if (ch != '0') { ? ? ? ?*value = 1; ? ?} else { ? ? ? ?*value = 0; ? ?} ? ?close(fd); ? ?return 0; } /**************************************************************** * gpio_set_edge ****************************************************************/ int gpio_set_edge(unsigned int gpio, char *edge) { ? ?int fd, len; ? ?char buf[MAX_BUF]; ? ?len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/edge", gpio); ? ?fd = open(buf, O_WRONLY); ? ?if (fd < 0) { ? ? ? ?printf("gpio/set-edge "); ? ? ? ?return fd; ? ?} ? ?write(fd, edge, strlen(edge) + 1); ? ?close(fd); ? ?return 0; } /**************************************************************** * gpio_fd_open ****************************************************************/ int gpio_fd_open(unsigned int gpio) { ? ?int fd, len; ? ?char buf[MAX_BUF]; ? ?len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/value", gpio); ? ?fd = open(buf, O_RDONLY | O_NONBLOCK ); ? ?if (fd < 0) { ? ? ? ?printf("gpio/fd_open "); ? ?} ? ?return fd; } /**************************************************************** * gpio_fd_close ****************************************************************/ int gpio_fd_close(int fd) { ? ?return close(fd); } void gpio_init() { ? ?gpio_export(GPIOH14); ? ?gpio_set_dir(GPIOH14, 0); ? ?//gpio_set_edge(GPIOH14, "rising"); } void gpio_uninit() { ? ?gpio_unexport(GPIOH14); } void mian(void) { ? ?gpio_init(); ? ?//將gpio238 設(shè)定為高電平輸出 ? ?gpio_set_value(GPIOH14, XGPIO_HIGH ); ? ?//將gpio238 設(shè)定為低電平輸出 ? ?gpio_set_value(GPIOH14, XGPIO_LOW); }
審核編輯:湯梓紅
-
觸摸屏
+關(guān)注
關(guān)注
42文章
2318瀏覽量
116787 -
soc
+關(guān)注
關(guān)注
38文章
4206瀏覽量
219126 -
Linux
+關(guān)注
關(guān)注
87文章
11347瀏覽量
210437 -
應(yīng)用程序
+關(guān)注
關(guān)注
38文章
3294瀏覽量
57928 -
GPIO
+關(guān)注
關(guān)注
16文章
1217瀏覽量
52403
原文標(biāo)題:簡易實(shí)現(xiàn)GPIO應(yīng)用程序在V85x上編寫
文章出處:【微信號:gh_79acfa3aa3e3,微信公眾號:全志在線】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
怎樣測試在am335x系統(tǒng)中編寫的應(yīng)用程序
【沁恒微CH32V307評估板試用體驗(yàn)】第一個(gè)應(yīng)用程序編寫-LED流水燈
全志V853芯片 如何在Tina V85x平臺切換sensor?
每日推薦 | Tina V85x 平臺E907啟動方式,OpenHarmony征文活動獲獎名單
全志V853芯片 如何在Tina V85x平臺切換sensor?
如何將引導(dǎo)加載程序0x00000000和應(yīng)用程序放置在0x00008000?
鼠標(biāo)應(yīng)用程序設(shè)計(jì)
如何編寫C應(yīng)用程序并在Cyclone V SoC DevKit中運(yùn)行ARM DS-5 AE
![如何<b class='flag-5'>編寫</b>C<b class='flag-5'>應(yīng)用程序</b>并在Cyclone <b class='flag-5'>V</b> SoC DevKit中運(yùn)行ARM DS-5 AE](https://file.elecfans.com/web1/M00/53/3B/o4YBAFsg4W2ARAFlAAAfFLbeY8o128.jpg)
如何利用C/C++編寫應(yīng)用程序加速內(nèi)核運(yùn)行
基于OpenHarmony編寫GPIO平臺驅(qū)動和應(yīng)用程序
![基于OpenHarmony<b class='flag-5'>編寫</b><b class='flag-5'>GPIO</b>平臺驅(qū)動和<b class='flag-5'>應(yīng)用程序</b>](https://file1.elecfans.com/web2/M00/A2/91/wKgZomT_x7OAd0SKAAAtzFUEmTo818.png)
評論