第一、二期衔接——4.6 字符驱动设备之按键驱动—异步机制

编写按键中断驱动——异步机制

  • 硬件平台:韦东山嵌入式Linxu开发板(S3C2440.v3)
  • 软件平台:运行于VMware Workstation 12 Player下UbuntuLTS16.04_x64 系统
  • 参考资料:《嵌入式Linux应用开发手册》、《嵌入式Linux应用开发手册第2版》
  • 开发环境:Linux 2.6.22.6 内核、arm-linux-gcc-3.4.5-glibc-2.3.6工具链


一、前言

  在之前按键中断中的查询方式、休眠——唤醒方式、poll机制都是属于同步机制,就是应用程序一直在调用驱动程序。
  提问:是否可以实现由驱动程序去提醒应用程序已经准备,可以调用。
  回答:可以,在Linux中,可以使用异步机制,实现驱动程序通知应用程序。
  场景:下面我借助一下韦东山老师的场景来理解一下:
在这里插入图片描述
妈妈怎么知道卧室里小孩醒了?

  1. 时不时进房间看一下: 查询方式——简单,但是累
  2. 进去房间陪小孩一起睡觉,小孩醒了会吵醒她: 休眠-唤醒 ——不累,但是妈妈干不了活了
  3. 妈妈要干很多活,但是可以陪小孩睡一会,定个闹钟: poll 方式——要浪费点时间,但是可以继续干活。妈妈要么是被小孩吵醒,要么是被闹钟吵醒。
  4. 妈妈在客厅干活,小孩醒了他会自己走出房门告诉妈妈: 异步通知——妈妈、小孩互不耽误

二、要点

在编程之前,我们需要对下面的问题有个清晰的答案。

1、谁发送:驱动程序发

采用异步机制的时候,驱动程序可以去提醒应用程序,可以调用。

2、 发什么:信号

这时候,驱动程序通过信号去提醒应用程序。

3、 发什么信号: SIGIO

/linux-2.6.22.6/include/asm-parisc/signal.h这个文件下定义了多种信号 ,而对于我们IO口的,采用SIGIO这个信号值。

#define SIGHUP		 1
#define SIGINT		 2
#define SIGQUIT		 3
#define SIGILL		 4
#define SIGTRAP		 5
#define SIGABRT		 6
#define SIGIOT		 6
#define SIGEMT		 7
#define SIGFPE		 8
#define SIGKILL		 9
#define SIGBUS		10
#define SIGSEGV		11
#define SIGSYS		12 /* Linux doesn't use this */
#define SIGPIPE		13
#define SIGALRM		14
#define SIGTERM		15
#define SIGUSR1		16
#define SIGUSR2		17
#define SIGCHLD		18
#define SIGPWR		19
#define SIGVTALRM	20
#define SIGPROF		21
#define SIGIO		22
#define SIGPOLL		SIGIO
#define SIGWINCH	23
#define SIGSTOP		24
#define SIGTSTP		25
#define SIGCONT		26
#define SIGTTIN		27
#define SIGTTOU		28
#define SIGURG		29
#define SIGLOST		30 /* Linux doesn't use this either */
#define	SIGUNUSED	31

4、怎么发:内核里提供有函数

在内核中了这个函数

void kill_fasync(struct fasync_struct **fp, int sig, int band);
  • struct fasync_struct **fp:定义一个结构体,用来区分是谁发送的,button_async->fa_file 非空时, 可以从中得到 PID,表示发给哪一个 APP
  • int sig:上面所提到的信号值
  • int band:标志位,当 FASYNC 位发生变化时,会导致驱动程序的 fasync 被调用

5、发给谁:应用程序(APP 要把自己告诉驱动)

fcntl(fd, F_SETOWN, getpid());

通过getid()把进程的PID传给驱动

6、应用程序收到后做什么:执行信号处理函数

定义一个执行信号处理函数

void my_signal_fun(int signum)
{
	unsigned char key_val;
	read(fd, &key_val, 1);
	printf("key_val: 0x%x\n", key_val);
}

