4412驱动-key 按键驱动

//key_driver.c
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


static struct class *keydrv_class;
static struct class_device	*keydrv_class_dev;


#define DEV_NAME	"key-dev"
//定义按键配置寄存器的地址 
#define GPX3CON     0x11000C60
volatile unsigned long *button_config = NULL ; 
volatile unsigned long *button_dat = NULL ; 
//open方法 
int key_open(struct inode *inode, struct file *filp)
{
	printk("key_open\n");
	//配置4个按键为输入状态,因为按键是从GPXCON[2]开始的,所以要左移8位到对应的位置,将8位以后的16位清0
	//这样的话就将按键配置的寄存器设置为输入状态了,因为输入是0x0 
	//*button_config &= ~(0xffff << 8);
	
	*button_config  &= ~((0xf<<(2*4)) | (0xf<<(3*4)) | (0xf<<(4*4)) | (0xf<<(5*4)));
	return 0;
}
//read方法 
ssize_t key_read(struct file *file , char __user *buf ,size_t size ,loff_t *offset)
{
	//如果传进来的size小于4,那么就返回-1 
	if(size < 4){
		return -1 ; 
	}
	unsigned char key_val  ;
	//获取按键的键值,因为按键是从该寄存器的第二位开始的,所以需要左移2位,接着与上0xf---1111
	//这样,如果用户按下按键,就会返回一个键值保存在key_val这个变量里 
	key_val = (*button_dat >> 2) & 0xf ;
	//将获取到的值拷贝到用户空间 
	copy_to_user(buf , &key_val , sizeof(key_val));
	//返回键值 
	return  key_val ;
}
//close方法 
int key_close(struct inode *inode, struct file *filp)
{
	printk("key_close\n");
	return 0;
}


struct file_operations fops = {
	.owner = THIS_MODULE ,
	.open = key_open,
	.read = key_read,
	.release = key_close,
};


int major ;
int test_init(void)
{
	printk("key_init\n");
	//注册设备 
	major = register_chrdev(0, DEV_NAME, &fops);
	keydrv_class = class_create(THIS_MODULE, DEV_NAME);
	keydrv_class_dev = device_create(keydrv_class, NULL, MKDEV(major, 0), NULL, DEV_NAME); /* /dev/xyz */


	
	//映射端口 
	button_config = (volatile unsigned long *)ioremap(GPX3CON , 16);
	button_dat = button_config + 1 ;	
	return 0;
}


void test_exit(void)
{
	printk("key_exit\n");
	//注销设备 
	unregister_chrdev(major, DEV_NAME);
	device_destroy(keydrv_class,  MKDEV(major, 0));
	class_destroy(keydrv_class);
	//取消映射 
	iounmap(button_config);
}


module_init(test_init);
module_exit(test_exit);


MODULE_LICENSE("GPL");
MODULE_AUTHOR("hai");
MODULE_VERSION("2017.4.30");

// key_test.c
#include 
#include 
#include 
#include 
#include 
#include 


#define DEV_NAME	"/dev/key-dev"
void delay(void);
int main(int argc, char **argv)
{
	int fd;
	unsigned int val = 0 ;
	char key_buf[4] = {0};
	fd = open(DEV_NAME,O_RDWR) ;
	if(-1 == fd)
	{
		printf("open fair!\n");
		return -1 ;
	}
	while(1){
		//在这里,我已经提前做了一个程序将按键的值读出来了,那么直接看就行了。 
		val = read(fd , key_buf , 4);
		switch(val)
		{
			case 7 :
						printf("the first_key press! key_val=%u\n",val);
						delay();
						break;
			case 11:
						printf("the second_key press! key_val=%u\n",val);
						delay();
						break ;	
			case 13:
						printf("the third_key press! key_val=%u\n",val);
						delay();
						break ;
			case 14:
						printf("the fourth_key press! key_val=%u\n",val);
						delay();
						break ;
			default : 
					  printf("no key is press!\n");
					  delay();
		}
	}
	return 0;
}


void delay(void)
{
	unsigned int i = 0xffffff ; 
	while(i--);
}
//Makefile
KERN_DIR = /home/tools/linux-3.5


all:
	make -C $(KERN_DIR) M=`pwd` modules 


clean:
	make -C $(KERN_DIR) M=`pwd` clean
	rm -rf modules.order


obj-m	+= key_driver.o


你可能感兴趣的:(4412驱动)