0 前言
本文描述如果通過文件IO sysfs方式控制EasyARM GPIO端口。通過sysfs方式控制GPIO,先訪問/sys/class/gpio目錄,向export文件寫入GPIO編號,使得該GPIO的操作接口從內核空間暴露到用戶空間,GPIO的操作接口包括direction和value等,direction控制GPIO方向,而value可控制GPIO輸出或獲得GPIO輸入。
用戶空間訪問gpio,即通過sysfs接口訪問gpio,下面是/sys/class/gpio目錄下的三種文件:
--export/unexport文件
--gpioN指代具體的gpio引腳
--gpio_chipN指代gpio控制器
(1) export/unexport文件接口:
/sys/class/gpio/export,該接口只能寫不能讀
用戶程序通過寫入gpio的編號來向內核申請將某個gpio的控制權導出到用戶空間
比如 echo 19 > export
上述操作會爲19號gpio創建一個節點gpio19,此時/sys/class/gpio目錄下邊生成一個gpio19的目錄
/sys/class/gpio/unexport和導出的效果相反。
比如 echo 19 > unexport
上述操作將會移除gpio19這個節點。
(2) /sys/class/gpio/gpioN
指代某個具體的gpio端口,裏邊有如下屬性文件
direction 表示gpio端口的方向,讀取結果是in或out。該文件也可以寫,寫入out 時該gpio設爲輸出同時電平默認爲低。寫入low或high則不僅可以設置爲輸出 還可以設置輸出的電平。 當然如果內核不支持或者內核代碼不願意,將不會存在這個屬性,比如內核調用了gpio_export(N,0)就表示內核不願意修改gpio端口方向屬性
value 表示gpio引腳的電平,0(低電平)1(高電平),如果gpio被配置爲輸出,這個值是可寫的,記住任何非零的值都將輸出高電平, 如果某個引腳能並且已經被配置爲中斷,則可以調用poll(2)函數監聽該中斷,中斷觸發後poll(2)函數就會返回。
edge 表示中斷的觸發方式,edge文件有如下四個值:"none", "rising", "falling","both"。
none 表示引腳爲輸入,不是中斷引腳
rising 表示引腳爲中斷輸入,上升沿觸發
falling 表示引腳爲中斷輸入,下降沿觸發
both 表示引腳爲中斷輸入,邊沿觸發
這個文件節點只有在引腳被配置爲輸入引腳的時候才存在。 當值是none時可以通過如下方法將變爲中斷引腳
echo "both" > edge;對於是both,falling還是rising依賴具體硬件的中斷的觸發方式。此方法即用戶態gpio轉換爲中斷引腳的方式
active_low 不怎麼明白,也木有用過
(3)/sys/class/gpio/gpiochipN
gpiochipN 表示的就是一個gpio_chip,用來管理和控制一組gpio端口的控制器,該目錄下存在一下屬性文件:
base 和N相同,表示控制器管理的最小的端口編號。
lable 診斷使用的標誌(並不總是唯一的)
ngpio 表示控制器管理的gpio端口數量(端口範圍是:N ~ N+ngpio-1)
引腳編號 = 控制引腳的寄存器基數 + 控制引腳寄存器位數
1 暴露GPIO操作接口
-
static int gpio_export(int pin)
-
{
-
char buffer[BUFFER_MAX];
-
int len;
-
int fd;
-
-
fd = open("/sys/class/gpio/export", O_WRONLY);
-
if (fd < 0) {
-
fprintf(stderr, "Failed to open export for writing!\n");
-
return(-1);
-
}
-
-
len = snprintf(buffer, BUFFER_MAX, "%d", pin);
-
if (write(fd, buffer, len) < 0) {
-
fprintf(stderr, "Fail to export gpio!");
-
return -1;
-
}
-
-
close(fd);
-
return 0;
-
}
2 隱藏GPIO操作接口
-
static int gpio_unexport(int pin)
-
{
-
char buffer[BUFFER_MAX];
-
int len;
-
int fd;
-
-
fd = open("/sys/class/gpio/unexport", O_WRONLY);
-
if (fd < 0) {
-
fprintf(stderr, "Failed to open unexport for writing!\n");
-
return -1;
-
}
-
-
len = snprintf(buffer, BUFFER_MAX, "%d", pin);
-
if (write(fd, buffer, len) < 0) {
-
fprintf(stderr, "Fail to unexport gpio!");
-
return -1;
-
}
-
-
close(fd);
-
return 0;
-
}
3 配置GPIO方向
-
static int gpio_direction(int pin, int dir)
-
{
-
static const char dir_str[] = "in\0out";
-
char path[DIRECTION_MAX];
-
int fd;
-
-
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
-
fd = open(path, O_WRONLY);
-
if (fd < 0) {
-
fprintf(stderr, "failed to open gpio direction for writing!\n");
-
return -1;
-
}
-
-
if (write(fd, &dir_str[dir == IN ? 0 : 3], dir == IN ? 2 : 3) < 0) {
-
fprintf(stderr, "failed to set direction!\n");
-
return -1;
-
}
-
-
close(fd);
-
return 0;
-
}
【簡單說明】
【1】dir_str[dir == IN ? 0 : 3], dir == IN ? 2 : 3 如果輸入爲常數宏IN, 取dir_str[0]=“in”;若輸入常數宏爲OUT,取dir_str[0]=“out”。此處巧妙的使用了在數組中的“\0”。
4 控制GPIO輸出
-
static int gpio_write(int pin, int value)
-
{
-
static const char values_str[] = "01";
-
char path[DIRECTION_MAX];
-
int fd;
-
-
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/value", pin);
-
fd = open(path, O_WRONLY);
-
if (fd < 0) {
-
fprintf(stderr, "failed to open gpio value for writing!\n");
-
return -1;
-
}
-
-
if (write(fd, &values_str[value == LOW ? 0 : 1], 1) < 0) {
-
fprintf(stderr, "failed to write value!\n");
-
return -1;
-
}
-
-
close(fd);
-
return 0;
-
}
5 獲得GPIO輸入
-
static int gpio_read(int pin)
-
{
-
char path[DIRECTION_MAX];
-
char value_str[3];
-
int fd;
-
-
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/value", pin);
-
fd = open(path, O_RDONLY);
-
if (fd < 0) {
-
fprintf(stderr, "failed to open gpio value for reading!\n");
-
return -1;
-
}
-
-
if (read(fd, value_str, 3) < 0) {
-
fprintf(stderr, "failed to read value!\n");
-
return -1;
-
}
-
-
close(fd);
-
return (atoi(value_str));
-
}
6 GPIO翻轉操作
【main函數】
-
int main(int argc, char *argv[])
-
{
-
int i = 0;
-
gpio_export(P24);
-
-
gpio_direction(P24, OUT);
-
for (i = 0; i < 10; i++) {
-
printf("LED Blink\n");
-
gpio_write(P24, i % 2);
-
usleep(500 * 1000);
-
}
-
gpio_write(P24, 0);
-
-
gpio_unexport(P24);
-
return 0;
-
}
7 其他例子
-
int set_io_value_high(int gpio)
-
{
-
FILE *fp;
-
char buffer[10];
-
int value;
-
char s[50]="";
-
char s1[50]="";
-
if ((fp = fopen("/sys/class/gpio/export", "w")) == NULL)
-
{
-
printf("Cannot open export file.\n");
-
return -1;
-
}
-
fprintf(fp, "%d", gpio);
-
fclose(fp);
-
-
sprintf(s,"/sys/class/gpio/gpio%d/direction",gpio);
-
if ((fp = fopen(s, "rb+")) == NULL)
-
{
-
printf("Cannot open %s.\n",s);
-
return -1;
-
}
-
fprintf(fp, "out");
-
fclose(fp);
-
-
sprintf(s1,"/sys/class/gpio/gpio%d/value",gpio);
-
-
if ((fp = fopen(s1, "rb+")) == NULL)
-
{
-
printf("Cannot open %s.\n",s1);
-
return -1;
-
}
-
strcpy(buffer,"1");
-
fwrite(buffer, sizeof(char), sizeof(buffer) - 1, fp);
-
fclose(fp);
-
return 1;
-
}
-
int set_io_value_low(int gpio)
-
{
-
FILE *fp;
-
char buffer[10];
-
int value;
-
char s[50]="";
-
char s1[50]="";
-
if ((fp = fopen("/sys/class/gpio/export", "w")) == NULL)
-
{
-
printf("Cannot open export file.\n");
-
return -1;
-
}
-
fprintf(fp, "%d", gpio);
-
fclose(fp);
-
-
sprintf(s,"/sys/class/gpio/gpio%d/direction",gpio);
-
if ((fp = fopen(s, "rb+")) == NULL)
-
{
-
printf("Cannot open %s.\n",s);
-
return -1;
-
}
-
fprintf(fp, "out");
-
fclose(fp);
-
-
sprintf(s1,"/sys/class/gpio/gpio%d/value",gpio);
-
-
if ((fp = fopen(s1, "rb+")) == NULL)
-
{
-
printf("Cannot open %s.\n",s1);
-
return -1;
-
}
-
strcpy(buffer,"0");
-
fwrite(buffer, sizeof(char), sizeof(buffer) - 1, fp);
-
fclose(fp);
-
return 1;
-
-
}