Operation of Gpio with file IO under Linux (/sys/class/gpio)

Source: Internet
Author: User
Tags usleep

by Sysfs control Gpio, first access the/sys/class/gpio directory, write the GPIO number to the export file, so that the Gpio operation interface from the kernel space exposed to the user space, The GPIO's operating interface includes direction and value, and direction controls the Gpio direction, while value controls the Gpio output or obtains gpio input. The file IO mode operates GPIO, using 4 functions open, close, read, write.


First, see if there is a "/sys/class/gpio" folder in the system. If not, please add Device drivers-> GPIO support->/sys/class/gpio/When compiling the kernel ... (Sysfs interface).

/sys/class/gpio Instructions for use:
Gpio_operation operation of the IO port Gpio to file system mappings via the/sys/file interface
◇ the directory where the Gpio is controlled is located in/sys/class/gpio
The ◇/sys/class/gpio/export file is used to notify the system of the GPIO PIN number that the control needs to be exported
◇/sys/class/gpio/unexport for notification system cancel export
◇/SYS/CLASS/GPIO/GPIOCHIPX directory holds information about GPIO registers in the system, including the starting number base for each register control pin, the register name, and the total number of pins export a pin operation step
◇ This PIN number is calculated first, PIN number = Register base of the control PIN + control PIN Register bit number
◇ write this number to/sys/class/gpio/export, for example, 12th pin, in the shell can be implemented by the following command, the command succeeds in generating the/sys/class/gpio/gpio12 directory, if there is no corresponding directory, the PIN is not exportable
The ◇direction file, which defines the input input direction, can be defined as output by the following command. Direction accepted parameters: In, out, high, low. The High/low also sets the direction to output, and the value is set to the corresponding 1/0
The ◇value file is the value of the port, 1 or 0

A few examples:
1. Export
/sys/class/gpio# echo > Export
2. Setting the direction
/sys/class/gpio/gpio44# echo out > direction
3. View Directions
/sys/class/gpio/gpio44# Cat Direction
4. Setting the output
/sys/class/gpio/gpio44# echo 1 > value
5. View output values
/sys/class/gpio/gpio44# Cat Value
6. Cancel Export
/sys/class/gpio# echo > Unexport

File read and Write routines:
#include stdlib.h
#include stdio.h
#include string.h
#include unistd.h
#include fcntl.h//define o_wronly and O_rdonly

Chip Reset PIN: P1_16
#define Sysfs_gpio_export "/sys/class/gpio/export"
#define SYSFS_GPIO_RST_PIN_VAL "48"
#define SYSFS_GPIO_RST_DIR "/sys/class/gpio/gpio48/direction"
#define Sysfs_gpio_rst_dir_val "Out"
#define SYSFS_GPIO_RST_VAL "/sys/class/gpio/gpio48/value"
#define SYSFS_GPIO_RST_VAL_H "1"
#define SYSFS_GPIO_RST_VAL_L "0"

int main ()
{
int FD;

Open port/sys/class/gpio# echo > Export
FD = open (Sysfs_gpio_export, o_wronly);
if (fd = =-1)
{
printf ("Err:radio hard reset pin open error.\n");
return exit_failure;
}
Write (fd, sysfs_gpio_rst_pin_val, sizeof (Sysfs_gpio_rst_pin_val));
Close (FD);

Set port direction/sys/class/gpio/gpio48# echo out > direction
FD = open (Sysfs_gpio_rst_dir, o_wronly);
if (fd = =-1)
{
printf ("Err:radio hard reset pin direction open error.\n");
return exit_failure;
}
Write (fd, sysfs_gpio_rst_dir_val, sizeof (Sysfs_gpio_rst_dir_val));
Close (FD);

Output reset signal: Pull High >100ns
FD = open (Sysfs_gpio_rst_val, O_RDWR);
if (fd = =-1)
{
printf ("Err:radio hard reset pin value open error.\n");
return exit_failure;
}
while (1)
{
Write (fd, sysfs_gpio_rst_val_h, sizeof (SYSFS_GPIO_RST_VAL_H));
Usleep (1000000);
Write (fd, sysfs_gpio_rst_val_l, sizeof (sysfs_gpio_rst_val_l));
Usleep (1000000);
}
Close (FD);

printf ("Info:radio hard reset pin value open error.\n");
return 0;

}
Also refer to the online user's program, here to do the verification, and implement the interrupt detection function. As follows:
#include stdlib.h
#include stdio.h
#include string.h
#include unistd.h
#include fcntl.h
#include poll.h

#define MSG (args ...) printf (args)

function declaration
static int gpio_export (int pin);
static int gpio_unexport (int pin);
static int gpio_direction (int pin, int dir);
static int gpio_write (int pin, int value);
static int gpio_read (int pin);


