当前位置: 首页 > article >正文

1.Linux按键驱动

1.首先查看原理图,确定按键的引脚

在这里插入图片描述
由上图可知,两个按键的引脚分别为:

GPIO5 1
GPIO4 14

2.修改设备树,在设备树中添加节点。
2.1进入设备树目录

/home/book/100ask_imx6ull-sdk/Linux-4.9.88/arch/arm/boot/dts

2.2打开设备树文件:

100ask_imx6ull-14x14.dts

2.3添加节点:

test_gpio_keys_100ask {
		compatible = "test_100ask,test_gpio_key";
		gpios = <&gpio5 1 GPIO_ACTIVE_HIGH
				 &gpio4 14 GPIO_ACTIVE_HIGH>;

		pinctrl-names = "default";
	    pinctrl-0 = <&key1_100ask &key2_100ask>;
	
	}; 

2.4编译设备树
在这里插入图片描述
2.5拷贝设备树文件到nfs目录下

cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb  ~/nfs_rootfs/

2.6更新设备树

cp /mnt/100ask_imx6ull-14x14.dtb /boot/

在这里插入图片描述
3.代码
3.1完整代码

#include <linux/irqreturn.h>
#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>




/* 定义结构体用来存储节点信息 */
struct gpio_key{
	int gpio;
	struct gpio_desc* gpiod;
	int flag;
	int irq;
};

/* 定义全局变量来存储gpio信息 */
static struct gpio_key *gpio_keys_100ask;


static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
	struct gpio_key *gpio_key =dev_id;
	int val;

	val = gpiod_get_value(gpio_key->gpiod);
	printk("key %d %d\n", gpio_key->gpio, val);
	return IRQ_HANDLED;
}


static int gpio_key_probe(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	int count;
	int i;
	enum of_gpio_flags flag;
	int err;
	count = of_gpio_count(node);
	if (!count)
	{
		printk("%s %s line %d, there isn't any gpio available\n", __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}

	/* 分配存储按键的信息 */
	gpio_keys_100ask = kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL);
	if (!gpio_keys_100ask)
	{
		printk("kzalloc error\n");
		return -1;
	}

	/* 初始化按键 */
	for(i = 0; i < count; i++)
	{
		gpio_keys_100ask[i].gpio = of_get_gpio_flags(node, i, &flag);
		if (gpio_keys_100ask[i].gpio < 0)
		{
			printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__);
			return -1;
		}
		gpio_keys_100ask[i].gpiod = gpio_to_desc(gpio_keys_100ask[i].gpio);
		gpio_keys_100ask[i].irq = gpio_to_irq(gpio_keys_100ask[i].gpio);

		/* 申请中断 */
		err = request_irq(gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "test_gpio_keys_100ask", &gpio_keys_100ask[i]);
		if (err) 
		{
			printk("request_irq err\n");
		}
	}
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}
static int gpio_key_remove(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	int count;
	int i;
	count = of_gpio_count(node);
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	for(i = 0; i < count; i++)
	{
		free_irq(gpio_keys_100ask[i].irq, &gpio_keys_100ask[i]);
	}
	kfree(gpio_keys_100ask);
	return 0;
}

static const struct of_device_id gpio_key_of_match[] = {
	{ .compatible = "test_100ask,test_gpio_key" },
	{ /* sentinel */ }
};

static struct platform_driver gpio_key_driver = {
	.driver = {
		.name	= "test_gpio_keys_100ask",
		.of_match_table = gpio_key_of_match,
	},
	.probe		= gpio_key_probe,
	.remove		= gpio_key_remove,
};


static int __init gpio_key_init(void)
{
	int ret;
	ret = platform_driver_register(&gpio_key_driver);
	if (ret)
	{
		printk("platform_driver_register err\n");
		return -1;
	}
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static void __exit gpio_key_exit(void)
{
	platform_driver_unregister(&gpio_key_driver);
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
}

module_init(gpio_key_init);
module_exit(gpio_key_exit);
MODULE_LICENSE("GPL");

3.2代码流程:

使用Source Insight 工具查看代码,从下往上看。

3.2.1 定义驱动程序的入口和出口函数。
在这里插入图片描述
3.2.2在入口和出口函数中,分别注册和释放platform_driver结构体
在这里插入图片描述
3.2.3填充这个结构体
在这里插入图片描述
注意:通过gpio_key_of_match中的compatible属性和设备树中进行比较,当二者相同时,probe函数被调用。
在这里插入图片描述

3.2.4当二者匹配时,probe函数读取设备树的信息,得到GPIO信息,将得到的信息存储到结构体中。
在这里插入图片描述
在这里插入图片描述

3.2.5当按下按键时,中断处理函数被调用,打印信息。
在这里插入图片描述

4.Makefile文件



KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88

all:
	make -C $(KERN_DIR) M=`pwd` modules

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m += gpio_key_drv_test.o

5.实验流程:
5.1 2.6更新完设备树后,重启开发板
在这里插入图片描述

在这里插入图片描述
5.2按下按键后,屏幕有反应,表示成功了
在这里插入图片描述
6.重新修改的代码(逻辑一样,只是再次实现了一下)
dts

test1_gpio_keys_100ask{
		compatible ="test1_100ask,test1_gpio_key";
		gpios = <&gpio5 1 GPIO_ACTIVE_HIGH
				&gpio4 14 GPIO_ACTIVE_HIGH>;
    };

Makefile



KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88

all:
	make -C $(KERN_DIR) M=`pwd` modules
clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
obj-m += test.o

code

#include <linux/irqreturn.h>
#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>



/* 定义结构体来描述gpio */
struct gpio_key{
	int gpio;
	struct gpio_desc* gpiod;
	int flag;
	int irq;
};

/* 定义全局变量来存储设备树中的所有gpio节点信息 */
static struct gpio_key* gpio_keys_100ask;




static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
	struct gpio_key* gpio_key = dev_id;
	int val;

	val = gpio_get_value(gpio_key->gpio);
	printk("key %d %d\n", gpio_key->gpio, val);
	return IRQ_HANDLED;
}


