Level11day8 中断编程2-作业

按键驱动之获取按键数据实验

1. 驱动代码

//---------------驱动代码------------------
//key_drv.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/input-event-codes.h>
#include <asm/io.h>

#define GPX1CON_ADDR 0x11000c20
#define GPX1_SIZE 8

//设计一个全局设备对象--描述按键信息
struct key_irq_str{
	int irqno;
	unsigned int dev_major;
	unsigned int dev_minor;
	struct class* pkey_class;	
	struct device* pkey_device;
	void* reg_virt_base;
};
struct key_irq_str *pkey_irq_str;

//设计一个描述按键的数据的对象
struct key_value_dsc{
	int code;		//表示按键的类型:home, esc, Q, W, E, R, T...
	int date;		//表示按下还是抬起 1/0
};
struct key_value_dsc key3_value_dsc = {
	.code = 0,
	.date = 0,
};

irqreturn_t key_irq_handler(int irqno, void *devid)
{
	unsigned int GPX1DAT;
	
	printk("--------------%s------------------\n",__FUNCTION__);
	
	GPX1DAT = readl(pkey_irq_str->reg_virt_base + 4);
	
	if( GPX1DAT &= 0x1<<2 ){
		printk("__KERN__:Enter up\n");	 
		key3_value_dsc.code = KEY_ENTER;
		key3_value_dsc.date = 0;
	}
	else{
		printk("__KERN__:Enter down\n");
		key3_value_dsc.code = KEY_ENTER;
		key3_value_dsc.date = 1;
	}
		
	
	return IRQ_HANDLED;
}

int key_drv_open (struct inode* inode, struct file *filp){
	printk("__KERN__:open\n");
	return 0;
}

ssize_t key_drv_read (struct file *filp, char __user *buf, size_t count, loff_t *fpos){
	int ret;

	ret = copy_to_user(buf, &key3_value_dsc, count);

	if(ret == 0)
	{
		//printk("__KERN__:read\n");
	}
	else
	{
		printk("__KERN__:Err to read\n");
	}
	
	key3_value_dsc.code = 0;
	key3_value_dsc.date = 0;

	
	return 0;
}

ssize_t key_drv_write (struct file *filp, const char __user *buf, size_t count, loff_t *fpos){
	

	return 0;
}


int key_drv_release (struct inode *inod, struct file *filp){
	printk("__KERN__:release\n");
	return 0;
}

const struct file_operations key_drv_fops = {
	.open = key_drv_open,
	.read = key_drv_read,
	.write = key_drv_write,
	.release = key_drv_release,
};

int get_irqno_from_node(void)
{
	//获取到设备树中的节点

	struct device_node *np = of_find_node_by_path("/key_int_node");
	
	if(np){
		printk("find node ok\n");
	}
	else{
		printk("find node failed\n");
	}
	//通过节点去获取到中断号码
	pkey_irq_str->irqno = irq_of_parse_and_map(np,0);
	
	printk("irqno = %d\n",pkey_irq_str->irqno);

	return pkey_irq_str->irqno;
}

static int __init key_drv_init(void)
{
	
	int ret;
	
	//1.设定一个全局的设备对象
	pkey_irq_str = kmalloc(sizeof(struct key_irq_str), 1);

	//2.申请主设备号,设置次设备号
	pkey_irq_str->dev_major = register_chrdev(0, "key_drv_test", &key_drv_fops);

	pkey_irq_str->dev_minor = 0;

	//3.创建设备节点
	pkey_irq_str->pkey_class = class_create(THIS_MODULE, "key_interrupt_dev");

	pkey_irq_str->pkey_device = device_create(pkey_irq_str->pkey_class,NULL,MKDEV(pkey_irq_str->dev_major, pkey_irq_str->dev_minor),NULL,"INTRRUPT KEY%d",3);
	
	//4.硬件初始化--地址映射或中断申请
	
	ret = get_irqno_from_node();

	ret = request_irq(pkey_irq_str->irqno, key_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "IRQ_EINT10", NULL);

	if(ret != 0)
	{
		printk("request_irq ERROR!\n");
		return ret;
	}

	pkey_irq_str->reg_virt_base = ioremap(GPX1CON_ADDR, GPX1_SIZE);
	
	return 0;
}

