s3c2410中断驱动--interrupt key
2014-04-24 09:53
225 查看
//驱动程序
#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");
}
}
#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");
}
}
相关文章推荐
- s3c2410触摸屏驱动(2.6内核)分析 -中断下半部
- s3c2410触摸屏驱动(2.6内核)分析 -中断下半部
- linux驱动开发--中断:工作者队列实现中断底半部
- 字符设备驱动之Buttons-中断
- linux驱动例3--计时器,带阻塞功能,中断分段
- Linux驱动开发九:按键中断+定时去抖
- 编写Linux中断驱动时,由于头文件缺失引起的编译错误
- 基于S3C2410的SD卡linux驱动工作原理
- S3C2410触摸屏驱动代码分析3
- 字符设备驱动笔记——中断方式按键驱动之linux中断处理结构(五)
- s3c2410 中断异常处理(转载)
- Linux驱动开发⑤--在驱动程序中测试CPU中断
- S3C2410 UART驱动
- linux驱动-中断
- 基于S3C2410的SD卡linux驱动工作原理(一)
- 为s3c2410写网卡驱动(1)
- Tiny6410 按键中断驱动笔记
- xenomai 定时器中断测试驱动
- 关于_linux外部中断驱动异步通知应用程序
- 设备驱动中的中断问题及实例解析