Linux下用檔案IO的方式操作GPIO(/sys/class/gpio)__Linux

來源:互聯網
上載者:User

一、概述

  通過 sysfs 方式控制 GPIO,先訪問 /sys/class/gpio 目錄,向 export 檔案寫入 GPIO 編號,使得該 GPIO 的操作介面從核心空間暴露到使用者空間,GPIO 的操作介面包括 direction 和 value 等,direction 控制 GPIO 方向,而 value 可控制 GPIO 輸出或獲得 GPIO 輸入。檔案 IO 方式操作 GPIO,使用到了4個函數 open、close、read、write。
  
  首先,看看系統中有沒有“/sys/class/gpio”這個檔案夾。如果沒有請在編譯核心的時候加入:

Device Drivers ->         GPIO Support ->                /sys/class/gpio/… (sysfs interface)。

二、/sys/class/gpio 的使用說明

  如果是在已經適配好的 Linux 核心上,那麼相信已經有了完成的 gpiochip,可以在使用者空間 /sys/class/gpio 目錄下看到如下檔案:

exportgpiochip0/gpiochip32/gpiochip64/gpiochip96/unexport

說明:

1、gpio_operation 通過 /sys/ 檔案介面操作 IO 連接埠 GPIO 到檔案系統的映射。
2、控制 GPIO 的目錄位於 /sys/class/gpio。
3、/sys/class/gpio/export 檔案用於通知系統需要匯出控制的 GPIO 引腳編號。
4、/sys/class/gpio/unexport 用於通知系統取消匯出。
5、/sys/class/gpio/gpiochipX 目錄儲存系統中 GPIO 寄存器的資訊,包括每個寄存器控制引腳的起始編號 base,寄存器名稱,引腳總數。

三、匯出一個引腳的操作步驟

1、首先計算此引腳編號。

引腳編號 = 控制引腳的寄存器基數 + 控制引腳寄存器位元

  舉個栗子(具體 GPIO 需要參考資料手冊),如果使想用 GPIO1_20,那麼引腳編號就可能等於 1 x 32 + 20 = 54。

2、向 /sys/class/gpio/export 寫入此編號,比如12號引腳,在 shell 中可以通過以下命令實現:

echo 12 > /sys/class/gpio/export

  命令成功後產生 /sys/class/gpio/gpio12 目錄,如果沒有出現相應的目錄,說明此引腳不可匯出。
  
3、direction 檔案,定義輸入輸入方向,可以通過下面命令定義為輸出。

echo out > /sys/class/gpio/gpio12/direction

  direction 接受的參數可以是:in、out、high、low。其中參數 high / low 在設定方向為輸出的同時,將 value 設定為相應的 1 / 0。
  
4、value 檔案是連接埠的數值,為1或0,通過下面命令將 gpio12 設定為高電平。

echo 1 > /sys/class/gpio/gpio12/value

四、重溫幾個簡單的例子

1、匯出

# echo 44 > /sys/class/gpio/export

2、設定方向

# echo out > /sys/class/gpio/gpio44/direction

3、查看方向

# cat /sys/class/gpio/gpio44/direction

4、設定輸出

# echo 1 > /sys/class/gpio/gpio44/value

5、查看輸出值

# cat /sys/class/gpio/gpio44/value

6、取消匯出

# echo 44 > /sys/class/gpio/unexport

五、檔案讀寫常式

1、在使用者空間使用

#include stdlib.h  #include stdio.h  #include string.h#include unistd.h#include fcntl.h   //define O_WRONLY and O_RDONLY  //晶片複位引腳: P1_16#define SYSFS_GPIO_EXPORT           "/sys/class/gpio/export"  #define SYSFS_GPIO_RST_PIN_VAL      "48"   #define SYSFS_GPIO_RST_DIR          "/sys/class/gpio/gpio48/direction"#define SYSFS_GPIO_RST_DIR_VAL      "OUT"  #define SYSFS_GPIO_RST_VAL          "/sys/class/gpio/gpio48/value"#define SYSFS_GPIO_RST_VAL_H        "1"#define SYSFS_GPIO_RST_VAL_L        "0"int main() {     int fd;          //開啟連接埠/sys/class/gpio# echo 48 > export         fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);         if(fd == -1)         {                   printf("ERR: Radio hard reset pin open error.\n");                   return EXIT_FAILURE;         }         write(fd, SYSFS_GPIO_RST_PIN_VAL ,sizeof(SYSFS_GPIO_RST_PIN_VAL));          close(fd);          //設定連接埠方向/sys/class/gpio/gpio48# echo out > direction         fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);         if(fd == -1)         {                   printf("ERR: Radio hard reset pin direction open error.\n");                   return EXIT_FAILURE;         }         write(fd, SYSFS_GPIO_RST_DIR_VAL, sizeof(SYSFS_GPIO_RST_DIR_VAL));          close(fd);          //輸出複位訊號: 拉高>100ns         fd = open(SYSFS_GPIO_RST_VAL, O_RDWR);         if(fd == -1)         {                   printf("ERR: Radio hard reset pin value open error.\n");                   return EXIT_FAILURE;         }                while(1)         {                   write(fd, SYSFS_GPIO_RST_VAL_H, sizeof(SYSFS_GPIO_RST_VAL_H));                   usleep(1000000);                   write(fd, SYSFS_GPIO_RST_VAL_L, sizeof(SYSFS_GPIO_RST_VAL_L));                   usleep(1000000);         }         close(fd);         printf("INFO: Radio hard reset pin value open error.\n");         return 0;}  

  除了上述常式的操作方法外,我們還可以有更簡單地做法,就是編寫 Shell 指令碼。例如:

#!/bin/bashecho 48 > /sys/class/gpio/gpio48/exportecho out > /sys/class/gpio/gpio48/directionecho 1 > /sys/class/gpio/gpio48/valueusleep 1000echo 0 > /sys/class/gpio/gpio48/value

2、在核心空間使用

#include <linux/gpio.h>// 這裡是配置成輸出,預設高電平,名稱為“gpio1_20”(就是給你的IO口取個名字)gpio_request_one(54, GPIOF_INIT_HIGH, "gpio1_20")// 這個就是配置成輸入。gpio_request_one(54, GPIOF_IN, "gpio1_20")// 使用完後別忘了freegpio_free(54);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.