嵌入式内核及驱动开发-06 中断编程,阻塞,非阻塞,多路复用select和poll,中断下半部,tasklet和work

实现文件IO模型之一阻塞,等同于休眠

文件io模型:
		1,非阻塞
		2,阻塞
		3,多路复用--select/poll
		4, 异步信号通知faync

阻塞: 当进程在读取外部设备的资源(数据),资源没有准备好,进程就会休眠
	linux应用中,大部分的函数接口都是阻塞
		scanf();
		read();
		write();
		accept();
驱动中需要调用
	1,将当前进程加入到等待队列头中
		add_wait_queue(wait_queue_head_t * q, wait_queue_t * wait)
	2,将当前进程状态设置成(可接受中断信号)TASK_INTERRUPTIBLE
		set_current_state(TASK_INTERRUPTIBLE)
	3,让出调度--休眠
		schedule(void)

更加智能的接口,等同于上面的三个接口:
	wait_event_interruptible(wq, condition)

驱动如何去写代码
	1,等待队列头
			wait_queue_head_t

			init_waitqueue_head(wait_queue_head_t *q);
	
	2,在需要等待(没有数据)的时候,进行休眠
		wait_event_interruptible(wait_queue_head_t wq, condition) // 内部会构建一个等待队列项/节点wait_queue_t
		参数1: 等待队列头
		参数2: 条件,如果是为假,就会等待,如果为真,就不会等待
				可以用一标志位,来表示是否有数据


	3,在一个合适的时候(有数据),会将进程唤醒
		wake_up_interruptible(wait_queue_head_t *q)

		用法:
			wake_up_interruptible(&key_dev->wq_head);
			//同时设置标志位
			key_dev->key_state  = 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/kdev_t.h>
#include <linux/err.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>


#define KEY_ENTER 28 
#define GPX1_CON 0X11000C20


//设计一个描述按键的数据对象

typedef struct _key_event 
{
	int code;	//表示按键的类型:home, ESC,Q,W,E,R,T, ENTER
	int value;  //表示按下还是抬起 1/0
}KEY_EVENT_T;

//设计一个类型,描述一个设备的信息
typedef struct _key_desc
{
	unsigned int dev_major; 	//主设备号
	struct class *cls;
	struct device *dev; 		//创建设备文件
	int irqno; 					//中断号
	void *reg_base;
	KEY_EVENT_T event;		 	//按键对象
	wait_queue_func_t wq_head;	//等待队列头
	int key_state;				//按键是否有数据
	
}KEY_DESC_T;

KEY_DESC_T *key_dev; 


irqreturn_t key_irq_handler(int irqno, void *devid)
{
	int value;		
	//printk("-------%s-------------\n", __FUNCTION__);

	//读取按键状态
	value = readl(key_dev->reg_base + 4) & (0x01 << 2);
	if(value)
	{
		printk("key up\n");
		key_dev->event.code = KEY_ENTER;
		key_dev->event.value = 0;
	}
	else
	{
		printk("key pressed\n");
		key_dev->event.code = KEY_ENTER;
		key_dev->event.value = 1;
	}

	//有数据,唤醒等待队列,同时设置标志位
	wake_up_interruptible(&key_dev->wq_head);
	key_dev->key_state = 1;
	
	return IRQ_HANDLED;

}




int get_irqno_from_node(void)
{
	// 获取到设备树中到节点
	int irqno;
	
	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");
	}

	// 通过节点去获取到中断号码
	//0表示拿第0个,我们只定义了1个,所以填0
	irqno  = irq_of_parse_and_map(np,0);
	
	printk("irqno = %d\n", irqno);
	

	return irqno;
}

ssize_t key_drv_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
	int ret;
	//printk("--------------%s-------------------\n",__FUNCTION__);

	//在需要等待(没有数据)的时候,进行休眠
	wait_event_interruptible(key_dev->wq_head, key_dev->key_state);
	
	ret = copy_to_user(buf, &key_dev->event, count);

	if(ret > 0)
	{
		printk(KERN_ERR "copy_to_usr error\n");
		return -EFAULT;
	}

	//数据传递给用户之后,需要将数据清除
	memset(&key_dev->event, 0, sizeof(key_dev->event));
	key_dev->key_state = 0;
	
	return count;
}

