Linux驅動之按鍵驅動編寫(中斷方式)


Linux驅動之按鍵驅動編寫(查詢方式)已經寫了一個查詢方式的按鍵驅動,但是查詢方式太占用CPU,接下來利用中斷方式編寫一個驅動程序,使得CPU占有率降低,在按鍵空閑時調用read系統調用的進程可以休眠,還是以以下步驟編寫:

1、查看原理圖,確定需要控制的IO端口

2、查看芯片手冊,確定IO端口的寄存器地址

3、編寫驅動代碼

4、確定應用程序功能,編寫測試代碼。

5、編寫Makefile,編譯驅動代碼與測試代碼,在開發板上運行

 

1、查看原理圖,確定需要控制的IO端口

打開原理圖,確定需要控制的IO端口為GPF0、GPF2、GPG3、GPG11。可以看到它的中斷號為IRQ_EINT0、IRQ_EINT2、IRQ_EINT11、IRQ_EINT19

 

2、查看芯片手冊,確定IO端口的寄存器地址,可以看到因為用了兩組GPIO端口,所以它的基地址分別為0x56000050、0x56000060。中斷方式的寄存器基地址為0x56000088、0x5600008c、0x56000090

 

3、編寫驅動代碼,編寫驅動代碼的步驟如下:

 1)、編寫出口、入口函數。代碼如下,具體說明參考Linux驅動之LED驅動編寫

static int second_drv_init(void)
{
    Secondmajor = register_chrdev(0, "buttons", &second_drv_ops);//注冊驅動程序

    if(Secondmajor < 0)
        printk("failes 1 buttons_drv register\n");
    
    second_drv_class = class_create(THIS_MODULE, "buttons");//創建類
    if(second_drv_class < 0)
        printk("failes 2 buttons_drv register\n");
    second_drv_class_dev = class_device_create(second_drv_class, NULL, MKDEV(Secondmajor,0), NULL,"buttons");//創建設備節點
    if(second_drv_class_dev < 0)
        printk("failes 3 buttons_drv register\n");

    
    gpfcon = ioremap(0x56000050, 16);//重映射
    gpfdat = gpfcon + 1;
    gpgcon = ioremap(0x56000060, 16);//重映射
    gpgdat = gpgcon + 1;

    printk("register buttons_drv\n");
    return 0;
}

static void second_drv_exit(void)
{
    unregister_chrdev(Secondmajor,"buttons");

    class_device_unregister(second_drv_class_dev);
    class_destroy(second_drv_class);

    iounmap(gpfcon);
    iounmap(gpgcon);

    printk("unregister buttons_drv\n");
}


module_init(second_drv_init);
module_exit(second_drv_exit);

2)、 添加file_operations 結構體,這個是字符設備驅動的核心結構,所有的應用層調用的函數最終都會調用這個結構下面定義的函數

static struct file_operations third_drv_ops = 
{
    .owner   = THIS_MODULE,
    .open    =  third_drv_open,
    .read     = third_drv_read,
    .release = third_drv_close,//增加關閉函數
};

3)、分別編寫file_operations 結構體下的open、read、release 函數。其中open函數主要將相應的IO端口配置成中斷功能,並且向內核注冊中斷;read函數主要是在按鍵引腳電平未改變時休眠,然后按鍵引腳電平改變后,將按鍵值傳給應用程序處理。(按鍵值的處理在中斷處理程序中);relase函數的功能主要是從內核釋放掉open函數注冊的中斷。程序如下:

static int third_drv_open (struct inode * inode, struct file * file)
{
    int ret;
    ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]);//注冊一個外部中斷S1,雙邊沿觸發,dev_id為&pins_desc[0]
    if(ret)
    {
        printk("open failed 1\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]);//注冊一個外部中斷S2,雙邊沿觸發,dev_id為&pins_desc[1]
    if(ret)
    {
        printk("open failed 2\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]);//注冊一個外部中斷S3,雙邊沿觸發,dev_id為&pins_desc[2]
    if(ret)
    {
        printk("open failed 3\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]);//注冊一個外部中斷S4,雙邊沿觸發,dev_id為&pins_desc[3]
    if(ret)
    {
        printk("open failed 4\n");
        return -1;
    }
    
    return 0;
}


static int third_drv_close(struct inode * inode, struct file * file)
{
    free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]);//釋放中斷,根據IRQ_EINT0找到irq_desc結構。根據pins_desc[0]找到irq_desc->action結構

     free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]);//釋放中斷,根據IRQ_EINT2找到irq_desc結構。根據pins_desc[2]找到irq_desc->action結構

    free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]);//釋放中斷,根據IRQ_EINT11找到irq_desc結構。根據pins_desc[3]找到irq_desc->action結構

    free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]);//釋放中斷,根據IRQ_EINT19找到irq_desc結構。根據pins_desc[4]找到irq_desc->action結構

    return 0;
}

