Linux kernel-driven GPIO subsystem (i) Use of Gpio

Source: Internet
Author: User
Tags event listener

Transferred from: http://blog.csdn.net/mirkerson/article/details/8464290

An overview

The Gpio in the Linux kernel is the simplest, most common resource (and interrupt, Dma,timer) driver, the application is able to use the corresponding interface using Gpio,gpio to use the integer ID between 0~max_int, can not use negative numbers, Gpio is closely related to the hardware system, but Linux has a framework that handles GPIO and can use a unified interface to manipulate Gpio. Before you talk about Gpio core (GPIOLIB.C), let's look at how Gpio is used

Use of Gpio in two cores

1 Test if the GPIO port is valid int gpio_is_valid (int number);

2 Apply for a Gpio port of course before applying the configuration that is required to display the Pinmux of the Gpio port

int gpio_request (unsigned gpio, const char *label)

3 mark the use direction of gpio including input or output

/* Successfully returned 0 failed to return negative error value */

int gpio_direction_input (unsigned gpio);

int gpio_direction_output (unsigned gpio, int value);

4 Get the value of the Gpio pin and set the value of the Gpio pin (for output)

int Gpio_get_value (unsigned gpio);

void Gpio_set_value (unsigned gpio, int value);

5 Gpio used as interrupt port

int Gpio_to_irq (unsigned gpio);

The value returned is the interrupt number that can be passed to REQUEST_IRQ () and FREE_IRQ ()

The kernel converts a GPIO port into an interrupt by calling this function, and there is a similar approach in user space

6 Exporting Gpio ports to user space

int Gpio_export (unsigned gpio, bool direction_may_change);

The kernel can explicitly manage the export of GPIO ports that have been requested by the Gpio_request ().

The parameter direction_may_change indicates whether the user program allows the direction of the Gpio to be modified, if

The parameter Direction_may_change is true

/* Revoke the GPIO's export */

void Gpio_unexport ();

Three-user space Gpio invocation

The user space accesses the GPIO, which accesses the Gpio via the Sysfs interface, which is the following three files in the/sys/class/gpio directory:

--export/unexport file

--gpion refers to specific gpio pins

--GPIO_CHIPN refers to GPIO controllers

It is important to know that the above interfaces do not have standard device files and their links.

(1) Export/unexport file interface:

/sys/class/gpio/export, this interface can only write unreadable

The user program writes the GPIO number to the kernel to request that the control of a gpio be exported to the user space without the kernel code requesting this Gpio port

such as echo > Export

This will create a node gpio19 for Gpio # 19th, where a gpio19 directory is generated underneath the/sys/class/gpio directory

The/sys/class/gpio/unexport and export effects are reversed.

such as echo > Unexport

The above operation will remove the GPIO19 node.

(2)/sys/class/gpio/gpion

Refers to a specific Gpio port, which is a subordinate file

The direction represents the direction of the Gpio port, and the read result is in or out. The file can also be written, and when written out, the Gpio is set to output while the level defaults to low. Writing low or high can not only

Set to output can also set the level of output. Of course, if the kernel does not support or the kernel code is not willing, there will be no such attribute, such as the kernel called Gpio_export (n,0)

Indicates that the kernel is unwilling to modify the Gpio Port direction attribute

Value indicates the level of the Gpio pin, 0 (Low level) 1 (high), if the GPIO is configured as output, this value is writable, remember that any nonzero value will be output high, if a pin

Can and has been configured to interrupt, the poll (2) function can be called to listen for the interrupt, and the poll (2) function will return after the interrupt is triggered.

Edge indicates the way the interrupt is triggered, the edge file has the following four values: "None", "Rising", "Falling", "both".

None indicates the pin is input, not the interrupt pin

Rising indicates that the PIN is interrupt input and the rising edge is triggered

Falling indicates that the PIN is an interrupt input and a falling edge is triggered

Both indicates that the PIN is interrupt input, edge trigger

This file node exists only when the PIN is configured as an input pin. When the value is None, the interrupt pin can be changed by the following method

echo "Both" > edge, both,falling or rising dependent on the specific hardware of the interrupt triggering method. This method is the way that the user-state Gpio is converted to the interrupt pin

Active_low not understand, also wood useful too

(3)/SYS/CLASS/GPIO/GPIOCHIPN

GPIOCHIPN represents a gpio_chip, a controller that manages and controls a set of Gpio ports in which a subordinate file exists:

Base and N are the same, representing the smallest port number managed by the controller.

lable Diagnostic use of flags (not always unique)

Ngpio represents the number of GPIO ports managed by the controller (port range: N ~ n+ngpio-1)

Four user states use Gpio to listen for interrupts

First, you need to configure the Gpio as an interrupt

echo "Rising" >/sys/class/gpio/gpio12/edge

The following is a pseudo-code

int gpio_id;

struct POLLFD fds[1];

GPIO_FD = open ("/sys/class/gpio/gpio12/value", o_rdonly);

if (gpio_fd = =-1)

Err_print ("Gpio open");

FDS[0].FD = GPIO_FD;

Fds[0].events = Pollpri;

ret = read (gpio_fd,buff,10);

if (ret = =-1)

Err_print ("read");

while (1) {

RET = poll (fds,1,-1);

if (ret = =-1)

Err_print ("poll");

if (Fds[0].revents & Pollpri) {

ret = Lseek (gpio_fd,0,seek_set);

if (ret = =-1)

Err_print ("Lseek");

ret = read (gpio_fd,buff,10);

if (ret = =-1)

Err_print ("read");

/* This indicates that the interrupt has been triggered and the officer has

...............

}

}

Remember to use the poll () function to set the event listener type to POLLPRI and Pollerr after poll () is returned, use Lseek () to move to the beginning of the file to read the new value or turn it off and then reopen to read the new value. You must do this or the poll function will always return.

Linux kernel-driven GPIO subsystem (i) GPIO usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.