s3c2410中断驱动--interrupt key

//驱动程序

#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"); }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值