7、信号处理函数和信号,之间怎么挂钩: 应用程序注册信号处理函数

signal(SIGIO, my_signal_fun);
void* signal(int sig,void* func)(int)))(int;
  • int sig:设置处理功能的信号值。
  • void(* func)(int):定义一个信号处理函数

三、代码编写

1、驱动程序

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>

int major;
static struct class *buttondrv_class;
static struct class_device	*buttondrv_class_dev;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

static struct fasync_struct *button_async;

/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 
 * 键值: 松开时, 0x81, 0x82, 0x83, 0x84 
 */
static unsigned char key_val;

/* 按键信息 */
struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};

/* 存储4个按键的信息 */
struct pin_desc pins_desc[4] = {
 	{S3C2410_GPF0,  0x01},
 	{S3C2410_GPF2,  0x02},
 	{S3C2410_GPG3,  0x03},
	{S3C2410_GPG11, 0x04},
};

/* 中断注册信息 */
struct buttonirq_decs{
	unsigned int irq;
	unsigned long flags;
	const char *devname;
	void *dev_id;
};

struct buttonirq_decs buttonirqs_decs[4] = {
	{IRQ_EINT0,	 IRQT_BOTHEDGE, "S2", &pins_desc[0]},
	{IRQ_EINT2,	 IRQT_BOTHEDGE, "S3", &pins_desc[1]},
	{IRQ_EINT11, IRQT_BOTHEDGE, "S4", &pins_desc[2]},
	{IRQ_EINT19, IRQT_BOTHEDGE, "S5", &pins_desc[3]},
};

/* 生成一个等待队列的队头,名字为button_waitq */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,button_drv_read将它清0 */
static volatile int ev_press = 0;

/* 中断服务函数 */
static irqreturn_t button_irq(int irq, void *dev_id)
{
	struct pin_desc *pindesc = (struct pin_desc *)dev_id;
	unsigned int pinval;

	pinval = s3c2410_gpio_getpin(pindesc->pin);	//读取IO口电平

	if(pinval){
		/* 松开 */
		key_val = 0x80 | pindesc->key_val;
	}else{
		/* 按下 */
		key_val = pindesc->key_val;
	}

	ev_press = 1;	//发生中断
	wake_up_interruptible(&button_waitq);   // 唤醒休眠的进程

	kill_fasync(&button_async, SIGIO, POLL_IN);	//发SIGIO信号
	
	return IRQ_RETVAL(IRQ_HANDLED);
}

/* open驱动函数 */
static int button_drv_open(struct inode *inode, struct file *file)
{
	unsigned int i;
	int err;

	/* 注册中断 */
	for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++){
		err = request_irq(buttonirqs_decs[i].irq, button_irq, buttonirqs_decs[i].flags,
					buttonirqs_decs[i].devname, buttonirqs_decs[i].dev_id);
		if(err){
			printk("func button_drv_open err: request_irq num:%d\n", i);
			break;
		}
	}

	/* 出现错误,释放已经注册的中断 */
	if(err){
        i--;
        for (; i >= 0; i--)
            free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
        return -EBUSY;
    }	
	return 0;
}

/* read驱动函数 */
static ssize_t button_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
	
	unsigned long ret = 0;
	int err;

	/* 如果没有按键动作, 休眠 */
	err = wait_event_interruptible(button_waitq, ev_press);

	if (err){
			printk("func button_drv_read() err: wait_event_interruptible\n");
		return err;
	}
	

	/* 如果有按键动作, 返回键值 */
	ret = copy_to_user(buf, &key_val, 1);
	ev_press = 0;	//清中断标志
	
	if(ret < 0){
		printk("func button_drv_read() err: copy_to_user\n");
		return -EFAULT;
	}
	
 	return sizeof(key_val);
}

/* close驱动函数 */
int button_drv_close (struct inode *inode, struct file *file)
{
	int i;

	/* 取消中断 */
	for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++)
		free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);

	return 0;
}

unsigned int buton_drv_poll (struct file *file, poll_table *wait)
{
	unsigned int mask = 0;
	poll_wait(file, &button_waitq, wait); // 不会立即休眠

	/* 中断已经发生 */
	if (ev_press)
		mask |= POLLIN | POLLRDNORM;

	return mask;	//返回给do_poll函数中的mask
}

