内核驱动 - 混杂设备驱动 + 中断处理函数

一、概念

内核驱动 - 混杂设备驱动 + 中断处理函数_第1张图片

二、逻辑图

内核驱动 - 混杂设备驱动 + 中断处理函数_第2张图片

三、Misc_key 代码下载点击打开链接

key.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define	GPH0CON 0xE0200C00
#define	GPH0DAT 0xE0200C04
#define DEVICE_NAME	"tqkey"

#define	LEDCON 0xE0200060
#define LEDDAT 0xE0200064
volatile unsigned int *led_config;
volatile unsigned int *led_data;

static irqreturn_t key_int(int irq, void *dev_id)    //按键key_1产生中断

{
	//1.检测是否发生了按键中断

	//2.清除已产生的按键中断

	//3.打印按键值
	printk("key down!\n");
	writel(0x0, led_data);   //熄灭LED灯
	return 0;
}

void key_hw_init(void)
{
	volatile unsigned short data;
	volatile unsigned int *gpio_config;
	gpio_config = (volatile unsigned int *)ioremap(GPH0CON, 4);
	data = readw(gpio_config);		//读出原有寄存器中的值
	data &= ~0x0F;
	data |= 0x0F;    //设置为中断模式
	writew(data, gpio_config);
	printk("key_hw_init!\n");

	
	led_config = (volatile unsigned int *)ioremap(LEDCON, 4);	//物理地址转换为虚拟地址
	writel(0x00011000, led_config);

	led_data = (volatile unsigned int *)ioremap(LEDDAT, 4);
	writel(0xFF, led_data);   //点亮LED灯
}

/*static long key_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	return -EINVAL;
}*/

static int key_open(struct inode *inode, struct file *file)
{
	return 0;
}

static struct file_operations key_fops =
{
	.owner = THIS_MODULE,
	//.unlocked_ioctl = key_ioctl,
	.open = key_open,
	.release = NULL,
};

struct miscdevice key_miscdev = 
{
	.minor = 200,
	.name = DEVICE_NAME,
	.fops = &key_fops,
};

//注册函数
static int __init button_init(void)
{
	int ret = 0;

	misc_register(&key_miscdev);

	//硬件初始化
	key_hw_init();

	//注册中断处理程序
	ret = request_irq(IRQ_EINT0, key_int, IRQF_TRIGGER_FALLING, DEVICE_NAME, 0);
	
	return 0;
}

//注销函数
static void __exit button_exit(void)
{
	misc_deregister(&key_miscdev);

	//注销中断程序
	free_irq(IRQ_EINT0, 0);
}

module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jerry.Gou");
MODULE_DESCRIPTION("TQ210 button driver");
Makefile
obj-m := key.o
KDIR := /root/code/Kernel_3.0.80_stable

all:
	@make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm

clean:
	@rm -rf *.o *.ko *.order *.symvers *.bak .*.cmd *.mod.o *.mod.c .tmp_versions

cp:
	@make clean
	@make
	cp *.ko /root/code/rootfs/Drive/



你可能感兴趣的:(S5PV210)