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:

high Set GPIO to an output with a starting value of 1
low Set GPIO to an output with a starting value of 0
out Same as low
in Set 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状态的更多相关文章

  1. Magento后台手动修改订单状态方法及手动修改方法php

    订单详细内容页手动修改订单状态方法: 打开此文件:app\design\adminhtml\default\default\template\sales\order\view\history.phtm ...

  2. Linux echo, sort, sed 等一些命令总结

    linux echo, sort, sed是初学linux shell script 的一些常用的命令.基本上来说,如果能够掌握了这些命令,我们就能写出一些不错的linux脚本.以下是我遇到的以下常用 ...

  3. 【修改端口号】linux下修改apache,nginx服务端口号

    一.linux下修改apache端口号 yum安装后,apache配置文件: /etc/httpd/conf/httpd.conf 找到apache目录下的 httpd.conf, 使用vi 打开,找 ...

  4. Linux 下操作GPIO(两种方法,驱动和mmap)(转载)

    目前我所知道的在Linux下操作GPIO有两种方法: 1.编写驱动,这当然要熟悉Linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据io ...

  5. Linux下修改PATH的方法

    Linux下修改PATH的方法 1.直接在命令行里敲 PATH=$PATH:/path1:/path2:/pathN用户登出之后PATH恢复原样. 只是临时起作用. 2.修改~目录下bash_prof ...

  6. Linux 下操作gpio(两种方法,驱动和mmap)

    目前我所知道的在linux下操作GPIO有两种方法: 1.  编写驱动,这当然要熟悉linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据 ...

  7. linux下修改hostid

    linux下修改hostid 网上有很多版本,总结了这几点. 1> 一个以16进制显示的int字符串: 2> 配置文件: /etc/hostid; 如果有值,输出, 结束. 3> 从 ...

  8. Linux创建修改删除用户和组

    Linux 创建修改删除用户和组 介绍 在日常的维护过程中创建用户操作用的相对会多一些,但是在这个过程中涉及到的知识点就不单单就是useradd了,接下来就来详细了解账号管理的相关信息. 用户信息 先 ...

  9. 树莓派安装FLASK服务;并在端网页读取 GPIO状态和系统时间

    做过一些物联网的作品:因为不想一直做APP来控制,因为不能每个人都去下载你自己做的APP,浏览器大家都是有的:那么每个人通过浏览器WEB来访问我们服务器,岂不是很简单和方便,采用flask+pytho ...

随机推荐

  1. 从零开始学Electron笔记(四)

    在之前的文章我们介绍了一下Electron的这个remote模块,接下来我们继续说一下Electron的右键菜单的制作. 在我们日常我们使用的软件中都会存在右键菜单的情况,比如我们用到的浏览器,开发所 ...

  2. shell专题(一):Shell概述

    大数据程序员为什么要学习Shell呢? 1)需要看懂运维人员编写的Shell程序. 2)偶尔会编写一些简单Shell程序来管理集群.提高开发效

  3. redis(二十四):Redis分布式锁以及实现(python)

    阅读目录 什么事分布式锁 基于redis实现分布式锁 一.什么是分布式锁 我们在开发应用的时候,如果需要对某一个共享变量进行多线程同步访问的时候,可以使用我们学到的锁进行处理,并且可以完美的运行,毫无 ...

  4. linux专题(七):账号管理

    http://dwz.date/UDf 简介 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账 ...

  5. Python并发编程03 /僵孤进程,孤儿进程、进程互斥锁,进程队列、进程之间的通信

    Python并发编程03 /僵孤进程,孤儿进程.进程互斥锁,进程队列.进程之间的通信 目录 Python并发编程03 /僵孤进程,孤儿进程.进程互斥锁,进程队列.进程之间的通信 1. 僵尸进程/孤儿进 ...

  6. NVIDIA GPU Volta架构简述

    NVIDIA GPU Volta架构简述 本文摘抄自英伟达Volta架构官方白皮书:https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Ce ...

  7. .Net微服务实战之负载均衡(上)

    系列文章 .Net微服务实战之技术选型篇 .Net微服务实战之技术架构分层篇 .Net微服务实战之DevOps篇 相关源码:https://github.com/SkyChenSky/Sikiro P ...

  8. [jvm] -- 判断对象是否死亡篇

    判断对象是否死亡的两种方法 引用计数法 给对象中添加一个引用计数器,每当有一个地方引用它,计数器就加 1:当引用失效,计数器就减 1:任何时候计数器为 0 的对象就是不可能再被使用的. 优点: 简单 ...

  9. Jmeter(十八) - 从入门到精通 - JMeter后置处理器 -下篇(详解教程)

    1.简介 后置处理器是在发出“取样器请求”之后执行一些操作.取样器用来模拟用户请求,有时候服务器的响应数据在后续请求中需要用到,我们的势必要对这些响应数据进行处理,后置处理器就是来完成这项工作的.例如 ...

  10. 坚果云如何使用二次验证码/谷歌身份验证器/两步验证/虚拟MFA?

    一般步骤:登陆后点邮箱名——安全设置——开通两步验证,用二次验证码微信小程序绑定即可 具体步骤见链接  坚果云如何使用二次验证码/谷歌身份验证器/两步验证/虚拟MFA? 二次验证码小程序于谷歌身份验证 ...