专题1-按键驱动程序设计-第3课-按键驱动硬件操作实现
作者:互联网
1、回顾裸机玩中断写的程序
底下这个程序主要就是把GPIO的功能设置为中断功能,这样当中断发生了就通过读该寄存器的值判断。
#define GPFCON (volatile unsigned long *)0x56000050 /* * K1,K2,K3,K4对应GPF1、GPF4、GPF2、GPF0 */ #define GPF0_int (0x2<<(0*2)) #define GPF1_int (0x2<<(1*2)) #define GPF2_int (0x2<<(2*2)) #define GPF4_int (0x2<<(4*2)) #define GPF0_msk (3<<(0*2)) #define GPF1_msk (3<<(1*2)) #define GPF2_msk (3<<(2*2)) #define GPF4_msk (3<<(4*2)) void button_init() { *(GPFCON) &= ~(GPF0_msk | GPF1_msk | GPF2_msk | GPF4_msk); *(GPFCON) |= GPF0_int | GPF1_int | GPF2_int | GPF4_int; }
2、按键中断程序
#include <linux/module.h> #include <linux/init.h> #include <linux/miscdevice.h> #include <linux/interrupt.h> #include <linux/fs.h> #include <linux/io.h> #define GPFCON 0x56000050 irqreturn_t key_int(int irq, void *dev_id) { //1. 检测是否发生了按键中断 //2. 清除已经发生的按键中断 //3. 打印按键值 printk("key down!\n"); return 0; } void key_hw_init()//设置GPIO引脚功能 { unsigned short data; unsigned int *gpio_config; gpio_config = ioremap(GPFCON,4); data = readw(gpio_config);//读一个32位的寄存器 data &= ~0b11; data |= 0b10; writew(data,gpio_config);//写32位到该寄存器 } int key_open(struct inode *node,struct file *filp) { return 0; } struct file_operations key_fops = { .open = key_open, }; struct miscdevice key_miscdev = { .minor = 200, .name = "tq2440key", .fops = &key_fops, }; static int button_init() { misc_register(&key_miscdev); //按键硬件初始化 key_hw_init(); //注册中断处理程序,按下的时候产生中断,如何查找中断号呢??? request_irq(IRQ_EINT0,key_int,IRQF_TRIGGER_FALLING,"tq2440key",0); return 0; } static void button_exit() { misc_deregister(&key_miscdev); } module_init(button_init); module_exit(button_exit);
makefile
obj-m := key.o KDIR := /home/S5-driver/lesson7/linux-tq2440/ all: make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order
标签:驱动程序,int,硬件,中断,init,key,按键,include 来源: https://www.cnblogs.com/luckdog0623/p/16552698.html