Linux中如何使用GPIO信号

在sysfs中配置内核以支持GPIO

在menuconfig中,

         -> GPIO Support (GPIOLIB [=y])

从用户空间启用GPIO访问

GPIO=22

cd /sys/class/gpio
ls
echo $GPIO > export
ls

设置GPIO的方向并查看当前值

echo "in" > direction
cat value


GPIO测试程序

测试程序  gpio-int-test.c 使用轮询()来唤醒每3秒(使用轮询()超时机制),此时将其打印的期间。poll()函数还在监视来自stdin的输入和来自GPIO 0的中断。

查看GPIO配置

启动debugfs

Symbol: DEBUG_FS [=y]
   Prompt: Debug Filesystem
     Defined at lib/Kconfig.debug:77
     Depends on: SYSFS     
     Location:
       -> Kernel configuration
         -> Kernel hacking  

挂载debugfs

mount -t debugfs none / sys / kernel / debug

查看GPIO配置

cat / sys / kernel / debug / gpio
cat /sys/kernel/debug/omap_mux/board      # for OMAP
cat /sys/kernel/debug/dm365_mux           # for DM36x

存在示例shell来操作GPIO

#!bin/sh

show_usage()
{
    printf "\ngpio.sh <gpio pin number> [in|out [<value>]]\n"
}

if [ \( $# -eq 0 \) -o \( $# -gt 3 \) ] ; then
    show_usage
    printf "\n\nERROR: incorrect number of parameters\n"
    exit 255
fi

#doesn't hurt to export a gpio more than once
echo $1 > /sys/class/gpio/export

if [  $# -eq 1 ] ; then
   cat /sys/class/gpio/gpio$1/value
   exit 0
fi

if [ \( "$2" != "in" \) -a  \( "$2" != "out" \) ] ; then
    show_usage
    printf "\n\nERROR: second parameter must be 'in' or 'out'\n"
    exit 255
fi

echo $2 > /sys/class/gpio/gpio$1/direction

if [  $# -eq 2 ] ; then
   cat /sys/class/gpio/gpio$1/value
   exit 0
fi


VAL=$3

if [ $VAL -ne 0 ] ; then
    VAL=1
fi

echo $VAL > /sys/class/gpio/gpio$1/value

GPIO在系统文件系统 sysfs中处于/ sys / class / gpio

为避免两个设备驱动相同信号的硬件问题,GPIO默认配置为输入。如果要将GPIO用作输出,则需要更改配置

猜你喜欢

转载自blog.csdn.net/qq_33600019/article/details/82841806
今日推荐