Abstract: This article introduces GPIO read and write, introduces the basic principle, and the performance of different read and write methods.
This article is shared by huawei Cloud community “Linux BASED on SYSFS GPIO read and write operations”, by a small tree X.
preface
Recently I came into contact with THE DEVELOPMENT of GPIO in Linux system. Here I will make a summary and share with you. This article will introduce GPIO read and write, introduce the basic principles, and the performance of different read and write methods.
GPIO SYSFS interface
In Linux, the most common way to read and write GPIO is to use the GPIO sysfs interface, /sys/class/gpio export, unexport, gPIO {N}/direction, gpio{N}/value (with actual pin numbers instead of {N}).
First declare GPIO port, such as GPIO258 :(this command requires root permission)
echo 258 > /sys/class/gpio/export
Copy the code
Then at /sys/class/ gPIO, a new directory named gPIo258 is generated
For example: GPIO258, N corresponds to 258; Go to the directory CD /sys/class/ gPIO/gPIo258
The following commands can be seen: Active_low, Device, Direction, EDGE, power, Subsystem, uEvent, value
The most common one is the value file, which stores the value of GPIO. The range is 0 or 1. We can read and write to it directly to achieve the effect of reading and writing GPIO.
2. Define GPIO as input
For example, to define GPIO258 as the input, run the following command :(this command requires root permission)
echo in > /sys/class/gpio/gpio258/direction
Copy the code
Define GPIO as output
For example, to define GPIO258 as output, run the following command :(this command requires root permission)
echo out > /sys/class/gpio/gpio258/direction
Copy the code
Read the value of GPIO
/sys/class/ gPIO/gPIo258 / gPIo258 /sys/class/ gPIO/gPIo258 / gPIo258 /sys/class/ GPIO/gPIo258 /sys/class/ GPIO/gPIo258 /sys/class/ GPIO/gPIo258
You can use cat to check the value of GPIO, such as GPIO258:
cat /sys/class/gpio/gpio258/value
Copy the code
Value is just a file that can be read in other ways; Write a Python program that reads the value of GPIO:
Define a function to read the value of 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)Copy the code
Write the value of GPIO
/sys/class/ gPIO/gPIo258 /gpio258 /sys/class/ gPIO/gPIo258 / gPIo258 /sys/class/ gPIO/gPIo258 /sys/class/ gPIO/gPIo258
We can write GPIO values with EHCO, such as GPIO258 values:
echo 1 > /sys/class/gpio/gpio258/value # output logic 1 level
echo 0 > /sys/class/gpio/gpio258/value # output logic 0 level
Copy the code
Write a Python program that writes the value of GPIO:
Define a function to write the value of GPIO258. def write_258(io_str): with open('/sys/class/gpio/gpio258/value', 'w+') as f: F. write(io_str) print("write_258:%s"% s")Copy the code
After the test, the program to write a operation, time-consuming about 0.6ms; Ehco mode is relatively long, about 10ms.
Set GPIO as input and read IO value
Method 1: Pure shell command
# set GPIO20 to input echo > 20 / sys/class/gpio/export echo in > / sys/class/gpio/GPIO20 / direction # read IO cat /sys/class/gpio/gpio20/valueCopy the code
Method 2: Shell command + Python program (more efficient)
# set GPIO20 to input echo > 20 / sys/class/gpio/export echo in > / sys/class/gpio/GPIO20 / directionCopy the code
Read IO value:
Define a function to read the value of GPIO258. def read_20(): with open('/sys/class/gpio/gpio20/value', 'r') as f: Io_20 = int (f.r ead ()) print (" read_20: % d % io_20) # calling function read_20 ()Copy the code
Set GPIO as output and read/write IO values
Method 1: Pure shell command
# set GPIO40 to output echo 40 > / sys/class/gpio/export echo out > / sys/class/gpio GPIO40 / direction # write IO values, High level echo 1 > / sys/class/gpio gpio40 / value # write IO value, low level echo 0 > / sys/class/gpio/gpio40 / valueCopy the code
Method 2: Shell command + Python program (more efficient)
# set GPIO40 to output echo 40 > / sys/class/gpio/export echo out > / sys/class/gpio/GPIO40 / directionCopy the code
Read/write I/O value:
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 (" time to read/write I/O ", end-start)Copy the code
Effect: time-consuming 0.6ms.
Click to follow, the first time to learn about Huawei cloud fresh technology ~