ssize_t key_drv_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
	printk("-------%s-------------\n", __FUNCTION__);
	return 0;

}


int key_drv_open (struct inode *inode, struct file *filep)
{
	printk("--------------%s-------------------\n",__FUNCTION__);


	return 0;
}

int key_drv_close (struct inode *inode, struct file *filep)
{
	printk("--------------%s-------------------\n",__FUNCTION__);

	return 0;
}


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


static int __init key_drv_init(void)
{
	int ret;
	
	//step1. 实例化全局设备对象---分配空间
	key_dev = kzalloc(sizeof(KEY_DESC_T),  GFP_KERNEL);//kzalloc()等价于kmalloc()之后,再把空间清零

	if(NULL == key_dev)
	{
		printk(KERN_ERR "malloc error\n");
		return -ENOMEM;
	}
	
	//step2. 动态申请主设备号
	key_dev->dev_major = register_chrdev(0,"key_dev_test", &key_fops);

	if(key_dev->dev_major < 0)
	{
		printk(KERN_ERR "malloc error\n");
		ret = -ENOMEM;
		goto err_0;
	}
	else
	{
		printk("register_chrdev ok\n");
	}
	
	//3.自动创建设备节点文件

	//3.1 创建类
	key_dev->cls = class_create(THIS_MODULE, "key_cls");

	if(IS_ERR(key_dev->cls))
	{
		printk(KERN_ERR "class_create error\n");
		ret = PTR_ERR(key_dev->cls);
		goto err_1;
		
	}
	
	//3.1 创建节点
	key_dev->dev = device_create(key_dev->cls, NULL, MKDEV(key_dev->dev_major, 3), NULL, "key%d",3);

	if(IS_ERR(key_dev->dev))
	{
		printk(KERN_ERR "device_create error\n");
		ret = PTR_ERR(key_dev->dev);
		goto err_2;
	}
	
	//4.硬件初始化---地址映射或中断申请
	//映射中断对应的DATA寄存器,用以获取按键状态

	key_dev->reg_base = ioremap(GPX1_CON, 8);

	if(key_dev->reg_base == NULL)
	{
		printk(KERN_ERR "ioremap error\n");
		ret = -ENOMEM;
		goto err_3;
	}
	else
	{
		printk("ioremap ok\n");
	}
		
	// 初始化等待队列头
	init_waitqueue_head(&key_dev->wq_head);
	
	key_dev->irqno = get_irqno_from_node();
	ret = request_irq(key_dev->irqno,key_irq_handler,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING,"key3_eint10", NULL);

	if(ret != 0)
	{
		printk("request_irq error\n");
		goto err_4;
	}

	return 0;

err_4:
	free_irq(key_dev->irqno, NULL);

err_3:
	device_destroy(key_dev->cls, MKDEV(key_dev->dev_major, 3));

err_2:
	class_destroy(key_dev->cls);

err_1:
	unregister_chrdev(key_dev->dev_major, "key_dev_test");

err_0:
	kfree(key_dev);

	return ret;	
	
}


static void __exit key_drv_exit(void)
{
	//释放中断
	free_irq(key_dev->irqno, NULL);

	//销毁类和节点
	device_destroy(key_dev->cls, MKDEV(key_dev->dev_major, 3));
	class_destroy(key_dev->cls);

	//释放主设备号
	unregister_chrdev(key_dev->dev_major, "key_dev_test");

	//释放动态内存
	kfree(key_dev);
}


module_init(key_drv_init);
module_exit(key_drv_exit);

MODULE_LICENSE("GPL");

应用程序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>

typedef struct _key_event{
	int code; // 表示按键的类型:  home, esc, Q,W,E,R,T, ENTER
	int value; // 表示按下还是抬起 1 / 0
}KEY_EVENT_T;

#define KEY_ENTER		28


int main(int argc, char* argv[])
{
	KEY_EVENT_T event;
	
	int fd = open("/dev/key3",O_RDWR);

	if(fd < 0)
	{
		perror("open");
		exit(1);
	}
	
	while(1)
	{
		read(fd, &event, sizeof(KEY_EVENT_T));
		if(event.code == KEY_ENTER)
		{
			if(event.value)
			{
				printf("APP__ key enter pressed\n");
			}
			else
			{
				printf("APP__ key enter up\n");
			}
		}
	}
}

