第一、二期銜接——4.4 字符驅動設備之按鍵驅動—中斷方式

編寫按鍵中斷驅動


一、程序框架圖

在這裏插入圖片描述

1、request_irq()註冊中斷服務函數解析

1.1 函數原型與作用

  • 源碼處於:\arch\arm26\kernel\irq.c
  • 作用:向內核註冊中斷,不需要像裸板驅動配置各種寄存器
int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
		 unsigned long irq_flags, const char * devname, void *dev_id)
{
	unsigned long retval;
	struct irqaction *action;

	if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
	    (irq_flags & IRQF_SHARED && !dev_id))
		return -EINVAL;

	action = kmalloc(sizeof(struct irqaction), GFP_KERNEL);
	if (!action)
		return -ENOMEM;

	action->handler = handler;
	action->flags = irq_flags;
	cpus_clear(action->mask);
	action->name = devname;
	action->next = NULL;
	action->dev_id = dev_id;

	retval = setup_irq(irq, action);

	if (retval)
		kfree(action);
	return retval;
}

1.2 參數講解

  • unsigned int irq:申請的硬件中斷號在Linux內核中,已有開發板的驅動中有對應的宏定義
  • irqreturn_t (*handler)(int, void *, struct pt_regs *),:系統註冊的中斷處理函數自己編寫
  • unsigned long irq_flags,中斷處理的屬性,如按鍵中斷中的上升沿觸發、下升沿觸發等,在Linux內核中,已有開發板的驅動中有對應的宏定義
  • const char * devname,:設置中斷名稱,通常是設備驅動程序的名稱,自己定義
  • void *dev_id:在中斷共享時會用到,傳入中斷處理程序的參數,註冊共享中斷時不能爲NULL,因爲卸載時需要這個做參數,避免卸載其它中斷服務函數自己定義
1.3 返回值

函數運行正常時返回 0 ,否則返回對應錯誤的負值。

2、free_irq()函數解析

2.1 函數原型與作用

  • 源碼處於:\arch\arm26\kernel\irq.c
  • 作用:向內核取消中斷,不需要像裸板驅動配置各種寄存器
void free_irq(unsigned int irq, void *dev_id)
{
	struct irqaction * action, **p;
	unsigned long flags;

	if (irq >= NR_IRQS || !irq_desc[irq].valid) {
		printk(KERN_ERR "Trying to free IRQ%d\n",irq);
#ifdef CONFIG_DEBUG_ERRORS
		__backtrace();
#endif
		return;
	}

	spin_lock_irqsave(&irq_controller_lock, flags);
	for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
		if (action->dev_id != dev_id)
			continue;

	    	/* Found it - now free it */
		*p = action->next;
		kfree(action);
		goto out;
	}
	printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
#ifdef CONFIG_DEBUG_ERRORS
	__backtrace();
#endif
out:
	spin_unlock_irqrestore(&irq_controller_lock, flags);
}

2.2 參數講解

  • unsigned int irq:申請的硬件中斷號在Linux內核中,已有開發板的驅動中有對應的宏定義
  • void *dev_id:卸載的中斷action下的哪個服務函數*

3、wait_event_interruptible()宏解析

3.1 宏原型

  • 源碼處於:\include\linux\wait.h
  • 作用:將本進程置爲可中斷的掛起狀態,反覆檢查condition是否成立,如果成立則退出,如果不成立則繼續休眠;條件滿足後,即把本進程運行狀態置爲運行態
#define wait_event_interruptible(wq, condition)				
({									
	int __ret = 0;							
	if (!(condition))						
		__wait_event_interruptible(wq, condition, __ret);	
	__ret;								
})

3.2 宏參數講解

  • wq: 是一個等待隊列對頭的名字,名字也是通過下面這宏定義
#define DECLARE_WAIT_QUEUE_HEAD(name) 
	wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
  • condition:是一個標誌位,爲0時休眠,爲1時喚醒

4、wake_up_interruptible()宏解析

4.1 宏原型

  • 源碼處於:\include\linux\wait.h
  • 作用: // 喚醒休眠的進程
#define wake_up_interruptible(x)	__wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
			int nr_exclusive, void *key)
{
	unsigned long flags;

	spin_lock_irqsave(&q->lock, flags);
	__wake_up_common(q, mode, nr_exclusive, 0, key);
	spin_unlock_irqrestore(&q->lock, flags);
}

4.2 宏參數講解

  • x:爲隊列名字,根據名字找到等待隊列,喚醒隊列所在的進程。

二、驅動文件編寫

1、定義的變量

int major;
static struct class *buttondrv_class;
static struct class_device	*buttondrv_class_dev;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 
 * 鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84 
 */
static unsigned char key_val;

/* 按鍵信息 */
struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};

/* 存儲4個按鍵的信息 */
struct pin_desc pins_desc[4] = {
 	{S3C2410_GPF0,  0x01},
 	{S3C2410_GPF2,  0x02},
 	{S3C2410_GPG3,  0x03},
	{S3C2410_GPG11, 0x04},
};

/* 中斷註冊信息 */
struct buttonirq_decs{
	unsigned int irq;
	unsigned long flags;
	const char *devname;
	void *dev_id;
};

