imx6用文件io操作gpio

1 在用户空间配置并操作GPIO的必要性

有时,为了快速方便的测试IO口,我们可以将GPIO暴露给用户空间,直接在用户空间配置并操作GPIO,前提是没有驱动使用这个GPIO。

2 IMX6UL处理器GPIO编号算法

GPIO计算公式:
        id = (m - 1) * 32 + n
例如:
有gpio3.13,则m = 3,n = 13,则id = (3 - 1) * 32 + 13 = 77
这是IMX6UL GPIO的编号算法,理论上所有IMX6处理其都是这样的编号算法。

3 Linux下用文件IO的方式操作GPIO

通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction和value等,direction控制GPIO方向(是控制输出还是输入),而value可控制GPIO输出或获得GPIO输入(通过它设置引脚的值是写入1或0)。文件IO方式操作GPIO,使用到了4个函数open、close、read、write。

首先,看看系统中有没有“/sys/class/gpio”这个文件夹。如果没有请在编译内核的时候加入 Device Drivers-> GPIO Support ->/sys/class/gpio/… (sysfs interface)。

/sys/class/gpio 的使用说明

编程步骤:

◇ 控制GPIO的目录位于/sys/class/gpio

◇ /sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号

◇ /sys/class/gpio/unexport 用于通知系统取消导出

◇ /sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息,包括每个寄存器控制引脚的起始编号base,寄存器名称,引脚总数 导出一个引脚的操作步骤

◇ 首先计算此引脚编号,引脚编号 = 控制引脚的寄存器基数 + 控制引脚寄存器位数(例如GPIO4_21的引脚编号计算:首先查你使用的开发板的用户手册我的是i.MX 6UltraLite Applications Processor Reference Manual。在GPIO那章节中找到GPIO1_0与/sys/class/gpio/gpiochipX的对应关系,我的是GPIO1_0对应/sys/class/gpio/gpiochip0,所以GPIO4_21对应/sys/class/gpio/gpiochip96,即GPIO4的基数是96,GPIO4_21=96+21=117引脚编号)

◇ 向/sys/class/gpio/export写入此编号,比如GPIO4_21号引脚,在shell中可以通过以下命令实现,命令成功后生成/sys/class/gpio/gpio117目录,如果没有出现相应的目录,说明此引脚不可导出

◇ direction文件,定义输入输入方向,可以通过下面命令定义为输出。direction接受的参数:in, out, high, low。high/low同时设置方向为输出,并将value设置为相应的1/0

◇ value文件是端口的数值,为1或0

几个例子:

操作GPIO4_21引脚

1. 导出

/sys/class/gpio# echo 117 > export

2. 设置方向

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

3. 查看方向

/sys/class/gpio/gpio117# cat direction
out

4. 设置输出

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

5. 查看输出值

/sys/class/gpio/gpio117# cat value 
1

6. 取消导出

/sys/class/gpio# echo 117 > unexport

示例代码

在这里插入图片描述

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
/***************************************************************
Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
文件名		: gpio_op.c
作者	  	: ***
版本	   	: V1.0
描述	   	: gpio测试APP。
其他	   	: ./gpio_op 1000000
使用方法	 :直接操作GPIO1_26
论坛 	   	: www.openedv.com
日志	   	: 初版V1.0 2022/01/19 ***创建
***************************************************************/
#define SYSFS_GPIO_EXPORT       "/sys/class/gpio/export"
#define SYSFS_GPIO_PIN_NUM      "26"
#define SYSFS_GPIO_DIR          "/sys/class/gpio/gpio26/direction"
#define SYSFS_GPIO_DIR_VAL      "out"
#define SYSFS_GPIO_VAL          "/sys/class/gpio/gpio26/value"
#define SYSFS_GPIO_VAL_H        "1"
#define SYSFS_GPIO_VAL_L        "0"

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd;
	char *filename;
    int cnt_delay_us = 1000000;

    cnt_delay_us = atoi(argv[1]);	/* 延时时间 */
    if(cnt_delay_us < 10000 || cnt_delay_us > 100000000)
        cnt_delay_us = 1000000;
	
    //打开端口 /sys/class/gpio#  echo 26 > export
    fd  = open(SYSFS_GPIO_EXPORT, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 open failed!\n");
        return EXIT_FAILURE;
    }

    write(fd, SYSFS_GPIO_PIN_NUM, sizeof(SYSFS_GPIO_PIN_NUM));
    close(fd);

    //设置端口方向 /sys/class/gpio/gpio26# echo out > direction
    fd = open(SYSFS_GPIO_DIR, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 direction open failed!\n");
        return EXIT_FAILURE;
    }
    
    write(fd, SYSFS_GPIO_DIR_VAL, sizeof(SYSFS_GPIO_DIR_VAL));
    close(fd);

    //设置端口高低电平
    fd = open(SYSFS_GPIO_VAL, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 value open failed!\n");
        return EXIT_FAILURE;
    }
    while(1){
        printf("toggle the pin value.\n");
        write(fd, SYSFS_GPIO_VAL_H, sizeof(SYSFS_GPIO_VAL_H));
        usleep(cnt_delay_us);
        write(fd, SYSFS_GPIO_VAL_L, sizeof(SYSFS_GPIO_VAL_L));
        usleep(cnt_delay_us);
    }
    
	close(fd);	/* 关闭文件 */	
	return 0;
}

