4412驱动-poll 阻塞

参考博客:

poll机制按键驱动

驱动代码

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h> //request_irq
#include <mach/irqs.h> //中断号,已包含plat/irqs.h
#include <linux/fs.h>
#include <linux/device.h> //class_create device_create
#include <mach/regs-gpio.h>
#include <linux/io.h> //ioremap ioread32 iowrite32
#include <linux/sched.h>
#include <linux/of.h>  
#include <linux/of_device.h>  
#include <linux/delay.h>  
#include <linux/poll.h>
#include <linux/device.h>

#define DEV_NAME    "poll-dev"  
static struct class *polldrv_class;
static struct class_device	*polldrv_class_dev;



//LED灯IO口的地址,也就是刚刚我们在上面的芯片手册看到的Address  
#define GPM4COM     0x110002E0  
//定义配置模式的指针变量  
volatile unsigned long *led_config = NULL ;   
//定义配置状态的指针变量  
volatile unsigned long *led_dat = NULL ;   

//定义蜂鸣器配置IO的地址 
#define GPD0CON  0x114000A0
volatile unsigned long *bell_config = NULL ; 
volatile unsigned long *bell_dat = NULL ; 

//定义按键配置寄存器的地址 
#define GPX3CON     0x11000C60
volatile unsigned long *button_config = NULL ; 
volatile unsigned long *button_dat = NULL ; 

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,poll_drv_read将它清0 */
static volatile int ev_press = 0;


static unsigned char key_val;
int major;
/*
  * 确定按键值
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)
{	

   printk("buttons_irq\n");
    //获取按键的键值,因为按键是从该寄存器的第二位开始的,所以需要左移2位,接着与上0xf---1111
	//这样,如果用户按下按键,就会返回一个键值保存在key_val这个变量里 
     key_val = (*button_dat >> 2) & 0xf ;
	printk("key_val_irq= %d\n",key_val);
    ev_press = 1;                  /* 表示中断发生了 */
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
   
	
	return IRQ_RETVAL(IRQ_HANDLED);
}

static int poll_drv_open(struct inode *inode, struct file *file)
{
	int ret ;
	printk("poll_open\n");
	//配置4个按键为输入状态,因为按键是从GPXCON[2]开始的,所以要左移8位到对应的位置,将8位以后的16位清0
	//这样的话就将按键配置的寄存器设置为输入状态了,因为输入是0x0 

	
	*button_config  &= ~((0xf<<(2*4)) | (0xf<<(3*4)) | (0xf<<(4*4)) | (0xf<<(5*4)));
	
	//先对LED的端口进行清0操作
	*led_config &= ~((0xf<<(3*4)) | (0xf<<(2*4)) | (0xf<<(1*4)) | (0xf<<(0*4)));
	
	//将4个IO口16位都设置为Output输出状态
	*led_config |= ((0x1<<(3*4)) | (0x1<<(2*4)) | (0x1<<(1*4)) | (0x1<<(0*4)));
	
	//清寄存器 
	*bell_config &= ~(0xf);
	//设置io为输出 
	*bell_config |= (0x1);
	

        //申请中断	/* request_irq	 * @irq: 中断号,从两个irqs.h中获取描述中断号的宏来填充该参数,不允许直接填入硬件手册上的中断号
        //* @handler:中断处理函数,当中断触发时将自动执行	 * @flags:外部中断触发方式	 
        //* @name: 注册中断的名字,可任取,中断注册成功后可从/proc/interrupts中看到	 
        //* @dev: 传递给回调函数handler的参数,可以从handler函数的第二个参数获取	 
        //* @return: 错误码
	
	 ret =request_irq(IRQ_EINT(27), buttons_irq, IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING, "k2", "k2");	
	 ret =request_irq(IRQ_EINT(28), buttons_irq, IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING, "k3", "k3");
	 ret =request_irq(IRQ_EINT(29), buttons_irq, IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING, "k4", "k4");	
        ret = request_irq(IRQ_EINT(26), buttons_irq, IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING, "k1", "k1");	
	if (ret < 0) 
	{		
		printk("request_irq faild\n");		
		return -EINVAL;	
	}
	return 0;
}