static int gpio_export (int pin)
{
Char buffer[64];
int Len;
int FD;

FD = open ("/sys/class/gpio/export", o_wronly);
if (FD < 0) {
MSG ("Failed to open export for writing!\n");
Return (-1);
}

Len = snprintf (buffer, sizeof (buffer), "%d", pin);
if (write (fd, buffer, Len) < 0) {
MSG ("Failed to export gpio!");
return-1;
}

Close (FD);
return 0;
}

Static int gpio_unexport (int pin)
{
Char buffer[64];
int len;
Int fd;

FD = open ("/sys/class/gpio/unexport", o_wronly);
if (FD < 0) {
MSG ("Failed to open Unexport for writing!\n");
Return-1;
}

len = snprintf (buffer, sizeof (buffer), "%d", pin);
if (write (fd, buffer, Len) < 0) {
MSG ("Failed to Unexport gpio!");
Return-1;
}

Close (FD);
return 0;
}

//dir:0-->in, 1-->out
static int gpio_direction (int pin, int dir)
{
Static const char dir_str[] = "I N\0out ";
Char path[64];
Int fd;

snprintf (path, sizeof (path), "/sys/class/gpio/gpio%d/direction", pin);
FD = open (path, o_wronly);
if (FD < 0) {
MSG ("Failed to open Gpio direction for writing!\n");
Return-1;
}

if (write (fd, &dir_str[dir = = 0? 0:3], dir = = 0? 2:3) < 0) {
MSG ("Failed to set Directi On!\n ");
Return-1;
}

Close (FD);
return 0;
}

//value:0-->low, 1-->high
static int gpio_write (int pin, int value)
{
Static const char values_str[] = "01";
Char path[64];
Int fd;

snprintf (path, sizeof (path), "/sys/class/gpio/gpio%d/value", pin);
FD = open (path, o_wronly);
if (FD < 0) {
MSG ("Failed to open Gpio value for writing!\n");
Return-1;
}

if (write (fd, &values_str[value = = 0? 0:1], 1) < 0) {
MSG ("Failed to write value!\n");
return-1;
}

Close (FD);
return 0;
}

Static int gpio_read (int pin)
{
Char path[64];
Char value_str[3];
Int fd;

snprintf (path, sizeof (path), "/sys/class/gpio/gpio%d/value", pin);
FD = open (path, o_rdonly);
if (FD < 0) {
MSG ("Failed to open Gpio value for reading!\n");
Return-1;
}

if (read (FD, VALUE_STR, 3) < 0) {
MSG ("Failed to read value!\n");
Return-1;
}

Close (FD);
Return (Atoi (VALUE_STR));
}

//None means pin is input, not interrupt pin
//rising indicates pin is interrupt input, rising edge trigger
///falling indicates pin is interrupt input, falling edge trigger
//Both indicates pin is interrupt input, edge trigger
//0- ->none, 1-->rising, 2-->falling, 3-->both
static int gpio_edge (int pin, int edge)
{
const char Dir_ Str[] = "None\0rising\0falling\0both";
Char ptr;
Char path[64];
Int fd;
Switch (EDGE) {
Case 0:
ptr = 0;
Break
Case 1:
ptr = 5;
Break
Case 2:
ptr = n;
Break
Case 3:
ptr =;
Break
Default:
ptr = 0;
}

snprintf (path, sizeof (path), "/sys/class/gpio/gpio%d/edge", pin);
FD = open (path, o_wronly);
if (FD < 0) {
MSG ("Failed to open Gpio edge for writing!\n");
Return-1;
}

if (write (FD, &dir_str[ptr, strlen (&dir_str[ptr))) < 0) {
MSG ("Failed to set edge!\n");
Return-1;
}

Close (FD);
return 0;
}

Gpio1_17
int main ()
{
int GPIO_FD, ret;
struct POLLFD fds[1];
Char buff[10];
unsigned char cnt = 0;
LED PIN Initialization
Gpio_export (115);
Gpio_direction (115, 1);
Gpio_write (115, 0);
Key-pin Initialization
Gpio_export (49);
Gpio_direction (49, 0);
Gpio_edge (49,1);
GPIO_FD = open ("/sys/class/gpio/gpio49/value", o_rdonly);
if (GPIO_FD < 0) {
MSG ("Failed to open value!\n");
return-1;
}
FDS[0].FD = GPIO_FD;
Fds[0].events = Pollpri;
ret = read (gpio_fd,buff,10);
if (ret = =-1)
MSG ("read\n");
while (1) {
RET = poll (fds,1,0);
if (ret = =-1)
MSG ("poll\n");
if (Fds[0].revents & Pollpri) {
ret = Lseek (gpio_fd,0,seek_set);
if (ret = =-1)
MSG ("lseek\n");
ret = read (gpio_fd,buff,10);
if (ret = =-1)
MSG ("read\n");
Gpio_write (cnt++%2);
}
Usleep (100000);
}
return 0;
}

Operation of Gpio with file IO under Linux (/sys/class/gpio)

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.