设备树模式下操作

在这里插入图片描述

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
/***************************************************************
Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
文件名		: gpio_op.c
作者	  	: ***
版本	   	: V1.0
描述	   	: gpio测试APP。
其他	   	: ./gpio_op 1000000
使用方法	 :直接操作GPIO1_26
论坛 	   	: www.openedv.com
日志	   	: 初版V1.0 2022/01/19 ***创建
***************************************************************/
#define SYSFS_GPIO_VAL          "/sys/devices/platform/dtsleds/leds/led1/brightness"
#define SYSFS_GPIO_VAL_H        "1"
#define SYSFS_GPIO_VAL_L        "0"

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd;
	char *filename;
    int cnt_delay_us = 1000000;

    cnt_delay_us = atoi(argv[1]);	/* 延时时间 */
    if(cnt_delay_us < 10000 || cnt_delay_us > 100000000)
        cnt_delay_us = 1000000;
	
    
    //设置端口高低电平
    fd = open(SYSFS_GPIO_VAL, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 value open failed!\n");
        return EXIT_FAILURE;
    }
    while(1){
        printf("toggle the pin value.\n");
        write(fd, SYSFS_GPIO_VAL_H, sizeof(SYSFS_GPIO_VAL_H));
        usleep(cnt_delay_us);
        write(fd, SYSFS_GPIO_VAL_L, sizeof(SYSFS_GPIO_VAL_L));
        usleep(cnt_delay_us);
    }
    
	close(fd);	/* 关闭文件 */	
	return 0;
}

参考内容

原文链接:https://blog.csdn.net/eurphan_y/article/details/104328210

原文链接:https://blog.csdn.net/dragon101788/article/details/79446356

版权声明:本文为CSDN博主「宁静致远2021」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_46577050/article/details/122578690

1 在用户空间配置并操作GPIO的必要性

有时,为了快速方便的测试IO口,我们可以将GPIO暴露给用户空间,直接在用户空间配置并操作GPIO,前提是没有驱动使用这个GPIO。

2 IMX6UL处理器GPIO编号算法

GPIO计算公式:
        id = (m - 1) * 32 + n
例如:
有gpio3.13,则m = 3,n = 13,则id = (3 - 1) * 32 + 13 = 77
这是IMX6UL GPIO的编号算法,理论上所有IMX6处理其都是这样的编号算法。

3 Linux下用文件IO的方式操作GPIO

通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction和value等,direction控制GPIO方向(是控制输出还是输入),而value可控制GPIO输出或获得GPIO输入(通过它设置引脚的值是写入1或0)。文件IO方式操作GPIO,使用到了4个函数open、close、read、write。

首先,看看系统中有没有“/sys/class/gpio”这个文件夹。如果没有请在编译内核的时候加入 Device Drivers-> GPIO Support ->/sys/class/gpio/… (sysfs interface)。

/sys/class/gpio 的使用说明

编程步骤:

◇ 控制GPIO的目录位于/sys/class/gpio

◇ /sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号

◇ /sys/class/gpio/unexport 用于通知系统取消导出

◇ /sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息,包括每个寄存器控制引脚的起始编号base,寄存器名称,引脚总数 导出一个引脚的操作步骤

◇ 首先计算此引脚编号,引脚编号 = 控制引脚的寄存器基数 + 控制引脚寄存器位数(例如GPIO4_21的引脚编号计算:首先查你使用的开发板的用户手册我的是i.MX 6UltraLite Applications Processor Reference Manual。在GPIO那章节中找到GPIO1_0与/sys/class/gpio/gpiochipX的对应关系,我的是GPIO1_0对应/sys/class/gpio/gpiochip0,所以GPIO4_21对应/sys/class/gpio/gpiochip96,即GPIO4的基数是96,GPIO4_21=96+21=117引脚编号)

◇ 向/sys/class/gpio/export写入此编号,比如GPIO4_21号引脚,在shell中可以通过以下命令实现,命令成功后生成/sys/class/gpio/gpio117目录,如果没有出现相应的目录,说明此引脚不可导出

◇ direction文件,定义输入输入方向,可以通过下面命令定义为输出。direction接受的参数:in, out, high, low。high/low同时设置方向为输出,并将value设置为相应的1/0

◇ value文件是端口的数值,为1或0

几个例子:

操作GPIO4_21引脚

1. 导出

/sys/class/gpio# echo 117 > export

2. 设置方向

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

3. 查看方向

/sys/class/gpio/gpio117# cat direction
out

4. 设置输出

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

5. 查看输出值

/sys/class/gpio/gpio117# cat value 
1

6. 取消导出

/sys/class/gpio# echo 117 > unexport

示例代码

