1. 程式人生 > >4---linux中斷之按鍵查詢

4---linux中斷之按鍵查詢

上一篇中我們用了簡單的字元裝置驅動來點亮led燈,這一篇我們來搞一搞中斷。

為了引入中斷,我們先看一看這麼一個程式

******led.c******

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h> #include <asm/hardware.h> int major; static struct class *seconddrv_class; static struct class_device *seconddrv_class_dev; volatile unsigned long *gpfcon; volatile unsigned long *gpfdat; volatile unsigned long *gpgcon; volatile unsigned long *gpgdat; static
int second_drv_open(struct inode *inode,struct file *file) { *gpfcon &= ~((0x3<<(0*2)) | (0x3 << (2*2))); *gpgcon &= ~((0x3<<(3*2)) | (0x3 << (11*2))); return 0; } ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) { /* 返回4個引腳的電平 */
unsigned char key_vals[4]; int regval; if (size != sizeof(key_vals)) return -EINVAL; /* 讀GPF0,2 */ regval = *gpfdat; key_vals[0] = (regval & (1<<0)) ? 1 : 0; key_vals[1] = (regval & (1<<2)) ? 1 : 0; /* 讀GPG3,11 */ regval = *gpgdat; key_vals[2] = (regval & (1<<3)) ? 1 : 0; key_vals[3] = (regval & (1<<11)) ? 1 : 0; copy_to_user(buf, key_vals, sizeof(key_vals)); return sizeof(key_vals); } static struct file_operations second_drv_fops={ .owner = THIS_MODULE, .open = second_drv_open, .read = second_drv_read, }; static int second_drv_init(void) { major = register_chrdev(0,"second_drv",&second_drv_fops); seconddrv_class = class_create(THIS_MODULE, "seconddrv"); seconddrv_class_dev = class_device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons*/ gpfcon = (volatile unsigned long *)ioremap(0x56000050,16); gpfdat = gpfcon + 1; gpgcon = (volatile unsigned long *)ioremap(0x56000060,16); gpgdat = gpgcon + 1; return 0; } static void second_drv_exit(void) { unregister_chrdev(major,"second_drv"); class_device_unregister(seconddrv_class_dev); class_destroy(seconddrv_class); iounmap(gpfcon); iounmap(gpgcon); } module_init(second_drv_init); module_exit(second_drv_exit); MODULE_LICENSE("GPL");

測試程式:

*****led_test*****

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

int main(int argc,char **argv)
{
        int cnt = 0;
        int fd;
        unsigned char key_vals[4];

        fd = open("/dev/buttons", O_RDWR);
        if(fd  < 0)
        {
                printf("can't open\n");
        }

        while(1)
        {
                read(fd,key_vals,sizeof(key_vals));
                if(!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3])
                {
                        printf("%04d key pressed : %d %d %d %d\n",cnt++,key_vals[0],key_vals[1],key_vals[2],key_vals[3]);
                }
        }
        return 0;

}

驅動程式提供了read函式,在read裡面我們讀出了key的電平
測試程式在一個死迴圈裡面檢視電平狀態

我們把它們拷貝到開發板並執行:

./led_test &

在使用ps命令檢視程序:

ps

會發現led_test cpu的佔有率竟然是99,這個可是非常的霸道了,讓其它程序怎麼活

因此我們可以使用中斷

檢視原理圖:中斷號為0 2 11 19

1.註冊中斷
request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE, “S1”, &pins_desc[0]);  
request_irq(IRQ_EINT2, buttons_irq,IRQT_ BOTHEDGE, “S2”, &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq,IRQT_ BOTHEDGE, “S3”, &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq,IRQT_ BOTHEDGE, “S4”, &pins_desc[3]);

在入口函式註冊中斷
int request_irq(unsigned int irq, irq_handler_t handler,unsigned long irqflags, const char *devname, void *dev_id)
irq:中斷號,一般在核心有巨集定義了
handler :中斷處理函式
irqgflags:觸發模式,一般核心裡也有巨集定義,
name : 中斷名字
dev_id : 中斷的id

2.read函式
static DECLARE_WAIT_QUEUE_HEAD(button_wait);//宣告等待佇列
static int led_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{       
         wait_event_interruptible(button_wait, even_press);            

        /*有按鍵按下,退出等待佇列,上傳key_val 給使用者層*/
         if(copy_to_user(buf,&key_val,sizeof(key_val)))
         return EFAULT;

     even_press=0; //資料發完後,立馬設為休眠狀態,避免誤操作
         return 0;
}