终端信息

(1)执行insmod key_drv.ko ,加载成功
在这里插入图片描述

(2)执行 ./key_test

在这里插入图片描述
(3)执行 top 观察信息
在这里插入图片描述

非阻塞

在读写的时候,如果没有数据,立刻返回,并且返回一个出错码
用的会比较少,因为比较耗资源
在这里插入图片描述

用户空间:
open("/dev/key0", O_RDWR|O_NONBLOCK);
------------------------------------
内核空间:
驱动中需要去区分,当前模式是阻塞还是非阻塞
//如果当前是非阻塞模式,并且没有数据,立马返回一个出错码
ssize_t key_drv_read (struct file *filep, char __user *buf, size_t count, loff_t *foops)
{
	if(filp->f_flags & O_NONBLOCK && !key_dev->key_state)
		return -EAGAIN;
		。
		。
		。
}

例—非阻塞模式

驱动代码key_drv.c

应用程序 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>

typedef struct _key_event{
	int code; // 表示按键的类型:  home, esc, Q,W,E,R,T, ENTER
	int value; // 表示按下还是抬起 1 / 0
}KEY_EVENT_T;

#define KEY_ENTER		28


int main(int argc, char* argv[])
{
	KEY_EVENT_T event;

	//文件读取模式修改
	int fd = open("/dev/key3",O_RDWR | O_NONBLOCK );

	if(fd < 0)
	{
		perror("open");
		exit(1);
	}
	
	while(1)
	{
		read(fd, &event, sizeof(KEY_EVENT_T));
		if(event.code == KEY_ENTER)
		{
			if(event.value)
			{
				printf("APP__ key enter pressed\n");
			}
			else
			{
				printf("APP__ key enter up\n");
			}
		}
	}
}

终端信息

(1)执行insmod key_drv.ko ,加载成功
在这里插入图片描述
(2)执行 ./key_test
在这里插入图片描述
(3)执行 top 观察信息,CPU占用率高
在这里插入图片描述

例—多路复用 poll实现

应用程序 poll的应用

1, 需要打开多个文件(多个设备)

2, 利用poll来实现监控fd的读,写,出错
	#include <poll.h>

   int poll(struct pollfd *fds, nfds_t nfds, int timeout);
   参数1: 表示多个文件描述符集合
		struct pollfd描述的是文件描述符的信息
		struct pollfd {
           int   fd;  //文件描述符
           short events;   //希望监控fd的什么事件:读,写,出错
						POLLIN 读,
						POLLOUT 写,
						POLLERR出错
           short revents;    //结果描述,表示当前的fd是否有读,写,出错
						//用于判断,是内核自动赋值
						POLLIN 读,
						POLLOUT 写,
						POLLERR出错
		};
	参数2:被监控的fd的个数
	参数3: 监控的时间:
				正: 表示监控多少ms
				负数: 无限的时间去监控
				0: 等待0ms,类似于非阻赛
	返回值: 负数:出错
			大于0,表示fd中有数据
			等于0: 时间到

应用程序 key_test.c

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <strings.h>


typedef struct _key_event{
	int code; // 表示按键的类型:  home, esc, Q,W,E,R,T, ENTER
	int value; // 表示按下还是抬起 1 / 0
}KEY_EVENT_T;

#define KEY_ENTER		28


int main(int argc, char* argv[])
{
	KEY_EVENT_T event;
	struct pollfd pfd[2];//监控多个文件描述符fd
	int ret;
	char in_buf[128];
	
	
	int fd = open("/dev/key3",O_RDWR);

	if(fd < 0)
	{
		perror("open");
		exit(1);
	}

	pfd[0].fd = fd;//监控按键
	pfd[0].events = POLLIN;
	pfd[1].fd = 0;	//标准输入
	pfd[1].events = POLLIN;
	
	while(1)		
	{
		ret = poll(pfd, 2, -1);
		printf("ret = %d\n", ret);

		if(ret > 0)
		{
			//表示两个fd中,至少有一个发生了有数据可读
			if(pfd[0].revents & POLLIN)
			{
				//可以直接读数据
				read(fd, &event, sizeof(KEY_EVENT_T));
				
				if(event.code == KEY_ENTER)
				{
					if(event.value)
					{
						printf("APP__ key enter pressed\n");
					}
					else
					{
						printf("APP__ key enter up\n");
					}
				}
				
				bzero(&event,sizeof(event));			
			}

			if(pfd[1].revents & POLLIN)
			{
				fgets(in_buf, 128, stdin);
				printf("in_buf = %s",in_buf);
				
			}
		}
		else
		{
			perror("poll");
			exit(1);
		}
		
		
	}

	close(fd);
	
}

