LINUX --- echo修改GPIO状态

摘要:
[*]GPIO支持---&gt
GPIO sysfs Interface

The GPIO sysfs interface allows users to manipulate any GPIO from userspace (also known as programmable flags). Since it uses gpiolib, it is able to dynamically utilize all GPIOs on the system, even ones on expander cards (like the ADP5520).

Userspace utilizes a sysfs control interface to dynamically request and release individual GPIOs. Once a GPIO has been requested, writing to the newly created path allows you to control the direction and the data while reading from it returns the GPIO data (which usually corresponds to a 0 or 1 which represents the signal level).

GPIOs are referred to by their numeric value. You can refer to the GPIO pin table to quickly figure out what number is used for a specific pin on a specific port in the Blackfin processor.

If you need non-blocking reads, support for poll() and select() system calls, or similar features, please refer to the gpio-keys input device driver

With recent kernels (2.6.33+) it is now possible to poll() on a GPIO simply set “rising”, “falling” or “both” to /sys/class/gpio/gpioX/edge then poll for POLLPRI on the relevant open+read fd.

Configuration

Since the pins are tied to a GPIO controller, there are no platform resources you need to declare. Simply enable the driver in your kernel configuration menu:

Device Drivers  --->
  [*] GPIO Support  --->
    [*]   /sys/class/gpio/... (sysfs interface)

Userspace Interface

All the GPIO interfaces are based in /sys/class/gpio/.

Request/Release

You first have to request a GPIO. So if we wanted to request GPIO 23, we would do:

root:/> echo 23 > /sys/class/gpio/export

If this process was successful, you would end up with a /sys/class/gpio/gpio23/ directory.

Then when we were done with it, we would release it by doing:

root:/> echo 23 > /sys/class/gpio/unexport

Reading/Writing

In the specific GPIO directory, there will be two files: direction and value. Reading from them returns the current state (direction / value) as you might expect. Writing to them sets the current state.

Possible commands for direction:

highSet GPIO to an output with a starting value of 1
lowSet GPIO to an output with a starting value of 0
outSame as low
inSet GPIO to an input

The value field simply uses numeric values, so 0 or 1.

Examples

To set GPIO 23 to an input:

root:/> echo in > /sys/class/gpio/gpio23/direction

To set GPIO 23 to a high output:

root:/> echo high > /sys/class/gpio/gpio23/direction

To set GPIO 23's value to 0:

root:/> echo 0 > /sys/class/gpio/gpio23/value

To read GPIO 23's current value:

root:/> cat /sys/class/gpio/gpio23/value
0

External Resources

The GPIO framework and GPIO sysfs interface are both documented in this file:

  • linux-2.6.x/Documentation/gpio.txt

GPIO Sysfs Helpers

Some simple example:

file: trunk/user/blkfin-test/ppifcd-test/gpio.h

/*
 * GPIO user space helpers
 *
 * Copyright 2009 Analog Devices Inc.
 * Michael Hennerich (hennerich@blackfin.uclinux.org)
 *
 * Licensed under the GPL-2 or later
 */
 
#ifndef GPIO_H
#define GPIO_H
int gpio_export(unsigned gpio);
int gpio_unexport(unsigned gpio);
int gpio_dir_out(unsigned gpio);
int gpio_dir_in(unsigned gpio);
int gpio_value(unsigned gpio, unsigned value);
#endif

file: trunk/user/blkfin-test/ppifcd-test/gpio.c