當我們在應用程式呼叫open時,如果按鍵沒有變化,那麼會進入休眠狀態,等待被喚醒
wait_event_interruptible(wq, condition) //將中斷進入等待佇列(休眠狀態)
wq:等待佇列,需要宣告
condition:當為0時,進入休眠狀態

3.中斷服務函式
static irqreturn_t  buttons_irq (int irq, void *dev_id)       //中斷服務函式
{
      struct pin_desc *pindesc=(struct pin_desc *)dev_id;     //獲取引腳描述結構體
      unsigned int  pin_val=0;                                               
      pin_val=s3c2410_gpio_getpin(pindesc->pin);

        if(pin_val)
        {
                   /*沒有按下 (下降沿),清除0x80*/                 
                   key_val=pindesc->pin_status&0xef;
         }
         else
         {
                   /*按下(上升沿),加上0x80*/
                   key_val=pindesc->pin_status|0x80;
         }             

            even_press=1;               //退出等待佇列
            wake_up_interruptible(&button_wait);
                          
         return IRQ_HANDLED;
}

中斷服務函式裡面,按鍵按下時我們改變key_val的值,按鍵鬆開時我們改變key_val的值
wake_up_interruptible(&button_wait); //喚醒 中斷

編寫測試程式
#include <sys/types.h>    //呼叫sys目錄下types.h檔案
#include <sys/stat.h>      //stat.h獲取檔案屬性
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
/*secondtext            while一直獲取按鍵資訊   */
int main(int argc,char **argv)
{
 int fd,ret;
 unsigned int val=0;               
 fd=open("/dev/buttons",O_RDWR);       
 if(fd<0)
     {printf("can't open!!!\n");
     return -1;}

 while(1)
 {
     ret=read(fd,&val,1);    //讀取一個值,(當在等待佇列時,本程序就會進入休眠狀態)
     if(ret<0)
     {
     printf("read err!\n");    
     continue;
     } 
  printf("key_val=0X%x\r\n",val);
}
 return 0;
}

在死迴圈裡面一直read()

編譯,拷貝,載入
insmod key.c

執行測試程式:

./key_test &

使用ps命令:

ps

這次我們發現測試程式對cpu佔有非常的低。按下按鍵,列印資訊,鬆開按鍵,列印資訊。

完成!!

*****key.c*****


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>


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

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;

volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;


static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

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


struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};


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

struct pin_desc pins_desc[4] = {
	{S3C2410_GPF0, 0x01},
	{S3C2410_GPF2, 0x02},
	{S3C2410_GPG3, 0x03},
	{S3C2410_GPG11, 0x04},
};


/*
  * 確定按鍵值
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
	struct pin_desc * pindesc = (struct pin_desc *)dev_id;
	unsigned int pinval;
	
	pinval = s3c2410_gpio_getpin(pindesc->pin);

	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);
}

static int key_drv_open(struct inode *inode, struct file *file)
{
	/* 配置GPF0,2為輸入引腳 */
	/* 配置GPG3,11為輸入引腳 */
	request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
	request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
	request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
	request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);	

	return 0;
}

ssize_t key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	if (size != 1)
		return -EINVAL;

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

	/* 如果有按鍵動作, 返回鍵值 */
	copy_to_user(buf, &key_val, 1);
	ev_press = 0;
	
	return 1;
}


int key_drv_close(struct inode *inode, struct file *file)
{
	free_irq(IRQ_EINT0, &pins_desc[0]);
	free_irq(IRQ_EINT2, &pins_desc[1]);
	free_irq(IRQ_EINT11, &pins_desc[2]);
	free_irq(IRQ_EINT19, &pins_desc[3]);
	return 0;
}


static struct file_operations sencod_drv_fops = {
    .owner   =  THIS_MODULE,    /* 這是一個巨集,推向編譯模組時自動建立的__this_module變數 */
    .open    =  key_drv_open,     
	.read	 =	key_drv_read,	   
	.release =  key_drv_close,	   
};


int major;
static int key_drv_init(void)
{
	major = register_chrdev(0, "key_drv", &sencod_drv_fops);

	keydrv_class = class_create(THIS_MODULE, "key_drv");

	keydrv_class_dev = class_device_create(keydrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;

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

	return 0;
}

static void key_drv_exit(void)
{
	unregister_chrdev(major, "key_drv");
	class_device_unregister(keydrv_class_dev);
	class_destroy(keydrv_class);
	iounmap(gpfcon);
	iounmap(gpgcon);
	return 0;
}


module_init(key_drv_init);

module_exit(key_drv_exit);

MODULE_LICENSE("GPL");