Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the operating principle of GPIO

2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what is the principle of GPIO operation, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The Linux system defines the principle that everything is a file, even the hardware: disks, floppy disks, etc. The advantage of this is that a unified invocation mode can be formed on different platforms and hardware

Here, from the raspberry pie, we discuss the operation principle of GPIO (non-kernel principle): control GPIO through sysfs.

Anyone who has come into contact with Linux should know that echo "0" > test.txt is to input a string 0 into the test.txt file of the current directory, so all the following operations are based on this form of operation.

GPIO port

Since Linux 2.6.21, GPIO has been included in Linux as a basic component, and we can use it directly according to the GPIO specification.

First, go to the / sys/class/gpio direction directory and we can see the following files:

# cd / sys/class/gpio & & ls-l Total usage 0murwxrwx ls-1 root gpio 4096 September 8 01:37 exportlrwxrwxrwx 1 root gpio 0 August 9 01:16 gpiochip0- >.. /.. / devices/platform/soc/3f200000.gpio/gpio/gpiochip0-rwxrwx--- 1 root gpio 4096 September 8 01:47 unexport

The directory functions are as follows:

Gpio_operation operates the mapping of the IO port GPIO to the file system through the / sys/ file interface.

The directory that controls GPIO is located in / sys/class/gpio.

The / sys/class/gpio/export file is used to inform the system of the GPIO pin number that needs to be exported.

/ sys/class/gpio/unexport is used to notify the system to cancel the export.

The / sys/class/gpio/gpiochipX directory holds information about the GPIO registers in the system, including the starting number base, register name, and total number of pins for each register control pin

Calculate pin number

Pin number = register base of control pin + number of control pin register bits

Take a chestnut (refer to the data manual for specific GPIO). If you want to use GPIO1_20, the pin number may be equal to 1 x 32 + 20 = 54.

We operate under raspberry pie 2B, where the register cardinality is 0, so the pin number is the number of pin registers.

Export pin

Write the pin number to the file / sys/class/gpio/export to activate the pin.

Echo 18 > / sys/class/gpio/export

After the command is executed successfully, the gpio18 folder appears in the directory, and if it does not appear, the pin is not exportable.

Control direction

After the pin is successfully exported, the pin [input] or [output] can be controlled by writing / sys/class/gpio/gpio18/direction.

# cd gpio18# echo "out" > direction

If no error is reported in the command, the operation is successful. The following values can be entered:

In pin input signal out output control to pin high output a high level (`value`: `1`) low output a low level (`value`: `0`) input and output value

The input and output values are controlled by the value file under gpioX

When input, ask echo / sys/class/gpio/gpio18/value to read the value

When it is output, please echo 1 > / sys/class/gpio/gpio18/value enter the value

Here the value of value corresponds to 1 / 0 of the above high and low.

Cancel pin export

When the control is complete, you need to release the control of the port and output the pin number to / sys/class/gpio/unexport as you did when exporting the pin.

Echo 18 > / sys/class/gpio/unexport

SHELL control

In the above explanation, GPIO is immediately controlled by shell. Here, all the commands are grouped together:

# enter the GPIO directory cd / sys/class/gpio# use ls to view the contents of the gpio directory ls# export gpiochip0 unexport# GPIO operation interface is exposed from kernel space to user space # after performing this operation, a gpio18 file directory echo 18 > export# is added to the GPIO18 directory This directory is generated by the previous operation cd gpio18# view gpio18 directory ls # active_low direction edge power subsystem uevent value# set GPIO18 to output direction echo out > direction# output logic high level, LED light echo 1 > value# output logic low level, LED off echo 0 > value# return to the parent directory cd.. / # release GPIO4 interface echo 18 > unexport on what is GPIO operation principle is shared here I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report