struct buttonirq_decs buttonirqs_decs[4] = {
	{IRQ_EINT0,	 IRQT_BOTHEDGE, "S2", &pins_desc[0]},
	{IRQ_EINT2,	 IRQT_BOTHEDGE, "S3", &pins_desc[1]},
	{IRQ_EINT11, IRQT_BOTHEDGE, "S4", &pins_desc[2]},
	{IRQ_EINT19, IRQT_BOTHEDGE, "S5", &pins_desc[3]},
};

/* 生成一個等待隊列的隊頭,名字爲button_waitq */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中斷事件標誌, 中斷服務程序將它置1,button_drv_read將它清0 */
static volatile int ev_press = 0;

2、告訴內核有這個驅動程序

2.1 構建file_operations()結構體

static struct file_operations button_drv_fops = {
	.owner   =  THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
    .open    =  button_drv_open,     
	.read    =	button_drv_read,
	.release =  button_drv_close,
};

2.2 定義驅動的入口函數button_drv_init()、註冊驅動register_chrdev()

/* 入口函數 
 * 執行”insmod button_drv.ko”命令時就會調用這個函數
 */
static int button_drv_init(void)
{
	//註冊一個字符設備,名字爲button_drv
	major = register_chrdev(0, "button_drv", &button_drv_fops);	
	
	//創建一個類,名字爲buttond_rv(/class/button_drv)
	buttondrv_class = class_create(THIS_MODULE, "button_drv");	
	
	//創建一個設備節點,名爲button(/dev/button)
	buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, "button"); 

	/* 映射物理地址 */
	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;

	gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
	gpgdat = gpgcon + 1;
	
	return 0;
}

2.3 定義驅動的出口函數button_drv_exit()、卸載驅動unregister_chrdev()

/* 出口函數 
 * 執行”rmmod button_drv.ko”命令時就會調用這個函數
 */
static void button_drv_exit(void)
{
	unregister_chrdev(major, "button_drv"); // 卸載字符

	class_device_unregister(buttondrv_class_dev);	//刪除設備節點
	class_destroy(buttondrv_class);	//銷燬類
	
	/* 取消映射 */
	iounmap(gpfcon);
	iounmap(gpgcon);
}

2.4 修飾出、入口函數,讓內核怎麼知道哪個設備對應哪個的出、入口

module_init(button_drv_init);
module_exit(button_drv_exit);

3、編寫中斷服務函數button_irq()

/* 中斷服務函數 */
static irqreturn_t button_irq(int irq, void *dev_id)
{
	struct pin_desc *pindesc = (struct pin_desc *)dev_id;
	unsigned int pinval;

	pinval = s3c2410_gpio_getpin(pindesc->pin);	//讀取IO口電平

	if(pinval){
		/* 鬆開 */
		key_val = 0x80 | pindesc->key_val;
	}else{
		/* 按下 */
		key_val = pindesc->key_val;
	}

	ev_press = 1;	//發生中斷
	wake_up_interruptible(&button_waitq);   // 喚醒休眠的進程
	
	return IRQ_RETVAL(IRQ_HANDLED);
}

4、編寫驅動函數

4.1 button_drv_open()函數

* open驅動函數 */
static int button_drv_open(struct inode *inode, struct file *file)
{
	unsigned int i;
	int err;

	/* 註冊中斷 */
	for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++){
		err = request_irq(buttonirqs_decs[i].irq, button_irq, buttonirqs_decs[i].flags,
					buttonirqs_decs[i].devname, buttonirqs_decs[i].dev_id);
		if(err){
			printk("func button_drv_open err: request_irq num:%d\n", i);
			break;
		}
	}

	/* 出現錯誤,釋放已經註冊的中斷 */
	if(err){
        i--;
        for (; i >= 0; i--)
            free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
        return -EBUSY;
    }	
	return 0;
}

4.2 button_drv_read()函數

/* read驅動函數 */
static ssize_t button_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
	
	unsigned long ret = 0;
	int err;

	/* 如果沒有按鍵動作, 休眠 */
	err = wait_event_interruptible(button_waitq, ev_press);

	if (err){
			printk("func button_drv_read() err: wait_event_interruptible\n");
		return err;
	}
	/* 如果有按鍵動作, 返回鍵值 */
	ret = copy_to_user(buf, &key_val, 1);
	ev_press = 0;	//清中斷標誌
	
	if(ret < 0){
		printk("func button_drv_read() err: copy_to_user\n");
		return -EFAULT;
	}
	
 	return sizeof(key_val);
}

4.3 button_drv_close()函數

/* close驅動函數 */
int button_drv_close (struct inode *inode, struct file *file)
{
	int i;

	/* 取消中斷 */
	for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++)
		free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);

	return 0;
}

三、Makefile文件編寫

KERN_DIR = /work/system/linux-2.6.22.6

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

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

obj-m	+= button_drv.o

四、測試程序的編寫

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	int fd;
	int ret = 0;
	unsigned char key_val;
	int count = 0;
	
	fd = open("/dev/button", O_RDWR);
	if (fd < 0){
		printf("can't open!\n");
		return -1;
	}

	while(1){
		ret = read(fd, &key_val, 1);
		if(ret < 0){
			printf(" func read() err\n");
		}else{
			printf("key_val = 0x%x\n", key_val);
			}
	}
	
	return ret;
}

五、實際運行

在這裏插入圖片描述
在這裏插入圖片描述

可以看到,驅動以及測試程序可以正常運行,此時CPU使用率不會像查詢方式一樣,高達99%。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章