1:Brief introduction

In the embedded systemGPIOThe operation is the most basic operation. existLinuxThere is a generalGPIOOperation method, letusink below to introduce to it /sys/class/gpio Way.

In/sys/class/gpio/BelowexportFile, XiangexportThe file is written to operateGPIONo.GPIOThe operate interface is exposed from the kernel space to the user space,GPIOThe operate interface includesdirectionandvalueetc.directioncontrolGPIOInput or output mode, andvalueCan controlGPIOState or read status. 

/sys/class/gpio/Each file description in the directory:

/sys/class/gpio/exportThe file is used to inform the system that needs to be exported to controlGPIOPin number;

/sys/class/gpio/unexport   Used to notify the system to cancel the export;

/sys/class/gpio/gpioX/directionFile, you can writein(Set input direction) orout(Set output direction);

/sys/class/gpio/gpioX/valueThe file can be read and writeGPIOstate;

/sys/class/gpio/gpiochipXDirectory storage systemGPIOThe information of the register, including the starting number of each registers Control pin, the name of the register, the total number of pins; among themXRepresents specific pins numbers.



1.1: First calculate this pin number

Pin number = CONTROL pins, register bases + Control pins register number

for example: RisaG2LChip as an example, dual -coreA55Processor, Gundam1.2GHZ, Industrial -grade chips, high stability, good cost -effective.

If you want to use P43_2Then Pin Number may be equal to 43 x 8 + 2 + 120 = 466

GPIO_ID = GPIO_port *   8 + GPIO_pin + 120                   


1.2:ExportThis pins

Towards /sys/class/gpio/export Write this number, for exampleP43_2Pin, in shell It can be implemented through the following command:

root@weathink:~# echo 466 > /sys/class/gpio/export      

After the command is successful /sys/class/gpio/ P43_2 Directory, if there is no corresponding directory, it means that this pin cannot be exported.



direction The file, define the input input direction, can be defined as output by the following command.

root@weathink:~#  echo out >   /sys/class/gpio/P43_2/direction      


direction Accept parameters can be:inouthighlowEssence The parameters high / low While setting the direction to the output, it will value Set to the corresponding 1 / 0



value The file is the value of the port, for1or0,通过下面命令将P43_2 设置为高电平。


root@weathink:~# echo 1 >   /sys/class/gpio/P43_2/value

root@weathink:~# echo 0 >   /sys/class/gpio/P43_2/value



root@weathink:~# cat   /sys/class/gpio/P43_2/value         

如果返回1表示高电平,返回0Indicates a low level.



This article comes from Weathink, please indicate the source for reprinting.

Tags: Array