Abstract: This article introduces the read and write of GPIO, introduces the basic principles, and the performance of different read and write methods.

This article is shared from the HUAWEI cloud community " Linux sysfs-based GPIO read and write operations ", author: a small tree x.

Preface

I recently came into contact with the development of GPIO in the Linux system. Here is a brief summary and share it; this article will introduce the read and write of GPIO, introduce the basic principles, and the performance of different read and write methods.

1. The basic principle of GPIO sysfs interface

In Linux, the most common way to read and write GPIO is to use the GPIO sysfs interface, which is by operating export, unexport, gpio{N}/direction, gpio{N}/value (using actual quotes) in the /sys/class/gpio directory. The pin number is implemented by replacing {N}) and other files, and it often appears in shell scripts.

First declare the GPIO port, such as GPIO258: (This command requires root privileges)

echo 258 > /sys/class/gpio/export
Then in /sys/class/gpio, a new directory will be generated, its name is gpio258

For example: GPIO258, N corresponds to 258; enter its directory: cd /sys/class/gpio/gpio258

You can see in turn: active_low, device, direction, edge, power, subsystem, uevent, value

One of the more commonly used is the value file, which stores the value of GPIO; the range is: 0 or 1. We can directly read and write to it to achieve the effect of reading and writing GPIO.

Two, define GPIO as input

For example, to define GPIO258 as input, the command is as follows: (This command requires root privileges)

echo in > /sys/class/gpio/gpio258/direction 

Three, define GPIO as output

For example, to define GPIO258 as output, the command is as follows: (This command requires root privileges)

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

Fourth, read the value of GPIO

We can directly read the value file (store the GPIO value) in the /sys/class/gpio/gpio258 directory.

You can use cat to view and read the value of GPIO, for example, view the value of GPIO258:

cat /sys/class/gpio/gpio258/value
value is just a file, which can be read in other ways; write a python program to read the value of GPIO:

# 定义一个函数,用于读取GPIO258的值。
def read_258():
    with open('/sys/class/gpio/gpio258/value', 'r') as f:
        io_258 = int(f.read())
        print("read_258:%d"%io_258)

# 调用函数
read_258()

Five, write the value of GPIO

We can directly write values to the value file (store the GPIO value) in the /sys/class/gpio/gpio258 directory.

You can use ehco to write the value of GPIO, for example, write the value of GPIO258:

echo 1 > /sys/class/gpio/gpio258/value             # output logic 1 level
echo 0 > /sys/class/gpio/gpio258/value             # output logic 0 level

Write a python program to write the value of GPIO:

# 定义一个函数,用于写GPIO258的值。
def write_258(io_str):
    with open('/sys/class/gpio/gpio258/value', 'w+') as f:
        f.write(io_str)
        print("write_258:%s"%(io_str))

# 调用函数
write_258()

After testing, the program takes about 0.6ms to perform a write operation; the ehco method is relatively long, about 10ms.

Six, small case-set GPIO as input, and read the IO value

Method 1: Pure shell command

# 设置GPIO20为输入
echo 20 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio20/direction

# 读取IO值
cat /sys/class/gpio/gpio20/value

Method 2: shell command + Python program (more efficient)

# 设置GPIO20为输入
echo 20 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio20/direction

Read IO value:

# 定义一个函数,用于读取GPIO258的值。
def read_20():
    with open('/sys/class/gpio/gpio20/value', 'r') as f:
        io_20 = int(f.read())
        print("read_20:%d"%io_20)

# 调用函数
read_20()

Seven, small case-set GPIO as output, and read and write the IO value

Method 1: Pure shell command

# 设置GPIO40为输出
echo 40 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio40/direction

# 写IO值,高电平
echo 1 > /sys/class/gpio/gpio40/value

# 写IO值,低电平
echo 0 > /sys/class/gpio/gpio40/value

Method 2: shell command + Python program (more efficient)

# 设置GPIO40为输出
echo 40 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio40/direction

Read and write IO values:

import time

def read_40():
    with open('/sys/class/gpio/gpio40/value', 'r') as f:
        io_40 = int(f.read())
        print("read_40:%d"%io_40)

def write_40(io_str):
    with open('/sys/class/gpio/gpio40/value', 'w') as f:
        f.write(io_str)
        print("write_40:%s"%(io_str))

start = time.time()
read_40()
write_40("1")
read_40()
end = time.time()
print("读写IO的时间", end-start)

Effect: It takes 0.6ms.
image.png

Click to follow, and learn about the fresh technology of Huawei Cloud for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量