驱动代码 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/kdev_t.h>
#include <linux/err.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>




#define KEY_ENTER 28 
#define GPX1_CON 0X11000C20


//设计一个描述按键的数据对象

typedef struct _key_event 
{
	int code;	//表示按键的类型:home, ESC,Q,W,E,R,T, ENTER
	int value;  //表示按下还是抬起 1/0
}KEY_EVENT_T;

//设计一个类型,描述一个设备的信息
typedef struct _key_desc
{
	unsigned int dev_major; 	//主设备号
	struct class *cls;
	struct device *dev; 		//创建设备文件
	int irqno; 					//中断号
	void *reg_base;
	KEY_EVENT_T event;		 	//按键对象
	wait_queue_func_t wq_head;	//等待队列头
	int key_state;				//按键是否有数据
	
}KEY_DESC_T;

KEY_DESC_T *key_dev; 


irqreturn_t key_irq_handler(int irqno, void *devid)
{
	int value;		
	//printk("-------%s-------------\n", __FUNCTION__);

	//读取按键状态
	value = readl(key_dev->reg_base + 4) & (0x01 << 2);
	if(value)
	{
		printk("key up\n");
		key_dev->event.code = KEY_ENTER;
		key_dev->event.value = 0;
	}
	else
	{
		printk("key pressed\n");
		key_dev->event.code = KEY_ENTER;
		key_dev->event.value = 1;
	}

	//有数据,唤醒等待队列,同时设置标志位
	wake_up_interruptible(&key_dev->wq_head);
	key_dev->key_state = 1;
	
	return IRQ_HANDLED;

}




int get_irqno_from_node(void)
{
	// 获取到设备树中到节点
	int irqno;
	
	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");
	}

	// 通过节点去获取到中断号码
	//0表示拿第0个,我们只定义了1个,所以填0
	irqno  = irq_of_parse_and_map(np,0);
	
	printk("irqno = %d\n", irqno);
	

	return irqno;
}

ssize_t key_drv_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
	int ret;
	//printk("--------------%s-------------------\n",__FUNCTION__);

	//***********************/非阻塞模式判断/*****************************/
	if(filp->f_flags & O_NONBLOCK && !key_dev->key_state)
		return -EAGAIN;
	
	//在需要等待(没有数据)的时候,进行休眠
	wait_event_interruptible(key_dev->wq_head, key_dev->key_state);
	
	ret = copy_to_user(buf, &key_dev->event, count);

	if(ret > 0)
	{
		printk(KERN_ERR "copy_to_usr error\n");
		return -EFAULT;
	}

	//数据传递给用户之后,需要将数据清除
	memset(&key_dev->event, 0, sizeof(key_dev->event));
	key_dev->key_state = 0;
	
	return count;
}

ssize_t key_drv_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
	printk("-------%s-------------\n", __FUNCTION__);
	return 0;

}


int key_drv_open (struct inode *inode, struct file *filep)
{
	printk("--------------%s-------------------\n",__FUNCTION__);


	return 0;
}



int key_drv_close (struct inode *inode, struct file *filep)
{
	printk("--------------%s-------------------\n",__FUNCTION__);

	return 0;
}

unsigned int key_drv_poll (struct file *filp, struct poll_table_struct *pts)
{
	//返回一个mask值
	unsigned int mask;

	//调用poll_waiti,将当前的等待队列注册到系统中
	poll_wait(filp, &key_dev->wq_head, pts);
	
	//1.当没有数据时返回一个0
	if(!key_dev->key_state)
		mask = 0;

	//2.有数据时返回一个POLLIN
	if(key_dev->key_state)
		mask |= POLLIN;
	return mask;
}

const struct file_operations key_fops = {
	.read = key_drv_read,
	.write = key_drv_write,
	.open = key_drv_open,
	.release=key_drv_close,
	.poll = key_drv_poll,
};


