1.首先查看原理图,确定按键的引脚
由上图可知,两个按键的引脚分别为:
c
GPIO5 1
GPIO4 14
2.修改设备树,在设备树中添加节点。
2.1进入设备树目录
c
/home/book/100ask_imx6ull-sdk/Linux-4.9.88/arch/arm/boot/dts
2.2打开设备树文件:
c
100ask_imx6ull-14x14.dts
2.3添加节点:
c
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目录下
c
cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/
2.6更新设备树
c
cp /mnt/100ask_imx6ull-14x14.dtb /boot/
3.代码
3.1完整代码
c
#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代码流程:
c
使用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文件
c
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
c
test1_gpio_keys_100ask{
compatible ="test1_100ask,test1_gpio_key";
gpios = <&gpio5 1 GPIO_ACTIVE_HIGH
&gpio4 14 GPIO_ACTIVE_HIGH>;
};
Makefile
c
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
c
#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.文件路径
c
/home/book/nfs_rootfs/CSDN/01_gpio_irq/01_simple