/*
 * GPIO user space helpers
 *
 * Copyright 2009 Analog Devices Inc.
 * Michael Hennerich (hennerich@blackfin.uclinux.org)
 *
 * Licensed under the GPL-2 or later
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
 
#define GPIO_DIR_IN	0
#define GPIO_DIR_OUT	1
 
int gpio_export(unsigned gpio)
{
	int fd, len;
	char buf[11];
 
	fd = open("/sys/class/gpio/export", O_WRONLY);
	if (fd < 0) {
		perror("gpio/export");
		return fd;
	}
 
	len = snprintf(buf, sizeof(buf), "%d", gpio);
	write(fd, buf, len);
	close(fd);
 
	return 0;
}
 
int gpio_unexport(unsigned gpio)
{
	int fd, len;
	char buf[11];
 
	fd = open("/sys/class/gpio/unexport", O_WRONLY);
	if (fd < 0) {
		perror("gpio/export");
		return fd;
	}
 
	len = snprintf(buf, sizeof(buf), "%d", gpio);
	write(fd, buf, len);
	close(fd);
	return 0;
}
 
int gpio_dir(unsigned gpio, unsigned dir)
{
	int fd, len;
	char buf[60];
 
	len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
 
	fd = open(buf, O_WRONLY);
	if (fd < 0) {
		perror("gpio/direction");
		return fd;
	}
 
	if (dir == GPIO_DIR_OUT)
		write(fd, "out", 4);
	else
		write(fd, "in", 3);
 
	close(fd);
	return 0;
}
 
int gpio_dir_out(unsigned gpio)
{
	return gpio_dir(gpio, GPIO_DIR_OUT);
}
 
int gpio_dir_in(unsigned gpio)
{
	return gpio_dir(gpio, GPIO_DIR_IN);
}
 
int gpio_value(unsigned gpio, unsigned value)
{
	int fd, len;
	char buf[60];
 
	len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
 
	fd = open(buf, O_WRONLY);
	if (fd < 0) {
		perror("gpio/value");
		return fd;
	}
 
	if (value)
		write(fd, "1", 2);
	else
		write(fd, "0", 2);
 
	close(fd);
	return 0;
}
 
#if 0
int main(int argc, char *argv[])
{
	int i = 20;
 
	gpio_export(6);
	gpio_dir_out(6);
 
	while(i--) {
		gpio_value(6, i & 1);
		sleep(1);
	}
 
	gpio_unexport(6);
}
#endif

免责声明:文章转载自《LINUX --- echo修改GPIO状态》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇element-ui的table,切换左侧树数据替换后高度变小问题python之路 之一pyspark下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Ubuntu 16.04 LTS 安装 Nginx/PHP 5.6/MySQL 5.7 (LNMP) 与Laravel

Ubuntu 16.04 LTS 安装 Nginx/PHP 5.6/MySQL 5.7 (LNMP) 与Laravel 1、MySQL安装【安装 MariaDB】MariaDB是MySQL的一个分支首先,更新升级系统$ sudo apt update$ sudo apt upgrade安装MariaDB:$ sudo apt install mariadb...

树莓派上传数据错误一例

首先是源码: 1 #-*- utf-8 -*- 2 #env !/usr/bin/python 3 4 importRPi.GPIO as GPIO 5 importtime 6 importjson 7 importdatetime 8 importrequests 9 10 requests.adapters.DEFAULT_RETRIES = 5...

LAUNCHXL-28379D入门学习-第一篇

1. 首先安装controlSUITE或者C2000ware软件,TI官网下载,安装后包括C2000的函数库和例程之类的,还可以和CCS搭配使用。controlSUITE安装完之后大约4个G,所以我安装C2000ware。 2. 打开CCS,要使用CCS6以上的版本才行,打开搜索资源,可以扫到安装的这2个软件库。 3. 如果找不到芯片型号的话,可以 he...

树莓派wiringPi经常使用的函数介绍

 1.void pinMode (int pin, int mode) ; 这个函数式设置pin脚的输入和输出模式以及PWM的输入和输出模式。在wiringPi中仅仅有 pin 1 (BCM_GPIO 18)是支持PWM的输出的。 2.void digitalWrite (int pin, int value) ; 这个函数式用来设置pin脚的高低电...

C#自定义工业控件开发

由于工作需要,调研过一段时间的工业控制方面的“组态软件”(SCADA)的开发,组态软件常用于自动化工业控制领域,其中包括实时数据采集、数据储存、设备控制和数据展现等功能。其中工控组件的界面展现的实现类似于Windows系统下的各种开发控件,通过各种控件的组装,和硬件协议的集成,就可以实现对相应设备的控制和实时状态的显示。 每个对应的硬件UI展示都可以用一个...

sudoers的深入剖析

一、sudo权限的配置 1. 编辑 sudo权限 命令 visudo visudo 命令实际修改的是 /etc/sudoers 文件 2. /etc/sudoers 配置文件 [root/etc]# cat /etc/sudoers ... 省略部分内容 ... ## Allow root to run any commands anywhere #...