Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

一、概述

  通过 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 目录下看到如下文件:

export
gpiochip0/
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/bash

echo 48 > /sys/class/gpio/gpio48/export
echo out > /sys/class/gpio/gpio48/direction
echo 1 > /sys/class/gpio/gpio48/value
usleep 1000
echo 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")
// 使用完后别忘了free
gpio_free(54);
  • 20
    点赞
  • 162
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
全志R16调通USB接口的WIFI:RTL8188CUv1.0版本.txt 2017/4/5 16:56 开发板:SC3817R OS/SDK:Android4.4.2/ParrotV1.1 测试步骤/请眼中注意:打开WIFI之前必须将 USB接口的WIFI:RTL8188CU插入到USB1中去,否则会死机!!!!切记! 1、(如果SDK没有编译过,请跳过本次执行步骤!!!!) rootroot@cm-System-Product-Name:/home/wwt/rtl8188eu_parrotv1.1$ cd lichee/ rootroot@cm-System-Product-Name:/home/wwt/rtl8188eu_parrotv1.1/lichee$ ./build.sh config Welcome to mkscript setup progress All available chips: 0. sun8iw5p1 Choice: 0 All available platforms: 0. android 1. dragonboard 2. linux 3. tina Choice: 0 All available kernel: 0. linux-3.4 Choice: 0 All available boards: 0. bell-one 1. evb 2. evb-20 3. evb-30 4. evb-rtl8723bs 5. sc3813r Choice: 3 rootroot@cm-System-Product-Name:/home/wwt/rtl8188eu_parrotv1.1/lichee$ cd linux-3.4/ rootroot@cm-System-Product-Name:/home/wwt/rtl8188eu_parrotv1.1/lichee/linux-3.4$ make ARCH=arm menuconfig Device Drivers ---> [*] Network device support ---> [*] Wireless LAN ---> (其它的可以全部关闭的!!!!) <M> Realtek 8188E USB WiFi 2、 Z:\home\wwt\rtl8188eu_parrotv1.1\android\device\softwinner\astar-evb30\BoardConfig.mk 关闭这里: #BOARD_WIFI_VENDOR := broadcom 打开这里: # wifi and bt configuration # 1. Wifi Configuration # 1.1 realtek wifi support # 1.1 realtek wifi configuration BOARD_WIFI_VENDOR := realtek ifeq ($(BOARD_WIFI_VENDOR), realtek) WPA_SUPPLICANT_VERSION := VER_0_8_X BOARD_WPA_SUPPLICANT_DRIVER := NL80211 BOARD_WPA_SUPPLICANT_PRIVATE_LIB := lib_driver_cmd_rtl BOARD_HOSTAPD_DRIVER := NL80211 BOARD_HOSTAPD_PRIVATE_LIB := lib_driver_cmd_rtl SW_BOARD_USR_WIFI := rtl8188eu BOARD_WLAN_DEVICE := rtl8188eu #SW_BOARD_USR_WIFI := rtl8189es #BOARD_WLAN_DEVICE := rtl8189es #SW_BOARD_USR_WIFI := rtl8723bs #BOARD_WLAN_DEVICE := rtl8723bs endif 3、 Z:\home\wwt\rtl8188eu_parrotv1.1\android\device\softwinner\astar-evb30\init.sun8i.rc 关闭这里: #insmod /system/vendor/modules/bcm_btlpm.ko ## bluetooth # # UART device # chmod 0660 /dev/ttyS1 # chown bluetooth net_bt_stack /dev/ttyS1 # # # power up/down interface # chmod 0660 /sys/class/rfkill/rfkill0/state # chmod 0660 /sys/class/rfkill/rfkill0/type # chown bluetooth net_bt_stack /sys/class/rfkill/rfkill0/state # chown bluetooth net_bt_stack /sys/class/rfkill/rfkill0/type # write /sys/class/rfkill/rfkill0/state 0 # # # bluetooth LPM # chmod 0220 /proc/bluetooth/sleep/lpm # chmod 0220 /proc/bluetooth/sleep/btwrite # chown bluetooth net_bt_stack /proc/bluetooth/sleep/lpm # chown bluetooth net_bt_stack /proc/bluetooth/sleep/btwrite ## 2. broadcom wifi service ## 2.1 broadcom wifi station and softap #service wpa_supplicant /system/bin/wpa_supplicant \ # -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \ # -I/system/etc/wifi/wpa_supplicant_overlay.conf \ # -O/data/misc/wifi/sockets \ # -e/data/misc/wifi/entropy.bin -g@android:wpa_wlan0 # # we will start as root and wpa_supplicant will switch to user wifi # # after setting up the capabilities required for WEXT # # user wifi # # group wifi inet keystore # class main # socket wpa_wlan0 dgram 660 wifi wifi # disabled # oneshot # ## 2.2 broadcom wifi sta p2p concurrent service #service p2p_supplicant /system/bin/wpa_supplicant \ # -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \ # -I/system/etc/wifi/wpa_supplicant_overlay.conf \ # -O/data/misc/wifi/sockets -N \ # -ip2p0 -Dnl80211 -c/data/misc/wifi/p2p_supplicant.conf \ # -I/system/etc/wifi/p2p_supplicant_overlay.conf \ # -puse_p2p_group_interface=1 -e/data/misc/wifi/entropy.bin \ # -g@android:wpa_wlan0 # # we will start as root and wpa_supplicant will switch to user wifi # # after setting up the capabilities required for WEXT # # user wifi # # group wifi inet keystore # class main # socket wpa_wlan0 dgram 660 wifi wifi # disabled # oneshot 打开这里: # 1. realtek & eagle wifi service # 1.1 realtek & eagle wifi sta service service wpa_supplicant /system/bin/logwrapper /system/bin/wpa_supplicant \ -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \ -O/data/misc/wifi/sockets \ -e/data/misc/wifi/entropy.bin -g@android:wpa_wlan0 # we will start as root and wpa_supplicant will switch to user wifi # after setting up the capabilities required for WEXT # user wifi # group wifi inet keystore class main socket wpa_wlan0 dgram 660 wifi wifi disabled oneshot # 1.2 realtek & eagle wifi sta p2p concurrent service service p2p_supplicant /system/bin/logwrapper /system/bin/wpa_supplicant \ -ip2p0 -Dnl80211 -c/data/misc/wifi/p2p_supplicant.conf \ -e/data/misc/wifi/entropy.bin -N \ -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \ -O/data/misc/wifi/sockets \ -g@android:wpa_wlan0 class main socket wpa_wlan0 dgram 660 wifi wifi disabled oneshot #rtl8189es cob set macaddr #service engsetmacaddr /system/bin/setmacaddr /data/wifimac.txt # class main # oneshot 4、 Z:\home\wwt\rtl8188eu_parrotv1.1\android\device\softwinner\astar-evb30\astar_evb30.mk 关闭这里: # wifi features PRODUCT_COPY_FILES += \ frameworks/native/data/etc/android.hardware.wifi.xml:system/etc/permissions/android.hardware.wifi.xml \ frameworks/native/data/etc/android.hardware.wifi.direct.xml:system/etc/permissions/android.hardware.wifi.direct.xml #frameworks/native/data/etc/android.hardware.bluetooth.xml:system/etc/permissions/android.hardware.bluetooth.xml \ #frameworks/native/data/etc/android.hardware.bluetooth_le.xml:system/etc/permissions/android.hardware.bluetooth_le.xml # ap6181/6210/6330 sdio wifi fw and nvram #$(call inherit-product-if-exists, hardware/broadcom/wlan/firmware/ap6181/device-bcm.mk) #$(call inherit-product-if-exists, hardware/broadcom/wlan/firmware/ap6212/device-bcm.mk) #$(call inherit-product-if-exists, hardware/broadcom/wlan/firmware/ap6330/device-bcm.mk) #PRODUCT_PACKAGES += bt_vendor.conf \ # libbt-client-api \ # com.broadcom.bt \ # com.broadcom.bt.xml \ # com.dsi.ant.antradio_library \ # com.dsi.ant.antradio_library.xml \ # AntHalService \ # ANTRadioService PRODUCT_PACKAGES += com.dsi.ant.antradio_library \ com.dsi.ant.antradio_library.xml \ AntHalService \ ANTRadioService #PRODUCT_PACKAGES += Bluetooth 5、 Z:\home\wwt\rtl8188eu_parrotv1.1\android\device\softwinner\astar-evb30\overlay\frameworks\base\core\res\res\values\config.xml 关闭这里: <!-- List of regexpressions describing the interface (if any) that represent tetherable bluetooth interfaces. If the device doesn't want to support tethering over bluetooth this should be empty. --> <!-- default: disable Bluetooth PAN feature --> <string-array translatable="false" name="config_tether_bluetooth_regexs"> <item>"bt-pan"</item> </string-array> Z:\home\wwt\rtl8188eu_parrotv1.1\lichee\tools\pack\chips\sun8iw5p1\configs\evb-30\sys_config.fex 关闭这里: [wakeup_src_para] cpu_en = 0 cpu_freq = 48 ; (cpu:apb:ahb) pll_ratio = 0x111 dram_selfresh_en= 1 dram_freq = 36 wakeup_src_wl = port:PL07<4><default><default><0> ;wakeup_src_bt = port:PL09<4><default><default><0> bb_wake_ap = port:PL02<4><default><default><0> [usbc1] usb_used = 1 usb_drv_vbus_gpio = port:PD12<1><0><default><0> usb_restrict_gpio = usb_host_init_state = 0 usb_restric_flag = 0 usb_regulator_io = "nocare" usb_regulator_vol = 0 usb_not_suspend = 0 [rf_para] module_num = 3 module_power1 = "axp22_dldo1" module_power1_vol = 3300000 module_power2 = "axp22_dldo2" module_power2_vol = 3300000 module_power3 = "axp22_aldo1" module_power3_vol = 3300000 power_switch = chip_en = lpo_use_apclk = "losc_out" [bt_para] bt_used = 0 ;bt_uart_id = 1 ;bt_rst_n = port:PL08<1><default><default><0> ;bt_wake = port:PL10<1><default><default><0> ;bt_host_wake = port:PL09<4><default><default><0> ;bt_host_wake_invert = 0

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿基米东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值