static int __init key_drv_init(void)
{
	int ret;
	
	//step1. 实例化全局设备对象---分配空间
	key_dev = kzalloc(sizeof(KEY_DESC_T),  GFP_KERNEL);//kzalloc()等价于kmalloc()之后,再把空间清零

	if(NULL == key_dev)
	{
		printk(KERN_ERR "malloc error\n");
		return -ENOMEM;
	}
	
	//step2. 动态申请主设备号
	key_dev->dev_major = register_chrdev(0,"key_dev_test", &key_fops);

	if(key_dev->dev_major < 0)
	{
		printk(KERN_ERR "malloc error\n");
		ret = -ENOMEM;
		goto err_0;
	}
	else
	{
		printk("register_chrdev ok\n");
	}
	
	//3.自动创建设备节点文件

	//3.1 创建类
	key_dev->cls = class_create(THIS_MODULE, "key_cls");

	if(IS_ERR(key_dev->cls))
	{
		printk(KERN_ERR "class_create error\n");
		ret = PTR_ERR(key_dev->cls);
		goto err_1;
		
	}
	
	//3.1 创建节点
	key_dev->dev = device_create(key_dev->cls, NULL, MKDEV(key_dev->dev_major, 3), NULL, "key%d",3);

	if(IS_ERR(key_dev->dev))
	{
		printk(KERN_ERR "device_create error\n");
		ret = PTR_ERR(key_dev->dev);
		goto err_2;
	}
	
	//4.硬件初始化---地址映射或中断申请
	//映射中断对应的DATA寄存器,用以获取按键状态

	key_dev->reg_base = ioremap(GPX1_CON, 8);

	if(key_dev->reg_base == NULL)
	{
		printk(KERN_ERR "ioremap error\n");
		ret = -ENOMEM;
		goto err_3;
	}
	else
	{
		printk("ioremap ok\n");
	}
		
	// 初始化等待队列头
	init_waitqueue_head(&key_dev->wq_head);
	
	key_dev->irqno = get_irqno_from_node();
	ret = request_irq(key_dev->irqno,key_irq_handler,IRQF_TRIGGER_FALLING,"key3_eint10", NULL);

	if(ret != 0)
	{
		printk("request_irq error\n");
		goto err_4;
	}

	return 0;

err_4:
	free_irq(key_dev->irqno, NULL);

err_3:
	device_destroy(key_dev->cls, MKDEV(key_dev->dev_major, 3));

err_2:
	class_destroy(key_dev->cls);

err_1:
	unregister_chrdev(key_dev->dev_major, "key_dev_test");

err_0:
	kfree(key_dev);

	return ret;	
	
}


static void __exit key_drv_exit(void)
{
	//释放中断
	free_irq(key_dev->irqno, NULL);

	//销毁类和节点
	device_destroy(key_dev->cls, MKDEV(key_dev->dev_major, 3));
	class_destroy(key_dev->cls);

	//释放主设备号
	unregister_chrdev(key_dev->dev_major, "key_dev_test");

	//释放动态内存
	kfree(key_dev);
}


module_init(key_drv_init);
module_exit(key_drv_exit);

MODULE_LICENSE("GPL");

终端信息

(1)执行insmod key_drv.ko ,加载成功
在这里插入图片描述
(2)执行 ./key_test
在这里插入图片描述

异步信号通知

当有数据到时候,驱动会发送信号(SIGIO)给应用,就可以异步去读写数据,不用主动去读写

应用–处理信号,主要是读写数据

 void catch_signale(int signo)
	{
		if(signo == SIGIO)
		{
			printf("we got sigal SIGIO");
			// 读取数据
			read(fd, &event, sizeof(struct key_event));
			if(event.code == KEY_ENTER)
			{
				if(event.value)
				{
					printf("APP__ key enter pressed\n");
				}else
				{
					printf("APP__ key enter up\n");
				}
			}
		}

	}

	// 1,设置信号处理方法
	signal(SIGIO,catch_signale);
	// 2,将当前进程设置成SIGIO的属主进程
	fcntl(fd, F_SETOWN, getpid());

	// 3,将io模式设置成异步模式
	int flags  = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, flags | FASYNC );

驱动–发送信号