/* 调用fasync_helper根据on值是否button_async->fa_file=驱动文件file,里面有PID */
static int button_drv_fasync(int fd, struct file *file, int on)
{
	printk("driver: fifth_drv_fasync\n");
	return fasync_helper(fd, file, on, &button_async);
}

static struct file_operations button_drv_fops = {
	.owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open    =  button_drv_open,     
	.read    =	button_drv_read,
	.release =  button_drv_close,
	.poll    =  buton_drv_poll,
	.fasync	 =  button_drv_fasync,
};

/* 入口函数 
 * 执行”insmod button_drv.ko”命令时就会调用这个函数
 */
static int button_drv_init(void)
{
	//注册一个字符设备,名字为button_drv
	major = register_chrdev(0, "button_drv", &button_drv_fops);	
	
	//创建一个类,名字为buttond_rv(/class/button_drv)
	buttondrv_class = class_create(THIS_MODULE, "button_drv");	
	
	//创建一个设备节点,名为button(/dev/button)
	buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, "button"); 

	/* 映射物理地址 */
	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;

	gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
	gpgdat = gpgcon + 1;
	
	return 0;
}

/* 出口函数 
 * 执行”rmmod button_drv.ko”命令时就会调用这个函数
 */
static void button_drv_exit(void)
{
	unregister_chrdev(major, "button_drv"); // 卸载字符

	class_device_unregister(buttondrv_class_dev);	//删除设备节点
	class_destroy(buttondrv_class);	//销毁类
	
	/* 取消映射 */
	iounmap(gpfcon);
	iounmap(gpgcon);
}

module_init(button_drv_init);
module_exit(button_drv_exit);

MODULE_LICENSE("GPL");

2、测试(应用)程序

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>


int fd;

/* 信号处理函数 */
void my_signal_fun(int signum)
{
	int ret; 
	unsigned char key_val;
	ret = read(fd, &key_val, 1);
	if(ret < 0)
		printf(" func read() err\n");
	else
		printf("key_val = 0x%x\n", key_val);
}

int main(int argc, char **argv)
{

	int Oflags;

	signal(SIGIO, my_signal_fun);	//给SIGIO 信号注册信号处理函数my_signal_fun
	
	fd = open("/dev/button", O_RDWR);	
	if (fd < 0)
	{
		printf("can't open!\n");
	}

	fcntl(fd, F_SETOWN, getpid());	//把进程 ID 告诉驱动
		
	Oflags = fcntl(fd, F_GETFL);	//取得文件描述符filedes的文件状态标志
	fcntl(fd, F_SETFL, Oflags | FASYNC);	//FASYNC位发生变化时,会导致驱动程序的fasync函数被调用
	
	while (1)
	{
		sleep(1000);
	}

	return 0;
}


四、软件执行流程

在这里插入图片描述
重点从②开始:

  • ② APP 给 SIGIO 这个信号注册信号处理函数 func, 以后 APP 收到 SIGIO 信号时,这个函数会被自动调用
  • ③ 把 APP 的 PID(进程 ID)告诉驱动程序,这个调用不涉及驱动程序,在内核的文件系统层次记录 PID;
  • 读取驱动程序文件 Flag
  • ⑤ 设置 Flag 里面的 FASYNC 位为 1: 当 FASYNC 位发生变化时,会导致驱动程序的 fasync 被调用
  • ⑥⑦ 调用 faync_helper,它会根据 FAYSNC 的值决定是否设置 button_async->fa_file=驱动文件
    filp: 驱动文件 filp 结构体里面含有之前设置的 PID。
  • ⑧ APP 可以做其他事;
  • ⑨⑩ 按下按键,发生中断,驱动程序的中断服务程序被调用,里面调用 kill_fasync 发信号
  • ⑪⑫⑬ APP 收到信号后,它的信号处理函数被自动调用,可以在里面调用 read 函数读取按键

五、程序运行截图

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章