The use of Linux kernel-driven Gpio __linux

Source: Internet
Author: User
Tags event listener
an overview

Linux kernel Gpio is the simplest, most commonly used resource (and interrupt, Dma,timer) driver, applications can use the corresponding interface Gpio,gpio 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 for GPIO that can operate GPIO using a unified interface. Before speaking Gpio core (GPIOLIB.C), let's see how Gpio uses the two kernel .

1 Test whether the GPIO port is legal int gpio_is_valid (int number);

2 Apply for a Gpio port of course before applying for the configuration of the Gpio port to be displayed Pinmux

int gpio_request (unsigned gpio, const char *label)

3 The direction of use of the tag Gpio includes input or output

/* Successful return of 0 failed 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 an interruption 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 the Gpio port to interrupts by calling the function, and there are similar methods in user space

6 export Gpio port 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 gpio_request ().

Parameter Direction_may_change indicates whether the user program allows you to modify the direction of the GPIO, if you can

The parameter Direction_may_change is true

/* Undo GPIO Export * *

void Gpio_unexport (); three user-space Gpio calls

User space access Gpio, that is, access to Gpio through the Sysfs interface, below are three files in the/sys/class/gpio directory:

--export/unexport file

--gpion refers to the specific GPIO pin

--GPIO_CHIPN Finger Gpio Controller

You must 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 cannot read

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

Like echo > Export

The above operation creates a node gpio19 for number 19th Gpio, where a gpio19 directory is generated below the/sys/class/gpio directory

/sys/class/gpio/unexport and exporting have the opposite effect.

Like Echo > Unexport.

The above operation will remove the GPIO19 node. (2)/sys/class/gpio/gpion

Refer to a specific Gpio port with a subordinate file inside

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

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

Indicates that the kernel is unwilling to modify the Gpio Port orientation Properties

Value represents the level of the Gpio pin, 0 (Low level) 1 (High level), if the GPIO is configured to output, this value is writable, remember that any Non-zero value will output a high level, if a pin

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

Edge of the interrupt means the trigger, edge file has the following four values: "None", "Rising", "Falling", "both."

None indicates pin for input, not interrupt pins

Rising indicates that the PIN is an interrupt input, the ascent is triggered

The falling indicates that the PIN is the interrupt input, and the descent along the trigger

Both indicates that the PIN is an interrupt input, edge trigger

This file node only exists when the pins are configured to enter pins. When the value is None, you can change it to a break pin by using the following method

echo "both" > edge; for triggers that are both,falling or rising rely on specific hardware interrupts. This method is the way that user state Gpio is converted to interrupt pins

Active_low not understand, also wood useful (3)/sys/class/gpio/gpiochipn

GPIOCHIPN represents a gpio_chip that manages and controls a set of Gpio-port controllers that have a subordinate file in the directory:

Base and N are the same, representing the smallest port number that the controller manages.

Lable Diagnostics Use flags (not always unique)

Ngpio represents the number of GPIO ports managed by the controller (port range is: N ~ n+ngpio-1) Four user state using Gpio listening interrupt

First you need to configure the Gpio to be interrupted

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

The following are 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");

/* At this time said that the interruption has been heard the trigger, the officer's * *

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

}

}

Remember to use the poll () function to set the event listener type to POLLPRI and Pollerr after the poll () is returned, use Lseek () to move to the beginning of the file to read the new value, or close it to reopen the read new value.     This must be done otherwise the poll function will always return. Five user state using Gpio control led

Example code:

1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/stat.h>
4 #include <linux/fs.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <termios.h>
8 #include <sys/ioctl.h>
9
10
one void tdelay (int d)
12 {
volatile Int J;
for (j=0;j<d*1000000;j++);
15}
//int Main (int argc,char **argv)
int main (void)
18 {
int gpio_fd =-1;
int ret;
//Led D5 connected to gpio_jtag_tdi,pin number is 242
Char gpio[]= "242";
dir[]= char "out";
GPIO_FD = open ("/sys/class/gpio/export", o_wronly);
if (GPIO_FD < 0) {
Num printf ("Open gpio/export failed\n");
return-1;
28}
ret = Write (Gpio_fd,gpio,strlen (GPIO));
if (Ret < 0) {
-printf ("Write to Gpio/export failed\n");
return-1;
33}
Close (GPIO_FD);

GPIO_FD = open ("/sys/class/gpio/gpio242/direction", O_RDWR);
if (GPIO_FD < 0) {
Notoginseng printf ("Open gpio242/direction failed\n");
return-1;
39}
40
a ret = write (Gpio_fd,dir,strlen (dir));
if (Ret < 0) {
("Write to Gpio242/direction failed\n");
return-1;
45}
Close (GPIO_FD);
47
GPIO_FD = open ("/sys/class/gpio/gpio242/value", O_RDWR);
if (GPIO_FD < 0) {
printf ("Open gpio242/value failed\n");
Wuyi return-1;
52}
53
int i;
off[]= char "1";
on[char] = "0";
For (I=0;i < 10;i++) {
To printf ("led off\n");
ret = Write (Gpio_fd,off,strlen (off));
if (Ret < 0) {
-printf ("Write to Gpio242/value failed\n");
return-1;
63}
Tdelay (10);
In printf ("led on\n");
ret = Write (Gpio_fd,on,strlen (on));
if (Ret < 0) {

printf ("Write to Gpio242/value failed\n");
return-1;
70}
Tdelay (10);
72}
Close (GPIO_FD);
74
GPIO_FD = open ("/sys/class/gpio/unexport", o_wronly);
The IF (GPIO_FD < 0) {
-printf ("Open gpio/unexport failed\n");
return-1;
79}
ret = Write (Gpio_fd,gpio,strlen (GPIO));
Bayi if (Ret < 0) {
failed\n printf ("Write to Gpio/unexport");
return-1;
84}
Close (GPIO_FD);
86
printf ("Test Gpio led ok\n");
88
0;
90}
91

Most of the content is transferred from http://blog.csdn.net/ohyes158/article/details/8134262

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.