Through the Sysfs control Gpio, first access to the/sys/class/gpio directory, the export file to write the Gpio number, so that the Gpio operation interface from the kernel space exposed to the user space, Gpio operating interface including direction and value, etc. The direction controls the Gpio direction, while value controls the Gpio output or obtains the 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[] = "In\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 direction!\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 indicates that the pin is input,?? is 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
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 = 12;
Break
Case 3:
ptr = 20;
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;
}
This article permanently updates the link address : http://www.linuxidc.com/Linux/2016-03/129596.htm
Operation of Gpio (/sys/class/gpio) (RPM) with file IO under Linux