linux应用层消息机制处理,Linux驱动开发七:按键中断+poll机制+异步通知机制



分析:

前面几个驱动都是获得按键值:

.查询:应用程序很耗资源

.中断:使用read()函数获取

.poll:指定超时时间

他们的共同点是:

都是应用程序主动去读或者查询

疑问:

是否有驱动主动告诉应用程序

答案:

有,使用异步通知机制signal发信号

进程间发信号:

Kill-9PID

发送9接收

Kill-USR1PID

要点:

注册信号处理函数

谁发

发给谁

怎么发

Example:

Signal.c

#include

#include

void my_signal_fun(int signum)

{

staticint cnt = 0;

printf("signal= %d , %d times \n",signum,++cnt);

}

int main(int argc,char **argv)

{

signal(SIGUSR1,my_signal_fun);

while(1)

{

sleep(1000);

}

return0;

}

Ps

57700:00 [flush-0:12]

61700:00 ./signal

61800:00 ps

/ # kill -USR1 617

/ # signal = 10 , 1 times

/ # kill -USR1 617

/ # signal = 10 , 2 times

/ # kill -USR1 617

/ # signal = 10 , 3 times

目标:按下按键时,驱动通知应用程序

应用程序:注册信号处理函数

谁发:驱动

发给谁:发给APP的PID

怎么发:用kill_fasync函数法

为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:

1.支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID。

不过此项工作已由内核完成,设备驱动无须处理。

2.支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数将得以执行。

驱动中应该实现fasync()函数。

3.在设备资源可获得时,调用kill_fasync()函数激发相应的信号

应用程序:

fcntl(fd, F_SETOWN, getpid());//告诉内核,发给谁

Oflags = fcntl(fd, F_GETFL);

fcntl(fd, F_SETFL, Oflags | FASYNC);//改变fasync标记,最终会调用到驱动的faync> fasync_helper:初始化/释放fasync_struct

驱动代码:

fifth_drv.c

/*

*功能:中断驱动,在按键驱动基础上修改,原理图见按键驱动,增加poll机制,增加异步通知机制

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

//#include

//#include

#include

#include

#include

#include

int major;

const char *major_name = "fifth_drv";

const char *minor_name = "button";

static struct class *fifthdrv_class;

static struct class_device *fifthdrv_class_dev;

volatile unsigned long *gpncon = NULL;

volatile unsigned long *gpndat = NULL;

//声明一个按键的等待队列

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,fifth_drv_read将它清0 */

static volatile int ev_press = 0;

//异步

static struct fasync_struct *button_fasync;

struct pin_desc{

unsigned int pin;

unsigned int key_val;

};

/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */

/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */

static unsigned char key_val;

/*

* K1,K2,K3,K4对应GPN0,GPGN1,GPN2,GPN3

*/

/*

struct pin_desc pins_desc[4] = {

{S3C64XX_GPN0_EINT0, 0x01},

{S3C64XX_GPN1_EINT1, 0x02},

{S3C64XX_GPN2_EINT2, 0x03},

{S3C64XX_GPN3_EINT3, 0x04},

};

struct pin_desc pins_desc[4] = {

{0, 0x01},

{1, 0x02},

{2, 0x03},

{3, 0x04},

};

*/

struct pin_desc pins_desc[4] = {

{S3C64XX_GPN(0), 0x01},

{S3C64XX_GPN(1), 0x02},

{S3C64XX_GPN(2), 0x03},

{S3C64XX_GPN(3), 0x04},

};

/*

* 确定按键值

*中断处理程序,并置标志位为1,唤醒等待队列上等待的进程,注册用户中断处理函数,设置触发方式为双边沿触发

*/

static irqreturn_t button_irq(int irq,void *dev_id)

{

struct pin_desc * pindesc = (struct pin_desc *)dev_id;

unsigned int pinval;

//pinval = ioread32(S3C64XX_GPNDAT);

//pinval = gpio_get_value(S3C64XX_GPN(pindesc->pin));/* 读取中断引脚的状态 */

pinval = gpio_get_value(pindesc->pin);/* 读取中断引脚的状态 */

/* 确定按键值 */

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_fasync,SIGIO,POLL_IN);//发信号

return IRQ_RETVAL(IRQ_HANDLED);

}

/* open函数 */

static int fifth_drv_open(struct inode *inode,struct file *file)

{

/* 配置GPN0、1、2、3为输入(配置为0) */

//*gpncon &=~((0x3<

request_irq(IRQ_EINT(0),button_irq,IRQ_TYPE_EDGE_BOTH,"K1",&pins_desc[0]);

request_irq(IRQ_EINT(1),button_irq,IRQ_TYPE_EDGE_BOTH,"K2",&pins_desc[1]);

request_irq(IRQ_EINT(2),button_irq,IRQ_TYPE_EDGE_BOTH,"K3",&pins_desc[2]);

request_irq(IRQ_EINT(3),button_irq,IRQ_TYPE_EDGE_BOTH,"K4",&pins_desc[3]);

//request_irq(IRQ_EINT(4),button_irq,IRQ_TYPE_EDGE_BOTH,"K4",1);

return 0;

}

