Operating Gpio (/sys/class/gpio) __linux with file IO under Linux

Source: Internet
Author: User
Tags usleep

I. Overview

Through the SYSFS mode control GPIO, first access to the/sys/class/gpio directory, write GPIO number to the export file, so that the GPIO operating interface from the kernel space exposure to user space, GPIO operating interface, including direction and value And so on, direction controls the GPIO direction, while value controls GPIO output or obtains GPIO input. File IO mode operation 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 when compiling the kernel:

Device Drivers-> 
        GPIO Support->
                ... (Sysfs interface).

Ii. Description of the use of/sys/class/gpio

If it is on the already adapted Linux kernel, then believe that there is already completed gpiochip, you can see the following files in the user space/sys/class/gpio directory:

Export
gpiochip0/
gpiochip32/
gpiochip64/
gpiochip96/
unexport

Description

1, gpio_operation through the/sys/file interface operation IO Port Gpio to the file system mapping.
2, the control GPIO directory is located in/sys/class/gpio.
3. The/sys/class/gpio/export file is used to inform the system of the GPIO PIN number for which control needs to be exported.
4,/sys/class/gpio/unexport used to notify the system to cancel the export.
5,/sys/class/gpio/gpiochipx directory to save the system Gpio register information, including each register control pin starting number base, register name, PIN total.

third, the operation step of exporting a pin

1, first calculate this pin number.

PIN Number = Register base of control PIN + control PIN Register bit number

Give a chestnut (specific GPIO need reference data manual), if make want to use gpio1_20, then pin number may equal 1 x 32 + 20 = 54.

2, write this number to/sys/class/gpio/export, such as Pin 12th, in the shell can be implemented by the following command:

echo >/sys/class/gpio/export

When the command succeeds, the/sys/class/gpio/gpio12 directory is generated, and if the corresponding directory does not appear, the pin is not exportable.
  
3, direction file, define input direction, can be defined as output by the following command.

echo Out >/sys/class/gpio/gpio12/direction

Direction accepted parameters can be: In, out, high, low. Where the parameter High/low sets the value to the corresponding 1/0, while setting the direction to output.
  
4, the value file is the value of the port, 1 or 0, the following command to set the gpio12 to a high level.

Echo 1 >/sys/class/gpio/gpio12/value

Four, review a few simple examples

1. Export

# echo >/sys/class/gpio/export

2, set the direction

# echo out >/sys/class/gpio/gpio44/direction

3, view the direction

# cat/sys/class/gpio/gpio44/direction

4, set the output

# echo 1 >/sys/class/gpio/gpio44/value

5. View output values

# Cat/sys/class/gpio/gpio44/value

6. Cancel Export

# echo >/sys/class/gpio/unexport

v. File read and write routines

1. Use in user space

#include stdlib.h #include stdio.h #include string.h #include unistd.h #include fcntl.h//define o_wronly and O_rdon LY//Chip reset pins: p1_16 #define SYSFS_GPIO_EXPORT "/sys/class/gpio/export" #define SYSFS_GPIO_RST_PIN_VAL "  
#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_g 

         pio_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;   }

In addition to the routines described above, there's a simpler way to do this is to write a Shell script. For example:

#!/bin/bash

echo >/sys/class/gpio/gpio48/export
echo out >/sys/class/gpio/gpio48/direction
echo 1 >/sys/class/gpio/gpio48/value
usleep 1000
echo 0 >/sys/class/gpio/gpio48/value

2. Use in kernel space

#include <linux/gpio.h>

//This is configured to output, the default high level, the name is "Gpio1_20" (is to give your IO port name)
Gpio_request_one (gpiof_ Init_high, "gpio1_20")
//This is configured as input.
Gpio_request_one (gpiof_in, "gpio1_20")
//Don't forget to free
gpio_free (54);

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.