static ssize_t third_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
{
    int ret;

    if(count != 1)
    {
        printk("read error\n");
        return -1;
    }

    wait_event_interruptible(button_waitq, ev_press);//將當前進程放入等待隊列button_waitq中,並且釋放CPU進入睡眠狀態
    
    ret = copy_to_user(userbuf, &key_val, 1);//將取得的按鍵值傳給上層應用
    ev_press = 0;//按鍵已經處理可以繼續睡眠
    
    if(ret)
    {
        printk("copy error\n");
        return -1;
    }
    
    return 1;
}

4)、中斷處理函數的編寫,中斷處理函數利用注冊中斷時傳入的dev_id這個值來判斷是哪個按鍵發生了中斷,dev_iq被賦值為pin_desc結構,如下:

struct pin_desc 
{
    unsigned int pin;      //是哪個按鍵
    unsigned int key_val;  //按鍵的按鍵值
};

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

取得哪個引腳發生的中斷信息后,取得相應的引腳電平,然后確定按鍵值。接着將值傳給key_val,再喚醒調用read的進程,將值直接拷貝給應用程序。具體函數如下

static unsigned int key_val;//全局變量
 
/*
  *0x01、0x02、0x03、0x04表示按鍵被按下
  */
  
/*
  *0x81、0x82、0x83、0x84表示按鍵被松開
  */

/*
  *利用dev_id的值為pins_desc來判斷是哪一個按鍵被按下或松開
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
    unsigned int pin_val;
    struct pin_desc * pin_desc = (struct pin_desc *)dev_id;//取得哪個按鍵被按下的狀態,dev_id是action->dev_id,即在注冊中斷時傳入的&pin_desc[num]
    
    pin_val = s3c2410_gpio_getpin(pin_desc->pin);          //取得按鍵對應的IO口的電平狀態
    
    if(pin_val) //按鍵松開
        key_val = 0x80 | pin_desc->key_val;
    else
        key_val = pin_desc->key_val;


    wake_up_interruptible(&button_waitq);   /* 喚醒休眠的進程,即調用read函數的進程 */
    ev_press = 1;    
    
    return IRQ_HANDLED;
}

5)、整體代碼

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>        //含有iomap函數iounmap函數
#include <asm/uaccess.h>//含有copy_from_user函數
#include <linux/device.h>//含有類相關的處理函數
#include <asm/arch/regs-gpio.h>//含有S3C2410_GPF0等相關的
#include <linux/irq.h>    //含有IRQ_HANDLED\IRQ_TYPE_EDGE_RISING
#include <asm-arm/irq.h>   //含有IRQT_BOTHEDGE觸發類型
#include <linux/interrupt.h> //含有request_irq、free_irq函數

//#include <asm-arm\arch-s3c2410\irqs.h>



static struct class *third_drv_class;//
static struct class_device *third_drv_class_dev;//類下面的設備
static int thirdmajor;

static unsigned long *gpfcon = NULL;
static unsigned long *gpfdat = NULL;
static unsigned long *gpgcon = NULL;
static unsigned long *gpgdat = NULL;




struct pin_desc 
{
    unsigned int pin;      //是哪個按鍵
    unsigned int key_val;  //按鍵的按鍵值
};

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


unsigned int ev_press;
DECLARE_WAIT_QUEUE_HEAD(button_waitq);//注冊一個等待隊列button_waitq

static unsigned int key_val;//全局變量
 
/*
  *0x01、0x02、0x03、0x04表示按鍵被按下
  */
  
/*
  *0x81、0x82、0x83、0x84表示按鍵被松開
  */

/*
  *利用dev_id的值為pins_desc來判斷是哪一個按鍵被按下或松開
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
    unsigned int pin_val;
    struct pin_desc * pin_desc = (struct pin_desc *)dev_id;//取得哪個按鍵被按下的狀態,dev_id是action->dev_id,即在注冊中斷時傳入的&pin_desc[num]
    
    pin_val = s3c2410_gpio_getpin(pin_desc->pin);          //取得按鍵對應的IO口的電平狀態
    
    if(pin_val) //按鍵松開
        key_val = 0x80 | pin_desc->key_val;
    else
        key_val = pin_desc->key_val;


    wake_up_interruptible(&button_waitq);   /* 喚醒休眠的進程,即調用read函數的進程 */
    ev_press = 1;    
    
    return IRQ_HANDLED;
}



