字符設(shè)備驅(qū)動(dòng)-定時(shí)器
按下按鍵由于機(jī)械按鍵的波動(dòng)性,會(huì)導(dǎo)致中斷處理函數(shù)發(fā)生多次。當(dāng)我們用阻塞的方式來驅(qū)動(dòng)按鍵時(shí):
Makefile:
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += seventh_drv.o
驅(qū)動(dòng)程序:seventh_drv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct fasync_struct *button_async;
static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_dev;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中斷事件標(biāo)志, 中斷服務(wù)程序?qū)⑺?,s3c24xx_sixth_read將它清0 */
static volatile int ev_press = 0;
struct timer_list buttons_timer;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 鍵值: 按下時(shí),0x01、0x02、0x03 */
/* 鍵值: 松開時(shí),0x81、0x82、0x83 */
static unsigned char key_val;
struct pin_desc pin_desc[3] = {
{S3C2410_GPF0,0X01},
{S3C2410_GPF2,0X02},
{S3C2410_GPG3,0X03},
};
//atomic_t canopen = ATOMIC_INIT(1);//定義原子變量canopen并初始化為1
static DECLARE_MUTEX(button_lock); //定義互斥鎖
static struct pin_desc *irq_pd;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
/* 設(shè)置定時(shí)器,發(fā)生中斷10ms后再去讀電平值 */
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer,jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int sixth_drv_open(struct inode *inode,struct file *file)
{
#if 0
if(!atomic_dec_and_test(&canopen))
{
atomic_inc(&canopen);
return -EBUSY;
}
#endif
if (file->f_flags & O_NONBLOCK)
{
if (down_trylock(&button_lock))
return -EBUSY;
}
else
{
/* 獲 取 信 號(hào) 量 */
down(&button_lock);
}
/* 配置GPF0,2、GPG3為中斷引腳 */
request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE,"s2",&pin_desc[0]);
request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"s3",&pin_desc[1]);
request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"s4",&pin_desc[2]);
/* 配置GPF4、5、6為輸入引腳 */
*gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2));
*gpfcon |= ((1<<4*2) | (1<<5*2) | (1<<6*2));
return 0;
}
static ssize_t sixth_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
//看用戶需要讀取的空間,和這里的是否相同
if(count != 1)
return -EINVAL;
if(file->f_flags & O_NONBLOCK)
{
if(!ev_press)
return -EAGAIN;
}
else
{
/* 如果無按鍵動(dòng)作發(fā)生,則進(jìn)行休眠狀態(tài) */
/* 如果ev_press等于0,休眠 */
wait_event_interruptible(button_waitq,ev_press);
}
/* 如果有按鍵動(dòng)作發(fā)生,則返回按鍵的值 */
copy_to_user(buf,&key_val,1);
ev_press = 0;
return 1;
}
static int sixth_drv_close (struct inode *inode, struct file *file)
{
//atomic_inc(&canopen);
/* 釋放信號(hào)量 */
up(&button_lock);
free_irq(IRQ_EINT0, &pin_desc[0]);
free_irq(IRQ_EINT2, &pin_desc[1]);
free_irq(IRQ_EINT11, &pin_desc[2]);
return 0;
}
static unsigned int sixth_drv_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int sixth_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: sixth_drv_fasyncn");
return fasync_helper (fd, filp, on, &button_async);
}
static struct file_operations sixth_drv_fops = {
.owner = THIS_MODULE, /* 這是一個(gè)宏,推向編譯模塊時(shí)自動(dòng)創(chuàng)建的__this_module變量 */
.open = sixth_drv_open,
.read = sixth_drv_read,
.release = sixth_drv_close,
.poll = sixth_drv_poll,
.fasync = sixth_drv_fasync,
};
int major;
static int buttons_timer_function(unsigned long data)
{
struct pin_desc *pindesc = irq_pd;
unsigned int pinval;
if(!pindesc)
return;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
/* 松開 */
key_val = 0x80 | (pindesc->key_val);
*gpfdat |= ((1<<4) | (1<<5) | (1<<6));
}
else
{
/* 按下 */
key_val = pindesc->key_val;
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
}
ev_press = 1; /* 表示中斷發(fā)生了 */
wake_up_interruptible(&button_waitq); /* 喚醒休眠的進(jìn)程 */
kill_fasync (&button_async, SIGIO, POLL_IN);
}
static int sixth_drv_init(void)
{
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
major = register_chrdev(0,"sixth_drv",&sixth_drv_fops);
sixthdrv_class = class_create(THIS_MODULE,"sixthdrv");
sixthdrv_class_dev = class_device_create(sixthdrv_class,NULL,MKDEV(major,0),NULL,"buttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon + 1;
return 0;
}
static int sixth_drv_exit(void)
{
unregister_chrdev(major,"sixth_drv");
class_device_unregister(sixthdrv_class_dev);
class_destroy(sixthdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL");
驅(qū)動(dòng)測試程序:seventhdrvtest.c
#include
#include
#include
#include
#include
#include
#include
#include
/*
* sixthdrvtest
*/
int fd;
void czg_signal_handler(int signum)
{
unsigned char key_val = 0;
read(fd,&key_val,1);
printf("key_val: 0x%xn",key_val);
}
int main(int argc, char **argv)
{
int ret;
int oflags;
unsigned char key_val = 0;
//signal(SIGIO,czg_signal_handler);
fd = open("/dev/buttons",O_RDWR);
if(fd < 0)
{
printf("can't open!n");
return -1;
}
//fcntl(fd,F_SETOWN,getpid()); // 告訴內(nèi)核,發(fā)給誰
//oflags = fcntl(fd,F_GETFL);
//fcntl(fd,F_SETFL,oflags | FASYNC); // 改變fasync標(biāo)記,
//最終會(huì)調(diào)用到驅(qū)動(dòng)的faync > fasync_helper:初始化/釋放fasync_struct
//然后當(dāng)按鍵按下時(shí)候,在irqreturn_t buttons_irq中斷處理中調(diào)用kill_fasync
while(1)
{
ret = read(fd,&key_val,1);
printf("key_val: 0x%x,ret = %dn",key_val,ret);
//sleep(4);
}
return 0;
}