Example of operating gpio port in embedded linux file io mode

Example of operating gpio port in embedded linux file io mode

To operate the gpio port at the application layer in embedded linux, if you are too lazy to write a driver or read the register manual, there is a simple way to operate, you only need the terminal to apply for io, and then set the direction and you can operate directly. Here is the imx board of Wildfire Demonstration, a LED flashing light program is used as an example later

1. Command line operation demonstration
1. Confirm that there is a sys directory in the root directory of the file system. If not, you need to open Device Drivers when compiling the kernel —> GPIO Support —> /sys/class/gpio/… (sysfs interface)

2. Check the number of the calculation application, here we take IO1.1 as an example

root@npi:/sys/class/gpio# cat gpiochip0/label
209c000.gpio
where the base address is 209c000. In P1141 in the imx6ullrm manual, the GPIO memory map
finds GPIO1 root@npi:/sys/class/gpio# cat gpiochip0/base
0
we want to apply for IO1.1, 0+1=1

2. The application io is
in the /sys/class/gpio directory, and the command terminal enters echo 1> export, and a gpio1 directory
root@npi:/sys/class/gpio# echo 1> export will be generated in the current directory

3. Set the output
Enter the gpio1 directory and modify the direction
root@npi:/sys/class/gpio/gpio1# echo out> direction

4. Set the level
Enter the gpio1 directory, modify the value
root@npi:/sys/class/gpio/gpio1# echo 0> value

5. Configure input
root@npi:/sys/class/gpio/gpio1# echo in> direction

6. Read level
root@npi:/sys/class/gpio/gpio1# cat value
0
Attached: active_low device direction edge power subsystem uevent value
active_low: interrupt configuration active low level active high level
edge: interrupt configuration, rising Edge falling edge
uevent: interrupt event

Two, c program operation

#include <stdio.h>
#include <fcntl.h>

int led_init()
{
    
    
	#defien led "1"
	#define led_path "/sys/class/gpio/gpio1"
	#define led_dir "/sys/class/gpio/gpio1/direction"
	//申请io
	int fd = 0;
	fd = open("/sys/class/gpio/export", O_WRONLY);
    if(-1 == fd)
    {
    
    
       printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
       exit(1);
	}
	if(-1 == write(fd, led, 1))
    {
    
    
        printf("write file operation error\r\n");
        exit(1);
    }
    close(fd);
	//配置
	fd = open(led_dir, O_WRONLY);
    if(-1 == fd)
    {
    
    
       printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);       
       exit(1);
    }
    if(-1 == write(fd, "in", sizeof("in")))
    {
    
    
       printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
       close(direction_fd);
       exit(1);
    }
    close(fd);
	
	return 0;
}

int led_ctrl(unsigned char x)
{
    
    
	#define led_value "/sys/class/gpio/gpio1/value"
	int fd = 0;
	
	fd = open(led_value, O_WRONLY);
	if(-1 == fd)
    {
    
    
       printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
       exit(1);
	}
	if(x)
	{
    
    
		if(-1 == write(fd, "1", sizeof("in")))
		{
    
    
		   printf("[%s]:[%d] write operation value error\r\n", __FUNCTION__, __LINE__);
		   close(direction_fd);
		   exit(1);
		}
	}
	else
	{
    
    
		if(-1 == write(fd, "0", sizeof("in")))
		{
    
    
		   printf("[%s]:[%d] write operation value error\r\n", __FUNCTION__, __LINE__);
		   close(direction_fd);
		   exit(1);
		}
	}
	
	return 0;
}

int read_ledstate()
{
    
    
	#define led_value "/sys/class/gpio/gpio1/value"
	int fd = 0;
	char value;
	
	fd = open(led_value, O_WRONLY);
	if(-1 == fd)
    {
    
    
       printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
       exit(1);
	}
	if(-1 == read(gpiovalue_fd, &value, sizeof(value)))
    {
    
    
        printf("[%s]:[%d] read gpiovalue is fail\r\n", __FUNCTION__, __LINE__);
        close(gpiovalue_fd);
        exit(1);
    }
	
	return value;
}

int main()
{
    
    
	int x = 0;
	
	led_init();
	led_ctrl(1);
	x = read_ledstate();
	printf("led state = %d \r\n", x);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/u010835747/article/details/108531832