在这里插入图片描述

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
/***************************************************************
Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
文件名		: gpio_op.c
作者	  	: ***
版本	   	: V1.0
描述	   	: gpio测试APP。
其他	   	: ./gpio_op 1000000
使用方法	 :直接操作GPIO1_26
论坛 	   	: www.openedv.com
日志	   	: 初版V1.0 2022/01/19 ***创建
***************************************************************/
#define SYSFS_GPIO_EXPORT       "/sys/class/gpio/export"
#define SYSFS_GPIO_PIN_NUM      "26"
#define SYSFS_GPIO_DIR          "/sys/class/gpio/gpio26/direction"
#define SYSFS_GPIO_DIR_VAL      "out"
#define SYSFS_GPIO_VAL          "/sys/class/gpio/gpio26/value"
#define SYSFS_GPIO_VAL_H        "1"
#define SYSFS_GPIO_VAL_L        "0"

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd;
	char *filename;
    int cnt_delay_us = 1000000;

    cnt_delay_us = atoi(argv[1]);	/* 延时时间 */
    if(cnt_delay_us < 10000 || cnt_delay_us > 100000000)
        cnt_delay_us = 1000000;
	
    //打开端口 /sys/class/gpio#  echo 26 > export
    fd  = open(SYSFS_GPIO_EXPORT, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 open failed!\n");
        return EXIT_FAILURE;
    }

    write(fd, SYSFS_GPIO_PIN_NUM, sizeof(SYSFS_GPIO_PIN_NUM));
    close(fd);

    //设置端口方向 /sys/class/gpio/gpio26# echo out > direction
    fd = open(SYSFS_GPIO_DIR, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 direction open failed!\n");
        return EXIT_FAILURE;
    }
    
    write(fd, SYSFS_GPIO_DIR_VAL, sizeof(SYSFS_GPIO_DIR_VAL));
    close(fd);

    //设置端口高低电平
    fd = open(SYSFS_GPIO_VAL, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 value open failed!\n");
        return EXIT_FAILURE;
    }
    while(1){
        printf("toggle the pin value.\n");
        write(fd, SYSFS_GPIO_VAL_H, sizeof(SYSFS_GPIO_VAL_H));
        usleep(cnt_delay_us);
        write(fd, SYSFS_GPIO_VAL_L, sizeof(SYSFS_GPIO_VAL_L));
        usleep(cnt_delay_us);
    }
    
	close(fd);	/* 关闭文件 */	
	return 0;
}

设备树模式下操作

在这里插入图片描述

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
/***************************************************************
Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
文件名		: gpio_op.c
作者	  	: ***
版本	   	: V1.0
描述	   	: gpio测试APP。
其他	   	: ./gpio_op 1000000
使用方法	 :直接操作GPIO1_26
论坛 	   	: www.openedv.com
日志	   	: 初版V1.0 2022/01/19 ***创建
***************************************************************/
#define SYSFS_GPIO_VAL          "/sys/devices/platform/dtsleds/leds/led1/brightness"
#define SYSFS_GPIO_VAL_H        "1"
#define SYSFS_GPIO_VAL_L        "0"

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd;
	char *filename;
    int cnt_delay_us = 1000000;

    cnt_delay_us = atoi(argv[1]);	/* 延时时间 */
    if(cnt_delay_us < 10000 || cnt_delay_us > 100000000)
        cnt_delay_us = 1000000;
	
    
    //设置端口高低电平
    fd = open(SYSFS_GPIO_VAL, O_WRONLY);
    if(-1 == fd){
        printf("ERR: gpio1_26 value open failed!\n");
        return EXIT_FAILURE;
    }
    while(1){
        printf("toggle the pin value.\n");
        write(fd, SYSFS_GPIO_VAL_H, sizeof(SYSFS_GPIO_VAL_H));
        usleep(cnt_delay_us);
        write(fd, SYSFS_GPIO_VAL_L, sizeof(SYSFS_GPIO_VAL_L));
        usleep(cnt_delay_us);
    }
    
	close(fd);	/* 关闭文件 */	
	return 0;
}

参考内容

原文链接:https://blog.csdn.net/eurphan_y/article/details/104328210

原文链接:https://blog.csdn.net/dragon101788/article/details/79446356

版权声明:本文为CSDN博主「宁静致远2021」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_46577050/article/details/122578690

生成海报
点赞 0

宁静致远2021

我还没有学会写个人说明!

暂无评论

相关推荐

CMSIS/FreeRTOS中队列(Queue)的使用

在有流式数据处理的嵌入式系统中,队列(Queue)是几乎必然被使用的工具,但大多数开发板提供的FreeRTOS例程是不包含队列的,要使用还要自己研究。这次我的样例把串口收到的数据按字节塞入队列&#xff

一篇博客实现嵌入式入门

前言 今天是参加百问网7天智能家居项目的第二天,感谢黄老师的教学。通过今天的学习,再次温习了之前学习的基础知识,同时对MCU也有了新的理解和学习的方法。 最小系统和C语言 最小系统 单片机的最小系