static int third_drv_open (struct inode * inode, struct file * file)
{
    int ret;
    ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]);//注冊一個外部中斷S1,雙邊沿觸發,dev_id為&pins_desc[0]
    if(ret)
    {
        printk("open failed 1\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]);//注冊一個外部中斷S2,雙邊沿觸發,dev_id為&pins_desc[1]
    if(ret)
    {
        printk("open failed 2\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]);//注冊一個外部中斷S3,雙邊沿觸發,dev_id為&pins_desc[2]
    if(ret)
    {
        printk("open failed 3\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]);//注冊一個外部中斷S4,雙邊沿觸發,dev_id為&pins_desc[3]
    if(ret)
    {
        printk("open failed 4\n");
        return -1;
    }
    
    return 0;
}


static int third_drv_close(struct inode * inode, struct file * file)
{
    free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]);//釋放中斷,根據IRQ_EINT0找到irq_desc結構。根據pins_desc[0]找到irq_desc->action結構

     free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]);//釋放中斷,根據IRQ_EINT2找到irq_desc結構。根據pins_desc[2]找到irq_desc->action結構

    free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]);//釋放中斷,根據IRQ_EINT11找到irq_desc結構。根據pins_desc[3]找到irq_desc->action結構

    free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]);//釋放中斷,根據IRQ_EINT19找到irq_desc結構。根據pins_desc[4]找到irq_desc->action結構

    return 0;
}

static ssize_t third_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
{
    int ret;

    if(count != 1)
    {
        printk("read error\n");
        return -1;
    }

    wait_event_interruptible(button_waitq, ev_press);//將當前進程放入等待隊列button_waitq中,並且釋放CPU進入睡眠狀態
    
    ret = copy_to_user(userbuf, &key_val, 1);//將取得的按鍵值傳給上層應用
    ev_press = 0;//按鍵已經處理可以繼續睡眠
    
    if(ret)
    {
        printk("copy error\n");
        return -1;
    }
    
    return 1;
}



static struct file_operations third_drv_ops = 
{
    .owner   = THIS_MODULE,
    .open    =  third_drv_open,
    .read     = third_drv_read,
    .release = third_drv_close,//增加關閉函數
};

static int third_drv_init(void)
{
    thirdmajor = register_chrdev(0, "buttons", &third_drv_ops);//注冊驅動程序

    if(thirdmajor < 0)
        printk("failes 1 buttons_drv register\n");
    
    third_drv_class = class_create(THIS_MODULE, "buttons");//創建類
    if(third_drv_class < 0)
        printk("failes 2 buttons_drv register\n");
    third_drv_class_dev = class_device_create(third_drv_class, NULL, MKDEV(thirdmajor,0), NULL,"buttons");//創建設備節點
    if(third_drv_class_dev < 0)
        printk("failes 3 buttons_drv register\n");

    
    gpfcon = ioremap(0x56000050, 16);//重映射
    gpfdat = gpfcon + 1;
    gpgcon = ioremap(0x56000060, 16);//重映射
    gpgdat = gpgcon + 1;

    printk("register buttons_drv\n");
    return 0;
}

static void third_drv_exit(void)
{
    unregister_chrdev(thirdmajor,"buttons");

    class_device_unregister(third_drv_class_dev);
    class_destroy(third_drv_class);

    iounmap(gpfcon);
    iounmap(gpgcon);

    printk("unregister buttons_drv\n");
}


module_init(third_drv_init);
module_exit(third_drv_exit);

MODULE_LICENSE("GPL");

 

4、確定應用程序功能,編寫測試代碼。

測試程序實現四個按鍵中有一個按鍵按下時,打印出這個按鍵的按鍵值。./third_test。直接看代碼

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

/*
  *usage ./buttonstest
  */
int main(int argc, char **argv)
{
    int fd;
    char* filename="dev/buttons";
   unsigned char key_val;
  unsigned long cnt=0;
    fd = open(filename, O_RDWR);//打開dev/firstdrv設備文件
    if (fd < 0)//小於0說明沒有成功
    {
        printf("error, can't open %s\n", filename);
        return 0;
    }
    
    if(argc !=1)
    {
        printf("Usage : %s ",argv[0]);
     return 0;
    }

  while(1)
  {
     read(fd, &key_val, 1);
     printf("key_val: %x\n",key_val);
  }
    
   return 0;
}

 

5、編寫Makefile,編譯驅動代碼與測試代碼,在開發板上運行

Makefile源碼如下:

KERN_DIR = /work/system/linux-2.6.22.6

all:
        make -C $(KERN_DIR) M=`pwd` modules //M='pwd'表示當前目錄。這句話的意思是利用內核目錄下的Makefile規則來編譯當前目錄下的模塊

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

obj-m   +=third_drv.o//調用內核目錄下Makefile編譯時需要用到這個參數

1)、然后在當前目錄下make后編譯出third_drv.ko文件

2)、arm-linux-gcc -o third_test third_test.c編譯出third_test測試程序

3)、cp third_drv.ko third_test /work/nfs_root將編譯出來的文件拷貝到開發板掛接的網絡文件系統上

4)、執行insmod third_drv.ko加載驅動。

5)、./third_test測試程序,按下按鍵,成功打印按鍵值,用top命令查看應用程序發現third_test程序占用了0%的CPU資源,驅動程序相比查詢方式的驅動改善了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM