//驱动程序
#include <linux/config.h>
#include <linux/module.h>#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/irq.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>
#define DEVICE_NAME "s3c2410-key-int"
#define KEYRAW_MINOR 1
static int key_int_major = 0;
static int key_down = 0;
static ssize_t key_int_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
{ int len=sizeof(key_down);
if(count<len) len=count;
copy_from_user(&key_down,buffer,len);
return len;}
static ssize_t key_int_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{ copy_to_user(buffer, (char *)&key_down, sizeof(key_down));
key_down=0;
return sizeof(key_down);}
int key_int_open(struct inode *inode,struct file *filp)
{ printk("open ok\n");
return 0;
}
int key_int_release(struct inode *inode,struct file *filp)
{ printk("release ok\n"); return 0;}
static struct file_operations key_int_fops={
open: key_int_open,
release: key_int_release,
read:key_int_read,
write:key_int_write,
};
//中断处理程序
void test_interrupt(int irq,void *dev_id,struct pt_regs *regs)
{
printk("key down!^-^\n");
key_down=1;
printk("%d\n",key_down);
}
devfs_handle_t devfs_keyintraw;
int __init key_int_init(void)
{ static int result;
set_external_irq(IRQ_EINT5,EXT_FALLING_EDGE, GPIO_PULLUP_EN);
result=request_irq(IRQ_EINT5, test_interrupt, 0, DEVICE_NAME,0);
if(result)
{printk("Can't get assigned irq %d, result=%d\n",IRQ_EINT5,result);}
devfs_keyintraw = devfs_register(NULL, DEVICE_NAME,
DEVFS_FL_DEFAULT, key_int_major,0
, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, &key_int_fops,NULL);
return 0;
}
void __exit key_int_exit(void)
{
devfs_unregister(devfs_keyintraw);
free_irq(IRQ_EINT5,NULL);
}
module_init(key_int_init);
module_exit(key_int_exit);
MODULE_LICENSE("GPL");
//驱动测试程序
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DEVICE_INTTEST "/dev/s3c2410-key-int"
int main()
{ int fd, key;
if((fd=open(DEVICE_INTTEST,O_RDONLY|O_NONBLOCK))<0)
{ perror("can not open device");
exit(1); }
while(1)
{ read(fd,&key,sizeof(key));
if(key==1) printf("key pressed\n"); }
}