arm驱动Linux内核开发之阻塞非阻塞IO轮询操作
equest_irq(unsigned int irq, irq_handler_t handler, unsigned long irqflags, const char * devname, void * dev_id); dev_id随意
request_irq(IRQ_EINT0, irq_handle, IRQ_TYPE_EDGE_BOTH, "s2", &pins_desc[0]);
request_irq(IRQ_EINT2, irq_handle, IRQ_TYPE_EDGE_BOTH, "s3", &pins_desc[1]);
request_irq(IRQ_EINT11, irq_handle, IRQ_TYPE_EDGE_BOTH, "s4", &pins_desc[2]);
return 0;
}
static ssize_t irq_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos){
if(count != 1)return -EINVAL;
wait_event_interruptible(button_waitq, ev_press);//ev_press标志(if!(ev_press)),那么一直休眠
copy_to_user(buf, &key_val, 1);//一个 char 0xff
ev_press = 0;
return 1;//warn :return the size of val
}
static ssize_t irq_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk("irq_dev write\n");
return 0;
}
static ssize_t irq_drv_release(struct inode *inode, struct file *file){
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
return 0;
}
static struct file_operations irq_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = irq_drv_open,
.write = irq_drv_write,
.read = irq_drv_read,
.release = irq_drv_release,
.poll = irq_drv_poll,
};
int major;
static int irq_drv_init(void)
{
major = register_chrdev(0, "irq_drv", &irq_drv_fops); // 注册, 告诉内核
if (major < 0) {
printk(" cant register major number\n");
return major;
}
irq_class = class_create(THIS_MODULE, "irq_drv");
if (IS_ERR(irq_class))
return PTR_ERR(irq_class);
irq_class_dev = class_device_create(irq_class, NULL, MKDEV(major, 0), NULL, "irq"); /* /dev/xyz */
if (IS_ERR(irq_class_dev))
return PTR_ERR(irq_class_dev);
return 0;
}
static void irq_drv_exit(void)
{
unregister_chrdev(major, "irq_drv"); // 卸载
class_device_unregister(irq_class_dev);
class_destroy(irq_class);
}
module_init(irq_drv_init);
module_exit(irq_drv_exit);
MODULE_LICENSE("GPL");
2、测试应用程序
#include
#include
#include
#include
#include
#include
/* irq
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
int cnt = 0;
int ret;
struct pollfd fds[1];
fd = open("/dev/irq", O_RDWR);
if (fd < 0)
{
printf("cant open!\n");
exit(1);
}
fds[0].fd = fd;
fds[0].events = POLLIN;
while (1)
{
ret = poll(fds, 1, 5000);
if(ret == 0){
printf("time out!\n");
}else{
read(fd, &key_val, 1);
printf("key_Vals = 0x%x\n", key_val);
}
}
return 0;
}
Makefile
#myirq.bin
objs := $(patsubst %c, %o, $(shell ls *.c))
myarmgcc := /workspacearm/armlinuxgcc2626/bin/arm-linux-gcc
myirq.bin:$(objs)
$(myarmgcc) -o $@ $^
cp *.bin /opt/fsmini/
%.o:%.c
$(myarmgcc) -c -o $@ $
clean:
rm -f *.bin *.o
Curtiss Falcon
arm驱动Linux内核开发轮询操 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)