1,需要和进程进行关联--记录信号该发送给谁
		实现一个fasync的接口

		int key_drv_fasync(int fd, struct file *filp, int on)
		{
			//只需要调用一个函数记录信号该发送给谁
			return fasync_helper(fd, filp, on,  &key_dev->faysnc);

		}
	2,在某个特定的时候(有数据的时候)去发送信号
		//发送信号
		kill_fasync(&key_dev->faysnc, SIGIO, POLLIN);

例—中断异步信号通知

应用程序和驱动

见中断的下半部

终端信息

(1)执行insmod key_drv.ko ,加载成功
在这里插入图片描述
(2)执行 ./key_test
在这里插入图片描述

中断的下半部

下半部的由来

在这里插入图片描述
在这里插入图片描述

下半部实现

下半部实现方式— tasklet:
struct tasklet_struct
{
	struct tasklet_struct *next;
	unsigned long state;
	atomic_t count;
	void (*func)(unsigned long); // 下半部的实现逻辑
	unsigned long data; // 传递给func
};

a, 初始化(在驱动模块入口,即init中初始化,中断申请之前)
	struct tasklet_struct mytasklet;

	tasklet_init(struct tasklet_struct * t, void(* func)(unsigned long), unsigned long data)

	例子:
	void key_tasklet_half_irq(unsigned long data)
	{
		// 表示有数据,需要去唤醒整个进程/等待队列
		wake_up_interruptible(&key_dev->wq_head);
		//同时设置标志位
		key_dev->key_state  = 1;

		//发送信号
		kill_fasync(&key_dev->faysnc, SIGIO, POLLIN);
	}

	tasklet_init(&key_dev->mytasklet, key_tasklet_half_irq, 45);


b,在上半部中放入到内核线程中--启动
	// 启动下半步
	tasklet_schedule(&key_dev->mytasklet);

c,模块卸载的时候:
	tasklet_kill(&key_dev->mytasklet);
下半部实现方式— 工作队列和工作
typedef void (*work_func_t)(struct work_struct *work);

struct work_struct {
	atomic_long_t data;
	struct list_head entry;
	work_func_t func;
};

a, 初始化
	
	void work_irq_half(struct work_struct *work)
	{
		printk("-------%s-------------\n", __FUNCTION__);
		// 表示有数据,需要去唤醒整个进程/等待队列
		wake_up_interruptible(&key_dev->wq_head);
		//同时设置标志位
		key_dev->key_state  = 1;

		//发送信号
		kill_fasync(&key_dev->faysnc, SIGIO, POLLIN);
		
	}
	struct work_struct mywork;

	INIT_WORK(struct work_struct *work, work_func_t func);

b, 在上半部中放入到内核线程中--启动

	schedule_work(&key_dev->mywork);

例— 中断下半部实现方式(tasklet或work)

应用程序 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>
#include <poll.h>
 #include <signal.h>

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

#define KEY_ENTER		28

static int fd;
static struct key_event event;

 void catch_signale(int signo)
{
	if(signo == SIGIO)
	{
		printf("we got sigal SIGIO\n");
		// 读取数据
		read(fd, &event, sizeof(struct key_event));
		if(event.code == KEY_ENTER)
		{
			if(event.value)
			{
				printf("APP__ key enter pressed\n");
			}
			else
			{
				printf("APP__ key enter up\n");
			}
		}
	}

}

int main(int argc, char *argv[])
{
	int ret;
	
	fd = open("/dev/key0", O_RDWR);
	if(fd < 0)
	{
		perror("open");
		exit(1);
	}

	// 1,设置信号处理方法
	signal(SIGIO,catch_signale);
	// 2,将当前进程设置成SIGIO的属主进程
	fcntl(fd, F_SETOWN, getpid());

	// 3,将io模式设置成异步模式
	int flags  = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, flags | FASYNC );


	while(1)
	{
		// 可以做其他的事情
		printf("I am waiting......\n");
		sleep(1);
	}

	close(fd);


	return 0;

}
驱动代码 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/sched.h>
#include <linux/poll.h>

#include <asm/io.h>
#include <asm/uaccess.h>

#define USE_TASKLET 

#define GPXCON_REG  0x11000C20
#define KEY_ENTER		28

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