static int gpio_probe(struct platform_device *pdev)
{
	int count, i;
	struct device_node *node;
	int err;

	node = pdev->dev.of_node;
	count = of_gpio_count(node);
	if (!count)
	{
		printk("%s %s line %d, there isn't any gpio available\n", __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}
	
	/* 申请资源 */
	gpio_keys_100ask = kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL);
	if (!gpio_keys_100ask)
	{
		printk("kzalloc error\n");
		return -1;
	}

	/* 获得资源 */
	for (i = 0; i < count; i++)
	{
		gpio_keys_100ask[i].gpio = of_get_gpio(node, i);
		if (gpio_keys_100ask[i].gpio < 0)
		{
			printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__);
			return -1;
		}
		gpio_keys_100ask[i].gpiod = gpio_to_desc(gpio_keys_100ask[i].gpio);
		gpio_keys_100ask[i].irq   = gpio_to_irq(gpio_keys_100ask[i].gpio);

		/* 申请中断 */
		err = request_irq(gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "test1_gpio_keys_100ask", &gpio_keys_100ask[i]);
		if (err) 
		{
			printk("request_irq err\n");
		}
	}
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static int gpio_remove(struct platform_device *pdev)
{
	int count, i;
	struct device_node *node;

	node = pdev->dev.of_node;
	count = of_gpio_count(node);

	for (i = 0; i < count; i++)
	{
		free_irq(gpio_keys_100ask[i].irq, &gpio_keys_100ask[i]);
	}
	kfree(gpio_keys_100ask);
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}


/*
* 在设备树中添加的节点的compatible属性为:"test1_100ask,test1_gpio_key"
*/
static const struct of_device_id gpio_key_of_match[] = {
	{.compatible = "test1_100ask,test1_gpio_key"},
	{/* 这里必须要有一个空项,表示结束 */}
};

static struct platform_driver gpio_driver = {
	.driver = {
		.name	= "test1_gpio_keys_100ask",
		.of_match_table = gpio_key_of_match,
	},
	.probe	= gpio_probe,
	.remove = gpio_remove,
};



/* 基于platform总线来实现这个程序 */
static int gpio_init(void)  
{
	int ret;

	ret = platform_driver_register(&gpio_driver);
	if (ret != 0)
	{
		printk("platform_driver_register err\n");
		return -1;
	}
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return ret;
}

static void gpio_exit(void)
{
	platform_driver_unregister(&gpio_driver);
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
}

module_init(gpio_init);
module_exit(gpio_exit);

MODULE_LICENSE("GPL");





7.文件路径

/home/book/nfs_rootfs/CSDN/01_gpio_irq/01_simple

http://www.kler.cn/news/366778.html

相关文章:

  • linux softirq tasklet 软中断实现
  • uniapp 引入了uview-ui后,打包错误,主包过大解决方案
  • 深入剖析 C 与 C++ 动态内存管理之术
  • C#之Aes加密解密
  • 61 mysql 存储引擎之动态格式 MyISAM
  • 【移动应用开发】界面设计(二)实现水果列表页面
  • AI绘画开源王者归来,Stable Diffusion 3.5 AI绘画模型开源发布
  • 矩阵的可解性:关于Ax=b的研究
  • WeakHashMap详解
  • 谷粒商城の订单服务分布式事务
  • 京准电钟:NTP网络授时服务器应用航管自控系统
  • jmeter学习(6)逻辑控制器
  • 【Python】Whoosh:全流程自建搜索引擎
  • 【c++篇】:从基础到实践--c++内存管理技巧与模版编程基础
  • 免费的CMS系统有哪些?
  • windows中的tracert命令
  • Apache Paimon介绍
  • IIS下FTP服务器的PASV端口范围修改方法
  • SQL 干货 | SQL 反连接
  • OBOO鸥柏丨液晶拼接大屏分布式基本管理系统架构显示技术曝光
  • python subproces模块
  • 安装Maven配置以及构建Maven项目(2023idea)
  • 智慧铜矿厂综合管控平台 | 图扑软件
  • 计算机网络的主要知识点小结
  • 反向代理服务器---NGINX
  • 【PHP】在ThinkPHP6中Swoole与FPM的简单性能测试对比