字元裝置驅動程式——按鍵中斷之非同步通知
阿新 • • 發佈:2019-01-12
一、驅動程式的實現
1、定義static struct fasync_struct *newdrv_async;
2、加入
static struct file_operations new_drv_fops = {
.fasync =new_drv_fasync,
};
static int new_drv_fasync (int fd, struct file *filp, int on)
{
return fasync_helper (fd, filp, on, &newdrv_async);//初始化newdrv_async
}
3、傳送訊號
kill_fasync(&newdrv_async,SIGIO,POLL_IN);//傳送訊號,給newdrv_async
二、應用程式實現
設定好目標裝置的SIGIO訊號處理函式;等待核心kill_fasync()釋放 SIGIO 訊號 signal(SIGIO,signal_fun); 讓裝置中的訊號發到當前程序;告訴核心(驅動程式)發給誰--本程式的pid fcntl(fd,F_SETOWN,getpid()); 獲得檔案fd的flags,即open函式的第二個引數O_RDWR oflags=fcntl(fd,F_GETFL); 給裝置檔案fd新增非同步通知模式,最終呼叫驅動的.fasync->fansync_help函式,初始化/釋放,將fd加入非同步IO通知佇列 fcntl(fd,F_SETFL,oflags|FASYNC);
驅動程式
#include <linux/module.h>
#include <linux/sched.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 <linux/device.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
static struct class *newdrv_class;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static unsigned char key;
static struct fasync_struct *newdrv_async;
struct pin_desc{
unsigned int pin;//引腳
unsigned int key_val;//鍵值,按下01 02 03 04;鬆開81 82 83 84
};
struct pin_desc pins_desc[4]={
{S3C2410_GPF(0), 0x01},
{S3C2410_GPF(2), 0x02},
};
static int buttons_irq (int irq, void *dev_id)
{
struct pin_desc *pindesc=(struct pin_desc *)dev_id;
unsigned int pinval;
pinval=s3c2410_gpio_getpin(pindesc->pin);//讀引腳
if(pinval)//1 鬆開
{
key=0x80|pindesc->key_val;
}
else//按下
key=pindesc->key_val;
kill_fasync(&newdrv_async,SIGIO,POLL_IN);//傳送訊號,給newdrv_async
return IRQ_RETVAL(IRQ_HANDLED);//接收到了中斷訊號,並處理,返回1
}
static int new_drv_open(struct inode *inode, struct file *file)
{
/* 中斷引腳 中斷函式 觸發方式 名稱 傳入dev—id */
request_irq(IRQ_EINT0, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &pins_desc[1]);
return 0;
}
static ssize_t new_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
if(count!=1)
return -EINVA;
copy_to_user(buf, &key, 1);
return 1;
}
static int new_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
return 0;
}
static int new_drv_fasync (int fd, struct file *filp, int on)
{
return fasync_helper (fd, filp, on, &newdrv_async);//初始化newdrv_async
}
static struct file_operations new_drv_fops = {
.owner =THIS_MODULE, /* 這是一個巨集,推向編譯模組時自動建立的__this_module變數 */
.open =new_drv_open,
.read =new_drv_read,
.release=new_drv_close,
.fasync =new_drv_fasync,
};
int major;
static int new_drv_init(void)
{
major = register_chrdev(0, "newdrv", &new_drv_fops); // 註冊, 自動分配並返回主裝置號
/*在/sys/class/下建立類目錄*/
newdrv_class = class_create(THIS_MODULE, "newdrv");
/*根據類目錄,在驅動模組初始化函式中實現裝置節點的自動建立*/
device_create(newdrv_class, NULL, MKDEV(major, 0), NULL, "newdrv");
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void new_drv_exit(void)
{
unregister_chrdev(major, "newdrv");
device_destroy(newdrv_class, MKDEV(major, 0));
class_destroy(newdrv_class);
iounmap(gpfcon);
}
module_init(new_drv_init);
module_exit(new_drv_exit);
MODULE_LICENSE("GPL");
應用程式
#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 signal_fun(int signum) //訊號值-USR1
{
unsigned char key_val,cnt;
read(fd,&key_val,1);
printf("signal=%d, %d times\n",signum,++cnt);
}
int main(int argc, char **argv)
{
unsigned char key_val;
int oflags;
/*設定好目標裝置的SIGIO訊號處理函式;等待核心kill_fasync()釋放 SIGIO 訊號*/
**signal(SIGIO,signal_fun);**
fd = open("/dev/newdrv", O_RDWR);
if(fd<0)
printf("can't open\n");
/*F_SETOWN:設定檔案描述詞fd上接收SIGIO
或 SIGURG事件訊號的程序或程序組標識;
F_SETFL :設定檔案狀態標誌;
F_GETFL:讀取檔案狀態標識
*/
/*讓裝置中的訊號發到當前程序;告訴核心(驅動程式)發給誰--本程式的pid*/
fcntl(fd,F_SETOWN,getpid());
/*獲得檔案fd的flags,即open函式的第二個引數O_RDWR*/
oflags=fcntl(fd,F_GETFL);
/*給裝置檔案fd新增非同步通知模式,最終呼叫驅動的.fasync->fansync_help函式,初始化/釋放,將fd加入非同步IO通知佇列*/
fcntl(fd,F_SETFL,oflags|FASYNC);
while (1)
{
sleep(1000);
}
return 0;
}