內(nèi)核定時器用于按鍵處理
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include ccess.h>
#include
#include
#include
#include
static struct timer_list buttons_timer;
static struct class *sixthdrv_class;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static struct fasync_struct *button_async;
//static atomic_t canopen = ATOMIC_INIT(1); //定義原子變量并初始化為1
static DECLARE_MUTEX(button_lock); //定義互斥鎖
/* 中斷事件標(biāo)志, 中斷服務(wù)程序?qū)⑺?,buttons_timer_read將它清0 */
static volatile int ev_press = 0;//等于0的話就會一直等待
static unsigned char key_val;
struct pin_desc {
unsigned int pin;
unsigned int val;
};
struct pin_desc pins [] = {
{S3C2410_GPG(0),1},
{S3C2410_GPG(3),2},
{S3C2410_GPG(5),3},
{S3C2410_GPG(6),4}
};
static struct pin_desc *irq_pd;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
/* 10ms后啟動定時器 */
/*
原理當(dāng)觸發(fā)中斷的時候,進(jìn)入此函數(shù),然后修改定時器的值10ms后
去執(zhí)行定時器指定的函數(shù),讀取鍵值
*/
irq_pd = (struct pin_desc *)dev_id;//這里為參數(shù)傳遞,因為
//buttons_timer_function里沒法得到傳入引腳值
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static void buttons_timer_function(unsigned long data)
{
//printk("irqno = %dn" , irqno);
struct pin_desc *pings = irq_pd;
if (pings==NULL)
return -EFAULT;
if(s3c2410_gpio_getpin(pings->pin))//傳入pin值
{
/* 松開 */
key_val = 0x80 | pings->val;
}
else
{
/* 按下 */
key_val = pings->val;
}
ev_press = 1; /* 表示中斷發(fā)生了 */
wake_up_interruptible(&button_waitq); /* 喚醒休眠的進(jìn)程 */
/*
通過系統(tǒng)函數(shù)kill_fasync向進(jìn)程發(fā)送異步IO信號
POLL_IN表示數(shù)據(jù)可讀
*/
kill_fasync (&button_async, SIGIO, POLL_IN);
//return IRQ_RETVAL(IRQ_HANDLED);//這里是一個宏,里面是一個條件判斷,最終返回1
}
static int buttons_timer_open(struct inode *inode, struct file *file)
{//S3C2410_GPG(0)
/*
* K1,K2,K3,K4對應(yīng)GPG0,GPG3,GPG5,GPG6
*/
#if 0
if (!atomic_dec_and_test(&canopen))//自減并判斷是否為0,為0返回true,不然返回false
{
atomic_inc(&canopen);//自增
return -EBUSY;
}
#endif
if (file->f_flags & O_NONBLOCK)//以非阻塞方式打開,不成功還是返回
{
//printk(KERN_EMERG "open failedn");
//down_trylock獲取信號量不成功不會睡眠而是正常退出
if (down_trylock(&button_lock))//申請信號量,如果成功申請則返回0,不然返回非0
return -EBUSY;//不會導(dǎo)致睡眠
//return 100;
}
else
{
/* 獲取信號量 */
down(&button_lock);//如果獲取不到信號量,將進(jìn)入不可中斷的睡眠
// down_killable(&button_lock)//如果獲取不到信號量,可以殺
// down_interruptible(&button_lock)//如果獲取不到信號量,可以中斷方式喚醒
}
request_irq(IRQ_EINT8, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1", &pins[0]);
request_irq(IRQ_EINT11, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2", &pins[1]);
request_irq(IRQ_EINT13, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3", &pins[2]);
request_irq(IRQ_EINT14, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4", &pins[3]);
return 0;
}
ssize_t buttons_timer_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 8)//格式控制,如果用戶程序傳過來的不是1長度,則直接返回
return -EINVAL;
if (file->f_flags & O_NONBLOCK)//非阻塞方式讀,沒有鍵值,返回,不會等待
{
if (!ev_press)
return -EAGAIN;
}
else
{
/* 如果沒有按鍵動作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
}
/* 如果沒有按鍵動作, 休眠 在此就使應(yīng)用程序休眠,直到有中斷來喚醒*/
//wait_event_interruptible(button_waitq, ev_press);//ev_press = 0時休眠
//執(zhí)行到這,說明已被喚醒,ev_press = 1,所以要清零
ev_press = 0;
copy_to_user(buf, &key_val, 1);
return 1;
}
int buttons_timer_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT8, &pins[0]);
free_irq(IRQ_EINT11, &pins[1]);
free_irq(IRQ_EINT13, &pins[2]);
free_irq(IRQ_EINT14, &pins[3]);
//atomic_inc(&canopen);
up(&button_lock);
return 0;
}
static unsigned buttons_timer_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0; //為0則poll_wait加入隊列后進(jìn)入休眠
poll_wait(file, &button_waitq, wait); // 先加入隊列,不會立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;//返回非零直接喚醒進(jìn)程
}
static int buttons_timer_fasync (int fd, struct file *filp, int on)
{
printk("driver: buttons_timer_fasyncn");
return fasync_helper (fd, filp, on, &button_async);//此函數(shù)會初始化button_async結(jié)構(gòu)體
}
static struct file_operations buttons_timer_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創(chuàng)建的__this_module變量 */
.open = buttons_timer_open,
.read=buttons_timer_read,
.release = buttons_timer_close,
.poll = buttons_timer_poll,
.fasync = buttons_timer_fasync,//注冊異步處理函數(shù)
};//當(dāng)應(yīng)用程序一打開異步模式,就會調(diào)用此函數(shù)
int major;
static int buttons_timer_init(void)
{
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
//buttons_timer.expires = 0;
add_timer(&buttons_timer);
major = register_chrdev(0, "buttons_timer", &buttons_timer_fops);
sixthdrv_class = class_create(THIS_MODULE, "buttons_timer");
device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons_timer"); /* /dev/buttons */
return 0;
}
static void buttons_timer_exit(void)
{
unregister_chrdev(major, "buttons_timer");
device_destroy(sixthdrv_class,MKDEV(major, 0));
class_destroy(sixthdrv_class);
}
module_init(buttons_timer_init);
module_exit(buttons_timer_exit);
MODULE_LICENSE("GPL");
這里有一點(diǎn)需要注意,就是關(guān)于空指針的問題如:
這里定義static struct pin_desc *irq_pd;指針,但是沒給它賦值的話,雖然定時器函數(shù)中irq_pd = (struct pin_desc *)dev_id;好像賦了值,但是在struct pin_desc *pings = irq_pd;中再次使用的時候,也會有空指針問題熱報錯 所以要加入
if (pings==NULL)
return -EFAULT;