之前学的应用层都是:
1)查询方式:一直读
以上3种,我们都是让应用程序主动去读,本节我们学习异步通知,它的作用就是当驱动层有数据时,主动告诉应用程序,然后应用程序再来读, 这样,应用程序就可以干其它的事情,不必一直读
比如:kill -9 pid ,其实就是通过发信号杀死进程,kill发数据9给指定id号进程
1.怎么来收信号?
通过signal函数来实现获取信号,先来看看以下例子:
头函数:
sighandler_t signal(int signum, sighandler_t handler);
函数说明:让一个信号与与一个函数对应,每当接收到这个信号就会调用相应的函数。
头文件: #include <signal.h>
参数1: 指明了所要处理的信号类型
信号有以下几种:
参数2: 信号产生后需要处理的方式,可以是个函数
代码如下:
#include <stdio.h>
#include <signal.h>
void my_signal_run(int signum) //信号处理函数
{
static int run_cnt=0;
printf("signal = %d, %d count\r\n",signum,++count);
}
int main(int argc,char **argv)
{
signal(SIGUSR1,my_signal_run); //调用signal函数,让指定的信号SIGUSR1与处理函数my_signal_run对应。
while(1)
{ sleep(1000); //去做其它事,睡眠1s
}
return 0;
}
然后运行后,使用kill -10 802,可以看到产生单信号USR1(10)时就会调用my_signal_run()打印数据。
# kill -10 802
# signal = 10, 1 count
# kill -10 802
# signal = 10, 2 count
2. 来实现异步通知
要求:
3先来写驱动程序,我们在之前的中断程序上修改
3.1定义 异步信号结构体 变量:
static struct fasync_struct * button_async;
3.2在file_operations结构体添加成员.fasync函数,并写函数
static struct file_operations third_drv_fops={
.owner = THIS_MODULE,
.open = fourth_drv_open,
.read = fourth _drv_read,
.release= fourth _drv_class,
.poll = fourth _poll,
.fasync = fourth_fasync //添加初始化异步信号函数
};
static int fourth_fasync (int fd, struct file *file, int on)
{
return fasync_helper(fd, file, on, & button_async); //初始化button_async结构体,就能使用kill_fasync()了
}
成员.fasync函数又是什么情况下使用?
是被应用程序调用,在下面第4小节会见到。
3.3在buttons_irq中断服务函数里发送信号:
kill_fasync(&button_async, SIGIO, POLL_IN);
//当有中断时,就发送SIGIO信号给应用层,应用层就会触发与SIGIO信号对应的函数
3.4 驱动程序代码如下:
#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/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/poll.h>
static struct class *fourthdrv_class;
static struct class_device *fourthdrv_class_devs;
/* 声明等待队列类型中断 button_wait */
static DECLARE_WAIT_QUEUE_HEAD(button_wait);
/* 异步信号结构体变量 */
static struct fasync_struct * button_async;
/*
* 定义中断事件标志
* 0:进入等待队列 1:退出等待队列
*/
static int even_press=0;
/*
* 定义全局变量key_val,保存key状态
*/
static int key_val=0;
/*
*引脚描述结构体
*/
struct pin_desc{
unsigned int pin;
unsigned int pin_status;
};
/*
*key初始状态(没有按下): 0x01,0x02,0x03,0x04
*key状态(按下): 0x81,0x82,0x83,0x84
*/
struct pin_desc pins_desc[4]={
{S3C2410_GPF0,0x01 },
{S3C2410_GPF2, 0x02 },
{S3C2410_GPG3, 0x03 },
{S3C2410_GPG11,0x04},
} ;
int fourth_drv_class(struct inode *inode, struct file *file) //卸载中断
{
free_irq(IRQ_EINT0,&pins_desc[0]);
free_irq(IRQ_EINT2,&pins_desc[1]);
free_irq(IRQ_EINT11,&pins_desc[2]);
free_irq(IRQ_EINT19,&pins_desc[3]);
return 0;
}
/*
* 确定是上升沿还是下降沿
*/
static irqreturn_t buttons_irq (int irq, void *dev_id) //中断服务函数
{
struct pin_desc *pindesc=(struct pin_desc *)dev_id; //获取引脚描述结构体
unsigned int pin_val=0;
pin_val=s3c2410_gpio_getpin(pindesc->pin);
if(pin_val)
{
/*没有按下 (下降沿),清除0x80*/
key_val=pindesc->pin_status&0xef;
}
else
{
/*按下(上升沿),加上0x80*/
key_val=pindesc->pin_status|0x80;
}
even_press=1; //退出等待队列
wake_up_interruptible(&button_wait); //唤醒 中断
kill_fasync(&button_async, SIGIO, POLL_IN); //发送SIGIO信号给应用层
return IRQ_RETVAL(IRQ_HANDLED);
}
static int fourth_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S1",&pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE, "S2", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq,IRQT_BOTHEDGE, "S3", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq,IRQT_BOTHEDGE, "S4", &pins_desc[3]);
return 0;
}
static int fourth_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
/*将中断 进入等待队列(休眠状态)*/
wait_event_interruptible(button_wait, even_press);
/*有按键按下,退出等待队列,上传key_val 给用户层*/
if(copy_to_user(buf,&key_val,sizeof(key_val)))
return EFAULT;
even_press=0;
return 0;
}
static unsigned fourth_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_wait, wait); // 不会立即休眠
if (even_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int fourth_fasync (int fd, struct file *file, int on)
{
return fasync_helper(fd, file, on, & button_async); //初始化button_async结构体,就能使用kill_fasync()了
}
static struct file_operations fourth_drv_fops={
.owner = THIS_MODULE,
.open = fourth_drv_open,
.read = fourth_drv_read,
.release=fourth_drv_class, //里面添加free_irq函数,来释放中断服务函数
.poll = fourth_poll,
.fasync= fourth_fasync, //初始化异步信号函数
};
volatile int fourth_major;
static int fourth_drv_init(void)
{
fourth_major=register_chrdev(0,"fourth_drv",&fourth_drv_fops); //创建驱动
fourthdrv_class=class_create(THIS_MODULE,"fourth_dev"); //创建类名
fourthdrv_class_devs=class_device_create(fourthdrv_class, NULL, MKDEV(fourth_major,0), NULL,"buttons");
return 0;
}
static int fourth_drv_exit(void)
{
unregister_chrdev(fourth_major,"fourth_drv"); //卸载驱动
class_device_unregister(fourthdrv_class_devs); //卸载类设备
class_destroy(fourthdrv_class); //卸载类
return 0;
}
module_init(fourth_drv_init);
module_exit(fourth_drv_exit);
MODULE_LICENSE("GPL v2");
4 写应用测试程序
步骤如下:
1) signal(SIGIO, my_signal_fun);
调用signal函数,当接收到SIGIO信号就进入my_signal_fun函数,读取驱动层的数据
2) fcntl(fd,F_SETOWN,getpid());
指定进程做为fd文件的”属主”,内核收到F_SETOWN命令,就会设置pid(驱动无需处理),这样fd驱动程序就知道发给哪个进程
3) oflags=fcntl(fd,F_GETFL);
获取fd的文件状态标志
4) fcntl(fd,F_SETFL, oflags| FASYNC );
添加FASYNC状态标志,会调用驱动中成员.fasync函数,执行fasync_helper()来初始化异步信号结构体
这4个步骤执行后,一旦有驱动层有SIGIO信号时,进程就会收到
应用层代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
int fd,ret;
void my_signal_fun(int signame) //有信号来了
{
read( fd, &ret, 1); //读取驱动层数据
printf("key_vale=0X%x\r\n",ret);
}
/*useg: fourthtext */
int main(int argc,char **argv)
{
int oflag;
unsigned int val=0;
fd=open("/dev/buttons",O_RDWR);
if(fd<0)
{printf("can't open!!!\n");
return -1;}
signal(SIGIO,my_signal_fun); //指定的信号SIGIO与处理函数my_signal_run对应
fcntl( fd, F_SETOWN, getip()); //指定进程作为fd 的属主,发送pid给驱动
oflag=fcntl( fd, F_GETFL); //获取fd的文件标志状态
fcntl( fd, F_SETFL, oflag|FASYNC); //添加FASYNC状态标志,调用驱动层.fasync成员函数
while(1)
{
sleep(1000); //做其它的事情
}
return 0;
}
5 运行查看结果
下节开始学习——按键之互斥、阻塞机制