/* read函数 */

static ssize_t fifth_drv_read(struct file *file,char __user *buf,size_t size,loff_t *ppos)

{

if(size != 1)

return -EINVAL;

/* 如果没有按键动作, 休眠 */

wait_event_interruptible(button_waitq, ev_press);

/* 如果有按键动作, 返回键值 */

copy_to_user(buf, &key_val, 1);

ev_press = 0;//重新清0

return 1;

}

static unsigned fifth_drv_poll(struct file *file,poll_table *wait)

{

unsigned int mask = 0;

/* 不会立即休眠 只是将进程挂到key_waitq队列, 并不会休眠 */

poll_wait(file,&button_waitq,wait);

if(ev_press)//如果没有中断产生

mask |= POLLIN | POLLRDNORM;

//printk("\t*******************\n\tpoll\n\t***********************\n");

return mask;

}

//主要是卸载用户中断处理程序

int fifth_drv_close(struct inode* inode,struct file* file)

{

free_irq(IRQ_EINT(0),&pins_desc[0]);

free_irq(IRQ_EINT(1),&pins_desc[1]);

free_irq(IRQ_EINT(2),&pins_desc[2]);

free_irq(IRQ_EINT(3),&pins_desc[3]);

return 0;

}

static int fifth_drv_fasync(int fd,struct file *file,int on)

{

printk("dirver:fifth_drv_fasync\n");

return fasync_helper(fd,file,on,&button_fasync);//初始化结构button_fasync

}

/* 结构体 */

static struct file_operations fifth_drv_fops={

.owner = THIS_MODULE,/* 这是一个宏,推向编译模块时自动创建到__this_module变量 */

.open = fifth_drv_open,

.read = fifth_drv_read,

.release = fifth_drv_close,

.poll = fifth_drv_poll,

.fasync = fifth_drv_fasync,

};

/* 入口函数 */

static int fifth_drv_init(void)

{

major = register_chrdev(0,major_name,&fifth_drv_fops);

if(major < 0)

{

printk("Can't register major number\n");

return major;

}

/* 自动生成设备节点机制,获取系统消息,cat /proc/class会发现 first_drv */

fifthdrv_class = class_create(THIS_MODULE, major_name);

/* register your own device in sysfs, and this will cause udev to create corresponding device node */

fifthdrv_class_dev = device_create( fifthdrv_class, NULL, MKDEV(major, 0),NULL, minor_name );

/* 地址映射 */

gpncon = (volatile unsigned long *)ioremap(0x7F008830,16);

gpndat = gpncon + 1;//指针加1

return 0;

}

/* 出口函数 */

static void fifth_drv_exit(void)

{

unregister_chrdev(major,major_name);

device_unregister(fifthdrv_class_dev);

class_destroy(fifthdrv_class);

iounmap(gpncon);

}

module_init(fifth_drv_init);

module_exit(fifth_drv_exit);

MODULE_LICENSE("GPL");

Makefile:

obj-m := fifth_drv.o

KER_DIR := /work/work/linux/

all:

$(MAKE) -C $(KER_DIR) M=`pwd` modules

#cp first_drv.ko /work/tftpboot/

cp fifth_drv.ko /work/nfsroot/

echo $^

clean:

make -C $(KER_DIR) M=`pwd` modules clean

test.c:

/*

*功能:在一定时间内没有按键中断,也返回

*/

#include

#include

#include

#include

#include

#include

int fd;

/*test

*/

void my_signal_fun(int signum)

{

unsigned char key_vals;

read(fd,&key_vals,1);

printf("key_vals = 0x%x\n",key_vals);

}

int main(int argc,char *argv[])

{

int Oflags ;

fd = open("/dev/button",O_RDWR);

if(fd < 0)

{

printf("can't opent /dev/button\n");

return 0;

}

signal(SIGIO,my_signal_fun);

fcntl(fd,F_SETOWN,getpid());

Oflags = fcntl(fd,F_GETFL);

fcntl(fd,F_SETFL,Oflags|FASYNC);

while(1)

{

sleep(1000);

}

return 0;

}

结果:

/ # insmod  fifth_drv.ko / # ./test & / # dirver:fifth_drv_fasync key_vals = 0x1 key_vals = 0x1 key_vals = 0x1 key_vals = 0x81 key_vals = 0x2 key_vals = 0x2 key_vals = 0x2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值