//设计一个全局设备对象--描述按键信息
typedef struct _key_desc{
	unsigned int dev_major;				//主设备号
	struct class *cls;
	struct device *dev;				    //创建设备文件
	int irqno;							//中断号
	void *reg_base;
	KEY_EVENT_T event;
	wait_queue_head_t  wq_head;			//等待队列头
	int key_state; //表示是否有数据
	struct fasync_struct *faysnc;		//信号记录体
	struct tasklet_struct mytasklet;  	//中断下半部tasklet对象
	struct work_struct mywork; 			//中断下半部work对象
}KEY_DESC_T;

KEY_DESC_T *key_dev;


#ifdef USE_TASKLET

void key_tasklet_half_irq(unsigned long data)
{
	printk("---------------%s-------------------\n",__FUNCTION__);
	// 表示有数据,需要去唤醒整个进程/等待队列
	wake_up_interruptible(&key_dev->wq_head);
	
	//同时设置标志位
	key_dev->key_state  = 1;

	//发送信号
	kill_fasync(&key_dev->faysnc, SIGIO, POLLIN);

}

#else 

void key_work_half_irq(struct work_struct *work)
{
	printk("---------------%s-------------------\n",__FUNCTION__);

	// 表示有数据,需要去唤醒整个进程/等待队列
	wake_up_interruptible(&key_dev->wq_head);
	
	//同时设置标志位
	key_dev->key_state	= 1;

	//发送信号
	kill_fasync(&key_dev->faysnc, SIGIO, POLLIN);

}


#endif


irqreturn_t key_irq_handler(int irqno, void *devid)
{
	printk("-------%s-------------\n", __FUNCTION__);

	//读取数据寄存器
	int value = readl(key_dev->reg_base + 4) & (1<<2);

	if(value){ // 抬起
		printk("key3 up\n");
		key_dev->event.code = KEY_ENTER;
		key_dev->event.value = 0;

	}else{//按下
		printk("key3 pressed\n");
		key_dev->event.code = KEY_ENTER;
		key_dev->event.value = 1;
	}
	// 表示有数据,需要去唤醒整个进程/等待队列
	wake_up_interruptible(&key_dev->wq_head);
	//同时设置标志位
	key_dev->key_state  = 1;

	
	//发送信号
	kill_fasync(&key_dev->faysnc, SIGIO, POLLIN);

	// /启动中断下半部
#ifdef  USE_TASKLET
	tasklet_schedule(&key_dev->mytasklet);
#else
	schedule_work(&key_dev->mywork);
#endif

	return IRQ_HANDLED;
}

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");
	}

	// 通过节点去获取到中断号码
	int irqno = irq_of_parse_and_map(np, 0);
	printk("irqno = %d\n", irqno);
	
	return irqno;
}

int key_drv_open(struct inode *inode, struct file *filp)
{

	printk("-------%s-------------\n", __FUNCTION__);
	return 0;
}

ssize_t key_drv_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
	//printk("-------%s-------------\n", __FUNCTION__);

	//如果当前是非阻塞模式,并且没有数据,立马返回一个出错码
	if(filp->f_flags & O_NONBLOCK && !key_dev->key_state)
		return -EAGAIN;
	
	int ret;
	//2,在需要等待(没有数据)的时候,进行休眠
	wait_event_interruptible(key_dev->wq_head, key_dev->key_state);
	
	// 表示有数据
	ret = copy_to_user(buf, &key_dev->event,  count);
	if(ret > 0)
	{
		printk("copy_to_user error\n");
		return -EFAULT;
	}

	// 传递给用户数据之后,将数据清除掉
	memset(&key_dev->event, 0,  sizeof(key_dev->event));
	key_dev->key_state = 0;
	
	return count;
	
}
ssize_t key_drv_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
	printk("-------%s-------------\n", __FUNCTION__);
	return 0;

}

int key_drv_close (struct inode *inode, struct file *filp)
{
	printk("-------%s-------------\n", __FUNCTION__);
	return 0;

}

unsigned int key_drv_poll(struct file *filp, struct poll_table_struct *pts)
{
	// 返回一个mask值
	unsigned int mask;
	// 调用poll_wait,将当前到等待队列注册系统中
	poll_wait(filp, &key_dev->wq_head, pts);
	
	// 1,当没有数据到时候返回一个0
	if(!key_dev->key_state)
		mask = 0;

	// 2,有数据返回一个POLLIN
	if(key_dev->key_state)
		mask |= POLLIN;

	return mask;
}