ssize_t poll_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	//如果传进来的size小于4,那么就返回-1 
	if(size < 4){
		return -1 ; 
	}
	printk("key_read");
	/* 如果没有按键动作, 休眠 */
	wait_event_interruptible(button_waitq, ev_press);

	/* 如果有按键动作, 返回键值 */
	copy_to_user(buf, &key_val, sizeof(key_val));
	printk("read_val_irq= %d\n",key_val);
	//ev_press = 0;
	
	return 1;
}



//write方法
int poll_drv_write(struct file *filp , const char __user *buf , size_t count , loff_t *f_pos)
{
	int val ; 
	//注意,这里是在内核中进行操作,我们需要使用copy_from_user这个函数将用户态的内容拷贝到内核态
	copy_from_user(&val , buf , count);	
	//*led_dat &= ~(1<<0) ;
	//以下就是当val是哪个值的时候,led就执行相应的操作,这里不多说
	switch(val)
	{
		case 0 : 
		        //对状态寄存器进行赋值,以下雷同
				printk(KERN_EMERG"led1_on\n");
				*led_dat &= ~(1<<0) ;
				*bell_dat |= 0x1 ;
				break ;
		case 1 :
				printk(KERN_EMERG"led2_on\n");
				*led_dat &= ~(1<<1) ;
				//*bell_dat |= 0x1 ;
				*bell_dat &=~0x1 ;
				break ;
		case 2 :
				printk(KERN_EMERG"led3_on\n");
				*led_dat &= ~(1<<2) ;
				*bell_dat |= 0x1 ;
				break ;
		case 3 :
				printk(KERN_EMERG"ledall_off\n");
				*led_dat |= ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ;
				*bell_dat &=~0x1 ;
				break ;
		case 4 :
				printk(KERN_EMERG"ledall_on\n");
				*led_dat &=~ ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ;
				*bell_dat &=~0x1 ;
				break ;
		case 5 : 
				printk(KERN_EMERG"ledall_off\n");
				*led_dat |= ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ;
				*bell_dat &=~0x1 ;  //蜂鸣器不响
				break ;

	} 

	ev_press = 0;
	return 0;
}
int poll_drv_close(struct inode *inode, struct file *file)
{
	printk("poll_close\n");
	*led_dat |= (1<<0) | (1<<1) | (1<<2) | (1<<3);  //全灭,因为高电平是灭的,0xf ----> 1111
	//关闭蜂鸣器 
	*bell_dat &= ~0x1 ;
	free_irq(IRQ_EINT(26), "k1");
	free_irq(IRQ_EINT(27), "k2");
	free_irq(IRQ_EINT(28), "k3");
	free_irq(IRQ_EINT(29), "k4");
	return 0;
}

static unsigned poll_drv_poll(struct file *file, poll_table *wait)
{
	unsigned int mask = 0;
	poll_wait(file, &button_waitq, wait); // 不会立即休眠

	if (ev_press)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}



static struct file_operations  poll_drv_fops = {
    .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open    =  poll_drv_open,     
	.read	 =	poll_drv_read,	   
	.release =  poll_drv_close,
	.poll    =  poll_drv_poll,
	.write = poll_drv_write,
};



static int poll_drv_init(void)
{
	major = register_chrdev(0, DEV_NAME, & poll_drv_fops);

	polldrv_class = class_create(THIS_MODULE, DEV_NAME);

	polldrv_class_dev = device_create(polldrv_class, NULL, MKDEV(major, 0), NULL, DEV_NAME); /* /dev/buttons */

	led_config = (volatile unsigned long *)ioremap(GPM4COM , 16);
	led_dat = led_config + 1 ;	
	
	
	//映射IO 
	bell_config = (volatile unsigned long *)ioremap(GPD0CON , 16);
	//加4个字节偏移到GP0DAT顺便映射该物理地址 
	bell_dat = bell_config + 1 ;	
	
	//映射端口 
	button_config = (volatile unsigned long *)ioremap(GPX3CON , 16);
	button_dat = button_config + 1 ;	
	return 0;
}

static void poll_drv_exit(void)
{
	printk("poll_exit\n");
	unregister_chrdev(major, DEV_NAME);
	device_destroy(polldrv_class, MKDEV(major, 0));
	class_destroy(polldrv_class);
	
	iounmap(button_config);
	iounmap(bell_config);
	iounmap(led_config);
	
}


module_init(poll_drv_init);

module_exit(poll_drv_exit);

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

完整驱动,应用代码:

代码



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHULINHAIBAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值