static void __exit key_drv_exit(void)
{
	iounmap(pkey_irq_str->reg_virt_base);
	free_irq(pkey_irq_str->irqno,NULL);
	device_destroy(pkey_irq_str->pkey_class, MKDEV(pkey_irq_str->dev_major, pkey_irq_str->dev_minor));
	class_destroy(pkey_irq_str->pkey_class);
	unregister_chrdev(pkey_irq_str->dev_major, "key_drv_test");
	kfree(pkey_irq_str);
}


module_init(key_drv_init);
module_exit(key_drv_exit);
MODULE_LICENSE("GPL");


2. APP代码

//---------------APP代码------------------
//key_test.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define KEY_ENTER 28

struct key_value_dsc{
	int code;		//表示按键的类型:home, esc, Q, W, E, R, T...
	int date;		//表示按下还是抬起 1/0
};

int main(int argc, char **argv)
{
	int ret, fd;
	struct key_value_dsc key_value;
	fd = open("/dev/INTRRUPT KEY3",O_RDWR);
	
	
	if(fd < 0){
		perror("open:");
		exit(1);
	}

	while(1)
	{
		ret = read(fd, &key_value, sizeof(struct key_value_dsc));
		if(ret < 0){
			perror("read:");
			exit(1);
		}
		if(key_value.code == KEY_ENTER)
		{
			if(key_value.date == 1)
			{
				printf("__APP__key enter pressed\n",key_value.date);
			}
			else if(key_value.date == 0) 
			{
				printf("__APP__key enter up\n",key_value.date);
			}
		}
	}
	close(fd);
	return 0;
}

3. Makefile代码

#---------------Makefile代码------------------
ROOTFS_DIR = /home/linux/Level11

APP_NAME = key_test

CROSS_COMPILE = arm-none-linux-gnueabi-
CC = $(CROSS_COMPILE)gcc

ifeq ($(KERNELRELEASE), )

KERNEL_DIR = /home/linux/Level10/day6/linux-5.4.79
CUR_DIR = $(shell pwd)

all :
		make -C  $(KERNEL_DIR) M=$(CUR_DIR) modules -j6 ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
		$(CC) $(APP_NAME).c -o $(APP_NAME)

clean :
		make -C  $(KERNEL_DIR) M=$(CUR_DIR) clean
		clear

install:
		cp -raf  *.ko  $(APP_NAME)   ./1/
		
else
obj-m += key_drv.o

endif

4. 效果展示

[root@farsight ]# insmod key_drv.ko
[   25.439098] key_drv: loading out-of-tree module taints kernel.
[   25.445184] find node ok
[   25.446293] irqno = 107
[root@farsight ]# ./key_test
[   34.467473] __KERN__:open
[   37.399903] --------------key_irq_handler------------------
[   37.404006] __KERN__:Enter down
__APP__key enter pressed
[   37.608308] --------------key_irq_handler------------------
[   37.612413] __KERN__:Enter up
__APP__key enter up
[   48.758372] --------------key_irq_handler------------------
[   48.762471] __KERN__:Enter down
__APP__key enter pressed
[   48.962295] --------------key_irq_handler------------------
[   48.966389] __KERN__:Enter up
__APP__key enter up
[   50.628633] --------------key_irq_handler------------------
[   50.632733] __KERN__:Enter down
__APP__key enter pressed
[   50.841618] --------------key_irq_handler------------------
[   50.845714] __KERN__:Enter up
__APP__key enter up
[   51.770571] --------------key_irq_handler------------------
[   51.774665] __KERN__:Enter down
__APP__key enter pressed
[   51.989095] --------------key_irq_handler------------------
[   51.993193] __KERN__:Enter up
__APP__key enter up
[   52.798415] --------------key_irq_handler------------------
[   52.802516] __KERN__:Enter down
__APP__key enter pressed
[   52.986474] --------------key_irq_handler------------------
[   52.990567] __KERN__:Enter up
__APP__key enter up
[   54.331778] __KERN__:release