int key_drv_fasync(int fd, struct file *filp, int on)
{
	//只需要调用一个函数记录信号该发送给谁
	return fasync_helper(fd, filp, on,  &key_dev->faysnc);

}

const struct file_operations key_fops = {
	.open = key_drv_open,
	.read = key_drv_read,
	.write = key_drv_write,
	.release = key_drv_close,
	.poll = key_drv_poll,
	.fasync = key_drv_fasync,
	
};

static int __init key_drv_init(void)
{
	//演示如何获取到中断号
	int ret;

	// 1,设定一个全局的设备对象
	key_dev = kzalloc(sizeof(KEY_DESC_T),  GFP_KERNEL);
	if(NULL == key_dev)
	{
		printk(KERN_ERR "malloc error\n");
		return -ENOMEM;
	}
	
	
	// 2,申请主设备号
	key_dev->dev_major = register_chrdev(0, "key_drv", &key_fops);
	if(key_dev->dev_major < 0)
	{
		printk(KERN_ERR "register_chrdev error\n");
		ret = -ENODEV;
		goto err_0;
	}
	else
	{
		printk("register_chrdev ok\n");
	}


	// 3,创建设备节点文件
	key_dev->cls = class_create(THIS_MODULE, "key_cls");
	if(IS_ERR(key_dev->cls))
	{
		printk(KERN_ERR "class_create error\n");
		ret = PTR_ERR(key_dev->cls);
		goto err_1;
	}

	
	key_dev->dev = device_create(key_dev->cls, NULL, 
									MKDEV(key_dev->dev_major,0), NULL, "key0");

	if(IS_ERR(key_dev->dev))
		{
			printk(KERN_ERR "device_create error\n");
			ret = PTR_ERR(key_dev->dev);
			goto err_2;
		}

	// a,硬件如何获取数据--gpx1
	key_dev->reg_base  = ioremap(GPXCON_REG, 8);
	if(key_dev->reg_base == NULL)
	{
		printk(KERN_ERR "ioremap error\n");
		ret = -ENOMEM;
		goto err_3;
	}
	else
	{
		printk("ioremap ok\n");
	}


	// 4,硬件的初始化--地址映射或者中断申请
	key_dev->irqno = get_irqno_from_node();

	ret = request_irq(key_dev->irqno, key_irq_handler, IRQF_TRIGGER_FALLING, 
					"key3_eint10", NULL);
	if(ret != 0)
	{
		printk("request_irq error\n");
		
		goto err_4;
	}

	// 初始化等待队列头
	init_waitqueue_head(&key_dev->wq_head);

	
#ifdef USE_TASKLET 
	//中断下半部tasklet初始化
	tasklet_init(&key_dev->mytasklet, key_tasklet_half_irq,45);
	printk(" Use tasklet handle !!! \n.");
#else
	//中断下半部work初始化
	INIT_WORK(&key_dev->mywork, key_work_half_irq);
	printk(" Use work handle !!! \n.");

#endif

	return 0;
	
err_4:
	iounmap(key_dev->reg_base);
err_3:
	device_destroy(key_dev->cls,  MKDEV(key_dev->dev_major, 3));
err_2:
	class_destroy(key_dev->cls);
err_1:
	unregister_chrdev(key_dev->dev_major, "key_dev_test");
err_0:
	kfree(key_dev);
	return ret;
	
	
}


static void __exit key_drv_exit(void)
{
	//销毁中断下半部

#ifdef  USE_TASKLET
	tasklet_kill(&key_dev->mytasklet);

#else 

#endif



	//去映射
	iounmap(key_dev->reg_base);

	//释放中断
	free_irq(key_dev->irqno, NULL);

	//销毁类和节点
	device_destroy(key_dev->cls, MKDEV(key_dev->dev_major,0));
	class_destroy(key_dev->cls);

	//释放主设备号
	unregister_chrdev(key_dev->dev_major, "key_drv");

	//释放动态内存
	kfree(key_dev);

}

module_init(key_drv_init);
module_exit(key_drv_exit);
MODULE_LICENSE("GPL");
终端界面
下半部tasklet

在这里插入图片描述
在这里插入图片描述

下半部工作队列

在这里插入图片描述
在这里插入图片描述

参考

https://blog.csdn.net/m0_37542524/article/details/86594667#5_841

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值